Skip to content

Commit d6f4184

Browse files
fixing issue with data comments
1 parent 44ee7e0 commit d6f4184

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

Diff for: lib/Language/Haskell/Stylish/Step/Data.hs

+17-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE DoAndIfThenElse #-}
44
{-# LANGUAGE FlexibleContexts #-}
5-
{-# LANGUAGE LambdaCase #-}
65
{-# LANGUAGE MultiWayIf #-}
76
{-# LANGUAGE NamedFieldPuns #-}
87
{-# LANGUAGE RecordWildCards #-}
@@ -38,7 +37,7 @@ import Language.Haskell.Stylish.Printer
3837
import Language.Haskell.Stylish.Step
3938
import Language.Haskell.Stylish.Util
4039

41-
40+
import Debug.Trace
4241
--------------------------------------------------------------------------------
4342
data Indent
4443
= SameLine
@@ -93,16 +92,17 @@ step cfg = makeStep "Data" \ls m -> Editor.apply (changes m) ls
9392
changes :: Module -> Editor.Edits
9493
changes = foldMap (formatDataDecl cfg) . dataDecls
9594

95+
getComments :: GHC.SrcSpanAnnA -> [GHC.LEpaComment]
96+
getComments (GHC.EpAnn _ _ c)= GHC.priorComments c
97+
9698
dataDecls :: Module -> [DataDecl]
9799
dataDecls m = do
98-
let hsModule = GHC.unLoc m
99-
modAnns = GHC.anns . GHC.hsmodAnn . GHC.hsmodExt $ hsModule
100-
ldecl <- GHC.hsmodDecls hsModule
101-
GHC.TyClD _ tycld <- pure $ GHC.unLoc ldecl
100+
ldecl <- GHC.hsmodDecls . GHC.unLoc $ m
101+
(GHC.TyClD _ tycld, annos) <- pure $ (\(GHC.L anno ty) -> (ty, anno)) ldecl
102102
loc <- maybeToList $ GHC.srcSpanToRealSrcSpan $ GHC.getLocA ldecl
103103
case tycld of
104104
GHC.DataDecl {..} -> pure $ MkDataDecl
105-
{ dataComments = GHC.am_cs modAnns
105+
{ dataComments = getComments annos
106106
, dataLoc = loc
107107
, dataDeclName = tcdLName
108108
, dataTypeVars = tcdTyVars
@@ -147,7 +147,7 @@ putDataDecl cfg@Config {..} decl = do
147147

148148
onelineEnum =
149149
isEnum decl && not cBreakEnums &&
150-
all (not . commentGroupHasComments) constructorComments
150+
(not . any commentGroupHasComments) constructorComments
151151

152152
putText $ newOrData decl
153153
space
@@ -176,9 +176,14 @@ putDataDecl cfg@Config {..} decl = do
176176
forM_ (GHC.dd_cons defn) $ putNewtypeConstructor cfg
177177
| not . null $ GHC.dd_cons defn -> do
178178
forM_ (flagEnds constructorComments) $ \(CommentGroup {..}, firstGroup, lastGroup) -> do
179-
forM_ cgPrior $ \lc -> do
180-
putComment $ GHC.unLoc lc
181-
consIndent lineLengthAfterEq
179+
forM_ cgPrior $ \(GHC.L l c) -> do
180+
-- ugly workaround to make sure we don't reprint a haddock
181+
-- comment before a data declaration after a data
182+
-- declaration…
183+
let GHC.EpaSpan (GHC.RealSrcSpan comLoc _) = l
184+
when (GHC.srcSpanStartLine comLoc >= GHC.srcSpanStartLine (dataLoc decl)) $ do
185+
putComment c
186+
consIndent lineLengthAfterEq
182187

183188
forM_ (flagEnds cgItems) $ \((lcon, mbInlineComment), firstItem, lastItem) -> do
184189
unless (isGADT decl) $ do
@@ -332,7 +337,7 @@ putConstructor cfg consIndent lcons = case GHC.unLoc lcons of
332337
GHC.ConDeclGADT {..} -> do
333338
-- Put argument to constructor first:
334339
case con_g_args of
335-
GHC.PrefixConGADT _ _ -> sep (comma >> space) $ fmap putRdrName $ toList con_names
340+
GHC.PrefixConGADT _ _ -> sep (comma >> space) (putRdrName <$> toList con_names)
336341
GHC.RecConGADT _ _ -> error . mconcat $
337342
[ "Language.Haskell.Stylish.Step.Data.putConstructor: "
338343
, "encountered a GADT with record constructors, not supported yet"

Diff for: lib/Language/Haskell/Stylish/Step/ModuleHeader.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import qualified Language.Haskell.Stylish.Step.Imports as Imports
3232
import Language.Haskell.Stylish.Util (flagEnds)
3333
import qualified GHC.Unit.Module.Warnings as GHC
3434

35-
3635
data Config = Config
3736
{ indent :: Int
3837
, sort :: Bool
@@ -152,7 +151,7 @@ printHeader conf mbName mbDeprec mbExps mbModuleComment mbWhereComment = do
152151
attachModuleComment
153152
Single | [egroup] <- exports
154153
, not (commentGroupHasComments egroup)
155-
, [(export, _)] <- (cgItems egroup) -> do
154+
, [(export, _)] <- cgItems egroup -> do
156155
printSingleLineExportList conf [export]
157156
attachModuleComment
158157
Inline | [] <- exports -> do

0 commit comments

Comments
 (0)