@@ -20,6 +20,7 @@ module Dodo
20
20
, appendSpaceBreak
21
21
, flexAlt
22
22
, flexGroup
23
+ , flexSelect
23
24
, paragraph
24
25
, textParagraph
25
26
, enclose
@@ -72,11 +73,20 @@ annotate :: forall a. a -> Doc a -> Doc a
72
73
annotate = notEmpty <<< Annotate
73
74
74
75
-- | Attempts to layout the document with flex alternatives, falling back
75
- -- | to defaults if they don 't fit the page width.
76
+ -- | to defaults if it doesn 't fit the page width.
76
77
flexGroup :: forall a . Doc a -> Doc a
77
78
flexGroup = notEmpty case _ of
78
- doc@(FlexGroup _) -> doc
79
- doc -> FlexGroup doc
79
+ doc@(FlexSelect _ a b) | isEmpty a && isEmpty b -> doc
80
+ doc -> FlexSelect doc Empty Empty
81
+
82
+ -- | Attempts to layout the first document with flex alternatives, falling
83
+ -- | back to defaults if it doesn't fit the page width. If the flex alternatives
84
+ -- | are used then the second document will be appended, otherwise the third
85
+ -- | document will be appended.
86
+ flexSelect :: forall a . Doc a -> Doc a -> Doc a -> Doc a
87
+ flexSelect doc1 doc2 doc3
88
+ | isEmpty doc1 = doc2
89
+ | otherwise = FlexSelect doc1 doc2 doc3
80
90
81
91
-- | Attempts to layout the first document when in a flex group, falling back
82
92
-- | to the second as a default.
@@ -244,11 +254,11 @@ data DocCmd a
244
254
= Doc (Doc a )
245
255
| Dedent String Int
246
256
| LeaveAnnotation a (List a )
247
- | LeaveFlexGroup
257
+ | LeaveFlexGroup ( Doc a ) ( Doc a )
248
258
249
259
data FlexGroupStatus b a
250
260
= NoFlexGroup
251
- | FlexGroupOpen
261
+ | FlexGroupPending
252
262
| FlexGroupReset (FlexGroupState b a )
253
263
254
264
type FlexGroupState b a =
@@ -381,12 +391,12 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
381
391
{ position { nextIndent = state.position.nextIndent + width }
382
392
, indentSpaces = state.indentSpaces <> power " " width
383
393
}
384
- FlexGroup doc1 -> case state.flexGroup of
394
+ FlexSelect doc1 doc2 doc3 -> case state.flexGroup of
385
395
NoFlexGroup ->
386
- go (Doc doc1 : LeaveFlexGroup : stk) state
387
- { flexGroup = FlexGroupOpen
396
+ go (Doc doc1 : LeaveFlexGroup doc2 doc3 : stk) state
397
+ { flexGroup = FlexGroupPending
388
398
}
389
- FlexGroupOpen | state.position.ribbonWidth > 0 ->
399
+ FlexGroupPending | state.position.ribbonWidth > 0 ->
390
400
go (Doc doc1 : stk) state
391
401
{ flexGroup = FlexGroupReset $ storeState stack state
392
402
, buffer = Buffer .branch state.buffer
@@ -396,7 +406,7 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
396
406
FlexAlt flexDoc doc1 -> case state.flexGroup of
397
407
FlexGroupReset _ ->
398
408
go (Doc flexDoc : stk) state
399
- FlexGroupOpen | state.position.ribbonWidth > 0 ->
409
+ FlexGroupPending | state.position.ribbonWidth > 0 ->
400
410
go (Doc flexDoc : stk) state
401
411
{ flexGroup = FlexGroupReset $ storeState (Doc doc1 : stk) state
402
412
, buffer = Buffer .branch state.buffer
@@ -415,11 +425,16 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
415
425
}
416
426
Empty ->
417
427
go stk state
418
- LeaveFlexGroup ->
419
- go stk state
420
- { flexGroup = NoFlexGroup
421
- , buffer = Buffer .commit state.buffer
422
- }
428
+ LeaveFlexGroup doc1 doc2 -> case state.flexGroup of
429
+ NoFlexGroup ->
430
+ go (Doc doc2 : stk) state
431
+ { buffer = Buffer .commit state.buffer
432
+ }
433
+ _ ->
434
+ go (Doc doc1 : stk) state
435
+ { flexGroup = NoFlexGroup
436
+ , buffer = Buffer .commit state.buffer
437
+ }
423
438
Dedent indSpaces ind ->
424
439
go stk state
425
440
{ position { nextIndent = ind }
0 commit comments