-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathres_doc.res
More file actions
362 lines (337 loc) · 11.1 KB
/
res_doc.res
File metadata and controls
362 lines (337 loc) · 11.1 KB
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
module MiniBuffer = Res_minibuffer
type mode = Break | Flat
type lineStyle =
| Classic /* fits? -> replace with space */
| Soft /* fits? -> replaced with nothing */
| Hard /* always included, forces breaks in parents */
/* always included, forces breaks in parents, but doesn't increase indentation
use case: template literals, multiline string content */
| Literal
type rec t =
| Nil
| Text(string)
| Concat(list<t>)
| Indent(t)
| IfBreaks({
yes: t,
no: t,
mutable broken: bool,
}) /* when broken is true, treat as the yes branch */
| LineSuffix(t)
| LineBreak(lineStyle)
| Group({mutable shouldBreak: bool, doc: t})
| CustomLayout(list<t>)
| BreakParent
let nil = Nil
let line = LineBreak(Classic)
let hardLine = LineBreak(Hard)
let softLine = LineBreak(Soft)
let literalLine = LineBreak(Literal)
let text = s => Text(s)
/* Optimization. We eagerly collapse and reduce whatever allocation we can */
let rec _concat = (acc, l) =>
switch l {
| list{Text(s1), Text(s2), ...rest} => list{Text(s1 ++ s2), ..._concat(acc, rest)}
| list{Nil, ...rest} => _concat(acc, rest)
| list{Concat(l2), ...rest} => _concat(_concat(acc, rest), l2) /* notice the order here */
| list{x, ...rest} =>
let rest1 = _concat(acc, rest)
if rest1 === rest {
l
} else {
list{x, ...rest1}
}
| list{} => acc
}
let concat = l => Concat(_concat(list{}, l))
let indent = d => Indent(d)
let ifBreaks = (t, f) => IfBreaks({yes: t, no: f, broken: false})
let lineSuffix = d => LineSuffix(d)
let group = d => Group({shouldBreak: false, doc: d})
let breakableGroup = (~forceBreak, d) => Group({shouldBreak: forceBreak, doc: d})
let customLayout = gs => CustomLayout(gs)
let breakParent = BreakParent
let space = Text(" ")
let comma = Text(",")
let dot = Text(".")
let dotdot = Text("..")
let dotdotdot = Text("...")
let lessThan = Text("<")
let greaterThan = Text(">")
let lbrace = Text("{")
let rbrace = Text("}")
let lparen = Text("(")
let rparen = Text(")")
let lbracket = Text("[")
let rbracket = Text("]")
let question = Text("?")
let tilde = Text("~")
let equal = Text("=")
let trailingComma = ifBreaks(comma, nil)
let doubleQuote = Text("\"")
let propagateForcedBreaks = doc => {
let rec walk = doc =>
switch doc {
| Text(_) | Nil | LineSuffix(_) => false
| BreakParent => true
| LineBreak(Hard | Literal) => true
| LineBreak(Classic | Soft) => false
| Indent(children) =>
let childForcesBreak = walk(children)
childForcesBreak
| IfBreaks({yes: trueDoc, no: falseDoc} as ib) =>
let falseForceBreak = walk(falseDoc)
if falseForceBreak {
let _ = walk(trueDoc)
ib.broken = true
true
} else {
let forceBreak = walk(trueDoc)
forceBreak
}
| Group({shouldBreak: forceBreak, doc: children} as gr) =>
let childForcesBreak = walk(children)
let shouldBreak = forceBreak || childForcesBreak
gr.shouldBreak = shouldBreak
shouldBreak
| Concat(children) => List.fold_left((forceBreak, child) => {
let childForcesBreak = walk(child)
forceBreak || childForcesBreak
}, false, children)
| CustomLayout(children) =>
/* When using CustomLayout, we don't want to propagate forced breaks
* from the children up. By definition it picks the first layout that fits
* otherwise it takes the last of the list.
* However we do want to propagate forced breaks in the sublayouts. They
* might need to be broken. We just don't propagate them any higher here */
let _ = walk(Concat(children))
false
}
let _ = walk(doc)
}
/* See documentation in interface file */
let rec willBreak = doc =>
switch doc {
| LineBreak(Hard | Literal) | BreakParent | Group({shouldBreak: true}) => true
| Group({doc}) | Indent(doc) | CustomLayout(list{doc, ..._}) => willBreak(doc)
| Concat(docs) => List.exists(willBreak, docs)
| IfBreaks({yes, no}) => willBreak(yes) || willBreak(no)
| _ => false
}
let join = (~sep, docs) => {
let rec loop = (acc, sep, docs) =>
switch docs {
| list{} => List.rev(acc)
| list{x} => List.rev(list{x, ...acc})
| list{x, ...xs} => loop(list{sep, x, ...acc}, sep, xs)
}
concat(loop(list{}, sep, docs))
}
let fits = (w, stack) => {
let width = ref(w)
let result = ref(None)
let rec calculate = (indent, mode, doc) =>
switch (mode, doc) {
| _ if result.contents !== None => ()
| _ if width.contents < 0 => result := Some(false)
| (_, Nil)
| (_, LineSuffix(_))
| (_, BreakParent) => ()
| (_, Text(txt)) => width := width.contents - String.length(txt)
| (_, Indent(doc)) => calculate(indent + 2, mode, doc)
| (Flat, LineBreak(Hard))
| (Flat, LineBreak(Literal)) =>
result := Some(true)
| (Flat, LineBreak(Classic)) => width := width.contents - 1
| (Flat, LineBreak(Soft)) => ()
| (Break, LineBreak(_)) => result := Some(true)
| (_, Group({shouldBreak: true, doc})) => calculate(indent, Break, doc)
| (_, Group({doc})) => calculate(indent, mode, doc)
| (_, IfBreaks({yes: breakDoc, broken: true})) => calculate(indent, mode, breakDoc)
| (Break, IfBreaks({yes: breakDoc})) => calculate(indent, mode, breakDoc)
| (Flat, IfBreaks({no: flatDoc})) => calculate(indent, mode, flatDoc)
| (_, Concat(docs)) => calculateConcat(indent, mode, docs)
| (_, CustomLayout(list{hd, ..._})) =>
/* TODO: if we have nested custom layouts, what we should do here? */
calculate(indent, mode, hd)
| (_, CustomLayout(list{})) => ()
}
and calculateConcat = (indent, mode, docs) =>
if result.contents === None {
switch docs {
| list{} => ()
| list{doc, ...rest} =>
calculate(indent, mode, doc)
calculateConcat(indent, mode, rest)
}
}
let rec calculateAll = stack =>
switch (result.contents, stack) {
| (Some(r), _) => r
| (None, list{}) => width.contents >= 0
| (None, list{(indent, mode, doc), ...rest}) =>
calculate(indent, mode, doc)
calculateAll(rest)
}
calculateAll(stack)
}
let toString = (~width, doc) => {
propagateForcedBreaks(doc)
let buffer = MiniBuffer.create(1000)
let rec process = (~pos, lineSuffices, stack) =>
switch stack {
| list{(ind, mode, doc) as cmd, ...rest} =>
switch doc {
| Nil | BreakParent => process(~pos, lineSuffices, rest)
| Text(txt) =>
MiniBuffer.add_string(buffer, txt)
process(~pos=String.length(txt) + pos, lineSuffices, rest)
| LineSuffix(doc) => process(~pos, list{(ind, mode, doc), ...lineSuffices}, rest)
| Concat(docs) =>
let ops = List.map(doc => (ind, mode, doc), docs)
process(~pos, lineSuffices, List.append(ops, rest))
| Indent(doc) => process(~pos, lineSuffices, list{(ind + 2, mode, doc), ...rest})
| IfBreaks({yes: breakDoc, broken: true}) =>
process(~pos, lineSuffices, list{(ind, mode, breakDoc), ...rest})
| IfBreaks({yes: breakDoc, no: flatDoc}) =>
if mode == Break {
process(~pos, lineSuffices, list{(ind, mode, breakDoc), ...rest})
} else {
process(~pos, lineSuffices, list{(ind, mode, flatDoc), ...rest})
}
| LineBreak(lineStyle) =>
if mode == Break {
switch lineSuffices {
| list{} =>
if lineStyle == Literal {
MiniBuffer.add_char(buffer, '\n')
process(~pos=0, list{}, rest)
} else {
MiniBuffer.flush_newline(buffer)
MiniBuffer.add_string(buffer, @doesNotRaise String.make(ind, ' '))
process(~pos=ind, list{}, rest)
}
| _docs =>
process(~pos=ind, list{}, List.concat(list{List.rev(lineSuffices), list{cmd, ...rest}}))
}
} else {
/* mode = Flat */
let pos = switch lineStyle {
| Classic =>
MiniBuffer.add_string(buffer, " ")
pos + 1
| Hard =>
MiniBuffer.flush_newline(buffer)
0
| Literal =>
MiniBuffer.add_char(buffer, '\n')
0
| Soft => pos
}
process(~pos, lineSuffices, rest)
}
| Group({shouldBreak, doc}) =>
if shouldBreak || !fits(width - pos, list{(ind, Flat, doc), ...rest}) {
process(~pos, lineSuffices, list{(ind, Break, doc), ...rest})
} else {
process(~pos, lineSuffices, list{(ind, Flat, doc), ...rest})
}
| CustomLayout(docs) =>
let rec findGroupThatFits = groups =>
switch groups {
| list{} => Nil
| list{lastGroup} => lastGroup
| list{doc, ...docs} =>
if fits(width - pos, list{(ind, Flat, doc), ...rest}) {
doc
} else {
findGroupThatFits(docs)
}
}
let doc = findGroupThatFits(docs)
process(~pos, lineSuffices, list{(ind, Flat, doc), ...rest})
}
| list{} =>
switch lineSuffices {
| list{} => ()
| suffices => process(~pos=0, list{}, List.rev(suffices))
}
}
process(~pos=0, list{}, list{(0, Flat, doc)})
MiniBuffer.contents(buffer)
}
@live
let debug = t => {
let rec toDoc = x =>
switch x {
| Nil => text("nil")
| BreakParent => text("breakparent")
| Text(txt) => text("text(\"" ++ (txt ++ "\")"))
| LineSuffix(doc) =>
group(
concat(list{text("linesuffix("), indent(concat(list{line, toDoc(doc)})), line, text(")")}),
)
| Concat(list{}) => text("concat()")
| Concat(docs) =>
group(
concat(list{
text("concat("),
indent(
concat(list{line, join(~sep=concat(list{text(","), line}), List.map(toDoc, docs))}),
),
line,
text(")"),
}),
)
| CustomLayout(docs) =>
group(
concat(list{
text("customLayout("),
indent(
concat(list{line, join(~sep=concat(list{text(","), line}), List.map(toDoc, docs))}),
),
line,
text(")"),
}),
)
| Indent(doc) => concat(list{text("indent("), softLine, toDoc(doc), softLine, text(")")})
| IfBreaks({yes: trueDoc, broken: true}) => toDoc(trueDoc)
| IfBreaks({yes: trueDoc, no: falseDoc}) =>
group(
concat(list{
text("ifBreaks("),
indent(
concat(list{line, toDoc(trueDoc), concat(list{text(","), line}), toDoc(falseDoc)}),
),
line,
text(")"),
}),
)
| LineBreak(break_) =>
let breakTxt = switch break_ {
| Classic => "Classic"
| Soft => "Soft"
| Hard => "Hard"
| Literal => "Liteal"
}
text("LineBreak(" ++ (breakTxt ++ ")"))
| Group({shouldBreak, doc}) =>
group(
concat(list{
text("Group("),
indent(
concat(list{
line,
text("{shouldBreak: " ++ (string_of_bool(shouldBreak) ++ "}")),
concat(list{text(","), line}),
toDoc(doc),
}),
),
line,
text(")"),
}),
)
}
let doc = toDoc(t)
toString(~width=10, doc) |> print_endline
}