2017-01-10 12:18:22 -05:00
|
|
|
"use strict";
|
2017-01-28 07:50:22 -08:00
|
|
|
|
2016-12-23 13:38:10 -05:00
|
|
|
const MODE_BREAK = 1;
|
|
|
|
|
const MODE_FLAT = 2;
|
|
|
|
|
|
2017-04-06 23:49:37 -03:00
|
|
|
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) {
|
2017-05-01 14:28:11 -07:00
|
|
|
if (n === -Infinity) {
|
|
|
|
|
return {
|
|
|
|
|
indent: 0,
|
|
|
|
|
align: {
|
|
|
|
|
spaces: 0,
|
|
|
|
|
tabs: 0
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 23:49:37 -03:00
|
|
|
return {
|
|
|
|
|
indent: ind.indent,
|
|
|
|
|
align: {
|
|
|
|
|
spaces: ind.align.spaces + n,
|
|
|
|
|
tabs: ind.align.tabs + (n ? 1 : 0)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-23 13:38:10 -05:00
|
|
|
function fits(next, restCommands, width) {
|
|
|
|
|
let restIdx = restCommands.length;
|
2017-01-28 07:50:22 -08:00
|
|
|
const cmds = [next];
|
2017-01-05 12:21:57 -05:00
|
|
|
while (width >= 0) {
|
|
|
|
|
if (cmds.length === 0) {
|
|
|
|
|
if (restIdx === 0) {
|
2016-12-23 13:38:10 -05:00
|
|
|
return true;
|
2017-01-05 12:21:57 -05:00
|
|
|
} else {
|
2016-12-23 13:38:10 -05:00
|
|
|
cmds.push(restCommands[restIdx - 1]);
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2016-12-23 13:38:10 -05:00
|
|
|
restIdx--;
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2016-12-23 13:38:10 -05:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 12:18:22 -05:00
|
|
|
const x = cmds.pop();
|
2017-01-18 15:31:46 -08:00
|
|
|
const ind = x[0];
|
|
|
|
|
const mode = x[1];
|
|
|
|
|
const doc = x[2];
|
2017-01-05 12:21:57 -05:00
|
|
|
|
|
|
|
|
if (typeof doc === "string") {
|
2016-12-23 13:38:10 -05:00
|
|
|
width -= doc.length;
|
2017-01-05 12:21:57 -05:00
|
|
|
} else {
|
|
|
|
|
switch (doc.type) {
|
2017-01-13 15:03:53 -05:00
|
|
|
case "concat":
|
|
|
|
|
for (var i = doc.parts.length - 1; i >= 0; i--) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, mode, doc.parts[i]]);
|
2016-12-23 13:38:10 -05:00
|
|
|
}
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
break;
|
|
|
|
|
case "indent":
|
2017-04-06 23:49:37 -03:00
|
|
|
cmds.push([makeIndent(ind), mode, doc.contents]);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case "align":
|
|
|
|
|
cmds.push([makeAlign(ind, doc.n), mode, doc.contents]);
|
2017-01-13 15:03:53 -05:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case "group":
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, doc.break ? MODE_BREAK : mode, doc.contents]);
|
2017-01-13 15:03:53 -05:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case "if-break":
|
|
|
|
|
if (mode === MODE_BREAK) {
|
2017-01-18 12:13:50 -08:00
|
|
|
if (doc.breakContents) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, mode, doc.breakContents]);
|
2017-01-18 12:13:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mode === MODE_FLAT) {
|
|
|
|
|
if (doc.flatContents) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, mode, doc.flatContents]);
|
2017-01-18 12:13:50 -08:00
|
|
|
}
|
2017-01-13 15:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case "line":
|
|
|
|
|
switch (mode) {
|
|
|
|
|
// fallthrough
|
|
|
|
|
case MODE_FLAT:
|
|
|
|
|
if (!doc.hard) {
|
|
|
|
|
if (!doc.soft) {
|
|
|
|
|
width -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case MODE_BREAK:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-12-23 13:38:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 23:49:37 -03:00
|
|
|
function printDocToString(doc, options) {
|
|
|
|
|
let width = options.printWidth;
|
|
|
|
|
let newLine = options.newLine || "\n";
|
2016-12-23 13:38:10 -05:00
|
|
|
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`.
|
2017-04-06 23:49:37 -03:00
|
|
|
let cmds = [[rootIndent(), MODE_BREAK, doc]];
|
2016-12-23 13:38:10 -05:00
|
|
|
let out = [];
|
2017-01-04 15:24:10 -05:00
|
|
|
let shouldRemeasure = false;
|
2017-02-13 07:13:16 -08:00
|
|
|
let lineSuffix = [];
|
2017-01-26 14:57:43 -05:00
|
|
|
|
2017-01-05 12:21:57 -05:00
|
|
|
while (cmds.length !== 0) {
|
2017-01-10 12:18:22 -05:00
|
|
|
const x = cmds.pop();
|
2017-01-18 15:31:46 -08:00
|
|
|
const ind = x[0];
|
|
|
|
|
const mode = x[1];
|
|
|
|
|
const doc = x[2];
|
2016-12-23 13:38:10 -05:00
|
|
|
|
2017-01-05 12:21:57 -05:00
|
|
|
if (typeof doc === "string") {
|
2016-12-23 13:38:10 -05:00
|
|
|
out.push(doc);
|
2017-01-04 15:24:10 -05:00
|
|
|
|
2017-01-05 12:21:57 -05:00
|
|
|
pos += doc.length;
|
|
|
|
|
} else {
|
|
|
|
|
switch (doc.type) {
|
2017-01-13 15:03:53 -05:00
|
|
|
case "concat":
|
|
|
|
|
for (var i = doc.parts.length - 1; i >= 0; i--) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, mode, doc.parts[i]]);
|
2017-01-05 12:21:57 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
break;
|
|
|
|
|
case "indent":
|
2017-04-06 23:49:37 -03:00
|
|
|
cmds.push([makeIndent(ind), mode, doc.contents]);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case "align":
|
|
|
|
|
cmds.push([makeAlign(ind, doc.n), mode, doc.contents]);
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
break;
|
|
|
|
|
case "group":
|
|
|
|
|
switch (mode) {
|
|
|
|
|
// fallthrough
|
|
|
|
|
case MODE_FLAT:
|
|
|
|
|
if (!shouldRemeasure) {
|
|
|
|
|
cmds.push([
|
|
|
|
|
ind,
|
|
|
|
|
doc.break ? MODE_BREAK : MODE_FLAT,
|
|
|
|
|
doc.contents
|
|
|
|
|
]);
|
2017-01-05 12:21:57 -05:00
|
|
|
|
|
|
|
|
break;
|
2017-01-13 15:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case MODE_BREAK:
|
|
|
|
|
shouldRemeasure = false;
|
|
|
|
|
|
2017-01-28 07:50:22 -08:00
|
|
|
const next = [ind, MODE_FLAT, doc.contents];
|
2017-01-26 14:16:42 -08:00
|
|
|
let rem = width - pos;
|
2017-01-13 15:03:53 -05:00
|
|
|
|
|
|
|
|
if (!doc.break && fits(next, cmds, rem)) {
|
|
|
|
|
cmds.push(next);
|
2017-01-05 12:21:57 -05:00
|
|
|
} else {
|
2017-01-13 15:03:53 -05:00
|
|
|
// 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) {
|
2017-04-12 10:16:11 -07:00
|
|
|
const mostExpanded =
|
|
|
|
|
doc.expandedStates[doc.expandedStates.length - 1];
|
2017-01-13 15:03:53 -05:00
|
|
|
|
|
|
|
|
if (doc.break) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, MODE_BREAK, mostExpanded]);
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-04 15:24:10 -05:00
|
|
|
break;
|
2017-01-05 12:21:57 -05:00
|
|
|
} else {
|
2017-01-13 15:03:53 -05:00
|
|
|
for (var i = 1; i < doc.expandedStates.length + 1; i++) {
|
|
|
|
|
if (i >= doc.expandedStates.length) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, MODE_BREAK, mostExpanded]);
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
const state = doc.expandedStates[i];
|
2017-01-28 07:50:22 -08:00
|
|
|
const cmd = [ind, MODE_FLAT, state];
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
if (fits(cmd, cmds, rem)) {
|
|
|
|
|
cmds.push(cmd);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-04 15:24:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-13 15:03:53 -05:00
|
|
|
} else {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, MODE_BREAK, doc.contents]);
|
2017-01-04 15:24:10 -05:00
|
|
|
}
|
2016-12-23 13:38:10 -05:00
|
|
|
}
|
2017-01-13 15:03:53 -05:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "if-break":
|
|
|
|
|
if (mode === MODE_BREAK) {
|
2017-01-18 12:13:50 -08:00
|
|
|
if (doc.breakContents) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, mode, doc.breakContents]);
|
2017-01-18 12:13:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mode === MODE_FLAT) {
|
|
|
|
|
if (doc.flatContents) {
|
2017-01-28 07:50:22 -08:00
|
|
|
cmds.push([ind, mode, doc.flatContents]);
|
2017-01-18 12:13:50 -08:00
|
|
|
}
|
2016-12-23 13:38:10 -05:00
|
|
|
}
|
2016-12-31 14:38:33 -05:00
|
|
|
|
2017-01-26 14:57:43 -05:00
|
|
|
break;
|
|
|
|
|
case "line-suffix":
|
2017-02-13 07:13:16 -08:00
|
|
|
lineSuffix.push([ind, mode, doc.contents]);
|
2016-12-23 13:38:10 -05:00
|
|
|
break;
|
2017-02-23 09:26:26 -08:00
|
|
|
case "line-suffix-boundary":
|
|
|
|
|
if (lineSuffix.length > 0) {
|
|
|
|
|
cmds.push([ind, mode, { type: "line", hard: true }]);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2017-01-13 15:03:53 -05:00
|
|
|
case "line":
|
|
|
|
|
switch (mode) {
|
|
|
|
|
// fallthrough
|
|
|
|
|
case MODE_FLAT:
|
|
|
|
|
if (!doc.hard) {
|
|
|
|
|
if (!doc.soft) {
|
|
|
|
|
out.push(" ");
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
pos += 1;
|
|
|
|
|
}
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
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;
|
|
|
|
|
}
|
2017-01-05 12:21:57 -05:00
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
case MODE_BREAK:
|
2017-02-13 07:13:16 -08:00
|
|
|
if (lineSuffix.length) {
|
|
|
|
|
cmds.push([ind, mode, doc]);
|
|
|
|
|
[].push.apply(cmds, lineSuffix.reverse());
|
|
|
|
|
lineSuffix = [];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 15:03:53 -05:00
|
|
|
if (doc.literal) {
|
2017-02-13 07:13:16 -08:00
|
|
|
out.push(newLine);
|
2017-01-13 15:03:53 -05:00
|
|
|
pos = 0;
|
|
|
|
|
} else {
|
2017-01-18 12:38:33 -08:00
|
|
|
if (out.length > 0) {
|
|
|
|
|
// Trim whitespace at the end of line
|
2017-04-13 18:31:49 -07:00
|
|
|
while (
|
|
|
|
|
out.length > 0 &&
|
|
|
|
|
out[out.length - 1].match(/^[^\S\n]*$/)
|
|
|
|
|
) {
|
|
|
|
|
out.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 09:40:11 -07:00
|
|
|
if (out.length) {
|
|
|
|
|
out[out.length - 1] = out[out.length - 1].replace(
|
|
|
|
|
/[^\S\n]*$/,
|
|
|
|
|
""
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-01-18 12:38:33 -08:00
|
|
|
}
|
|
|
|
|
|
2017-04-06 23:49:37 -03:00
|
|
|
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;
|
2017-01-13 15:03:53 -05:00
|
|
|
}
|
|
|
|
|
break;
|
2017-01-05 12:21:57 -05:00
|
|
|
}
|
|
|
|
|
break;
|
2017-01-13 15:03:53 -05:00
|
|
|
default:
|
2016-12-23 13:38:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-05 12:21:57 -05:00
|
|
|
}
|
2016-12-23 13:38:10 -05:00
|
|
|
return out.join("");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-20 10:12:37 -08:00
|
|
|
module.exports = { printDocToString };
|