on master help sign in
Try to contain comments inside enclosing constructs

Attach the comment if the next element is not a sibling. I think this is quite often what we want, since if we put a comment inside a construct, we prefer it to stay inside the same element.

commitf86155a8eadce8e8793253ec0d4badb6e848018c authorUtku Demir <me@utdemir.com> dateThu 15 Aug 2019, 17:18 (7 years ago) parent58f8187
5 files changed+34−6
data/examples/declaration/rewrite-rule/prelude-out.hs+2−2 data/examples/declaration/value/other/comments-get-before-op-out.hs+2−2 data/examples/other/comment-inside-construct-out.hs+8 data/examples/other/comment-inside-construct.hs+9 src/Ormolu/Printer/Comments.hs+13−2
data/examples/declaration/rewrite-rule/prelude-out.hs+2−2
@@ -63,10 +63,10 @@
6363"unpack" [~1] forall a. unpackCString# a = build (unpackFoldrCString# a)
6464"unpack-list" [1] forall a. unpackFoldrCString# a (:) [] = unpackCString# a
6565"unpack-append" forall a n. unpackFoldrCString# a (:) n = unpackAppendCString# a n
66− #-}
67−
6866-- There's a built-in rule (in PrelRules.lhs) for
6967-- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n
68+ #-}
69+
7070{-# RULES
7171"foldr/build" forall f n (g :: forall b. (a -> b -> b) -> b -> b).
7272 foldr f n (build g) =
data/examples/declaration/value/other/comments-get-before-op-out.hs+2−2
@@ -4,7 +4,7 @@ main = do
44 [ migration1,
55 migration1,
66 migration3
7+ -- When adding migrations here, don't forget to update
8+ -- 'schemaVersion' in Galley.Data
79 ]
8− -- When adding migrations here, don't forget to update
9− -- 'schemaVersion' in Galley.Data
1010 `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) =
178178 goesAfter
179179 && logicallyFollows
180180 && noEltBetween
181− && (continuation || supersedesParentElt)
181+ && (continuation || lastInEnclosing || supersedesParentElt)
182182 where
183183 -- 1) The comment starts after end of the AST element:
184184 goesAfter =
185185 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:
187187 logicallyFollows
188188 = theSameLine l ref -- a) it's on the same line
189189 || isPrevHaddock comment -- b) it's a Haddock string starting with -- ^
190190 || continuation -- c) it's a continuation of a comment block
191+ || lastInEnclosing -- d) it's the last element in the enclosing construct
192+
191193 -- 3) There is no other AST element between this element and the comment:
192194 noEltBetween =
193195 case mnSpn of
@@ -217,6 +219,15 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
217219 Nothing -> False
218220 Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
219221
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+
220231-- | Output a 'Comment'. This is a low-level printing function.
221232
222233spitComment :: Comment -> R ()