on master help sign in
Try to contain comments inside enclosing constructs
f86155aUtku Demir
data/examples/declaration/rewrite-rule/prelude-out.hs+2−2
@@ -63,10 +63,10 @@
63"unpack" [~1] forall a. unpackCString# a = build (unpackFoldrCString# a)
63"unpack" [~1] forall a. unpackCString# a = build (unpackFoldrCString# a)
64"unpack-list" [1] forall a. unpackFoldrCString# a (:) [] = unpackCString# a
64"unpack-list" [1] forall a. unpackFoldrCString# a (:) [] = unpackCString# a
65"unpack-append" forall a n. unpackFoldrCString# a (:) n = unpackAppendCString# a n
65"unpack-append" forall a n. unpackFoldrCString# a (:) n = unpackAppendCString# a n
66− #-}
67−
68-- There's a built-in rule (in PrelRules.lhs) for
66-- There's a built-in rule (in PrelRules.lhs) for
69-- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n
67-- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n
68+ #-}
69+
70{-# RULES
70{-# RULES
71"foldr/build" forall f n (g :: forall b. (a -> b -> b) -> b -> b).
71"foldr/build" forall f n (g :: forall b. (a -> b -> b) -> b -> b).
72 foldr f n (build g) =
72 foldr f n (build g) =
data/examples/declaration/value/other/comments-get-before-op-out.hs+2−2
@@ -4,7 +4,7 @@ main = do
4 [ migration1,
4 [ migration1,
5 migration1,
5 migration1,
6 migration3
6 migration3
7+ -- When adding migrations here, don't forget to update
8+ -- 'schemaVersion' in Galley.Data
7 ]
9 ]
8− -- When adding migrations here, don't forget to update
9− -- 'schemaVersion' in Galley.Data
10 `finally` Log.close
10 `finally` Log.close
data/examples/other/comment-inside-construct-out.hsnew file+8
@@ -0,0 +1,8 @@
1+xs =
2+ [ outer list item,
3+ [ inner list first item,
4+ inner list second item
5+ -- inner list last item commented
6+ ],
7+ outer list item
8+ ]
data/examples/other/comment-inside-construct.hsnew file+9
@@ -0,0 +1,9 @@
1+xs =
2+ [ outer list item,
3+ [ inner list first item,
4+ inner list second item
5+ -- inner list last item commented
6+ ],
7+ outer list item
8+ ]
9+
src/Ormolu/Printer/Comments.hs+13−2
@@ -178,16 +178,18 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
178 goesAfter
178 goesAfter
179 && logicallyFollows
179 && logicallyFollows
180 && noEltBetween
180 && noEltBetween
181− && (continuation || supersedesParentElt)
181+ && (continuation || lastInEnclosing || supersedesParentElt)
182 where
182 where
183 -- 1) The comment starts after end of the AST element:
183 -- 1) The comment starts after end of the AST element:
184 goesAfter =
184 goesAfter =
185 realSrcSpanStart l >= realSrcSpanEnd ref
185 realSrcSpanStart l >= realSrcSpanEnd ref
186− -- 2) The comment logically belongs to the element, three cases:
186+ -- 2) The comment logically belongs to the element, four cases:
187 logicallyFollows
187 logicallyFollows
188 = theSameLine l ref -- a) it's on the same line
188 = theSameLine l ref -- a) it's on the same line
189 || isPrevHaddock comment -- b) it's a Haddock string starting with -- ^
189 || isPrevHaddock comment -- b) it's a Haddock string starting with -- ^
190 || continuation -- c) it's a continuation of a comment block
190 || continuation -- c) it's a continuation of a comment block
191+ || lastInEnclosing -- d) it's the last element in the enclosing construct
192+
191 -- 3) There is no other AST element between this element and the comment:
193 -- 3) There is no other AST element between this element and the comment:
192 noEltBetween =
194 noEltBetween =
193 case mnSpn of
195 case mnSpn of
@@ -217,6 +219,15 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
217 Nothing -> False
219 Nothing -> False
218 Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
220 Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
219
221
222+ lastInEnclosing =
223+ case (,) <$> meSpn <*> mnSpn of
224+ Nothing -> False
225+ Just (espn, nspn) ->
226+ -- Make sure that the comment is inside the parent
227+ realSrcSpanEnd l <= realSrcSpanEnd espn
228+ -- Check if the next element is outside of the parent
229+ && realSrcSpanEnd espn < realSrcSpanStart nspn
230+
220-- | Output a 'Comment'. This is a low-level printing function.
231-- | Output a 'Comment'. This is a low-level printing function.
221
232
222spitComment :: Comment -> R ()
233spitComment :: Comment -> R ()