on master help sign in
Fix the false positive about AST differences

Comments that have more than one consecutive blank line will be rendered with just a single blank line due to how our rendering engine works. I think that the engine work correctly in this case and we want to generally normalize blank lines. So what should be fixed is just the check. For that we remove extra consecutive blank lines when we construct ‘Comment’ values.

commit6ecae369225725b1cf5b0ad818db9365d2f0ad49 authorMark Karpov <markkarpov92@gmail.com> dateMon 11 May 2020, 12:26 (6 years ago) parent60752a2
3 files changed+51−21
data/examples/other/multiple-blank-line-comment-out.hs+9 data/examples/other/multiple-blank-line-comment.hs+10 src/Ormolu/Parser/CommentStream.hs+32−21
data/examples/other/multiple-blank-line-comment-out.hsnew file+9
@@ -0,0 +1,9 @@
1+module A where
2+
3+a =
4+ b
5+ [ f
6+ {-,
7+
8+ -}
9+ ]
data/examples/other/multiple-blank-line-comment.hsnew file+10
@@ -0,0 +1,10 @@
1+module A where
2+
3+a =
4+ b
5+ [ f
6+ {-,
7+
8+
9+ -}
10+ ]
src/Ormolu/Parser/CommentStream.hs+32−21
@@ -103,7 +103,7 @@ mkComment ::
103103mkComment ls (L l s) = (ls', comment)
104104 where
105105 comment =
106− L l . Comment atomsBefore . fmap dropTrailing $
106+ L l . Comment atomsBefore . removeConseqBlanks . fmap dropTrailing $
107107 if "{-" `L.isPrefixOf` s
108108 then case NE.nonEmpty (lines s) of
109109 Nothing -> s :| []
@@ -143,26 +143,6 @@ isMultilineComment (Comment _ (x :| _)) = "{-" `L.isPrefixOf` x
143143----------------------------------------------------------------------------
144144-- Helpers
145145
146−-- | Get a 'String' from 'GHC.AnnotationComment'.
147−unAnnotationComment :: GHC.AnnotationComment -> Maybe String
148−unAnnotationComment = \case
149− GHC.AnnDocCommentNext _ -> Nothing -- @-- |@
150− GHC.AnnDocCommentPrev _ -> Nothing -- @-- ^@
151− GHC.AnnDocCommentNamed _ -> Nothing -- @-- $@
152− GHC.AnnDocSection _ _ -> Nothing -- @-- *@
153− GHC.AnnDocOptions s -> Just s
154− GHC.AnnLineComment s -> Just s
155− GHC.AnnBlockComment s -> Just s
156−
157−liftMaybe :: Located (Maybe a) -> Maybe (Located a)
158−liftMaybe = \case
159− L _ Nothing -> Nothing
160− L l (Just a) -> Just (L l a)
161−
162−toRealSpan :: Located a -> Maybe (RealLocated a)
163−toRealSpan (L (RealSrcSpan l) a) = Just (L l a)
164−toRealSpan _ = Nothing
165−
166146-- | Detect and extract stack header if it is present.
167147extractStackHeader ::
168148 [RealLocated String] ->
@@ -196,3 +176,34 @@ extractPragmas input = go initialLs id id
196176 Just pragma ->
197177 let combined = (csSoFar [], pragma)
198178 in go ls id (pragmasSoFar . (combined :)) xs
179+
180+-- | Get a 'String' from 'GHC.AnnotationComment'.
181+unAnnotationComment :: GHC.AnnotationComment -> Maybe String
182+unAnnotationComment = \case
183+ GHC.AnnDocCommentNext _ -> Nothing -- @-- |@
184+ GHC.AnnDocCommentPrev _ -> Nothing -- @-- ^@
185+ GHC.AnnDocCommentNamed _ -> Nothing -- @-- $@
186+ GHC.AnnDocSection _ _ -> Nothing -- @-- *@
187+ GHC.AnnDocOptions s -> Just s
188+ GHC.AnnLineComment s -> Just s
189+ GHC.AnnBlockComment s -> Just s
190+
191+liftMaybe :: Located (Maybe a) -> Maybe (Located a)
192+liftMaybe = \case
193+ L _ Nothing -> Nothing
194+ L l (Just a) -> Just (L l a)
195+
196+toRealSpan :: Located a -> Maybe (RealLocated a)
197+toRealSpan (L (RealSrcSpan l) a) = Just (L l a)
198+toRealSpan _ = Nothing
199+
200+-- | Remove consecutive blank lines.
201+removeConseqBlanks :: NonEmpty String -> NonEmpty String
202+removeConseqBlanks (x :| xs) = x :| go (null x) id xs
203+ where
204+ go seenBlank acc = \case
205+ [] -> acc []
206+ (y : ys) ->
207+ if seenBlank && null y
208+ then go True acc ys
209+ else go (null y) (acc . (y :)) ys