Better handle multi-line following comments
commit1e14f9ff7e7c673ff94908f65025d9dfca4fe951
authormrkkrp <mark.karpov@tweag.io>
dateMon 9 Sep 2019, 11:28 (7 years ago)
parentd4761bf
data/examples/declaration/foreign/foreign-export-out.hs+1−1
@@ -2,7 +2,7 @@ foreign export ccall foo :: Int -> IO Int
22
33-- | 'foreignTomFun' is a very important thing
44foreign export ccall "tomography"
5− foreignTomFun :: StablePtr Storage -> TomForegin {- Storage is bad -}
5+ foreignTomFun :: StablePtr Storage {- Storage is bad -} -> TomForegin
66
77foreign export {- We can't use capi here -} ccall "dynamic"
88 export_nullaryMeth :: (IO HRESULT) -> IO (Ptr ())
data/examples/declaration/foreign/foreign-import-out.hs+1−1
@@ -17,7 +17,7 @@ foreign import javascript
1717
1818foreign import {- We use capi here -} capi "pi.h value pi" c_pi :: CDouble
1919
20−foreign import stdcall "dynamic" {- This is a bad place for a comment -}
20+foreign import stdcall {- This is a bad place for a comment -} "dynamic"
2121 dyn_gluBeginSurface
2222 :: FunPtr (Ptr GLUnurbs -> IO ())
2323 -- ^ This 'FunPtr' is extremely dangerous, beware
data/examples/declaration/value/function/operator-comments-out.hs → data/examples/declaration/value/function/operator-comments-0-out.hsrenamed+2−2
@@ -7,10 +7,10 @@ foo =
77
88foo =
99 bar
10− ++ case foo of {- some comment -}
10+ ++ {- some comment -} case foo of
1111 a -> a
1212
1313foo =
1414 bar
15− ++ case foo of {- some comment -}
15+ ++ case foo {- some comment -} of
1616 a -> a
data/examples/declaration/value/function/operator-comments.hs → data/examples/declaration/value/function/operator-comments-0.hsrenamed
The content is the same.
data/examples/declaration/value/function/operator-comments-1-out.hsnew file+16
@@ -0,0 +1,16 @@
1+foo =
2+ bar
3+ ++
4+ -- some comment
5+ case foo of
6+ a -> a
7+
8+foo =
9+ bar
10+ ++ case foo of -- some comment
11+ a -> a
12+
13+foo =
14+ bar
15+ ++ case foo of -- some comment
16+ a -> a
data/examples/declaration/value/function/operator-comments-1.hsnew file+17
@@ -0,0 +1,17 @@
1+foo =
2+ bar
3+ ++
4+ -- some comment
5+ case foo of
6+ a -> a
7+
8+foo =
9+ bar
10+ ++ -- some comment
11+ case foo of
12+ a -> a
13+
14+foo =
15+ bar
16+ ++ case foo of -- some comment
17+ a -> a
data/examples/other/comment-multiline-after-out.hsnew file+1
@@ -0,0 +1 @@
1+foo :: Int {- ^ start index -} -> Int {- ^ length -} -> t a -> t a
data/examples/other/comment-multiline-after.hsnew file+1
@@ -0,0 +1 @@
1+foo :: Int {- ^ start index -} -> Int {- ^ length -} -> t a -> t a
src/Ormolu/Parser/CommentStream.hs+6
@@ -11,6 +11,7 @@ module Ormolu.Parser.CommentStream
1111 , Comment (..)
1212 , mkCommentStream
1313 , isPrevHaddock
14+ , isMultilineComment
1415 , showCommentStream
1516 )
1617where
@@ -71,6 +72,11 @@ mkCommentStream extraComments pstate =
7172isPrevHaddock :: Comment -> Bool
7273isPrevHaddock (Comment (x :| _)) = "-- ^" `isPrefixOf` x
7374
75+-- | Is this comment multiline-style?
76+
77+isMultilineComment :: Comment -> Bool
78+isMultilineComment (Comment (x :| _)) = "{-" `isPrefixOf` x
79+
7480-- | Pretty-print a 'CommentStream'.
7581
7682showCommentStream :: CommentStream -> String
src/Ormolu/Printer/Comments.hs+3−1
@@ -91,7 +91,9 @@ spitFollowingComment (L ref a) mlastSpn = do
9191 meSpn <- getEnclosingSpan (/= ref)
9292 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
9393 if theSameLinePost l ref && not (isModule a)
94− then spitCommentPending OnTheSameLine l comment
94+ then if isMultilineComment comment
95+ then space >> spitCommentNow l comment
96+ else spitCommentPending OnTheSameLine l comment
9597 else do
9698 when (needsNewlineBefore l mlastSpn) $
9799 registerPendingCommentLine OnNextLine ""