on master help sign in
Only drop indentation present for every line in multiline comments
f8bad3amrkkrp
data/examples/other/ascii-out.hsnew file+9
@@ -0,0 +1,9 @@
1+{- -----------------------------------
2+ < What about ASCII art in comments? >
3+ -----------------------------------
4+ \ ^__^
5+ \ (oo)\_______
6+ (__)\ )\/\
7+ ||----w |
8+ || ||
9+-}
data/examples/other/ascii.hsnew file+9
@@ -0,0 +1,9 @@
1+{- -----------------------------------
2+ < What about ASCII art in comments? >
3+ -----------------------------------
4+ \ ^__^
5+ \ (oo)\_______
6+ (__)\ )\/\
7+ ||----w |
8+ || ||
9+-}
data/examples/other/multiline-comments-reindent-out.hs+3−3
@@ -1,6 +1,6 @@
1{-
1{-
2−And so here we have a
2+ And so here we have a
3−multiline comment.
3+ multiline comment.
4
4
5−Indeed.
5+ Indeed.
6-}
6-}
src/Ormolu/CommentStream.hs+17−6
@@ -12,6 +12,7 @@ module Ormolu.CommentStream
12 )
12 )
13where
13where
14
14
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.
79
80
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) ->
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
85
96
86-- | Get a 'String' from 'GHC.AnnotationComment'.
97-- | Get a 'String' from 'GHC.AnnotationComment'.
87
98