@@ -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