Skip to content

Commit 198d27c

Browse files
fix disappearing comments
1 parent 834bb29 commit 198d27c

File tree

1 file changed

+17
-8
lines changed
  • lib/Language/Haskell/Stylish/Step

1 file changed

+17
-8
lines changed

lib/Language/Haskell/Stylish/Step/Data.hs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,28 @@ step cfg = makeStep "Data" \ls m -> Editor.apply (changes m) ls
9393
changes :: Module -> Editor.Edits
9494
changes = foldMap (formatDataDecl cfg) . dataDecls
9595

96-
getComments :: GHC.AddEpAnn -> [GHC.LEpaComment]
97-
getComments (GHC.AddEpAnn _ epaLoc) = case epaLoc of
98-
GHC.EpaDelta _ comments -> comments
99-
GHC.EpaSpan _ -> []
96+
getComments :: GHC.SrcSpanAnnA -> [GHC.LEpaComment]
97+
getComments (GHC.EpAnn _ _ c)= GHC.priorComments c
98+
99+
-- ugly workaround to make sure we don't reprint a haddock
100+
-- comment before a data declaration after a data
101+
-- declaration…
102+
filterLoc :: GHC.RealSrcSpan -> [GHC.LEpaComment] -> [GHC.LEpaComment]
103+
filterLoc loc = filter afterStart
104+
where
105+
afterStart c = comLoc c >= GHC.srcSpanStartLine loc
106+
comLoc (GHC.L l _) = case l of
107+
GHC.EpaSpan (GHC.RealSrcSpan l' _) -> GHC.srcSpanStartLine l'
108+
_ -> undefined -- hopefully we don't get an EpaDelta or an UnhelpfulSpan passed to us
100109

101110
dataDecls :: Module -> [DataDecl]
102111
dataDecls m = do
103112
ldecl <- GHC.hsmodDecls $ GHC.unLoc m
104-
GHC.TyClD _ tycld <- pure $ GHC.unLoc ldecl
113+
(GHC.TyClD _ tycld, annos) <- pure $ (\(GHC.L anno ty) -> (ty, anno)) ldecl
105114
loc <- maybeToList $ GHC.srcSpanToRealSrcSpan $ GHC.getLocA ldecl
106115
case tycld of
107116
GHC.DataDecl {..} -> pure $ MkDataDecl
108-
{ dataComments = foldMap getComments tcdDExt
117+
{ dataComments = filterLoc loc . getComments $ annos
109118
, dataLoc = loc
110119
, dataDeclName = tcdLName
111120
, dataTypeVars = tcdTyVars
@@ -180,8 +189,8 @@ putDataDecl cfg@Config {..} decl = do
180189
| not . null $ GHC.dd_cons defn -> do
181190
forM_ (flagEnds constructorComments) $ \(CommentGroup {..}, firstGroup, lastGroup) -> do
182191
forM_ cgPrior $ \lc -> do
183-
putComment $ GHC.unLoc lc
184-
consIndent lineLengthAfterEq
192+
putComment . GHC.unLoc $ lc
193+
consIndent lineLengthAfterEq
185194

186195
forM_ (flagEnds cgItems) $ \((lcon, mbInlineComment), firstItem, lastItem) -> do
187196
unless (isGADT decl) $ do

0 commit comments

Comments
 (0)