@@ -99,6 +99,8 @@ data RC = RC
9999data SC = SC
100100 { -- | Index of the next column to render
101101 scColumn :: !Int,
102+ -- | Indentation level that was used for the current line
103+ scIndent :: !Int,
102104 -- | Rendered source code so far
103105 scBuilder :: Builder,
104106 -- | Span stream
@@ -109,7 +111,7 @@ data SC = SC
109111 scCommentStream :: CommentStream,
110112 -- | Pending comment lines (in reverse order) to be inserted before next
111113 -- newline, 'Int' is the indentation level
112− scPendingComments :: ![(CommentPosition, Int, Text)],
114+ scPendingComments :: ![(CommentPosition, Text)],
113115 -- | Whether to output a space before the next output
114116 scRequestedDelimiter :: !RequestedDelimiter,
115117 -- | An auxiliary marker for keeping track of last output element
@@ -175,6 +177,7 @@ runR (R m) sstream cstream anns recDot =
175177 sc =
176178 SC
177179 { scColumn = 0,
180+ scIndent = 0,
178181 scBuilder = mempty,
179182 scSpanStream = sstream,
180183 scThisLineSpans = [],
@@ -258,15 +261,20 @@ spit stype text = do
258261 i <- asks rcIndent
259262 c <- gets scColumn
260263 closestEnclosing <- listToMaybe <$> asks rcEnclosingSpans
261− let spaces =
262− if c < i
263− then T.replicate (i - c) " "
264− else bool mempty " " (requestedDel == RequestedSpace)
265− indentedTxt = spaces <> text
264+ let indentedTxt = spaces <> text
265+ spaces = T.replicate spacesN " "
266+ spacesN =
267+ if c == 0
268+ then i
269+ else bool 0 1 (requestedDel == RequestedSpace)
266270 modify $ \sc ->
267271 sc
268272 { scBuilder = scBuilder sc <> fromText indentedTxt,
269273 scColumn = scColumn sc + T.length indentedTxt,
274+ scIndent =
275+ if c == 0
276+ then i
277+ else scIndent sc,
270278 scThisLineSpans =
271279 let xs = scThisLineSpans sc
272280 in case stype of
@@ -310,24 +318,15 @@ space = R . modify $ \sc ->
310318-- hard to output more than one blank newline in a row.
311319newline :: R ()
312320newline = do
313− cs <- R (gets scPendingComments)
314− -- Indentation should be the same for the whole series otherwise there
315− -- will be problems with idempotence. Which identation level to use? The
316− -- last. It'll be the level at which the last comment were attached and
317− -- since the all pending comments are necessarily attached to elements on
318− -- the same line, they all will be attached to the very last element on
319− -- the next pass.
320− let indent = case cs of
321− [] -> 0
322− ((_, i, _) : _) -> i
323− csReversed = reverse cs
324− case csReversed of
321+ indent <- R (gets scIndent)
322+ cs <- reverse <$> R (gets scPendingComments)
323+ case cs of
325324 [] -> newlineRaw
326− ((position, _, _) : _) -> do
325+ ((position, _) : _) -> do
327326 case position of
328327 OnTheSameLine -> space
329328 OnNextLine -> newlineRaw
330− R . forM_ csReversed $ \(_, _, text) ->
329+ R . forM_ cs $ \(_, text) ->
331330 let modRC rc =
332331 rc
333332 { rcIndent = indent
@@ -355,6 +354,7 @@ newlineRaw = R . modify $ \sc ->
355354 VeryBeginning -> builderSoFar
356355 _ -> builderSoFar <> "\n",
357356 scColumn = 0,
357+ scIndent = 0,
358358 scThisLineSpans = [],
359359 scRequestedDelimiter = case scRequestedDelimiter sc of
360360 AfterNewline -> RequestedNewline
@@ -365,7 +365,7 @@ newlineRaw = R . modify $ \sc ->
365365
366366-- | Return 'True' if we should print record dot syntax.
367367useRecordDot :: R Bool
368−useRecordDot = R $ asks rcUseRecDot
368+useRecordDot = R (asks rcUseRecDot)
369369
370370-- | Increase indentation level by one indentation step for the inner
371371-- computation. 'inci' should be used when a part of code must be more
@@ -382,8 +382,7 @@ inci (R m) = R (local modRC m)
382382
383383-- | Set indentation level for the inner computation equal to current
384384-- column. This makes sure that the entire inner block is uniformly
385−-- \"shifted\" to the right. Only works (and makes sense) when enclosing
386−-- layout is multi-line.
385+-- \"shifted\" to the right.
387386sitcc :: R () -> R ()
388387sitcc (R m) = do
389388 requestedDel <- R (gets scRequestedDelimiter)
@@ -391,16 +390,9 @@ sitcc (R m) = do
391390 c <- R (gets scColumn)
392391 let modRC rc =
393392 rc
394− { rcIndent = max i c + bool 0 1 (requestedDel == RequestedSpace)
393+ { rcIndent = max i (c + bool 0 1 (requestedDel == RequestedSpace))
395394 }
396− vlayout (R m) . R $ do
397− modify $ \sc ->
398− sc
399− { scRequestedDelimiter = case requestedDel of
400− RequestedSpace -> RequestedNothing
401− other -> other
402− }
403− local modRC m
395+ R (local modRC m)
404396
405397-- | Set 'Layout' for internal computation.
406398enterLayout :: Layout -> R () -> R ()
@@ -442,10 +434,9 @@ registerPendingCommentLine ::
442434 Text ->
443435 R ()
444436registerPendingCommentLine position text = R $ do
445− i <- asks rcIndent
446437 modify $ \sc ->
447438 sc
448− { scPendingComments = (position, i, text) : scPendingComments sc
439+ { scPendingComments = (position, text) : scPendingComments sc
449440 }
450441
451442-- | Drop elements that begin before or at the same place as given
@@ -574,7 +565,7 @@ dontUseBraces (R r) = R (local (\i -> i {rcCanUseBraces = False}) r)
574565
575566-- | Return 'True' if we can use braces in this context.
576567canUseBraces :: R Bool
577−canUseBraces = R $ asks rcCanUseBraces
568+canUseBraces = R (asks rcCanUseBraces)
578569
579570----------------------------------------------------------------------------
580571-- Constants