Skip to content

Commit ea2b4bd

Browse files
committed
Fix position calculations
1 parent 79ca83c commit ea2b4bd

File tree

4 files changed

+86
-43
lines changed

4 files changed

+86
-43
lines changed

src/Dodo.purs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,27 @@ type FlexGroupState b a =
250250
{ position :: Position
251251
, buffer :: Buffer b
252252
, annotations :: List a
253-
, indent :: String
253+
, indent :: Int
254+
, indentSpaces :: String
254255
, stack :: List (DocCmd a)
255256
}
256257

257258
type DocState b a =
258259
{ position :: Position
259260
, buffer :: Buffer b
260261
, annotations :: List a
261-
, indent :: String
262+
, indent :: Int
263+
, indentSpaces :: String
262264
, flexGroup :: Maybe (FlexGroupState b a)
263265
}
264266

265267
resetState :: forall a b. FlexGroupState b a -> DocState b a
266-
resetState { position, buffer, annotations, indent: indent' } =
267-
{ position, buffer, annotations, indent: indent', flexGroup: Nothing }
268+
resetState { position, buffer, annotations, indent: indent', indentSpaces } =
269+
{ position, buffer, annotations, indent: indent', indentSpaces, flexGroup: Nothing }
268270

269271
storeState :: forall a b. List (DocCmd a) -> DocState b a -> FlexGroupState b a
270-
storeState stack { position, buffer, annotations, indent: indent' } =
271-
{ position, buffer, annotations, indent: indent', stack }
272+
storeState stack { position, buffer, annotations, indent: indent', indentSpaces } =
273+
{ position, buffer, annotations, indent: indent', indentSpaces, stack }
272274

273275
-- | Prints a documents given a printer and print options.
274276
-- |
@@ -299,7 +301,8 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
299301
}
300302
, buffer: Buffer.new printer.emptyBuffer
301303
, annotations: List.Nil
302-
, indent: ""
304+
, indent: 0
305+
, indentSpaces: ""
303306
, flexGroup: Nothing
304307
}
305308

@@ -312,10 +315,10 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
312315
Append doc1 doc2 ->
313316
go (Doc doc1 : Doc doc2 : stk) state
314317
Text len str
315-
| state.position.column == 0 && state.position.indent > 0 ->
318+
| state.position.column == 0 && state.indent > 0 ->
316319
go stack state
317-
{ position { column = state.position.indent }
318-
, buffer = Buffer.modify (printer.writeIndent state.position.indent state.indent) state.buffer
320+
{ position { column = state.indent }
321+
, buffer = Buffer.modify (printer.writeIndent state.indent state.indentSpaces) state.buffer
319322
}
320323
| otherwise -> do
321324
let nextColumn = state.position.column + len
@@ -332,27 +335,29 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
332335
go frame.stack $ resetState frame
333336
_ ->
334337
go stk state
335-
{ position { line = state.position.line + 1, column = 0 }
338+
{ position
339+
{ line = state.position.line + 1
340+
, column = 0
341+
, indent = state.indent
342+
, ribbonWidth = calcRibbonWidth (opts.pageWidth - state.indent)
343+
}
336344
, buffer = Buffer.modify printer.writeBreak state.buffer
337345
}
338346
Indent doc1
339347
| isJust state.flexGroup ->
340348
go (Doc doc1 : stk) state
341349
| otherwise ->
342-
go (Doc doc1 : Dedent state.indent state.position.indent : stk) state
343-
{ position
344-
{ indent = state.position.indent + opts.indentWidth
345-
, ribbonWidth = calcRibbonWidth (opts.pageWidth - state.position.indent)
346-
}
347-
, indent = state.indent <> opts.indentUnit
350+
go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state
351+
{ indent = state.indent + opts.indentWidth
352+
, indentSpaces = state.indentSpaces <> opts.indentUnit
348353
}
349354
Align width doc1
350355
| isJust state.flexGroup ->
351356
go (Doc doc1 : stk) state
352357
| otherwise ->
353-
go (Doc doc1 : Dedent state.indent state.position.indent : stk) state
354-
{ position { indent = state.position.indent + width }
355-
, indent = state.indent <> power " " width
358+
go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state
359+
{ indent = state.indent + width
360+
, indentSpaces = state.indentSpaces <> power " " width
356361
}
357362
FlexGroup doc1
358363
-- We only track the first flex group. This is equivalent to
@@ -370,8 +375,13 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
370375
| otherwise ->
371376
go (Doc doc1 : stk) state
372377
WithPosition k
373-
| state.position.column == 0 && state.position.indent > 0 -> do
374-
let renderPosition = state.position { column = state.position.indent }
378+
| state.position.column == 0 && state.indent > 0 -> do
379+
let
380+
renderPosition = state.position
381+
{ column = state.indent
382+
, indent = state.indent
383+
, ribbonWidth = calcRibbonWidth (opts.pageWidth - state.indent)
384+
}
375385
go (Doc (k renderPosition) : stk) state
376386
| otherwise ->
377387
go (Doc (k state.position) : stk) state
@@ -387,10 +397,10 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
387397
{ flexGroup = Nothing
388398
, buffer = Buffer.commit state.buffer
389399
}
390-
Dedent ind indWidth ->
400+
Dedent indSpaces ind ->
391401
go stk state
392-
{ position { indent = indWidth }
393-
, indent = ind
402+
{ indent = ind
403+
, indentSpaces = indSpaces
394404
}
395405
LeaveAnnotation ann anns ->
396406
go stk state
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
* Quisque finibus tellus non molestie porta. In non posuere metus, vitae tincidunt enim. Nam quis elit pharetra, elementum elit lacinia, efficitur nibh. Cras
2-
lobortis neque sed ante ornare rutrum. Maecenas sed urna nisl. Phasellus aliquam finibus ex vitae iaculis. Vestibulum ante ipsum primis in faucibus orci luctus
3-
et ultrices posuere cubilia curae; Suspendisse eget tortor eget sapien tincidunt vestibulum eu a velit. Pellentesque eu tortor ut lectus sodales ornare.
2+
lobortis neque sed ante ornare rutrum. Maecenas sed urna nisl. Phasellus aliquam finibus ex vitae iaculis. Vestibulum ante ipsum primis in faucibus orci
3+
luctus et ultrices posuere cubilia curae; Suspendisse eget tortor eget sapien tincidunt vestibulum eu a velit. Pellentesque eu tortor ut lectus sodales
4+
ornare.
45

5-
* Quisque finibus tellus non molestie porta. In non posuere metus, vitae tincidunt
6-
enim. Nam quis elit pharetra, elementum elit lacinia, efficitur nibh. Cras
7-
lobortis neque sed ante ornare rutrum. Maecenas sed urna nisl. Phasellus aliquam
8-
finibus ex vitae iaculis. Vestibulum ante ipsum primis in faucibus orci luctus
9-
et ultrices posuere cubilia curae; Suspendisse eget tortor eget sapien tincidunt
10-
vestibulum eu a velit. Pellentesque eu tortor ut lectus sodales ornare.
6+
* Quisque finibus tellus non molestie porta. In non posuere metus, vitae
7+
tincidunt enim. Nam quis elit pharetra, elementum elit lacinia, efficitur
8+
nibh. Cras lobortis neque sed ante ornare rutrum. Maecenas sed urna nisl.
9+
Phasellus aliquam finibus ex vitae iaculis. Vestibulum ante ipsum primis in
10+
faucibus orci luctus et ultrices posuere cubilia curae; Suspendisse eget
11+
tortor eget sapien tincidunt vestibulum eu a velit. Pellentesque eu tortor ut
12+
lectus sodales ornare.
1113

1214
* Quisque finibus tellus non molestie
1315
porta. In non posuere metus, vitae
14-
tincidunt enim. Nam quis elit pharetra,
15-
elementum elit lacinia, efficitur nibh.
16-
Cras lobortis neque sed ante ornare
17-
rutrum. Maecenas sed urna nisl.
18-
Phasellus aliquam finibus ex vitae
19-
iaculis. Vestibulum ante ipsum primis in
20-
faucibus orci luctus et ultrices posuere
21-
cubilia curae; Suspendisse eget tortor
22-
eget sapien tincidunt vestibulum eu a
23-
velit. Pellentesque eu tortor ut lectus
16+
tincidunt enim. Nam quis elit
17+
pharetra, elementum elit lacinia,
18+
efficitur nibh. Cras lobortis neque
19+
sed ante ornare rutrum. Maecenas sed
20+
urna nisl. Phasellus aliquam finibus
21+
ex vitae iaculis. Vestibulum ante
22+
ipsum primis in faucibus orci luctus
23+
et ultrices posuere cubilia curae;
24+
Suspendisse eget tortor eget sapien
25+
tincidunt vestibulum eu a velit.
26+
Pellentesque eu tortor ut lectus
2427
sodales ornare.
2528

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abcedefg 0 9 0 40 80
2+
abcedefg
3+
1 5 5 3 10
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module DodoWithPosition where
2+
3+
import Prelude
4+
5+
import Data.Foldable (intercalate)
6+
import Dodo (Doc, align, paragraph, plainText, print, text, twoSpaces, withPosition)
7+
import Effect (Effect)
8+
import Effect.Class.Console as Console
9+
10+
test1 :: forall a. Doc a
11+
test1 =
12+
paragraph
13+
[ text "abcedefg"
14+
, align 5 $ withPosition \pos ->
15+
text $ intercalate " "
16+
[ show pos.line
17+
, show pos.column
18+
, show pos.indent
19+
, show pos.ribbonWidth
20+
, show pos.pageWidth
21+
]
22+
]
23+
24+
main :: Effect Unit
25+
main = do
26+
Console.log $ print plainText (twoSpaces { ribbonRatio = 0.5 }) test1
27+
Console.log $ print plainText (twoSpaces { ribbonRatio = 0.5, pageWidth = 10 }) test1

0 commit comments

Comments
 (0)