Skip to content

Commit

Permalink
custom: Adapt up to internal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 16, 2024
1 parent 3671c53 commit e77f3cd
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 52 deletions.
27 changes: 18 additions & 9 deletions src/_customizations/doc-printer/compact.js
@@ -1,3 +1,12 @@
import {
DOC_TYPE_CONCAT,
DOC_TYPE_INDENT,
DOC_TYPE_GROUP,
DOC_TYPE_IF_BREAK,
DOC_TYPE_LABEL,
} from "../../document/constants.js";
import { getDocType } from "../../document/utils.js";

const clearUndefined = (obj) => {
for (const [key, value] of Object.entries(obj)) {
if (value === undefined) {
Expand Down Expand Up @@ -30,34 +39,34 @@ const resolveParts = (parts) => {
};

const compact = (doc) => {
switch (doc.type) {
case "concat":
switch (getDocType(doc)) {
case DOC_TYPE_CONCAT:
if (Array.isArray(doc)) {
return resolveParts(doc);
}
return {
...doc,
parts: resolveParts(doc.parts),
};
case "indent":
case "group":
case DOC_TYPE_INDENT:
case DOC_TYPE_GROUP:
return clearUndefined({
...doc,
contents: compact(doc.contents),
expandedStates: doc.expandedStates && doc.expandedStates.map(compact),
});
case "label":
case DOC_TYPE_LABEL:
return {
...doc,
contents: compact(doc.contents),
};
case "if-break":
case DOC_TYPE_IF_BREAK:
return {
...doc,
breakContents: doc.breakContents && compact(doc.breakContents),
flatContents: doc.flatContents && compact(doc.flatContents),
};
}
if (Array.isArray(doc)) {
return resolveParts(doc);
}
return doc;
};

Expand Down
40 changes: 27 additions & 13 deletions src/_customizations/doc-printer/group-lines.js
Expand Up @@ -5,16 +5,30 @@
// "444444"
// ]

import {
DOC_TYPE_CONCAT,
DOC_TYPE_INDENT,
DOC_TYPE_GROUP,
DOC_TYPE_IF_BREAK,
DOC_TYPE_LINE,
DOC_TYPE_LABEL,
} from "../../document/constants.js";

import isLineBreaking from "./is-line-breaking.js";

const mainGroupTypes = new Set(["concat", "group", "indent", "label"]);
const lineBreakTypes = new Set(["line", "if-break"]);
const mainGroupTypes = new Set([
DOC_TYPE_CONCAT,
DOC_TYPE_GROUP,
DOC_TYPE_INDENT,
DOC_TYPE_LABEL,
]);
const lineBreakTypes = new Set([DOC_TYPE_LINE, DOC_TYPE_IF_BREAK]);

const isVarDeclarationSeparator = (parts, index) =>
parts
.slice(index, index + 3)
.map(({ type }) => type)
.join("|") === "if-break|line|if-break";
.join("|") === `${DOC_TYPE_IF_BREAK}|${DOC_TYPE_LINE}|${DOC_TYPE_IF_BREAK}`;

const groupLines = function ({
ind,
Expand All @@ -26,7 +40,7 @@ const groupLines = function ({
fits,
cmds,
}) {
const contents = { type: "concat" };
const contents = { type: DOC_TYPE_CONCAT };
const next = { ind, MODE_FLAT, doc: contents };
const rem = width - pos;
const groupedParts = [];
Expand Down Expand Up @@ -58,16 +72,16 @@ const groupLines = function ({
//
// <token content>
// ","
// { "type": "line" }
// { "type": DOC_TYPE_LINE }
//
// We stop at every "group" and check if it fits curent line with eventual "," postfixes
// We stop at every DOC_TYPE_GROUP and check if it fits curent line with eventual "," postfixes
//
// One exception is variables declaration, which follows the pattern as:
//
// <token content>
// { "type": "if-break" } <- "," version if after the var name
// { "type": "line" }
// { "type": "if-break" } <- "," version if before the var name at begin of the line
// { "type": DOC_TYPE_IF_BREAK } <- "," version if after the var name
// { "type": DOC_TYPE_LINE }
// { "type": DOC_TYPE_IF_BREAK } <- "," version if before the var name at begin of the line
if (
currentPart &&
(typeof currentPart === "string" || mainGroupTypes.has(currentPart.type))
Expand Down Expand Up @@ -114,10 +128,10 @@ const groupLines = function ({
groupedParts.push(
// group of items
{
type: "group",
type: DOC_TYPE_GROUP,
break: false,
contents: {
type: "concat",
type: DOC_TYPE_CONCAT,
parts: parts.slice(
currentGroupStartIndex,
currentGroupLastItemEndIndex
Expand Down Expand Up @@ -177,10 +191,10 @@ const groupLines = function ({
currentGroupStartIndex += 3;
}
groupedParts.push({
type: "group",
type: DOC_TYPE_GROUP,
break: false,
contents: {
type: "concat",
type: DOC_TYPE_CONCAT,
parts: parts.slice(currentGroupStartIndex),
},
});
Expand Down
40 changes: 26 additions & 14 deletions src/_customizations/doc-printer/is-line-breaking.js
@@ -1,24 +1,42 @@
import {
DOC_TYPE_CONCAT,
DOC_TYPE_GROUP,
DOC_TYPE_FILL,
DOC_TYPE_IF_BREAK,
DOC_TYPE_LINE,
DOC_TYPE_LABEL,
DOC_TYPE_BREAK_PARENT,
} from "../../document/constants.js";
import { getDocType } from "../../document/utils.js";

const isLineBreaking = function (doc, mode, MODE_BREAK, MODE_FLAT) {
const remaining = [doc];
while (remaining.length > 0) {
const current = remaining.pop();
switch (current.type) {
case "break-parent":
switch (getDocType(current)) {
case DOC_TYPE_BREAK_PARENT:
return true;
case "concat":
case "fill":
case DOC_TYPE_CONCAT:
if (Array.isArray(current)) {
for (let i = current.length - 1; i >= 0; i--) {
remaining.push(current[i]);
}
break;
}
// fallthrough
case DOC_TYPE_FILL:
for (let i = current.parts.length - 1; i >= 0; i--) {
remaining.push(current.parts[i]);
}
break;
case "group":
case "label":
case DOC_TYPE_GROUP:
case DOC_TYPE_LABEL:
if (current.break) {
return true;
}
remaining.push(current.contents);
break;
case "if-break":
case DOC_TYPE_IF_BREAK:
if (mode === MODE_BREAK) {
if (current.breakContents) {
remaining.push(current.breakContents);
Expand All @@ -30,17 +48,11 @@ const isLineBreaking = function (doc, mode, MODE_BREAK, MODE_FLAT) {
}
}
break;
case "line":
case DOC_TYPE_LINE:
if (current.hard) {
return true;
}
break;
default:
if (Array.isArray(current)) {
for (let i = current.length - 1; i >= 0; i--) {
remaining.push(current[i]);
}
}
}
}
return false;
Expand Down
4 changes: 3 additions & 1 deletion src/_customizations/includes-assignment.js
@@ -1,6 +1,8 @@
import { DOC_TYPE_CONCAT } from "../document/constants.js";

import tokensToString from "./tokens-to-string.js";

const includesAssignment = (parts) =>
tokensToString({ type: "concat", parts }).includes("=");
tokensToString({ type: DOC_TYPE_CONCAT, parts }).includes("=");

export default includesAssignment;
38 changes: 23 additions & 15 deletions src/_customizations/tokens-to-string.js
@@ -1,31 +1,39 @@
import {
DOC_TYPE_STRING,
DOC_TYPE_CONCAT,
DOC_TYPE_INDENT,
DOC_TYPE_ALIGN,
DOC_TYPE_GROUP,
} from "../document/constants.js";
import { getDocType } from "../document/utils.js";

const tokensToString = (data) => {
const stack = [data];
let out = "";

while (stack.length > 0) {
const item = stack.pop();

if (typeof item === "string") {
out += item;
continue;
}
switch (item.type) {
case "concat":
for (let i = item.parts.length - 1; i >= 0; i--) {
stack.push(item.parts[i]);
}
break;
case "group":
case "align":
case "indent":
stack.push(item.contents);
switch (getDocType(item)) {
case DOC_TYPE_STRING:
out += item;
break;
default:
case DOC_TYPE_CONCAT:
if (Array.isArray(item)) {
for (let i = item.length - 1; i >= 0; i--) {
stack.push(item[i]);
}
break;
}
for (let i = item.parts.length - 1; i >= 0; i--) {
stack.push(item.parts[i]);
}
break;
case DOC_TYPE_GROUP:
case DOC_TYPE_ALIGN:
case DOC_TYPE_INDENT:
stack.push(item.contents);
break;
}
}
return out.trim();
Expand Down

0 comments on commit e77f3cd

Please sign in to comment.