on master help sign in
Fix the bug related to de-association of pragma comments
commite09fcc03168eec94e4cd998a291582df65352829 authorMark Karpov <markkarpov92@gmail.com> dateWed 17 Jun 2020, 17:02 (6 years ago) parent3fc444a
4 files changed+25−3
CHANGELOG.md+3 data/examples/other/pragma-comments-after-out.hs+4 data/examples/other/pragma-comments-after.hs+3 src/Ormolu/Parser/CommentStream.hs+15−3
CHANGELOG.md+3
@@ -32,6 +32,9 @@
3232* Fixed the bug in the de-duplicating algorithm for import lists. [Issue
3333 620](https://github.com/tweag/ormolu/issues/620).
3434
35+* Fixed the bug related to the de-association of pragma comments. [Issue
36+ 619](https://github.com/tweag/ormolu/issues/619).
37+
3538## Ormolu 0.1.0.0
3639
3740* Fixed rendering of type signatures concerning several identifiers. [Issue
data/examples/other/pragma-comments-after-out.hsnew file+4
@@ -0,0 +1,4 @@
1+{-# LANGUAGE AllowAmbiguousTypes #-}
2+{-# LANGUAGE ConstraintKinds #-}
3+-- TODO: Fix and delete this pragma
4+{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
data/examples/other/pragma-comments-after.hsnew file+3
@@ -0,0 +1,3 @@
1+{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} -- TODO: Fix and delete this pragma
2+{-# LANGUAGE AllowAmbiguousTypes #-}
3+{-# LANGUAGE ConstraintKinds #-}
src/Ormolu/Parser/CommentStream.hs+15−3
@@ -28,7 +28,7 @@ import qualified Lexer as GHC
2828import Ormolu.Parser.Pragma
2929import Ormolu.Parser.Shebang
3030import Ormolu.Processing.Common
31−import Ormolu.Utils (showOutputable)
31+import Ormolu.Utils (onTheSameLine, showOutputable)
3232import SrcLoc
3333
3434----------------------------------------------------------------------------
@@ -150,6 +150,7 @@ isMultilineComment (Comment _ (x :| _)) = "{-" `L.isPrefixOf` x
150150
151151-- | Detect and extract stack header if it is present.
152152extractStackHeader ::
153+ -- | Comment stream to analyze
153154 [RealLocated String] ->
154155 ([RealLocated String], Maybe (RealLocated Comment))
155156extractStackHeader = \case
@@ -165,7 +166,9 @@ extractStackHeader = \case
165166
166167-- | Extract pragmas and their associated comments.
167168extractPragmas ::
169+ -- | Input
168170 String ->
171+ -- | Comment stream to analyze
169172 [RealLocated String] ->
170173 ([RealLocated Comment], [([RealLocated Comment], Pragma)])
171174extractPragmas input = go initialLs id id
@@ -179,8 +182,17 @@ extractPragmas input = go initialLs id id
179182 let (ls', x') = mkComment ls x
180183 in go ls' (csSoFar . (x' :)) pragmasSoFar xs
181184 Just pragma ->
182− let combined = (csSoFar [], pragma)
183− in go ls id (pragmasSoFar . (combined :)) xs
185+ let combined ys = (csSoFar ys, pragma)
186+ go' ls' ys rest = go ls' id (pragmasSoFar . (combined ys :)) rest
187+ in case xs of
188+ [] -> go' ls [] xs
189+ (y : ys) ->
190+ let (ls', y') = mkComment ls y
191+ in if onTheSameLine
192+ (RealSrcSpan (getRealSrcSpan x))
193+ (RealSrcSpan (getRealSrcSpan y))
194+ then go' ls' [y'] ys
195+ else go' ls [] xs
184196
185197-- | Get a 'String' from 'GHC.AnnotationComment'.
186198unAnnotationComment :: GHC.AnnotationComment -> Maybe String