|
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 #-} |
@@ -93,19 +92,29 @@ 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 |
|
96 | | - getComments :: GHC.AddEpAnn -> [GHC.LEpaComment] |
97 | | - getComments (GHC.AddEpAnn _ epaLoc) = case epaLoc of |
98 | | - GHC.EpaDelta _ comments -> comments |
99 | | - GHC.EpaSpan _ -> [] |
| 95 | + getComments :: GHC.SrcSpanAnnA -> [GHC.LEpaComment] |
| 96 | + getComments (GHC.EpAnn _ _ c)= GHC.priorComments c |
| 97 | + |
| 98 | + -- ugly workaround to make sure we don't reprint a haddock |
| 99 | + -- comment before a data declaration after a data |
| 100 | + -- declaration… |
| 101 | + filterLoc :: GHC.RealSrcSpan -> [GHC.LEpaComment] -> [GHC.LEpaComment] |
| 102 | + filterLoc loc = filter afterStart |
| 103 | + where |
| 104 | + afterStart c = comLoc c >= GHC.srcSpanStartLine loc |
| 105 | + comLoc (GHC.L l _) = case l of |
| 106 | + GHC.EpaSpan (GHC.RealSrcSpan l' _) -> GHC.srcSpanStartLine l' |
| 107 | + GHC.EpaDelta (GHC.RealSrcSpan l' _) _ _ -> GHC.srcSpanStartLine l' |
| 108 | + _ -> undefined -- hopefully we don't get a UnhelpfulSpan passed to us |
100 | 109 |
|
101 | 110 | dataDecls :: Module -> [DataDecl] |
102 | 111 | dataDecls m = do |
103 | | - ldecl <- GHC.hsmodDecls $ GHC.unLoc m |
104 | | - GHC.TyClD _ tycld <- pure $ GHC.unLoc ldecl |
| 112 | + ldecl <- GHC.hsmodDecls . GHC.unLoc $ m |
| 113 | + (GHC.TyClD _ tycld, annos) <- pure $ (\(GHC.L anno ty) -> (ty, anno)) ldecl |
105 | 114 | loc <- maybeToList $ GHC.srcSpanToRealSrcSpan $ GHC.getLocA ldecl |
106 | 115 | case tycld of |
107 | 116 | GHC.DataDecl {..} -> pure $ MkDataDecl |
108 | | - { dataComments = foldMap getComments tcdDExt |
| 117 | + { dataComments = filterLoc loc . getComments $ annos |
109 | 118 | , dataLoc = loc |
110 | 119 | , dataDeclName = tcdLName |
111 | 120 | , dataTypeVars = tcdTyVars |
@@ -150,7 +159,7 @@ putDataDecl cfg@Config {..} decl = do |
150 | 159 |
|
151 | 160 | onelineEnum = |
152 | 161 | isEnum decl && not cBreakEnums && |
153 | | - all (not . commentGroupHasComments) constructorComments |
| 162 | + (not . any commentGroupHasComments) constructorComments |
154 | 163 |
|
155 | 164 | putText $ newOrData decl |
156 | 165 | space |
@@ -180,7 +189,7 @@ putDataDecl cfg@Config {..} decl = do |
180 | 189 | | not . null $ GHC.dd_cons defn -> do |
181 | 190 | forM_ (flagEnds constructorComments) $ \(CommentGroup {..}, firstGroup, lastGroup) -> do |
182 | 191 | forM_ cgPrior $ \lc -> do |
183 | | - putComment $ GHC.unLoc lc |
| 192 | + putComment . GHC.unLoc $ lc |
184 | 193 | consIndent lineLengthAfterEq |
185 | 194 |
|
186 | 195 | forM_ (flagEnds cgItems) $ \((lcon, mbInlineComment), firstItem, lastItem) -> do |
@@ -335,7 +344,7 @@ putConstructor cfg consIndent lcons = case GHC.unLoc lcons of |
335 | 344 | GHC.ConDeclGADT {..} -> do |
336 | 345 | -- Put argument to constructor first: |
337 | 346 | case con_g_args of |
338 | | - GHC.PrefixConGADT _ _ -> sep (comma >> space) $ fmap putRdrName $ toList con_names |
| 347 | + GHC.PrefixConGADT _ _ -> sep (comma >> space) (putRdrName <$> toList con_names) |
339 | 348 | GHC.RecConGADT _ _ -> error . mconcat $ |
340 | 349 | [ "Language.Haskell.Stylish.Step.Data.putConstructor: " |
341 | 350 | , "encountered a GADT with record constructors, not supported yet" |
|
0 commit comments