on master help sign in
Fix the difference in indentation levels in a comment series
commit194da3a19f501247fc76af842b6aa5794d82c2eb authorMark Karpov <markkarpov92@gmail.com> dateTue 5 May 2020, 13:50 (6 years ago) parent991bf8e
4 files changed+25−3
CHANGELOG.md+3 data/examples/declaration/value/function/operator-comments-2-out.hs+5 data/examples/declaration/value/function/operator-comments-2.hs+4 src/Ormolu/Printer/Internal.hs+13−3
CHANGELOG.md+3
@@ -10,6 +10,9 @@
1010 “continuation” of a series of comments [Issue
1111 449](https://github.com/tweag/ormolu/issues/449).
1212
13+* Fixed an idempotence issue related to different indentation levels in a
14+ comment series. [Issue 512](https://github.com/tweag/ormolu/issues/512).
15+
1316* Renamed the `--check-idempotency` flag to `--check-idempotence`.
1417 Apparently only the latter is correct.
1518
data/examples/declaration/value/function/operator-comments-2-out.hsnew file+5
@@ -0,0 +1,5 @@
1+x =
2+ y
3+ ++ f g -- commentA
4+ -- commentB
5+ -- commentC
data/examples/declaration/value/function/operator-comments-2.hsnew file+4
@@ -0,0 +1,4 @@
1+x =
2+ y ++ -- commentA
3+-- commentB
4+ f g -- commentC
src/Ormolu/Printer/Internal.hs+13−3
@@ -285,14 +285,24 @@ space = R . modify $ \sc ->
285285-- hard to output more than one blank newline in a row.
286286newline :: R ()
287287newline = do
288− cs <- reverse <$> R (gets scPendingComments)
289− case cs of
288+ cs <- R (gets scPendingComments)
289+ -- Indentation should be the same for the whole series otherwise there
290+ -- will be problems with idempotence. Which identation level to use? The
291+ -- last. It'll be the level at which the last comment were attached and
292+ -- since the all pending comments are necessarily attached to elements on
293+ -- the same line, they all will be attached to the very last element on
294+ -- the next pass.
295+ let indent = case cs of
296+ [] -> 0
297+ ((_, i, _) : _) -> i
298+ csReversed = reverse cs
299+ case csReversed of
290300 [] -> newlineRaw
291301 ((position, _, _) : _) -> do
292302 case position of
293303 OnTheSameLine -> space
294304 OnNextLine -> newlineRaw
295− R . forM_ cs $ \(_, indent, text) ->
305+ R . forM_ csReversed $ \(_, _, text) ->
296306 let modRC rc =
297307 rc
298308 { rcIndent = indent