|
2 | 2 | {-# LANGUAGE DataKinds #-}
|
3 | 3 | {-# LANGUAGE DoAndIfThenElse #-}
|
4 | 4 | {-# LANGUAGE FlexibleContexts #-}
|
5 |
| -{-# LANGUAGE LambdaCase #-} |
6 | 5 | {-# LANGUAGE MultiWayIf #-}
|
7 | 6 | {-# LANGUAGE NamedFieldPuns #-}
|
8 | 7 | {-# LANGUAGE RecordWildCards #-}
|
@@ -38,7 +37,7 @@ import Language.Haskell.Stylish.Printer
|
38 | 37 | import Language.Haskell.Stylish.Step
|
39 | 38 | import Language.Haskell.Stylish.Util
|
40 | 39 |
|
41 |
| - |
| 40 | +import Debug.Trace |
42 | 41 | --------------------------------------------------------------------------------
|
43 | 42 | data Indent
|
44 | 43 | = SameLine
|
@@ -93,16 +92,17 @@ step cfg = makeStep "Data" \ls m -> Editor.apply (changes m) ls
|
93 | 92 | changes :: Module -> Editor.Edits
|
94 | 93 | changes = foldMap (formatDataDecl cfg) . dataDecls
|
95 | 94 |
|
| 95 | + getComments :: GHC.SrcSpanAnnA -> [GHC.LEpaComment] |
| 96 | + getComments (GHC.EpAnn _ _ c)= GHC.priorComments c |
| 97 | + |
96 | 98 | dataDecls :: Module -> [DataDecl]
|
97 | 99 | 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 |
102 | 102 | loc <- maybeToList $ GHC.srcSpanToRealSrcSpan $ GHC.getLocA ldecl
|
103 | 103 | case tycld of
|
104 | 104 | GHC.DataDecl {..} -> pure $ MkDataDecl
|
105 |
| - { dataComments = GHC.am_cs modAnns |
| 105 | + { dataComments = getComments annos |
106 | 106 | , dataLoc = loc
|
107 | 107 | , dataDeclName = tcdLName
|
108 | 108 | , dataTypeVars = tcdTyVars
|
@@ -147,7 +147,7 @@ putDataDecl cfg@Config {..} decl = do
|
147 | 147 |
|
148 | 148 | onelineEnum =
|
149 | 149 | isEnum decl && not cBreakEnums &&
|
150 |
| - all (not . commentGroupHasComments) constructorComments |
| 150 | + (not . any commentGroupHasComments) constructorComments |
151 | 151 |
|
152 | 152 | putText $ newOrData decl
|
153 | 153 | space
|
@@ -176,9 +176,14 @@ putDataDecl cfg@Config {..} decl = do
|
176 | 176 | forM_ (GHC.dd_cons defn) $ putNewtypeConstructor cfg
|
177 | 177 | | not . null $ GHC.dd_cons defn -> do
|
178 | 178 | 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 |
182 | 187 |
|
183 | 188 | forM_ (flagEnds cgItems) $ \((lcon, mbInlineComment), firstItem, lastItem) -> do
|
184 | 189 | unless (isGADT decl) $ do
|
@@ -332,7 +337,7 @@ putConstructor cfg consIndent lcons = case GHC.unLoc lcons of
|
332 | 337 | GHC.ConDeclGADT {..} -> do
|
333 | 338 | -- Put argument to constructor first:
|
334 | 339 | 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) |
336 | 341 | GHC.RecConGADT _ _ -> error . mconcat $
|
337 | 342 | [ "Language.Haskell.Stylish.Step.Data.putConstructor: "
|
338 | 343 | , "encountered a GADT with record constructors, not supported yet"
|
|
0 commit comments