Skip to content

Commit 4c792a7

Browse files
committed
Backoff group optimization
1 parent 75934c2 commit 4c792a7

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/Dodo.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
386386
go (Doc doc1 : LeaveFlexGroup : stk) state
387387
{ flexGroup = FlexGroupOpen
388388
}
389+
FlexGroupOpen | state.position.ribbonWidth > 0 ->
390+
go (Doc doc1 : stk) state
391+
{ flexGroup = FlexGroupReset $ storeState stack state
392+
, buffer = Buffer.branch state.buffer
393+
}
389394
_ ->
390395
go (Doc doc1 : stk) state
391396
FlexAlt flexDoc doc1 -> case state.flexGroup of

test/snapshots/DodoFlexPrefixOptimization.output

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ out: e
1010
abce
1111
out: a
1212
out: b
13+
out: b
1314
out: c
1415
out: d
1516
abcd
@@ -18,3 +19,17 @@ out: b
1819
out: c
1920
out: e
2021
abce
22+
out: a
23+
out: b
24+
out: c
25+
out: d
26+
out: e
27+
out: b
28+
out: c
29+
out: d
30+
out: e
31+
out: e
32+
out: f
33+
out: g
34+
* a b c d
35+
e f g

test/snapshots/DodoFlexPrefixOptimization.purs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module DodoFlexPrefixOptimization where
33
import Prelude
44

55
import Data.Foldable (fold)
6-
import Dodo (Doc, flexAlt, flexGroup, fourSpaces, plainText, print, text, withPosition)
6+
import Dodo (Doc, align, flexAlt, flexGroup, fourSpaces, paragraph, plainText, print, text, withPosition)
77
import Effect (Effect)
88
import Effect.Class.Console as Console
99
import Effect.Unsafe (unsafePerformEffect)
@@ -34,9 +34,28 @@ test2 = flexGroup $ fold
3434
]
3535
]
3636

37+
-- Bad output due to aggressive optimization:
38+
-- * a
39+
-- b c d e
40+
-- f g
41+
test3 :: forall a. Doc a
42+
test3 = flexGroup $ text "* " <> align 2 letters
43+
where
44+
letters =
45+
paragraph $ map logText
46+
[ "a"
47+
, "b"
48+
, "c"
49+
, "d"
50+
, "e"
51+
, "f"
52+
, "g"
53+
]
54+
3755
main :: Effect Unit
3856
main = do
3957
Console.log $ print plainText (fourSpaces { pageWidth = 1 }) test1
4058
Console.log $ print plainText fourSpaces test1
4159
Console.log $ print plainText (fourSpaces { pageWidth = 1 }) test2
4260
Console.log $ print plainText fourSpaces test2
61+
Console.log $ print plainText (fourSpaces { pageWidth = 10 }) test3

0 commit comments

Comments
 (0)