-
Notifications
You must be signed in to change notification settings - Fork 4
/
doc.gleam
774 lines (711 loc) · 20.4 KB
/
doc.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
import gleam/int
import gleam/list
import gleam/order.{Eq, Gt, Lt}
import gleam/string
/// A document that can be pretty printed with `to_string`.
///
pub opaque type Document {
Line(size: Int)
Concat(docs: List(Document))
Text(text: String, length: Int)
Nest(doc: Document, indentation: Int)
ForceBreak(doc: Document)
Break(unbroken: String, broken: String)
FlexBreak(unbroken: String, broken: String)
Group(doc: Document)
}
/// Joins a document into the end of another.
///
/// ## Examples
///
/// ```gleam
/// from_string("pretty")
/// |> append(from_string(" printer"))
/// |> to_string(80)
/// // -> "pretty printer"
/// ```
///
pub fn append(to first: Document, doc second: Document) -> Document {
case first {
Concat(docs) -> Concat(list.append(docs, [second]))
_ -> Concat([first, second])
}
}
/// Joins multiple documents into the end of another.
///
/// This is a shorthand for `append(to: first, doc: concat(docs))`.
///
/// ## Examples
///
/// ```gleam
/// from_string("pretty")
/// |> append_docs([
/// from_string("printing"),
/// space,
/// from_string("rocks!"),
/// ])
/// |> to_string(80)
/// // -> "pretty printing rocks!"
/// ```
///
pub fn append_docs(first: Document, docs: List(Document)) -> Document {
append(to: first, doc: concat(docs))
}
/// A document after which the pretty printer can insert a new line.
/// A newline is added after a `break` document if the `group` it's part of
/// could not be rendered on a single line.
///
/// If the pretty printer decides to add a newline after `break` it will be
/// rendered as its second argument, otherwise as its first argument.
///
/// ## Examples
///
/// ```gleam
/// let message =
/// [from_string("pretty"), break("•", "↩"), from_string("printed")]
/// |> concat
/// |> group
///
/// message |> to_string(20)
/// // -> "pretty•printed"
///
/// message |> to_string(10)
/// // -> "pretty↩
/// // printed"
/// ```
///
pub fn break(unbroken: String, broken: String) {
Break(unbroken, broken)
}
/// Joins a list of documents into a single document.
///
/// The resulting pretty printed document would be the same as pretty printing
/// each document separately and concatenating it together with `<>`:
///
/// ```gleam
/// docs |> concat |> to_string(n) ==
/// docs |> list.map(to_string(n)) |> string.concat
/// ```
///
/// ## Examples
///
/// ```gleam
/// ["pretty", " ", "printed"]
/// |> list.map(from_string)
/// |> concat
/// |> to_string(80)
/// // -> "pretty printed"
/// ```
///
pub fn concat(docs: List(Document)) -> Document {
Concat(docs)
}
/// Joins a list of documents into a single one by inserting the given
/// separators between each existing document.
///
/// This is a shorthand for `join(docs, concat(separators))`.
///
/// ## Examples
///
/// ```gleam
/// ["wow", "so", "many", "commas"]
/// |> list.map(from_string)
/// |> concat_join([from_string(","), space])
/// |> to_string(80)
/// // -> "wow, so, many, commas"
/// ```
///
pub fn concat_join(
docs: List(Document),
with separators: List(Document),
) -> Document {
join(docs, concat(separators))
}
/// An empty document that is printed as an empty string.
///
/// ## Examples
///
/// ```gleam
/// empty |> to_string(80)
/// // -> ""
/// ```
///
pub const empty: Document = Concat([])
/// A document after which the pretty printer can insert a new line.
/// The difference with a simple `break` is that, the pretty printer will decide
/// wether to add a new line or not on a space-by-space basis.
///
/// While all the `break` inside a group are broken or not, some `flex_breaks`
/// may be broken and some not, depending wether the document can fit on a
/// single line or not. Hence the name _flex_.
///
/// If the pretty printer decides to add a newline after `flex_break` it will be
/// rendered as its first argument, otherwise as its first argument.
///
/// ## Examples
///
/// ```gleam
/// let message =
/// [from_string("pretty"), from_string("printed"), from_string("string")]
/// |> join(with: flex_break("•", "↩"))
/// |> group
///
/// message |> to_string(80)
/// // -> "pretty•printed•string"
///
/// message |> to_string(20)
/// // -> "pretty•printed↩
/// // string"
/// ```
///
pub fn flex_break(unbroken: String, broken: String) -> Document {
FlexBreak(unbroken: unbroken, broken: broken)
}
/// A document that is rendered as a single whitespace `" "` but can be broken
/// by the pretty printer into newlines instead.
/// Instead of splitting all spaces or no spaces (like it would do with
/// `space` inside a group), the pretty printer can decide on a space-by-space
/// basis.
///
/// If a `flex_space` inside a group needs to be broken to fit it on multiple
/// lines, following `flex_spaces` may end up not being broken.
///
/// This is a shorthand for `flex_break(" ", "")`.
///
/// ## Examples
///
/// ```gleam
/// let message =
/// ["Gleam", "is", "fun!"]
/// |> list.map(from_string)
/// |> join(with: flex_space)
/// |> group
///
/// to_string(message, 80)
/// // -> "Gleam is fun!"
///
/// to_string(message, 10)
/// // -> "Gleam is
/// fun!"
/// ```
///
/// to_string(message, 4)
/// // -> "Gleam
/// // is
/// // fun!"
///
pub const flex_space: Document = FlexBreak(" ", "")
/// Forces the pretty printer to break all the `break`s of the outermost
/// document. This still _has no effect on `group`s_ as the pretty printer will
/// always try to put them on a single line before splitting them.
///
/// ## Examples
///
/// ```gleam
/// [from_string("pretty"), break("•", "↩"), from_string("printed")]
/// |> concat
/// |> force_break
/// |> group
/// |> to_string(100)
/// // -> "pretty↩
/// // printed"
/// ```
///
pub fn force_break(doc: Document) -> Document {
ForceBreak(doc)
}
/// Turns a string into a document.
///
/// ## Examples
///
/// ```gleam
/// "doc" |> from_string |> to_string(80)
/// // -> "doc"
/// ```
///
pub fn from_string(string: String) -> Document {
Text(string, string.length(string))
}
/// Turns a string into a document whose length is not taken into account when
/// formatting it.
///
/// This kind of string can be used to render non-visible characters like ansi
/// color codes.
///
/// ## Examples
///
/// ```gleam
/// // Should break in two lines, but doesn't because of the zero_width_string
/// // does not contribute to the total line length.
/// [
/// zero_width_string("\u{001b}[1;31m"),
/// from_string("I'm a red"),
/// break(", ", ","),
/// from_string("bold text"),
/// ]
/// |> concat
/// |> group
/// |> to_string(20)
/// // -> "\u{001b}[1;31mI'm a red, bold text"
/// ```
///
pub fn zero_width_string(string: String) -> Document {
Text(string, 0)
}
/// Allows the pretty printer to break the `break` documents inside the given
/// group.
///
/// When the pretty printer runs into a group it first tries to render it on a
/// single line, displaying all the breaks as their first argument.
/// If the group fits this is the final pretty printed result.
///
/// However, if the group does not fit on a single line _all_ the `break`s
/// inside that group are rendered as their second argument and immediately
/// followed by a newline.
///
/// Any nested group is considered on its own and may or may not be split,
/// depending if it fits on a single line or not. So, even if the outermost
/// group is broken, its nested groups may still end up on a single line.
///
/// ## Examples
///
/// ```gleam
/// let food =
/// ["lasagna", "ravioli", "pizza"]
/// |> list.map(from_string) |> join(with: space) |> group
/// let message =
/// [from_string("Food I love:"), space, food] |> concat |> group
///
/// message |> to_string(80)
/// // -> "Food I love: lasagna ravioli pizza"
///
/// message |> to_string(30)
/// // -> "Food I love:
/// // lasagna ravioli pizza"
/// // ^-- After splitting the outer group, the inner one can fit
/// // on a single line so the pretty printer does not split it
///
/// message |> to_string(20)
/// // "Food I love:
/// // lasagna
/// // ravioli
/// // pizza"
/// // ^-- Even after splitting the outer group, the inner one wouldn't
/// // fit on a single line, so the pretty printer splits that as well
/// ```
///
pub fn group(doc: Document) -> Document {
Group(doc)
}
/// Joins a list of documents inserting the given separator between
/// each existing document.
///
/// ## Examples
///
/// ```gleam
/// let message =
/// ["Gleam", "is", "fun!"]
/// |> list.map(from_string)
/// |> join(with: space)
///
/// message |> to_string(80)
/// // -> "Gleam is fun!"
/// ```
///
pub fn join(docs: List(Document), with separator: Document) -> Document {
concat(list.intersperse(docs, separator))
}
/// A document that is always printed as a single new line.
///
/// ## Examples
///
/// ```gleam
/// line |> to_string(80)
/// // -> "\n"
/// ```
///
pub const line: Document = Line(1)
/// A document that is always printed as a series of consecutive newlines.
///
/// ## Examples
///
/// ```gleam
/// lines(3) |> to_string(80)
/// // -> "\n\n\n"
/// ```
///
pub fn lines(size: Int) -> Document {
Line(size)
}
/// Increases the nesting level of a document by the given amount.
///
/// When the pretty printer breaks a group by inserting a newline, it also adds
/// a whitespace padding equal to its nesting level.
///
/// ## Examples
///
/// ```gleam
/// let one = [space, from_string("one")] |> concat |> nest(by: 1)
/// let two = [space, from_string("two")] |> concat |> nest(by: 2)
/// let three = [space, from_string("three")] |> concat |> nest(by: 3)
/// let list = [from_string("list:"), one, two, three] |> concat |> group
///
/// list |> to_string(10)
/// // -> "list:
/// // one
/// // two
/// // three"
/// ```
///
pub fn nest(doc: Document, by indentation: Int) -> Document {
Nest(doc, indentation)
}
/// Joins together a list of documents and increases their nesting level.
///
/// This is a shorthand for `nest(concat(docs), by: indentation)`.
///
/// ## Examples
///
/// ```gleam
/// [from_string("one"), space, from_string("two")]
/// |> nest_docs(by: 2)
/// |> append(space)
/// |> append(from_string("three"))
/// |> group
/// |> to_string(5)
/// // ->
/// // one
/// // two
/// // three
/// ```
///
pub fn nest_docs(docs: List(Document), by indentation: Int) -> Document {
Nest(concat(docs), indentation)
}
/// Prefixes a document to another one.
///
/// ## Examples
///
/// ```gleam
/// from_string("printed!")
/// |> prepend(from_string("pretty "))
/// |> to_string(80)
/// // -> "pretty printed!"
/// ```
///
pub fn prepend(to first: Document, doc second: Document) -> Document {
case first {
Concat(docs) -> Concat([second, ..docs])
_ -> Concat([second, first])
}
}
/// Prefixes multiple documents to another one.
///
/// This is a shorthand for `prepend(to: first, doc: concat(docs))`.
///
/// ## Examples
///
/// ```gleam
/// from_string("fun!")
/// |> prepend_docs([from_string("Gleam "), from_string("is ")])
/// |> to_string(80)
/// // -> "Gleam is fun!"
/// ```
///
pub fn prepend_docs(first: Document, docs: List(Document)) -> Document {
prepend(to: first, doc: concat(docs))
}
/// A document that is always rendered as an empty string but can act as a
/// breaking point for the pretty printer.
///
/// This is a shorthand for `break("", "")`.
///
/// ## Examples
///
/// ```gleam
/// let doc = [from_string("soft"), soft_break, from_string("break")]
///
/// doc |> to_string(80)
/// // -> "softbreak"
///
/// doc |> to_string(5)
/// // -> "soft
/// // break"
/// ```
///
pub const soft_break: Document = Break("", "")
/// A document that is rendered as a single whitespace `" "` but can be broken
/// by the pretty printer into newlines instead.
///
/// This is a shorthand for `break(" ", "")`.
///
/// ## Examples
///
/// ```gleam
/// let doc =
/// ["pretty", "printed"]
/// |> list.map(from_string)
/// |> join(with: space)
///
/// doc |> to_string(80)
/// // -> "pretty printed"
///
/// doc |> to_string(10)
/// // -> "pretty
/// // printed"
/// ```
///
pub const space: Document = Break(" ", "")
/// Turns a document into a pretty printed string, trying to fit it into lines
/// of maximum size specified by `limit`.
///
/// The pretty printed process can be thought of as follows:
/// - the pretty printer first tries to print every group on a single line
/// - all the `break` documents are rendered as their first argument
/// - if the string fits on the specified width this is the result
/// - if the string does not fit on a single line the outermost group is split:
/// - all of its `break` documents are rendered as their second argument
/// - a newline is inserted after every `break`
/// - a padding of the given nesting level is added after every inserted
/// newline
/// - all inner groups are then considered on their own: the splitting of the
/// outermost group does not imply that the inner groups will be split as
/// well
///
/// ## Examples
///
/// For some examples of how pretty printing works for each kind of document you
/// can have a look at the package documentation.
/// There's also a
/// [step-by-step tutorial](https://hexdocs.pm/glam/01_gleam_lists.html)
/// that will guide you through the implementation of a simple pretty printer,
/// covering most of the Glam API.
///
pub fn to_string(doc: Document, limit: Int) -> String {
do_to_string("", limit, 0, [#(0, Unbroken, doc)])
}
type Mode {
Broken
ForceBroken
Unbroken
}
fn fits(
docs: List(#(Int, Mode, Document)),
max_width: Int,
current_width: Int,
) -> Bool {
case docs {
_ if current_width > max_width -> False
[] -> True
[#(indent, mode, doc), ..rest] ->
case doc {
Line(..) -> True
ForceBreak(..) -> False
Text(text: _text, length: length) ->
fits(rest, max_width, current_width + length)
Nest(doc, i) ->
[#(indent + i, mode, doc), ..rest]
|> fits(max_width, current_width)
Break(unbroken: unbroken, ..) | FlexBreak(unbroken: unbroken, ..) ->
case mode {
Broken | ForceBroken -> True
Unbroken ->
fits(rest, max_width, current_width + string.length(unbroken))
}
Group(doc) ->
fits([#(indent, mode, doc), ..rest], max_width, current_width)
Concat(docs) ->
list.map(docs, fn(doc) { #(indent, mode, doc) })
|> list.append(rest)
|> fits(max_width, current_width)
}
}
}
fn do_to_string(
acc: String,
max_width: Int,
current_width: Int,
docs: List(#(Int, Mode, Document)),
) -> String {
case docs {
[] -> acc
[#(indent, mode, doc), ..rest] ->
case doc {
Line(size) ->
{ acc <> string.repeat("\n", size) <> indentation(indent) }
|> do_to_string(max_width, indent, rest)
// Flex breaks ignore the current mode and are always reevaluated
FlexBreak(unbroken: unbroken, broken: broken) -> {
let new_unbroken_width = current_width + string.length(unbroken)
case fits(rest, max_width, new_unbroken_width) {
True ->
{ acc <> unbroken }
|> do_to_string(max_width, new_unbroken_width, rest)
False ->
{ acc <> broken <> "\n" <> indentation(indent) }
|> do_to_string(max_width, indent, rest)
}
}
Break(unbroken: unbroken, broken: broken) ->
case mode {
Unbroken -> {
let new_width = current_width + string.length(unbroken)
do_to_string(acc <> unbroken, max_width, new_width, rest)
}
Broken | ForceBroken ->
{ acc <> broken <> "\n" <> indentation(indent) }
|> do_to_string(max_width, indent, rest)
}
ForceBreak(doc) -> {
let docs = [#(indent, ForceBroken, doc), ..rest]
do_to_string(acc, max_width, current_width, docs)
}
Concat(docs) -> {
let docs =
list.map(docs, fn(doc) { #(indent, mode, doc) })
|> list.append(rest)
do_to_string(acc, max_width, current_width, docs)
}
Group(doc) -> {
let fits = fits([#(indent, Unbroken, doc)], max_width, current_width)
let new_mode = case fits {
True -> Unbroken
False -> Broken
}
let docs = [#(indent, new_mode, doc), ..rest]
do_to_string(acc, max_width, current_width, docs)
}
Nest(doc, i) -> {
let docs = [#(indent + i, mode, doc), ..rest]
do_to_string(acc, max_width, current_width, docs)
}
Text(text: text, length: length) ->
do_to_string(acc <> text, max_width, current_width + length, rest)
}
}
}
// --- DEBUGGING UTILITIES -----------------------------------------------------
const debug_nesting = 2
/// Returns a debug version of the given document that can be pretty printed
/// to see the structure of a document.
///
/// This can help you see how your data structures get turned into documents
/// and check if the document is what you'd expect.
///
/// - `group`s are surrounded by square brackets.
/// - `nest`s are surrounded by angle brackets and have a smal superscript
/// with the nesting.
/// - `concat`enated documents are separated by dots.
/// - `break`s are rendered surrounded by curly brackets and show both the
/// broken and unbroken versions.
/// - `line`s are rendered as the string `lf` followed by a superscript
/// number of lines.
///
pub fn debug(document: Document) -> Document {
case document {
Text(text, _length) -> {
let escaped = string.replace(each: "\"", with: "\\\"", in: text)
from_string("\"" <> escaped <> "\"")
}
ForceBreak(doc) -> parenthesise(debug(doc), "force(", ")")
Group(doc) ->
parenthesise(debug(doc), "[", "]")
|> force_break
Nest(doc, indentation) ->
parenthesise(debug(doc), superscript_number(indentation) <> "⟨", "⟩")
|> force_break
Break(" ", "") -> from_string("space")
Break(unbroken, broken) ->
from_string("{ \"" <> unbroken <> "\", \"" <> broken <> "\" }")
FlexBreak(" ", "") -> from_string("flex_space")
FlexBreak(unbroken, broken) ->
from_string("flex{ \"" <> unbroken <> "\", \"" <> broken <> "\" }")
Line(size) ->
case int.compare(size, 1) {
Lt | Eq -> from_string("lf")
Gt -> from_string("lf" <> superscript_number(size))
}
Concat(docs) ->
split_groups(flatten(docs))
|> list.map(fn(docs) {
case docs {
[] -> panic as "empty"
_ -> Nil
}
list.map(docs, debug)
|> join(with: flex_break(" . ", " ."))
|> group
})
|> join(with: concat([flex_break(" . ", " ."), line]))
}
}
fn parenthesise(document: Document, open: String, close: String) -> Document {
[
from_string(open),
nest(line, by: debug_nesting),
nest(document, by: debug_nesting),
line,
from_string(close),
]
|> concat
|> group
}
fn flatten(docs: List(Document)) -> List(Document) {
do_flatten(docs, [])
}
fn do_flatten(docs: List(Document), acc: List(Document)) -> List(Document) {
case docs {
[] -> list.reverse(acc)
[one] -> list.reverse([one, ..acc])
[Concat(one), Concat(two), ..rest] ->
do_flatten([Concat(list.append(one, two)), ..rest], acc)
[Text(one, len_one), Text(two, len_two), ..rest] ->
do_flatten([Text(one <> two, len_one + len_two), ..rest], acc)
[one, two, ..rest] -> do_flatten([two, ..rest], [one, ..acc])
}
}
fn split_groups(docs: List(Document)) -> List(List(Document)) {
do_split_groups(docs, [], [])
}
fn do_split_groups(
docs: List(Document),
current_group: List(Document),
acc: List(List(Document)),
) -> List(List(Document)) {
case docs {
[] ->
case current_group {
[] -> list.reverse(acc)
_ -> list.reverse([list.reverse(current_group), ..acc])
}
[Group(_) as doc, ..rest] ->
case current_group {
[] -> do_split_groups(rest, [], [[doc], ..acc])
_ ->
do_split_groups(rest, [], [[doc], list.reverse(current_group), ..acc])
}
[doc, ..rest] -> do_split_groups(rest, [doc, ..current_group], acc)
}
}
fn superscript_number(number: Int) -> String {
let assert Ok(digits) = int.digits(number, 10)
use acc, digit <- list.fold(over: digits, from: "")
let digit = case digit {
0 -> "⁰"
1 -> "¹"
2 -> "²"
3 -> "³"
4 -> "⁴"
5 -> "⁵"
6 -> "⁶"
7 -> "⁷"
8 -> "⁸"
9 -> "⁹"
_ -> panic as "not a digit"
}
acc <> digit
}
// --- UTILITY FUNCTIONS -------------------------------------------------------
fn indentation(size: Int) -> String {
string.repeat(" ", size)
}