UNPKG

tolkfmt-test-dev

Version:

Code formatter for the Tolk programming language

83 lines 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.blank = exports.empty = exports.concat = exports.group = exports.indent = exports.ifBreak = exports.breakParent = exports.lineSuffix = exports.hardLine = exports.softLine = exports.line = exports.text = void 0; exports.blankLinesBetween = blankLinesBetween; const text = (value) => ({ $: "Text", value, }); exports.text = text; const line = () => ({ $: "Line", }); exports.line = line; const softLine = () => ({ $: "SoftLine", }); exports.softLine = softLine; const hardLine = () => ({ $: "HardLine", }); exports.hardLine = hardLine; const lineSuffix = (suffix) => ({ $: "LineSuffix", suffix, }); exports.lineSuffix = lineSuffix; const breakParent = () => ({ $: "BreakParent", }); exports.breakParent = breakParent; const ifBreak = (breakContent, flatContent = undefined) => ({ $: "IfBreak", breakContent, flatContent, }); exports.ifBreak = ifBreak; const indent = (content) => ({ $: "Indent", indent: 4, content, }); exports.indent = indent; const group = (content) => ({ $: "Group", content: (0, exports.concat)(content), }); exports.group = group; const concat = (parts) => ({ $: "Concat", parts, }); exports.concat = concat; const empty = () => ({ $: "Empty", }); exports.empty = empty; function blankLinesBetween(a, b, comments) { const aTrailing = comments.get(a.id)?.trailing ?? []; const bLeading = comments.get(b.id)?.leading ?? []; // If there are trailing comments after `a`, use the last trailing comment as the reference // If there are leading comments before `b`, use the first leading comment as the reference const { endPosition } = a; const { startPosition } = b; let endRow = endPosition.row; let startRow = startPosition.row; if (aTrailing.length > 0) { const lastTrailing = aTrailing.at(-1); if (lastTrailing) { const { endRow: lastEndRow } = lastTrailing; endRow = lastEndRow; } } if (bLeading.length > 0) { const [firstLeading] = bLeading; const { startRow: firstStartRow } = firstLeading; startRow = firstStartRow; } const raw = startRow - endRow - 1; return Math.max(0, raw); } const blank = (n) => (n === 0 ? (0, exports.hardLine)() : (0, exports.concat)([(0, exports.hardLine)(), (0, exports.hardLine)()])); exports.blank = blank; //# sourceMappingURL=doc.js.map