@@ -64,7 +64,7 @@ align n doc
64
64
65
65
-- | Increases the indentation level so that it aligns to the current column.
66
66
alignCurrentColumn :: forall a . Doc a -> Doc a
67
- alignCurrentColumn = notEmpty \doc -> withPosition \pos -> align (pos.column - pos.indent ) doc
67
+ alignCurrentColumn = notEmpty \doc -> withPosition \pos -> align (pos.column - pos.nextIndent ) doc
68
68
69
69
-- | Adds an annotation to a document. Printers can interpret annotations to style
70
70
-- | their output, eg. ANSI colors.
@@ -255,7 +255,6 @@ type FlexGroupState b a =
255
255
{ position :: Position
256
256
, buffer :: Buffer b
257
257
, annotations :: List a
258
- , indent :: Int
259
258
, indentSpaces :: String
260
259
, stack :: List (DocCmd a )
261
260
}
@@ -264,18 +263,17 @@ type DocState b a =
264
263
{ position :: Position
265
264
, buffer :: Buffer b
266
265
, annotations :: List a
267
- , indent :: Int
268
266
, indentSpaces :: String
269
267
, flexGroup :: FlexGroupStatus b a
270
268
}
271
269
272
270
resetState :: forall a b . FlexGroupState b a -> DocState b a
273
- resetState { position, buffer, annotations, indent: indent', indentSpaces } =
274
- { position, buffer, annotations, indent: indent', indentSpaces, flexGroup: NoFlexGroup }
271
+ resetState { position, buffer, annotations, indentSpaces } =
272
+ { position, buffer, annotations, indentSpaces, flexGroup: NoFlexGroup }
275
273
276
274
storeState :: forall a b . List (DocCmd a ) -> DocState b a -> FlexGroupState b a
277
- storeState stack { position, buffer, annotations, indent: indent', indentSpaces } =
278
- { position, buffer, annotations, indent: indent', indentSpaces, stack }
275
+ storeState stack { position, buffer, annotations, indentSpaces } =
276
+ { position, buffer, annotations, indentSpaces, stack }
279
277
280
278
-- | Prints a documents given a printer and print options.
281
279
-- |
@@ -301,12 +299,12 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
301
299
{ line: 0
302
300
, column: 0
303
301
, indent: 0
302
+ , nextIndent: 0
304
303
, pageWidth: opts.pageWidth
305
304
, ribbonWidth: calcRibbonWidth 0
306
305
}
307
306
, buffer: Buffer .new printer.emptyBuffer
308
307
, annotations: List.Nil
309
- , indent: 0
310
308
, indentSpaces: " "
311
309
, flexGroup: NoFlexGroup
312
310
}
@@ -320,10 +318,10 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
320
318
Append doc1 doc2 ->
321
319
go (Doc doc1 : Doc doc2 : stk) state
322
320
Text len str
323
- | state.position.column == 0 && state.indent > 0 ->
321
+ | state.position.column == 0 && state.position. indent > 0 ->
324
322
go stack state
325
- { position { column = state.indent }
326
- , buffer = Buffer .modify (printer.writeIndent state.indent state.indentSpaces) state.buffer
323
+ { position { column = state.position. indent }
324
+ , buffer = Buffer .modify (printer.writeIndent state.position. indent state.indentSpaces) state.buffer
327
325
}
328
326
| state.position.column + len <= state.position.indent + state.position.ribbonWidth ->
329
327
go stk state
@@ -347,40 +345,40 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
347
345
{ position
348
346
{ line = state.position.line + 1
349
347
, column = 0
350
- , indent = state.indent
351
- , ribbonWidth = calcRibbonWidth state.indent
348
+ , indent = state.position.nextIndent
349
+ , ribbonWidth = calcRibbonWidth state.position.nextIndent
352
350
}
353
351
, buffer = Buffer .modify printer.writeBreak state.buffer
354
352
, flexGroup = NoFlexGroup
355
353
}
356
354
Indent doc1
357
355
| state.position.column == 0 ->
358
- go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state
356
+ go (Doc doc1 : Dedent state.indentSpaces state.position.nextIndent : stk) state
359
357
{ position
360
- { indent = state.indent + opts.indentWidth
361
- , ribbonWidth = calcRibbonWidth (state.indent + opts.indentWidth)
358
+ { indent = state.position.nextIndent + opts.indentWidth
359
+ , nextIndent = state.position.nextIndent + opts.indentWidth
360
+ , ribbonWidth = calcRibbonWidth (state.position.nextIndent + opts.indentWidth)
362
361
}
363
- , indent = state.indent + opts.indentWidth
364
362
, indentSpaces = state.indentSpaces <> opts.indentUnit
365
363
}
366
364
| otherwise ->
367
- go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state
368
- { indent = state.indent + opts.indentWidth
365
+ go (Doc doc1 : Dedent state.indentSpaces state.position.nextIndent : stk) state
366
+ { position { nextIndent = state.position.nextIndent + opts.indentWidth }
369
367
, indentSpaces = state.indentSpaces <> opts.indentUnit
370
368
}
371
369
Align width doc1
372
370
| state.position.column == 0 ->
373
- go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state
371
+ go (Doc doc1 : Dedent state.indentSpaces state.position.nextIndent : stk) state
374
372
{ position
375
- { indent = state.indent + width
376
- , ribbonWidth = calcRibbonWidth (state.indent + width)
373
+ { indent = state.position.nextIndent + width
374
+ , nextIndent = state.position.nextIndent + width
375
+ , ribbonWidth = calcRibbonWidth (state.position.nextIndent + width)
377
376
}
378
- , indent = state.indent + width
379
377
, indentSpaces = state.indentSpaces <> power " " width
380
378
}
381
379
| otherwise ->
382
- go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state
383
- { indent = state.indent + width
380
+ go (Doc doc1 : Dedent state.indentSpaces state.position.nextIndent : stk) state
381
+ { position { nextIndent = state.position.nextIndent + width }
384
382
, indentSpaces = state.indentSpaces <> power " " width
385
383
}
386
384
FlexGroup doc1 -> case state.flexGroup of
@@ -401,8 +399,8 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
401
399
_ ->
402
400
go (Doc doc1 : stk) state
403
401
WithPosition k
404
- | state.position.column == 0 && state.indent > 0 ->
405
- go (Doc (k state.position { column = state.indent }) : stk) state
402
+ | state.position.column == 0 && state.position.nextIndent > 0 ->
403
+ go (Doc (k state.position { column = state.position.nextIndent }) : stk) state
406
404
| otherwise ->
407
405
go (Doc (k state.position) : stk) state
408
406
Annotate ann doc1 ->
@@ -419,7 +417,7 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
419
417
}
420
418
Dedent indSpaces ind ->
421
419
go stk state
422
- { indent = ind
420
+ { position { nextIndent = ind }
423
421
, indentSpaces = indSpaces
424
422
}
425
423
LeaveAnnotation ann anns ->
0 commit comments