@@ -12,6 +12,7 @@ module Ormolu.CommentStream
15+import Data.Char (isSpace)
15import Data.Data (Data)
16import Data.Data (Data)
16import Data.List (isPrefixOf, sortOn)
17import Data.List (isPrefixOf, sortOn)
17import Data.List.NonEmpty (NonEmpty (..))
18import Data.List.NonEmpty (NonEmpty (..))
@@ -44,7 +45,7 @@ mkCommentStream extraComments pstate
44 -- NOTE It's easier to normalize pragmas right when we construct comment
45 -- NOTE It's easier to normalize pragmas right when we construct comment
45 -- streams. Because this way we need to do it only once and when we
46 -- streams. Because this way we need to do it only once and when we
46 -- perform checking later they'll automatically match.
47 -- perform checking later they'll automatically match.
47− . fmap (fmap (Comment . normalizeComment . normalizePragma))
48+ . fmap (mkComment . (fmap normalizePragma))
48 . sortOn startOfSpan
49 . sortOn startOfSpan
49 . mapMaybe toRealSpan $
50 . mapMaybe toRealSpan $
50 extraComments ++
51 extraComments ++
@@ -77,11 +78,21 @@ normalizePragma x =
77-- into several lines for subsequent outputting with correct indentation for
78-- into several lines for subsequent outputting with correct indentation for
78-- each line.
79-- each line.
80−normalizeComment :: String -> NonEmpty String
81+mkComment :: RealLocated String -> RealLocated Comment
81−normalizeComment s =
82+mkComment (L l s) = L l . Comment $
83+ if "{-" `isPrefixOf` s
82− case NE.nonEmpty (lines s) of
84+ then case NE.nonEmpty (lines s) of
83− Nothing -> s :| []
85+ Nothing -> s :| []
84− Just xs -> dropWhile (== ' ') <$> xs
86+ Just (x:|xs) ->
90+ else length (takeWhile isSpace y)
91+ n = minimum (startIndent : fmap getIndent xs)
92+ in x :| (drop n <$> xs)
95+ startIndent = srcSpanStartCol l - 1
86-- | Get a 'String' from 'GHC.AnnotationComment'.
97-- | Get a 'String' from 'GHC.AnnotationComment'.