Skip to content

Commit 75934c2

Browse files
committed
Add the next indent to the position context. Fixes #2
1 parent f84b7cb commit 75934c2

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

src/Dodo.purs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ align n doc
6464

6565
-- | Increases the indentation level so that it aligns to the current column.
6666
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
6868

6969
-- | Adds an annotation to a document. Printers can interpret annotations to style
7070
-- | their output, eg. ANSI colors.
@@ -255,7 +255,6 @@ type FlexGroupState b a =
255255
{ position :: Position
256256
, buffer :: Buffer b
257257
, annotations :: List a
258-
, indent :: Int
259258
, indentSpaces :: String
260259
, stack :: List (DocCmd a)
261260
}
@@ -264,18 +263,17 @@ type DocState b a =
264263
{ position :: Position
265264
, buffer :: Buffer b
266265
, annotations :: List a
267-
, indent :: Int
268266
, indentSpaces :: String
269267
, flexGroup :: FlexGroupStatus b a
270268
}
271269

272270
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 }
275273

276274
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 }
279277

280278
-- | Prints a documents given a printer and print options.
281279
-- |
@@ -301,12 +299,12 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
301299
{ line: 0
302300
, column: 0
303301
, indent: 0
302+
, nextIndent: 0
304303
, pageWidth: opts.pageWidth
305304
, ribbonWidth: calcRibbonWidth 0
306305
}
307306
, buffer: Buffer.new printer.emptyBuffer
308307
, annotations: List.Nil
309-
, indent: 0
310308
, indentSpaces: ""
311309
, flexGroup: NoFlexGroup
312310
}
@@ -320,10 +318,10 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
320318
Append doc1 doc2 ->
321319
go (Doc doc1 : Doc doc2 : stk) state
322320
Text len str
323-
| state.position.column == 0 && state.indent > 0 ->
321+
| state.position.column == 0 && state.position.indent > 0 ->
324322
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
327325
}
328326
| state.position.column + len <= state.position.indent + state.position.ribbonWidth ->
329327
go stk state
@@ -347,40 +345,40 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
347345
{ position
348346
{ line = state.position.line + 1
349347
, column = 0
350-
, indent = state.indent
351-
, ribbonWidth = calcRibbonWidth state.indent
348+
, indent = state.position.nextIndent
349+
, ribbonWidth = calcRibbonWidth state.position.nextIndent
352350
}
353351
, buffer = Buffer.modify printer.writeBreak state.buffer
354352
, flexGroup = NoFlexGroup
355353
}
356354
Indent doc1
357355
| 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
359357
{ 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)
362361
}
363-
, indent = state.indent + opts.indentWidth
364362
, indentSpaces = state.indentSpaces <> opts.indentUnit
365363
}
366364
| 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 }
369367
, indentSpaces = state.indentSpaces <> opts.indentUnit
370368
}
371369
Align width doc1
372370
| 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
374372
{ 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)
377376
}
378-
, indent = state.indent + width
379377
, indentSpaces = state.indentSpaces <> power " " width
380378
}
381379
| 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 }
384382
, indentSpaces = state.indentSpaces <> power " " width
385383
}
386384
FlexGroup doc1 -> case state.flexGroup of
@@ -401,8 +399,8 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
401399
_ ->
402400
go (Doc doc1 : stk) state
403401
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
406404
| otherwise ->
407405
go (Doc (k state.position) : stk) state
408406
Annotate ann doc1 ->
@@ -419,7 +417,7 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
419417
}
420418
Dedent indSpaces ind ->
421419
go stk state
422-
{ indent = ind
420+
{ position { nextIndent = ind }
423421
, indentSpaces = indSpaces
424422
}
425423
LeaveAnnotation ann anns ->

src/Dodo/Internal.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Position =
77
{ line :: Int
88
, column :: Int
99
, indent :: Int
10+
, nextIndent :: Int
1011
, pageWidth :: Int
1112
, ribbonWidth :: Int
1213
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1234
2+
1234
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module DodoAlignCurrentColumnIdempotent where
2+
3+
import Prelude
4+
5+
import Dodo (Doc, alignCurrentColumn, flexGroup, fourSpaces, plainText, print, softBreak, text)
6+
import Effect (Effect)
7+
import Effect.Class.Console as Console
8+
9+
test :: forall a. Doc a
10+
test = text "1234" <> aligns
11+
where
12+
aligns = flexGroup
13+
$ alignCurrentColumn
14+
$ alignCurrentColumn
15+
$ alignCurrentColumn
16+
$ softBreak <> text "1234"
17+
18+
main :: Effect Unit
19+
main = Console.log $ print plainText (fourSpaces { pageWidth = 0 }) test

0 commit comments

Comments
 (0)