-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
doc-printer.js
386 lines (336 loc) · 10.8 KB
/
doc-printer.js
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
"use strict";
const docBuilders = require("./doc-builders");
const concat = docBuilders.concat;
const line = docBuilders.line;
const softline = docBuilders.softline;
const fill = docBuilders.fill;
const ifBreak = docBuilders.ifBreak;
const MODE_BREAK = 1;
const MODE_FLAT = 2;
function rootIndent() {
return {
indent: 0,
align: {
spaces: 0,
tabs: 0
}
};
}
function makeIndent(ind) {
return {
indent: ind.indent + 1,
align: ind.align
};
}
function makeAlign(ind, n) {
return {
indent: ind.indent,
align: {
spaces: ind.align.spaces + n,
tabs: ind.align.tabs + (n ? 1 : 0)
}
};
}
function fits(next, restCommands, width, mustBeFlat) {
let restIdx = restCommands.length;
const cmds = [next];
while (width >= 0) {
if (cmds.length === 0) {
if (restIdx === 0) {
return true;
} else {
cmds.push(restCommands[restIdx - 1]);
restIdx--;
continue;
}
}
const x = cmds.pop();
const ind = x[0];
const mode = x[1];
const doc = x[2];
if (typeof doc === "string") {
width -= doc.length;
} else {
switch (doc.type) {
case "concat":
for (var i = doc.parts.length - 1; i >= 0; i--) {
cmds.push([ind, mode, doc.parts[i]]);
}
break;
case "indent":
cmds.push([makeIndent(ind), mode, doc.contents]);
break;
case "align":
cmds.push([makeAlign(ind, doc.n), mode, doc.contents]);
break;
case "group":
if (mustBeFlat && doc.break) {
return false;
}
cmds.push([ind, doc.break ? MODE_BREAK : mode, doc.contents]);
break;
case "fill":
for (var i = doc.parts.length - 1; i >= 0; i--) {
cmds.push([ind, mode, doc.parts[i]]);
}
break;
case "if-break":
if (mode === MODE_BREAK) {
if (doc.breakContents) {
cmds.push([ind, mode, doc.breakContents]);
}
}
if (mode === MODE_FLAT) {
if (doc.flatContents) {
cmds.push([ind, mode, doc.flatContents]);
}
}
break;
case "line":
switch (mode) {
// fallthrough
case MODE_FLAT:
if (!doc.hard) {
if (!doc.soft) {
width -= 1;
}
break;
}
case MODE_BREAK:
return true;
}
break;
}
}
}
return false;
}
function printDocToString(doc, options) {
let width = options.printWidth;
let newLine = options.newLine || "\n";
let pos = 0;
// cmds is basically a stack. We've turned a recursive call into a
// while loop which is much faster. The while loop below adds new
// cmds to the array instead of recursively calling `print`.
let cmds = [[rootIndent(), MODE_BREAK, doc]];
let out = [];
let shouldRemeasure = false;
let lineSuffix = [];
while (cmds.length !== 0) {
const x = cmds.pop();
const ind = x[0];
const mode = x[1];
const doc = x[2];
if (typeof doc === "string") {
out.push(doc);
pos += doc.length;
} else {
switch (doc.type) {
case "concat":
for (var i = doc.parts.length - 1; i >= 0; i--) {
cmds.push([ind, mode, doc.parts[i]]);
}
break;
case "indent":
cmds.push([makeIndent(ind), mode, doc.contents]);
break;
case "align":
cmds.push([makeAlign(ind, doc.n), mode, doc.contents]);
break;
case "group":
switch (mode) {
// fallthrough
case MODE_FLAT:
if (!shouldRemeasure) {
cmds.push([
ind,
doc.break ? MODE_BREAK : MODE_FLAT,
doc.contents
]);
break;
}
case MODE_BREAK:
shouldRemeasure = false;
const next = [ind, MODE_FLAT, doc.contents];
let rem = width - pos;
if (!doc.break && fits(next, cmds, rem)) {
cmds.push(next);
} else {
// Expanded states are a rare case where a document
// can manually provide multiple representations of
// itself. It provides an array of documents
// going from the least expanded (most flattened)
// representation first to the most expanded. If a
// group has these, we need to manually go through
// these states and find the first one that fits.
if (doc.expandedStates) {
const mostExpanded =
doc.expandedStates[doc.expandedStates.length - 1];
if (doc.break) {
cmds.push([ind, MODE_BREAK, mostExpanded]);
break;
} else {
for (var i = 1; i < doc.expandedStates.length + 1; i++) {
if (i >= doc.expandedStates.length) {
cmds.push([ind, MODE_BREAK, mostExpanded]);
break;
} else {
const state = doc.expandedStates[i];
const cmd = [ind, MODE_FLAT, state];
if (fits(cmd, cmds, rem)) {
cmds.push(cmd);
break;
}
}
}
}
} else {
cmds.push([ind, MODE_BREAK, doc.contents]);
}
}
break;
}
break;
// Fills each line with as much code as possible before moving to a new
// line with the same indentation.
//
// Expects doc.parts to be an array of alternating code and
// whitespace. The whitespace contains the linebreaks.
//
// For example:
// ["I", line, "love", line, "monkeys"]
// or
// [{ type: group, ... }, softline, { type: group, ... }]
//
// It uses this parts structure to handle three main layout cases:
// * The first two non-whitespace items fit on the same line without
// breaking -> output both items and the whitespace "flat".
// * Only the first item fits on the line without breaking -> output the
// first item "flat" and the whitespace with "break".
// * Neither item fits on the line without breaking -> output the first
// item and the whitespace with "break".
case "fill":
let rem = width - pos;
const parts = doc.parts;
if (parts.length === 0) {
break;
}
const first = parts[0];
const firstCmd = [ind, MODE_FLAT, first];
if (parts.length === 1) {
if (fits(firstCmd, cmds, width - rem, true)) {
cmds.push(firstCmd);
} else {
cmds.push([ind, mode, first]);
}
break;
}
const split = parts[1];
if (parts.length === 2) {
if (fits(firstCmd, cmds, width - rem, true)) {
cmds.push([ind, MODE_FLAT, split]);
cmds.push(firstCmd);
} else {
cmds.push([ind, mode, split]);
cmds.push([ind, mode, first]);
}
break;
}
const second = parts[2];
const remaining = parts.slice(2);
const remainingCmd = [ind, MODE_BREAK, fill(remaining)];
const firstAndSecondCmd = [ind, MODE_FLAT, concat([first, split, second])];
if (fits(firstAndSecondCmd, cmds, rem, true)) {
cmds.push(remainingCmd)
cmds.push([ind, MODE_FLAT, split]);
cmds.push(firstCmd);
} else if (fits(firstCmd, cmds, width - rem, true)) {
cmds.push(remainingCmd)
cmds.push([ind, MODE_BREAK, split]);
cmds.push(firstCmd);
} else {
cmds.push(remainingCmd);
cmds.push([ind, MODE_BREAK, split]);
cmds.push([ind, MODE_BREAK, first]);
}
break;
case "if-break":
if (mode === MODE_BREAK) {
if (doc.breakContents) {
cmds.push([ind, mode, doc.breakContents]);
}
}
if (mode === MODE_FLAT) {
if (doc.flatContents) {
cmds.push([ind, mode, doc.flatContents]);
}
}
break;
case "line-suffix":
lineSuffix.push([ind, mode, doc.contents]);
break;
case "line-suffix-boundary":
if (lineSuffix.length > 0) {
cmds.push([ind, mode, { type: "line", hard: true }]);
}
break;
case "line":
switch (mode) {
// fallthrough
case MODE_FLAT:
if (!doc.hard) {
if (!doc.soft) {
out.push(" ");
pos += 1;
}
break;
} else {
// This line was forced into the output even if we
// were in flattened mode, so we need to tell the next
// group that no matter what, it needs to remeasure
// because the previous measurement didn't accurately
// capture the entire expression (this is necessary
// for nested groups)
shouldRemeasure = true;
}
case MODE_BREAK:
if (lineSuffix.length) {
cmds.push([ind, mode, doc]);
[].push.apply(cmds, lineSuffix.reverse());
lineSuffix = [];
break;
}
if (doc.literal) {
out.push(newLine);
pos = 0;
} else {
if (out.length > 0) {
// Trim whitespace at the end of line
while (
out.length > 0 &&
out[out.length - 1].match(/^[^\S\n]*$/)
) {
out.pop();
}
out[out.length - 1] = out[out.length - 1].replace(
/[^\S\n]*$/,
""
);
}
let length = ind.indent * options.tabWidth + ind.align.spaces;
let indentString = options.useTabs
? "\t".repeat(ind.indent + ind.align.tabs)
: " ".repeat(length);
out.push(newLine + indentString);
pos = length;
}
break;
}
break;
default:
}
}
}
return out.join("");
}
module.exports = { printDocToString };