mirror of
https://github.com/google/ormolu.git
synced 2024-11-30 14:46:39 +03:00
Only drop indentation present for every line in multiline comments
This commit is contained in:
parent
faf3d86f14
commit
f8bad3a855
9
data/examples/other/ascii-out.hs
Normal file
9
data/examples/other/ascii-out.hs
Normal file
@ -0,0 +1,9 @@
|
||||
{- -----------------------------------
|
||||
< What about ASCII art in comments? >
|
||||
-----------------------------------
|
||||
\ ^__^
|
||||
\ (oo)\_______
|
||||
(__)\ )\/\
|
||||
||----w |
|
||||
|| ||
|
||||
-}
|
9
data/examples/other/ascii.hs
Normal file
9
data/examples/other/ascii.hs
Normal file
@ -0,0 +1,9 @@
|
||||
{- -----------------------------------
|
||||
< What about ASCII art in comments? >
|
||||
-----------------------------------
|
||||
\ ^__^
|
||||
\ (oo)\_______
|
||||
(__)\ )\/\
|
||||
||----w |
|
||||
|| ||
|
||||
-}
|
@ -1,6 +1,6 @@
|
||||
{-
|
||||
And so here we have a
|
||||
multiline comment.
|
||||
And so here we have a
|
||||
multiline comment.
|
||||
|
||||
Indeed.
|
||||
Indeed.
|
||||
-}
|
||||
|
@ -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
|
||||
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 xs -> dropWhile (== ' ') <$> xs
|
||||
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'.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user