on master help sign in
Only drop indentation present for every line in multiline comments
commitf8bad3a855d100e4ea057ae56a51d5dfd1bfd959 authormrkkrp <mark.karpov@tweag.io> dateSun 30 Jun 2019, 23:59 (7 years ago) parentfaf3d86
4 files changed+38−9
data/examples/other/ascii-out.hs+9 data/examples/other/ascii.hs+9 data/examples/other/multiline-comments-reindent-out.hs+3−3 src/Ormolu/CommentStream.hs+17−6
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 @@
11{-
2−And so here we have a
3−multiline comment.
2+ And so here we have a
3+ multiline comment.
44
5−Indeed.
5+ Indeed.
66-}
src/Ormolu/CommentStream.hs+17−6
@@ -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