on master help sign in
Implement dropping of trailing whitespace in comments
875c79dmrkkrp
data/examples/other/trailing-whitespace-out.hsnew file+7
@@ -0,0 +1,7 @@
1+-- Here is a comment with trailing whitespace.
2+foo = 5
3+
4+{- Block comment with trailing whitespace.
5+ Bo.
6+ -}
7+bar = 6
data/examples/other/trailing-whitespace.hsnew file+7
@@ -0,0 +1,7 @@
1+-- Here is a comment with trailing whitespace.
2+foo = 5
3+
4+{- Block comment with trailing whitespace.
5+ Bo.
6+ -}
7+bar = 6
src/Ormolu/Parser/CommentStream.hs+3−2
@@ -18,7 +18,7 @@ where
18import Data.Char (isSpace)
18import Data.Char (isSpace)
19import Data.Data (Data)
19import Data.Data (Data)
20import Data.Either (partitionEithers)
20import Data.Either (partitionEithers)
21−import Data.List (isPrefixOf, sortOn)
21+import Data.List (isPrefixOf, sortOn, dropWhileEnd)
22import Data.List.NonEmpty (NonEmpty (..))
22import Data.List.NonEmpty (NonEmpty (..))
23import Data.Maybe (mapMaybe)
23import Data.Maybe (mapMaybe)
24import Data.Set (Set)
24import Data.Set (Set)
@@ -98,7 +98,7 @@ normalizePragma x =
98-- each line.
98-- each line.
99
99
100mkComment :: RealLocated String -> RealLocated Comment
100mkComment :: RealLocated String -> RealLocated Comment
101−mkComment (L l s) = L l . Comment $
101+mkComment (L l s) = L l . Comment . fmap dropTrailing $
102 if "{-" `isPrefixOf` s
102 if "{-" `isPrefixOf` s
103 then case NE.nonEmpty (lines s) of
103 then case NE.nonEmpty (lines s) of
104 Nothing -> s :| []
104 Nothing -> s :| []
@@ -111,6 +111,7 @@ mkComment (L l s) = L l . Comment $
111 in x :| (drop n <$> xs)
111 in x :| (drop n <$> xs)
112 else s :| []
112 else s :| []
113 where
113 where
114+ dropTrailing = dropWhileEnd isSpace
114 startIndent = srcSpanStartCol l - 1
115 startIndent = srcSpanStartCol l - 1
115
116
116-- | Get a 'String' from 'GHC.AnnotationComment'.
117-- | Get a 'String' from 'GHC.AnnotationComment'.