@@ -12,6 +12,7 @@ module Ormolu.CommentStream
1212 )
1313where
1414
15+import Data.Char (isSpace)
1516import Data.Data (Data)
1617import Data.List (isPrefixOf, sortOn)
1718import Data.List.NonEmpty (NonEmpty (..))
@@ -44,7 +45,7 @@ mkCommentStream extraComments pstate
4445 -- NOTE It's easier to normalize pragmas right when we construct comment
4546 -- streams. Because this way we need to do it only once and when we
4647 -- perform checking later they'll automatically match.
47− . fmap (fmap (Comment . normalizeComment . normalizePragma))
48+ . fmap (mkComment . (fmap normalizePragma))
4849 . sortOn startOfSpan
4950 . mapMaybe toRealSpan $
5051 extraComments ++
@@ -77,11 +78,21 @@ normalizePragma x =
7778-- into several lines for subsequent outputting with correct indentation for
7879-- each line.
7980
80−normalizeComment :: String -> NonEmpty String
81−normalizeComment s =
82− case NE.nonEmpty (lines s) of
83− Nothing -> s :| []
84− Just xs -> dropWhile (== ' ') <$> xs
81+mkComment :: RealLocated String -> RealLocated Comment
82+mkComment (L l s) = L l . Comment $
83+ if "{-" `isPrefixOf` s
84+ then case NE.nonEmpty (lines s) of
85+ Nothing -> s :| []
86+ Just (x:|xs) ->
87+ let getIndent y =
88+ if all isSpace y
89+ then startIndent
90+ else length (takeWhile isSpace y)
91+ n = minimum (startIndent : fmap getIndent xs)
92+ in x :| (drop n <$> xs)
93+ else s :| []
94+ where
95+ startIndent = srcSpanStartCol l - 1
8596
8697-- | Get a 'String' from 'GHC.AnnotationComment'.
8798