Files
test-repo/src/document/doc-debug.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-01-20 09:54:32 -08:00
"use strict";
2017-01-28 07:50:22 -08:00
2021-01-04 10:04:30 +08:00
const { isConcat, getDocParts } = require("./doc-utils");
2017-01-20 09:54:32 -08:00
2021-01-04 10:04:30 +08:00
function flattenDoc(doc) {
if (isConcat(doc)) {
const res = [];
const parts = getDocParts(doc);
for (let i = 0; i < parts.length; ++i) {
const doc2 = parts[i];
if (typeof doc2 !== "string" && isConcat(doc2)) {
res.push(...flattenDoc(doc2).parts);
2017-01-20 09:54:32 -08:00
} else {
const flattened = flattenDoc(doc2);
if (flattened !== "") {
res.push(flattened);
}
}
}
2021-01-04 10:04:30 +08:00
return { type: "concat", parts: res };
} else if (doc.type === "if-break") {
return {
...doc,
2017-07-08 14:07:39 +02:00
breakContents:
doc.breakContents != null ? flattenDoc(doc.breakContents) : null,
flatContents:
doc.flatContents != null ? flattenDoc(doc.flatContents) : null,
};
} else if (doc.type === "group") {
return {
...doc,
2017-01-20 09:54:32 -08:00
contents: flattenDoc(doc.contents),
2017-02-15 19:56:11 -08:00
expandedStates: doc.expandedStates
? doc.expandedStates.map(flattenDoc)
: doc.expandedStates,
};
} else if (doc.contents) {
return { ...doc, contents: flattenDoc(doc.contents) };
2017-01-20 09:54:32 -08:00
}
return doc;
2017-01-20 09:54:32 -08:00
}
function printDoc(doc) {
if (typeof doc === "string") {
2017-01-20 14:55:41 -08:00
return JSON.stringify(doc);
2017-01-20 09:54:32 -08:00
}
2021-01-04 10:04:30 +08:00
if (isConcat(doc)) {
return "[" + getDocParts(doc).map(printDoc).join(", ") + "]";
}
2017-01-20 09:54:32 -08:00
if (doc.type === "line") {
2018-09-10 13:14:55 -03:00
if (doc.literal) {
2017-01-20 09:54:32 -08:00
return "literalline";
}
if (doc.hard) {
return "hardline";
}
if (doc.soft) {
return "softline";
}
return "line";
}
if (doc.type === "break-parent") {
return "breakParent";
}
if (doc.type === "trim") {
return "trim";
}
2017-01-20 09:54:32 -08:00
if (doc.type === "indent") {
return "indent(" + printDoc(doc.contents) + ")";
}
if (doc.type === "align") {
return doc.n === Number.NEGATIVE_INFINITY
? "dedentToRoot(" + printDoc(doc.contents) + ")"
: doc.n < 0
2018-11-07 09:12:25 +08:00
? "dedent(" + printDoc(doc.contents) + ")"
: doc.n.type === "root"
? "markAsRoot(" + printDoc(doc.contents) + ")"
: "align(" + JSON.stringify(doc.n) + ", " + printDoc(doc.contents) + ")";
2017-01-20 09:54:32 -08:00
}
if (doc.type === "if-break") {
return (
"ifBreak(" +
2017-01-20 09:54:32 -08:00
printDoc(doc.breakContents) +
(doc.flatContents ? ", " + printDoc(doc.flatContents) : "") +
")"
);
2017-01-20 09:54:32 -08:00
}
if (doc.type === "group") {
2017-01-28 07:50:22 -08:00
if (doc.expandedStates) {
return (
"conditionalGroup(" +
2017-01-28 07:50:22 -08:00
"[" +
doc.expandedStates.map(printDoc).join(",") +
"])"
);
}
return (
(doc.break ? "wrappedGroup" : "group") +
2017-01-20 10:12:37 -08:00
"(" +
printDoc(doc.contents) +
")"
);
2017-01-20 09:54:32 -08:00
}
if (doc.type === "fill") {
return "fill" + "(" + doc.parts.map(printDoc).join(", ") + ")";
}
if (doc.type === "line-suffix") {
return "lineSuffix(" + printDoc(doc.contents) + ")";
}
if (doc.type === "line-suffix-boundary") {
return "lineSuffixBoundary";
}
2017-02-15 19:56:11 -08:00
throw new Error("Unknown doc type " + doc.type);
2017-01-20 09:54:32 -08:00
}
module.exports = {
2020-01-14 19:25:24 +08:00
printDocToDebug(doc) {
2017-01-20 09:54:32 -08:00
return printDoc(flattenDoc(doc));
},
2017-01-20 10:12:37 -08:00
};