1
1
mirror of https://github.com/google/ormolu.git synced 2024-08-15 11:00:28 +03:00

Only drop indentation present for every line in multiline comments

This commit is contained in:
mrkkrp 2019-06-30 23:59:40 +02:00 committed by Mark Karpov
parent faf3d86f14
commit f8bad3a855
4 changed files with 38 additions and 9 deletions

View File

@ -0,0 +1,9 @@
{- -----------------------------------
< What about ASCII art in comments? >
-----------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
-}

View File

@ -0,0 +1,9 @@
{- -----------------------------------
< What about ASCII art in comments? >
-----------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
-}

View File

@ -1,6 +1,6 @@
{-
And so here we have a
multiline comment.
And so here we have a
multiline comment.
Indeed.
Indeed.
-}

View File

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