2017-01-10 20:18:22 +03:00
|
|
|
"use strict";
|
2016-12-28 05:40:04 +03:00
|
|
|
const assert = require("assert");
|
|
|
|
|
|
|
|
function assertDoc(val) {
|
|
|
|
assert(
|
2017-01-05 20:21:57 +03:00
|
|
|
typeof val === "string" || val != null && typeof val.type === "string",
|
2016-12-28 05:40:04 +03:00
|
|
|
"Value is a valid document"
|
|
|
|
);
|
|
|
|
}
|
2016-12-23 21:38:10 +03:00
|
|
|
|
|
|
|
function fromString(text) {
|
2016-12-28 05:40:04 +03:00
|
|
|
return "" + text;
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function concat(parts) {
|
2016-12-28 05:40:04 +03:00
|
|
|
parts.forEach(assertDoc);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
return {type: "concat", parts};
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function indent(n, contents) {
|
2016-12-28 05:40:04 +03:00
|
|
|
assertDoc(contents);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
return {type: "indent", contents, n};
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-04 23:24:10 +03:00
|
|
|
function group(contents, opts) {
|
|
|
|
opts = opts || {};
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-28 05:40:04 +03:00
|
|
|
assertDoc(contents);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2017-01-04 23:24:10 +03:00
|
|
|
return {
|
2017-01-05 20:21:57 +03:00
|
|
|
type: "group",
|
2017-01-04 23:24:10 +03:00
|
|
|
contents: contents,
|
|
|
|
break: !!opts.shouldBreak,
|
|
|
|
expandedStates: opts.expandedStates
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function multilineGroup(contents, opts) {
|
2017-01-09 20:09:04 +03:00
|
|
|
return group(
|
|
|
|
contents,
|
|
|
|
Object.assign(opts || {}, {shouldBreak: hasHardLine(contents)})
|
|
|
|
);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-04 23:24:10 +03:00
|
|
|
function conditionalGroup(states, opts) {
|
2017-01-05 20:21:57 +03:00
|
|
|
return group(states[0], Object.assign(opts || {}, {expandedStates: states}));
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-05 19:36:41 +03:00
|
|
|
function ifBreak(contents) {
|
2017-01-05 20:21:57 +03:00
|
|
|
return {type: "if-break", contents};
|
2017-01-05 19:36:41 +03:00
|
|
|
}
|
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
function iterDoc(topDoc, func) {
|
2017-01-05 20:21:57 +03:00
|
|
|
const docs = [ topDoc ];
|
|
|
|
while (docs.length !== 0) {
|
2016-12-23 21:38:10 +03:00
|
|
|
const doc = docs.pop();
|
|
|
|
let res = undefined;
|
|
|
|
|
2017-01-05 20:21:57 +03:00
|
|
|
if (typeof doc === "string") {
|
2016-12-23 21:38:10 +03:00
|
|
|
const res = func("string", doc);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
if (res) {
|
2016-12-23 21:38:10 +03:00
|
|
|
return res;
|
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
} else {
|
2016-12-23 21:38:10 +03:00
|
|
|
const res = func(doc.type, doc);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
if (res) {
|
2016-12-23 21:38:10 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:21:57 +03:00
|
|
|
if (doc.type === "concat") {
|
|
|
|
for (var i = doc.parts.length - 1; i >= 0; i--) {
|
2016-12-23 21:38:10 +03:00
|
|
|
docs.push(doc.parts[i]);
|
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
} else if (doc.type !== "line") {
|
2016-12-23 21:38:10 +03:00
|
|
|
docs.push(doc.contents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:21:57 +03:00
|
|
|
const line = {type: "line"};
|
|
|
|
const softline = {type: "line", soft: true};
|
|
|
|
const hardline = {type: "line", hard: true};
|
|
|
|
const literalline = {type: "line", hard: true, literal: true};
|
2016-12-23 21:38:10 +03:00
|
|
|
|
|
|
|
function isEmpty(n) {
|
|
|
|
return typeof n === "string" && n.length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function join(sep, arr) {
|
|
|
|
var res = [];
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
if (i !== 0) {
|
2016-12-23 21:38:10 +03:00
|
|
|
res.push(sep);
|
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
res.push(arr[i]);
|
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
return concat(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFirstString(doc) {
|
|
|
|
return iterDoc(doc, (type, doc) => {
|
2017-01-05 20:21:57 +03:00
|
|
|
if (type === "string" && doc.trim().length !== 0) {
|
2016-12-23 21:38:10 +03:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasHardLine(doc) {
|
|
|
|
// TODO: If we hit a group, check if it's already marked as a
|
|
|
|
// multiline group because they should be marked bottom-up.
|
|
|
|
return !!iterDoc(doc, (type, doc) => {
|
2017-01-05 20:21:57 +03:00
|
|
|
switch (type) {
|
|
|
|
case "line":
|
|
|
|
if (doc.hard) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _makeIndent(n) {
|
|
|
|
var s = "";
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
for (var i = 0; i < n; i++) {
|
2016-12-23 21:38:10 +03:00
|
|
|
s += " ";
|
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MODE_BREAK = 1;
|
|
|
|
const MODE_FLAT = 2;
|
|
|
|
|
|
|
|
function fits(next, restCommands, width) {
|
|
|
|
let restIdx = restCommands.length;
|
2017-01-05 20:21:57 +03:00
|
|
|
const cmds = [ next ];
|
|
|
|
while (width >= 0) {
|
|
|
|
if (cmds.length === 0) {
|
|
|
|
if (restIdx === 0) {
|
2016-12-23 21:38:10 +03:00
|
|
|
return true;
|
2017-01-05 20:21:57 +03:00
|
|
|
} else {
|
2016-12-23 21:38:10 +03:00
|
|
|
cmds.push(restCommands[restIdx - 1]);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
restIdx--;
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 20:18:22 +03:00
|
|
|
const x = cmds.pop();
|
|
|
|
const ind = x[0];
|
|
|
|
const mode = x[1];
|
|
|
|
const doc = x[2];
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
if (typeof doc === "string") {
|
2016-12-23 21:38:10 +03:00
|
|
|
width -= doc.length;
|
2017-01-05 20:21:57 +03:00
|
|
|
} 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([ ind + doc.n, mode, doc.contents ]);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "group":
|
2017-01-09 20:09:04 +03:00
|
|
|
cmds.push([ ind, doc.break ? MODE_BREAK : mode, doc.contents ]);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
case "if-break":
|
|
|
|
if (mode === MODE_BREAK) {
|
|
|
|
cmds.push([ ind, mode, doc.contents ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "line":
|
|
|
|
switch (mode) {
|
|
|
|
// fallthrough
|
|
|
|
case MODE_FLAT:
|
|
|
|
if (!doc.hard) {
|
|
|
|
if (!doc.soft) {
|
|
|
|
width -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
case MODE_BREAK:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function print(w, doc) {
|
|
|
|
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-01-05 20:21:57 +03:00
|
|
|
let cmds = [ [ 0, MODE_BREAK, doc ] ];
|
2016-12-23 21:38:10 +03:00
|
|
|
let out = [];
|
2017-01-04 23:24:10 +03:00
|
|
|
let shouldRemeasure = false;
|
2017-01-05 20:21:57 +03:00
|
|
|
while (cmds.length !== 0) {
|
2017-01-10 20:18:22 +03:00
|
|
|
const x = cmds.pop();
|
|
|
|
const ind = x[0];
|
|
|
|
const mode = x[1];
|
|
|
|
const doc = x[2];
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-05 20:21:57 +03:00
|
|
|
if (typeof doc === "string") {
|
2016-12-23 21:38:10 +03:00
|
|
|
out.push(doc);
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
pos += doc.length;
|
2017-01-05 20:21:57 +03:00
|
|
|
} 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([ ind + doc.n, mode, doc.contents ]);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "group":
|
|
|
|
switch (mode) {
|
|
|
|
// fallthrough
|
|
|
|
case MODE_FLAT:
|
|
|
|
if (!shouldRemeasure) {
|
|
|
|
cmds.push([
|
|
|
|
ind,
|
2017-01-09 20:09:04 +03:00
|
|
|
doc.break ? MODE_BREAK : MODE_FLAT,
|
2017-01-05 20:21:57 +03:00
|
|
|
doc.contents
|
|
|
|
]);
|
|
|
|
|
|
|
|
break;
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
case MODE_BREAK:
|
|
|
|
shouldRemeasure = false;
|
|
|
|
|
|
|
|
const next = [ ind, MODE_FLAT, doc.contents ];
|
|
|
|
let rem = w - 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 ]);
|
|
|
|
|
2017-01-04 23:24:10 +03:00
|
|
|
break;
|
2017-01-05 20:21:57 +03:00
|
|
|
} else {
|
|
|
|
for (var i = 1; i < doc.expandedStates.length + 1; i++) {
|
|
|
|
if (i >= doc.expandedStates.length) {
|
|
|
|
cmds.push([ ind, MODE_BREAK, mostExpanded ]);
|
|
|
|
|
2017-01-04 23:24:10 +03:00
|
|
|
break;
|
2017-01-05 20:21:57 +03:00
|
|
|
} else {
|
|
|
|
const state = doc.expandedStates[i];
|
|
|
|
const cmd = [ ind, MODE_FLAT, state ];
|
|
|
|
|
|
|
|
if (fits(cmd, cmds, rem)) {
|
|
|
|
cmds.push(cmd);
|
|
|
|
|
|
|
|
break;
|
2017-01-04 23:24:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
} else {
|
|
|
|
cmds.push([ ind, MODE_BREAK, doc.contents ]);
|
|
|
|
}
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
break;
|
2017-01-05 20:21:57 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "if-break":
|
|
|
|
if (mode === MODE_BREAK) {
|
|
|
|
cmds.push([ ind, MODE_BREAK, doc.contents ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-01-05 19:36:41 +03:00
|
|
|
}
|
2016-12-31 22:38:33 +03:00
|
|
|
|
2017-01-05 20:21:57 +03:00
|
|
|
case MODE_BREAK:
|
|
|
|
if (out.length > 0) {
|
|
|
|
const lastString = out[out.length - 1];
|
|
|
|
|
|
|
|
if (lastString.match(/^\s*\n\s*$/)) {
|
|
|
|
out[out.length - 1] = "\n";
|
|
|
|
}
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
|
|
|
|
if (doc.literal) {
|
|
|
|
out.push("\n");
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
} else {
|
|
|
|
out.push("\n" + _makeIndent(ind));
|
|
|
|
|
|
|
|
pos = ind;
|
|
|
|
}
|
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
break;
|
2017-01-05 20:21:57 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-05 20:21:57 +03:00
|
|
|
}
|
2016-12-23 21:38:10 +03:00
|
|
|
return out.join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-01-05 20:21:57 +03:00
|
|
|
fromString,
|
|
|
|
concat,
|
|
|
|
isEmpty,
|
|
|
|
join,
|
|
|
|
line,
|
|
|
|
softline,
|
|
|
|
hardline,
|
|
|
|
literalline,
|
|
|
|
group,
|
|
|
|
multilineGroup,
|
|
|
|
conditionalGroup,
|
|
|
|
ifBreak,
|
|
|
|
hasHardLine,
|
|
|
|
indent,
|
|
|
|
print,
|
|
|
|
getFirstString
|
2016-12-23 21:38:10 +03:00
|
|
|
};
|