@codegraft/core 0.1.0-beta.0 → 0.1.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collection.d.ts +20 -5
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +101 -68
- package/dist/collection.js.map +1 -1
- package/dist/comment-attachment.d.ts.map +1 -1
- package/dist/comment-attachment.js +2 -0
- package/dist/comment-attachment.js.map +1 -1
- package/dist/containers.d.ts +15 -0
- package/dist/containers.d.ts.map +1 -0
- package/dist/containers.js +39 -0
- package/dist/containers.js.map +1 -0
- package/dist/edit-collector.d.ts +6 -11
- package/dist/edit-collector.d.ts.map +1 -1
- package/dist/edit-collector.js +6 -25
- package/dist/edit-collector.js.map +1 -1
- package/dist/evaluate.js +8 -3
- package/dist/evaluate.js.map +1 -1
- package/dist/format.d.ts +51 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +133 -0
- package/dist/format.js.map +1 -0
- package/dist/formatter.d.ts +73 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +225 -0
- package/dist/formatter.js.map +1 -0
- package/dist/generated/node-types.d.ts +9 -6
- package/dist/generated/node-types.d.ts.map +1 -1
- package/dist/generated/node-types.js +1 -1
- package/dist/generated/node-types.js.map +1 -1
- package/dist/types.d.ts +15 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edit-collector.d.ts","sourceRoot":"","sources":["../src/edit-collector.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE3C;;;;;;;;GAQG;AACH,qBAAa,aAAa;;
|
|
1
|
+
{"version":3,"file":"edit-collector.d.ts","sourceRoot":"","sources":["../src/edit-collector.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE3C;;;;;;;;GAQG;AACH,qBAAa,aAAa;;gBAIZ,MAAM,EAAE,MAAM;IAI1B,yFAAyF;IACzF,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI;IAMhE,6BAA6B;IAC7B,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAKxC;oGACgG;IAChG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAI7C,6DAA6D;IAC7D,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAI9C,QAAQ,IAAI,MAAM;IAIlB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS;CAWvC"}
|
package/dist/edit-collector.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import MagicString from 'magic-string';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* demand, a source map) via magic-string.
|
|
3
|
+
* A pure edit buffer: collects text edits against a source string and produces the transformed code
|
|
4
|
+
* (and, on demand, a source map) via magic-string. It knows nothing about lines, indentation, or
|
|
5
|
+
* formatting — that policy lives in the {@link Formatter}, which drives this buffer.
|
|
5
6
|
*
|
|
6
|
-
* Overlap is resolved **first-wins**: an edit overlapping one already accepted is
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* position, so the generated map is precise.
|
|
7
|
+
* Overlap is resolved **first-wins**: an edit overlapping one already accepted is dropped silently —
|
|
8
|
+
* the text-layer side of outer-wins. Because edits are applied through magic-string at their original
|
|
9
|
+
* offsets, the kept text keeps its original position, so the generated map is precise.
|
|
10
10
|
*/
|
|
11
11
|
export class EditCollector {
|
|
12
12
|
#magic;
|
|
13
|
-
#source;
|
|
14
13
|
#claimed = [];
|
|
15
14
|
constructor(source) {
|
|
16
15
|
this.#magic = new MagicString(source);
|
|
17
|
-
this.#source = source;
|
|
18
16
|
}
|
|
19
17
|
/** Replace `[start, end)` with `replacement` (an empty range inserts before `start`). */
|
|
20
18
|
overwrite(start, end, replacement) {
|
|
@@ -31,23 +29,6 @@ export class EditCollector {
|
|
|
31
29
|
return;
|
|
32
30
|
this.#magic.remove(start, end);
|
|
33
31
|
}
|
|
34
|
-
/** Delete the whole lines `[start, end)` touches — from the start of `start`'s line (leading
|
|
35
|
-
* indentation included) through the newline after `end`'s line, so nothing blank is left behind.
|
|
36
|
-
* With `collapseBlankBefore`, also absorb whole blank lines immediately above (a separator before
|
|
37
|
-
* a dropped block). */
|
|
38
|
-
removeLines(start, end, collapseBlankBefore = false) {
|
|
39
|
-
let lineStart = this.#source.lastIndexOf('\n', start - 1) + 1;
|
|
40
|
-
if (collapseBlankBefore) {
|
|
41
|
-
while (lineStart > 0) {
|
|
42
|
-
const prevStart = this.#source.lastIndexOf('\n', lineStart - 2) + 1;
|
|
43
|
-
if (this.#source.slice(prevStart, lineStart - 1).trim() !== '')
|
|
44
|
-
break; // a non-blank line stops it
|
|
45
|
-
lineStart = prevStart;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
const newline = this.#source.indexOf('\n', end);
|
|
49
|
-
this.remove(lineStart, newline === -1 ? this.#source.length : newline + 1);
|
|
50
|
-
}
|
|
51
32
|
/** Insert `text` at `index`, attached to the left chunk. A point insertion: it claims no
|
|
52
33
|
* range, so it composes with edits on either side (and survives removal of the right side). */
|
|
53
34
|
insertLeft(index, text) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edit-collector.js","sourceRoot":"","sources":["../src/edit-collector.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,cAAc,CAAA;AAGtC;;;;;;;;GAQG;AACH,MAAM,OAAO,aAAa;IACf,MAAM,CAAa;IACnB,
|
|
1
|
+
{"version":3,"file":"edit-collector.js","sourceRoot":"","sources":["../src/edit-collector.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,cAAc,CAAA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AAK,EAAE,IAAI,EAAE,CAAC,CAAA;IAC/E,CAAC;IAED,sFAAsF;IACtF,iFAAiF;IACjF,MAAM,CAAC,KAAa,EAAE,GAAW;QAC/B,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,IAAI,KAAK,GAAG,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;QACtE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAA;QAChC,OAAO,IAAI,CAAA;IACb,CAAC;CACF"}
|
package/dist/evaluate.js
CHANGED
|
@@ -37,10 +37,15 @@ function evalNode(node, context) {
|
|
|
37
37
|
return object[field(node, 'property').text];
|
|
38
38
|
}
|
|
39
39
|
case 'call_expression': {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
// A condition's only calls are methods on the context (`$$.BATI.has(x)`); calling the method on
|
|
41
|
+
// its object preserves the receiver, so one whose `this` matters (a real `Set`'s `has`) works.
|
|
42
|
+
const callee = field(node, 'function');
|
|
43
|
+
assert(callee.type === 'member_expression', `only method calls are supported: '${node.text}'`);
|
|
44
|
+
const object = evalNode(field(callee, 'object'), context);
|
|
45
|
+
const method = object[field(callee, 'property').text];
|
|
46
|
+
assert(typeof method === 'function', `not callable: '${callee.text}'`);
|
|
42
47
|
const args = field(node, 'arguments').children.map((arg) => evalNode(arg, context));
|
|
43
|
-
return
|
|
48
|
+
return method.apply(object, args);
|
|
44
49
|
}
|
|
45
50
|
case 'identifier':
|
|
46
51
|
return context; // the namespace root resolves to the context value
|
package/dist/evaluate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../src/evaluate.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,QAAQ,EA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K,IAAI;YACP,OAAO,IAAI,EAAE,IAAI,KAAK,EAAE,CAAA;QAC1B,KAAK,KAAK;YACR,OAAO,IAAI,EAAE,KAAK,KAAK,EAAE,CAAA;QAC3B,KAAK,KAAK;YACR,OAAO,IAAI,EAAE,KAAK,KAAK,EAAE,CAAA;QAC3B,KAAK,GAAG;YACN,OAAQ,IAAI,EAAa,GAAI,KAAK,EAAa,CAAA;QACjD,KAAK,IAAI;YACP,OAAQ,IAAI,EAAa,IAAK,KAAK,EAAa,CAAA;QAClD,KAAK,GAAG;YACN,OAAQ,IAAI,EAAa,GAAI,KAAK,EAAa,CAAA;QACjD,KAAK,IAAI;YACP,OAAQ,IAAI,EAAa,IAAK,KAAK,EAAa,CAAA;QAClD;YACE,MAAM,CAAC,KAAK,EAAE,yBAAyB,QAAQ,mBAAmB,IAAI,CAAC,IAAI,GAAG,CAAC,CAAA;IACnF,CAAC;AACH,CAAC;AAED,SAAS,KAAK,CAAC,IAAc,EAAE,IAAe;IAC5C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAC9B,MAAM,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,IAAI,sBAAsB,IAAI,GAAG,CAAC,CAAA;IACxD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAS,IAAI,CAAC,IAAc;IAC1B,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,+BAA+B,IAAI,CAAC,IAAI,GAAG,CAAC,CAAA;IAC/E,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;AACzB,CAAC"}
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** A source file's resolved formatting: the indent unit (`'\t'` or N spaces) and line ending. */
|
|
2
|
+
export interface FormatStyle {
|
|
3
|
+
indentUnit: string;
|
|
4
|
+
eol: string;
|
|
5
|
+
}
|
|
6
|
+
/** Per-apply formatting configuration (`transform(src, ctx, options)`) — each field overrides what
|
|
7
|
+
* would otherwise be detected from the source. The extension point prettier-like options (trailing
|
|
8
|
+
* comma, semicolons, quotes, print width) would grow on. */
|
|
9
|
+
export interface FormatOptions {
|
|
10
|
+
/** Force the indent unit (`'\t'` or N spaces) instead of guessing it. */
|
|
11
|
+
indentUnit?: string;
|
|
12
|
+
/** Force the line ending (`'\n'` / `'\r\n'`) instead of guessing it. */
|
|
13
|
+
eol?: string;
|
|
14
|
+
}
|
|
15
|
+
/** Guess the indent unit (most common indentation step, detect-indent style; tabs when they
|
|
16
|
+
* dominate) and EOL (first line break) of `source`, defaulting to two spaces and `'\n'`. */
|
|
17
|
+
export declare function detectStyle(source: string): FormatStyle;
|
|
18
|
+
/** The {@link FormatStyle} for an apply: detected from `source`, with any explicit `options` winning. */
|
|
19
|
+
export declare function resolveStyle(source: string, options?: FormatOptions): FormatStyle;
|
|
20
|
+
/** Re-indent a snippet for a line indented by `baseIndent`: the first line is left for the caller
|
|
21
|
+
* to position, every following non-blank line is re-anchored to `baseIndent` (its indentation
|
|
22
|
+
* relative to the block's own base preserved), and line breaks become `eol`.
|
|
23
|
+
*
|
|
24
|
+
* The block's base is the least-indented continuation line — *not* the first line, which a node's
|
|
25
|
+
* `.text` leaves at column 0 even when the node sits deep in the source. Stripping that base before
|
|
26
|
+
* applying `baseIndent` re-anchors a "hanging" block (continuation lines still at their source
|
|
27
|
+
* indent) to the target, instead of stacking `baseIndent` on top of the indent already there. */
|
|
28
|
+
export declare function reindent(text: string, baseIndent: string, eol: string): string;
|
|
29
|
+
/** Offset of the first character of the line containing `index`. */
|
|
30
|
+
export declare function lineStartOf(source: string, index: number): number;
|
|
31
|
+
/** The leading whitespace of the line containing `index` — the base indent an inserted block should
|
|
32
|
+
* match. Empty when the line starts with a non-whitespace character. */
|
|
33
|
+
export declare function indentOf(source: string, index: number): string;
|
|
34
|
+
/** A space or tab — the horizontal whitespace separating inline list elements (`undefined` past
|
|
35
|
+
* either end of the source is not whitespace). */
|
|
36
|
+
export declare function isHSpace(char: string | undefined): boolean;
|
|
37
|
+
/** The start of the run of whole blank lines immediately above the line beginning at `lineStart`
|
|
38
|
+
* — `lineStart` itself when the preceding line is non-blank. The "absorb blank lines above" step
|
|
39
|
+
* shared by whole-line removal and the line-collapse of a removed last element. */
|
|
40
|
+
export declare function blankRunStart(source: string, lineStart: number): number;
|
|
41
|
+
/** The end of the run of whole blank lines beginning at line start `from` — the start of the first
|
|
42
|
+
* non-blank line at or after it (`from` itself when that line is non-blank, the source length when
|
|
43
|
+
* only blank lines remain). The forward mirror of {@link blankRunStart}: collapsing the blank lines
|
|
44
|
+
* that followed a removed first element, which would dangle after the container's opening delimiter. */
|
|
45
|
+
export declare function blankRunEnd(source: string, from: number): number;
|
|
46
|
+
/** The whole-line span `[from, to)` covering the lines `[start, end)` touches — from the start of
|
|
47
|
+
* `start`'s line (leading indentation included) through the newline after `end`'s line, so nothing
|
|
48
|
+
* blank is left behind. With `collapseBlankBefore`, also absorb whole blank lines immediately above
|
|
49
|
+
* (a separator before a dropped block). */
|
|
50
|
+
export declare function wholeLineRange(source: string, start: number, end: number, collapseBlankBefore?: boolean): [number, number];
|
|
51
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAIA,iGAAiG;AACjG,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,MAAM,CAAA;IAClB,GAAG,EAAE,MAAM,CAAA;CACZ;AAED;;6DAE6D;AAC7D,MAAM,WAAW,aAAa;IAC5B,yEAAyE;IACzE,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,wEAAwE;IACxE,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED;6FAC6F;AAC7F,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAEvD;AAED,yGAAyG;AACzG,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,WAAW,CAMjF;AA0CD;;;;;;;kGAOkG;AAClG,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAU9E;AAKD,oEAAoE;AACpE,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED;yEACyE;AACzE,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE9D;AAED;mDACmD;AACnD,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAE1D;AAED;;oFAEoF;AACpF,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAQvE;AAED;;;yGAGyG;AACzG,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAShE;AAED;;;4CAG4C;AAC5C,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,mBAAmB,UAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAKxH"}
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Guessing a file's formatting and re-indenting inserted code. Codegraft edits byte ranges, so
|
|
2
|
+
// code it doesn't touch keeps its formatting for free; these let an *inserted* snippet adopt the
|
|
3
|
+
// file's indent unit and line ending instead of landing at column 0. Opt-in (the `format` option).
|
|
4
|
+
/** Guess the indent unit (most common indentation step, detect-indent style; tabs when they
|
|
5
|
+
* dominate) and EOL (first line break) of `source`, defaulting to two spaces and `'\n'`. */
|
|
6
|
+
export function detectStyle(source) {
|
|
7
|
+
return { indentUnit: detectIndentUnit(source), eol: detectEol(source) };
|
|
8
|
+
}
|
|
9
|
+
/** The {@link FormatStyle} for an apply: detected from `source`, with any explicit `options` winning. */
|
|
10
|
+
export function resolveStyle(source, options) {
|
|
11
|
+
const detected = detectStyle(source);
|
|
12
|
+
return {
|
|
13
|
+
indentUnit: options?.indentUnit ?? detected.indentUnit,
|
|
14
|
+
eol: options?.eol ?? detected.eol,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function detectEol(source) {
|
|
18
|
+
return /\r\n|\n/.exec(source)?.[0] ?? '\n';
|
|
19
|
+
}
|
|
20
|
+
function detectIndentUnit(source) {
|
|
21
|
+
const steps = new Map(); // space-step size → occurrences
|
|
22
|
+
let tabLines = 0;
|
|
23
|
+
let prevSpaces = 0;
|
|
24
|
+
let prevWasSpace = false;
|
|
25
|
+
for (const raw of source.split('\n')) {
|
|
26
|
+
const line = raw.endsWith('\r') ? raw.slice(0, -1) : raw;
|
|
27
|
+
if (line.trim() === '') {
|
|
28
|
+
prevWasSpace = false;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const indent = /^[ \t]*/.exec(line)[0];
|
|
32
|
+
if (indent.includes('\t')) {
|
|
33
|
+
tabLines++;
|
|
34
|
+
prevWasSpace = false;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (prevWasSpace) {
|
|
38
|
+
const step = Math.abs(indent.length - prevSpaces);
|
|
39
|
+
if (step > 0)
|
|
40
|
+
steps.set(step, (steps.get(step) ?? 0) + 1);
|
|
41
|
+
}
|
|
42
|
+
prevSpaces = indent.length;
|
|
43
|
+
prevWasSpace = true;
|
|
44
|
+
}
|
|
45
|
+
let unit = 0;
|
|
46
|
+
let best = 0;
|
|
47
|
+
for (const [step, count] of steps) {
|
|
48
|
+
if (count > best) {
|
|
49
|
+
best = count;
|
|
50
|
+
unit = step;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (tabLines > best)
|
|
54
|
+
return '\t';
|
|
55
|
+
return ' '.repeat(unit || 2);
|
|
56
|
+
}
|
|
57
|
+
/** Re-indent a snippet for a line indented by `baseIndent`: the first line is left for the caller
|
|
58
|
+
* to position, every following non-blank line is re-anchored to `baseIndent` (its indentation
|
|
59
|
+
* relative to the block's own base preserved), and line breaks become `eol`.
|
|
60
|
+
*
|
|
61
|
+
* The block's base is the least-indented continuation line — *not* the first line, which a node's
|
|
62
|
+
* `.text` leaves at column 0 even when the node sits deep in the source. Stripping that base before
|
|
63
|
+
* applying `baseIndent` re-anchors a "hanging" block (continuation lines still at their source
|
|
64
|
+
* indent) to the target, instead of stacking `baseIndent` on top of the indent already there. */
|
|
65
|
+
export function reindent(text, baseIndent, eol) {
|
|
66
|
+
if (!text.includes('\n'))
|
|
67
|
+
return text;
|
|
68
|
+
const lines = text.split(/\r\n|\n/);
|
|
69
|
+
let base = Infinity;
|
|
70
|
+
for (let i = 1; i < lines.length; i++) {
|
|
71
|
+
if (lines[i].trim() === '')
|
|
72
|
+
continue;
|
|
73
|
+
base = Math.min(base, /^[ \t]*/.exec(lines[i])[0].length);
|
|
74
|
+
}
|
|
75
|
+
if (base === Infinity)
|
|
76
|
+
base = 0; // a single-or-blank-continuation snippet: nothing to strip
|
|
77
|
+
return lines.map((line, i) => (i === 0 || line.trim() === '' ? line : baseIndent + line.slice(base))).join(eol);
|
|
78
|
+
}
|
|
79
|
+
// —— Pure line/whitespace queries over a source string (shared by the formatter and whole-line
|
|
80
|
+
// removal). They take the source explicitly so they stay independent of the edit buffer. ——
|
|
81
|
+
/** Offset of the first character of the line containing `index`. */
|
|
82
|
+
export function lineStartOf(source, index) {
|
|
83
|
+
return source.lastIndexOf('\n', index - 1) + 1;
|
|
84
|
+
}
|
|
85
|
+
/** The leading whitespace of the line containing `index` — the base indent an inserted block should
|
|
86
|
+
* match. Empty when the line starts with a non-whitespace character. */
|
|
87
|
+
export function indentOf(source, index) {
|
|
88
|
+
return /^[ \t]*/.exec(source.slice(lineStartOf(source, index), index))[0];
|
|
89
|
+
}
|
|
90
|
+
/** A space or tab — the horizontal whitespace separating inline list elements (`undefined` past
|
|
91
|
+
* either end of the source is not whitespace). */
|
|
92
|
+
export function isHSpace(char) {
|
|
93
|
+
return char === ' ' || char === '\t';
|
|
94
|
+
}
|
|
95
|
+
/** The start of the run of whole blank lines immediately above the line beginning at `lineStart`
|
|
96
|
+
* — `lineStart` itself when the preceding line is non-blank. The "absorb blank lines above" step
|
|
97
|
+
* shared by whole-line removal and the line-collapse of a removed last element. */
|
|
98
|
+
export function blankRunStart(source, lineStart) {
|
|
99
|
+
let from = lineStart;
|
|
100
|
+
while (from > 0) {
|
|
101
|
+
const prevStart = lineStartOf(source, from - 1);
|
|
102
|
+
if (source.slice(prevStart, from - 1).trim() !== '')
|
|
103
|
+
break; // a non-blank line stops it
|
|
104
|
+
from = prevStart;
|
|
105
|
+
}
|
|
106
|
+
return from;
|
|
107
|
+
}
|
|
108
|
+
/** The end of the run of whole blank lines beginning at line start `from` — the start of the first
|
|
109
|
+
* non-blank line at or after it (`from` itself when that line is non-blank, the source length when
|
|
110
|
+
* only blank lines remain). The forward mirror of {@link blankRunStart}: collapsing the blank lines
|
|
111
|
+
* that followed a removed first element, which would dangle after the container's opening delimiter. */
|
|
112
|
+
export function blankRunEnd(source, from) {
|
|
113
|
+
let to = from;
|
|
114
|
+
while (to < source.length) {
|
|
115
|
+
const newline = source.indexOf('\n', to);
|
|
116
|
+
const lineEnd = newline === -1 ? source.length : newline;
|
|
117
|
+
if (source.slice(to, lineEnd).trim() !== '')
|
|
118
|
+
break; // a non-blank line stops it
|
|
119
|
+
to = newline === -1 ? source.length : newline + 1;
|
|
120
|
+
}
|
|
121
|
+
return to;
|
|
122
|
+
}
|
|
123
|
+
/** The whole-line span `[from, to)` covering the lines `[start, end)` touches — from the start of
|
|
124
|
+
* `start`'s line (leading indentation included) through the newline after `end`'s line, so nothing
|
|
125
|
+
* blank is left behind. With `collapseBlankBefore`, also absorb whole blank lines immediately above
|
|
126
|
+
* (a separator before a dropped block). */
|
|
127
|
+
export function wholeLineRange(source, start, end, collapseBlankBefore = false) {
|
|
128
|
+
const lineStart = lineStartOf(source, start);
|
|
129
|
+
const from = collapseBlankBefore ? blankRunStart(source, lineStart) : lineStart;
|
|
130
|
+
const newline = source.indexOf('\n', end);
|
|
131
|
+
return [from, newline === -1 ? source.length : newline + 1];
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=format.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.js","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,iGAAiG;AACjG,mGAAmG;AAkBnG;6FAC6F;AAC7F,MAAM,UAAU,WAAW,CAAC,MAAc;IACxC,OAAO,EAAE,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,CAAA;AACzE,CAAC;AAED,yGAAyG;AACzG,MAAM,UAAU,YAAY,CAAC,MAAc,EAAE,OAAuB;IAClE,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM,CAAC,CAAA;IACpC,OAAO;QACL,UAAU,EAAE,OAAO,EAAE,UAAU,IAAI,QAAQ,CAAC,UAAU;QACtD,GAAG,EAAE,OAAO,EAAE,GAAG,IAAI,QAAQ,CAAC,GAAG;KAClC,CAAA;AACH,CAAC;AAED,SAAS,SAAS,CAAC,MAAc;IAC/B,OAAO,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAA;AAC5C,CAAC;AAED,SAAS,gBAAgB,CAAC,MAAc;IACtC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAA,CAAC,gCAAgC;IACxE,IAAI,QAAQ,GAAG,CAAC,CAAA;IAChB,IAAI,UAAU,GAAG,CAAC,CAAA;IAClB,IAAI,YAAY,GAAG,KAAK,CAAA;IACxB,KAAK,MAAM,GAA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I,EAAE,KAAK,EAAE;YAAE,MAAK,CAAC,4BAA4B;QAC/E,EAAE,GAAG,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,CAAA;IACnD,CAAC;IACD,OAAO,EAAE,CAAA;AACX,CAAC;AAED;;;4CAG4C;AAC5C,MAAM,UAAU,cAAc,CAAC,MAAc,EAAE,KAAa,EAAE,GAAW,EAAE,mBAAmB,GAAG,KAAK;IACpG,MAAM,SAAS,GAAG,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;IAC5C,MAAM,IAAI,GAAG,mBAAmB,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;IAC/E,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IACzC,OAAO,CAAC,IAAI,EAAE,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,CAAA;AAC7D,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { RichNode } from './types.js';
|
|
2
|
+
import type { EditCollector } from './edit-collector.js';
|
|
3
|
+
import { type FormatStyle } from './format.js';
|
|
4
|
+
/**
|
|
5
|
+
* The layout-formatting policy: how an edit is *rendered* once the codemod has decided *what* to
|
|
6
|
+
* edit. Built per-apply from the source plus a resolved {@link FormatStyle}. The {@link Collection}
|
|
7
|
+
* delegates the rendering of each edit to it; the {@link EditCollector} it drives stays a pure edit
|
|
8
|
+
* buffer.
|
|
9
|
+
*
|
|
10
|
+
* This is the single home for indentation/EOL re-rendering, container layout (separators, trailing
|
|
11
|
+
* commas, brace padding), and line-collapse on removal — and the place prettier-like options
|
|
12
|
+
* (trailing-comma/semicolon/quote/print-width) would grow.
|
|
13
|
+
*/
|
|
14
|
+
export declare class Formatter {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(collector: EditCollector, source: string, style: FormatStyle);
|
|
17
|
+
/** The line ending — for inserting whole statements (`ensureImport`). */
|
|
18
|
+
get eol(): string;
|
|
19
|
+
/** The leading whitespace of the line containing `index` — for restoring a displaced node's indent. */
|
|
20
|
+
indentAt(index: number): string;
|
|
21
|
+
/** `text` re-indented to the line at `index` (single-line text is unchanged) — for a replaced or
|
|
22
|
+
* inserted snippet. */
|
|
23
|
+
reindent(text: string, index: number): string;
|
|
24
|
+
/** Append `text` as the last element of `node`: a fresh indented line in a block/class body, after
|
|
25
|
+
* the last element of a comma- or `;`-separated list, or as the sole element of an empty one. */
|
|
26
|
+
append(node: RichNode, text: string): void;
|
|
27
|
+
/** Prepend `text` as the first element of `node` — the mirror of {@link append}. */
|
|
28
|
+
prepend(node: RichNode, text: string): void;
|
|
29
|
+
/** Delete `[start, end)`, collapsing whitespace the way Prettier would: when the span owns its
|
|
30
|
+
* line(s) — only indentation before `start`, only whitespace after `end` up to the line break —
|
|
31
|
+
* drop the whole lines so no blank line is left; otherwise an in-line delete that also clears one
|
|
32
|
+
* residual separating space, so a removed mid-list element (`[1, two(), 3]`) leaves `[1, 3]`, not
|
|
33
|
+
* `[1, 3]`.
|
|
34
|
+
*
|
|
35
|
+
* In the own-line case the content, the leading indent, and the trailing line break are removed
|
|
36
|
+
* separately so the deletion composes after an abutting edit: a prior `unwrap`/`dropDirective` that
|
|
37
|
+
* already consumed the run up to `start` leaves the content abutting it (no overlap, so it lands),
|
|
38
|
+
* while the leading-indent removal it overlaps is dropped on its own (first-wins) instead of taking
|
|
39
|
+
* the whole line removal down with it.
|
|
40
|
+
*
|
|
41
|
+
* `collapse.before`/`collapse.after` (the node was the last / first surviving element of its
|
|
42
|
+
* container) additionally drop the run of blank lines directly above / below: a blank separator
|
|
43
|
+
* left against the container's closing / opening delimiter, which Prettier strips. Each is its own
|
|
44
|
+
* edit, abutting (not overlapping) the line removal, so a prior edit on the line is unaffected. */
|
|
45
|
+
removeNode(start: number, end: number, collapse?: {
|
|
46
|
+
before?: boolean;
|
|
47
|
+
after?: boolean;
|
|
48
|
+
}): void;
|
|
49
|
+
/** Delete `[start, end)` where `end` begins a following node, collapsing the lines before it: when
|
|
50
|
+
* `start` opens its own line and `end` sits on a later line, drop `[start's line, end's line)` so
|
|
51
|
+
* the leading run (a directive comment and any comments stacked under it) vanishes whole-line while
|
|
52
|
+
* `end`'s own line — its indentation included — is left for a later edit to collapse independently.
|
|
53
|
+
* Otherwise (an inline `start`, or `end` on the same line) a plain `[start, end)` delete. Used by
|
|
54
|
+
* `dropDirective` so it composes with a following `remove`. */
|
|
55
|
+
removeLeadingTo(start: number, end: number): void;
|
|
56
|
+
/** Delete the whole lines `[start, end)` touches — leading indentation through the trailing newline,
|
|
57
|
+
* so nothing blank is left. With `collapseBlankBefore`, also absorb whole blank lines immediately
|
|
58
|
+
* above (a separator before a dropped block). The explicit whole-line removal `remove({ wholeLines })`
|
|
59
|
+
* asks for, where collapsing per node isn't enough. */
|
|
60
|
+
removeWholeLines(start: number, end: number, collapseBlankBefore?: boolean): void;
|
|
61
|
+
/** Register a dedent of the lifted range `[from, to)` by `dedent` columns: every continuation line
|
|
62
|
+
* (the lines after the first) loses up to `dedent` leading-whitespace columns. Used by `unwrap`,
|
|
63
|
+
* whose kept statements drop a level — their first line inherits the wrapper's indent for free,
|
|
64
|
+
* but the rest keep their deeper source indent.
|
|
65
|
+
*
|
|
66
|
+
* Deferred to {@link flush} rather than applied now so it yields (first-wins) to the codemod's own
|
|
67
|
+
* edits on the kept nodes: a kept statement removed or replaced in the same pass wins the overlap,
|
|
68
|
+
* so only lines that actually survive get dedented. A no-op unless `dedent` is positive. */
|
|
69
|
+
deferReindent(from: number, to: number, dedent: number): void;
|
|
70
|
+
/** Apply every {@link deferReindent}, once, after the codemod has recorded its explicit edits. */
|
|
71
|
+
flush(): void;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMapp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C,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAI7D,kGAAkG;IAClG,KAAK,IAAI,IAAI;CA+Ed"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { reindent, indentOf, isHSpace, lineStartOf, wholeLineRange, blankRunStart, blankRunEnd } from './format.js';
|
|
2
|
+
import { openDelimiter, trailingSeparator, isMultiline, NEWLINE_CONTAINERS, SEMI_CONTAINERS } from './containers.js';
|
|
3
|
+
/**
|
|
4
|
+
* The layout-formatting policy: how an edit is *rendered* once the codemod has decided *what* to
|
|
5
|
+
* edit. Built per-apply from the source plus a resolved {@link FormatStyle}. The {@link Collection}
|
|
6
|
+
* delegates the rendering of each edit to it; the {@link EditCollector} it drives stays a pure edit
|
|
7
|
+
* buffer.
|
|
8
|
+
*
|
|
9
|
+
* This is the single home for indentation/EOL re-rendering, container layout (separators, trailing
|
|
10
|
+
* commas, brace padding), and line-collapse on removal — and the place prettier-like options
|
|
11
|
+
* (trailing-comma/semicolon/quote/print-width) would grow.
|
|
12
|
+
*/
|
|
13
|
+
export class Formatter {
|
|
14
|
+
#collector;
|
|
15
|
+
#source;
|
|
16
|
+
#style;
|
|
17
|
+
/** Reindents deferred to {@link flush} — registered by `unwrap`, applied after the codemod's
|
|
18
|
+
* explicit edits so they yield to them (see {@link deferReindent}). */
|
|
19
|
+
#deferred = [];
|
|
20
|
+
constructor(collector, source, style) {
|
|
21
|
+
this.#collector = collector;
|
|
22
|
+
this.#source = source;
|
|
23
|
+
this.#style = style;
|
|
24
|
+
}
|
|
25
|
+
/** The line ending — for inserting whole statements (`ensureImport`). */
|
|
26
|
+
get eol() {
|
|
27
|
+
return this.#style.eol;
|
|
28
|
+
}
|
|
29
|
+
/** The leading whitespace of the line containing `index` — for restoring a displaced node's indent. */
|
|
30
|
+
indentAt(index) {
|
|
31
|
+
return indentOf(this.#source, index);
|
|
32
|
+
}
|
|
33
|
+
/** `text` re-indented to the line at `index` (single-line text is unchanged) — for a replaced or
|
|
34
|
+
* inserted snippet. */
|
|
35
|
+
reindent(text, index) {
|
|
36
|
+
return reindent(text, indentOf(this.#source, index), this.#style.eol);
|
|
37
|
+
}
|
|
38
|
+
// —— append / prepend an element to a container ——
|
|
39
|
+
/** Append `text` as the last element of `node`: a fresh indented line in a block/class body, after
|
|
40
|
+
* the last element of a comma- or `;`-separated list, or as the sole element of an empty one. */
|
|
41
|
+
append(node, text) {
|
|
42
|
+
const elements = node.children;
|
|
43
|
+
if (NEWLINE_CONTAINERS.has(node.type)) {
|
|
44
|
+
if (elements.length === 0)
|
|
45
|
+
this.#fillBlock(node, text);
|
|
46
|
+
else {
|
|
47
|
+
const last = elements[elements.length - 1];
|
|
48
|
+
this.#collector.insertRight(last.documentEndIndex, this.#line(text, last.documentStartIndex));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else if (elements.length === 0)
|
|
52
|
+
this.#fillContainer(node, text);
|
|
53
|
+
else
|
|
54
|
+
this.#appendElement(node, elements[elements.length - 1], text);
|
|
55
|
+
}
|
|
56
|
+
/** Prepend `text` as the first element of `node` — the mirror of {@link append}. */
|
|
57
|
+
prepend(node, text) {
|
|
58
|
+
const elements = node.children;
|
|
59
|
+
if (NEWLINE_CONTAINERS.has(node.type)) {
|
|
60
|
+
if (elements.length === 0)
|
|
61
|
+
this.#fillBlock(node, text);
|
|
62
|
+
else
|
|
63
|
+
this.#collector.insertRight(openDelimiter(node).documentEndIndex, this.#line(text, elements[0].documentStartIndex));
|
|
64
|
+
}
|
|
65
|
+
else if (elements.length === 0)
|
|
66
|
+
this.#fillContainer(node, text);
|
|
67
|
+
else
|
|
68
|
+
this.#prependElement(node, elements[0], text);
|
|
69
|
+
}
|
|
70
|
+
// —— removal: emit the collapse edits ——
|
|
71
|
+
/** Delete `[start, end)`, collapsing whitespace the way Prettier would: when the span owns its
|
|
72
|
+
* line(s) — only indentation before `start`, only whitespace after `end` up to the line break —
|
|
73
|
+
* drop the whole lines so no blank line is left; otherwise an in-line delete that also clears one
|
|
74
|
+
* residual separating space, so a removed mid-list element (`[1, two(), 3]`) leaves `[1, 3]`, not
|
|
75
|
+
* `[1, 3]`.
|
|
76
|
+
*
|
|
77
|
+
* In the own-line case the content, the leading indent, and the trailing line break are removed
|
|
78
|
+
* separately so the deletion composes after an abutting edit: a prior `unwrap`/`dropDirective` that
|
|
79
|
+
* already consumed the run up to `start` leaves the content abutting it (no overlap, so it lands),
|
|
80
|
+
* while the leading-indent removal it overlaps is dropped on its own (first-wins) instead of taking
|
|
81
|
+
* the whole line removal down with it.
|
|
82
|
+
*
|
|
83
|
+
* `collapse.before`/`collapse.after` (the node was the last / first surviving element of its
|
|
84
|
+
* container) additionally drop the run of blank lines directly above / below: a blank separator
|
|
85
|
+
* left against the container's closing / opening delimiter, which Prettier strips. Each is its own
|
|
86
|
+
* edit, abutting (not overlapping) the line removal, so a prior edit on the line is unaffected. */
|
|
87
|
+
removeNode(start, end, collapse = {}) {
|
|
88
|
+
const lineStart = lineStartOf(this.#source, start);
|
|
89
|
+
const newline = this.#source.indexOf('\n', end);
|
|
90
|
+
const lineEnd = newline === -1 ? this.#source.length : newline;
|
|
91
|
+
const ownsLines = this.#source.slice(lineStart, start).trim() === '' && this.#source.slice(end, lineEnd).trim() === '';
|
|
92
|
+
if (!ownsLines) {
|
|
93
|
+
// Inline hole: drop one separating space when both sides are spaced (a mid-list element), so no
|
|
94
|
+
// double space is left. Never at a list edge (one side non-space) or across a line break.
|
|
95
|
+
const trim = isHSpace(this.#source[end]) && isHSpace(this.#source[start - 1]) ? 1 : 0;
|
|
96
|
+
this.#collector.remove(start, end + trim);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
this.#collector.remove(start, end); // the content — abuts any prior edit at `start`, so it lands
|
|
100
|
+
this.#collector.remove(lineStart, start); // leading indent — may be already gone under a prior edit
|
|
101
|
+
const lineBreakEnd = newline === -1 ? this.#source.length : newline + 1;
|
|
102
|
+
this.#collector.remove(end, lineBreakEnd); // trailing line break
|
|
103
|
+
// Collapse a blank-line separator the removal leaves dangling against a container edge: `before`
|
|
104
|
+
// the run above the node (against the close), `after` the run below (against the open). With no
|
|
105
|
+
// such run each helper returns the offset it was given, so the remove is a no-op.
|
|
106
|
+
if (collapse.before)
|
|
107
|
+
this.#collector.remove(blankRunStart(this.#source, lineStart), lineStart);
|
|
108
|
+
if (collapse.after)
|
|
109
|
+
this.#collector.remove(lineBreakEnd, blankRunEnd(this.#source, lineBreakEnd));
|
|
110
|
+
}
|
|
111
|
+
/** Delete `[start, end)` where `end` begins a following node, collapsing the lines before it: when
|
|
112
|
+
* `start` opens its own line and `end` sits on a later line, drop `[start's line, end's line)` so
|
|
113
|
+
* the leading run (a directive comment and any comments stacked under it) vanishes whole-line while
|
|
114
|
+
* `end`'s own line — its indentation included — is left for a later edit to collapse independently.
|
|
115
|
+
* Otherwise (an inline `start`, or `end` on the same line) a plain `[start, end)` delete. Used by
|
|
116
|
+
* `dropDirective` so it composes with a following `remove`. */
|
|
117
|
+
removeLeadingTo(start, end) {
|
|
118
|
+
const startLine = lineStartOf(this.#source, start);
|
|
119
|
+
const endLine = lineStartOf(this.#source, end);
|
|
120
|
+
if (endLine > startLine && this.#source.slice(startLine, start).trim() === '')
|
|
121
|
+
this.#collector.remove(startLine, endLine);
|
|
122
|
+
else
|
|
123
|
+
this.#collector.remove(start, end);
|
|
124
|
+
}
|
|
125
|
+
/** Delete the whole lines `[start, end)` touches — leading indentation through the trailing newline,
|
|
126
|
+
* so nothing blank is left. With `collapseBlankBefore`, also absorb whole blank lines immediately
|
|
127
|
+
* above (a separator before a dropped block). The explicit whole-line removal `remove({ wholeLines })`
|
|
128
|
+
* asks for, where collapsing per node isn't enough. */
|
|
129
|
+
removeWholeLines(start, end, collapseBlankBefore = false) {
|
|
130
|
+
const [from, to] = wholeLineRange(this.#source, start, end, collapseBlankBefore);
|
|
131
|
+
this.#collector.remove(from, to);
|
|
132
|
+
}
|
|
133
|
+
// —— unwrap reindent (deferred) ——
|
|
134
|
+
/** Register a dedent of the lifted range `[from, to)` by `dedent` columns: every continuation line
|
|
135
|
+
* (the lines after the first) loses up to `dedent` leading-whitespace columns. Used by `unwrap`,
|
|
136
|
+
* whose kept statements drop a level — their first line inherits the wrapper's indent for free,
|
|
137
|
+
* but the rest keep their deeper source indent.
|
|
138
|
+
*
|
|
139
|
+
* Deferred to {@link flush} rather than applied now so it yields (first-wins) to the codemod's own
|
|
140
|
+
* edits on the kept nodes: a kept statement removed or replaced in the same pass wins the overlap,
|
|
141
|
+
* so only lines that actually survive get dedented. A no-op unless `dedent` is positive. */
|
|
142
|
+
deferReindent(from, to, dedent) {
|
|
143
|
+
if (dedent > 0)
|
|
144
|
+
this.#deferred.push({ from, to, dedent });
|
|
145
|
+
}
|
|
146
|
+
/** Apply every {@link deferReindent}, once, after the codemod has recorded its explicit edits. */
|
|
147
|
+
flush() {
|
|
148
|
+
for (const { from, to, dedent } of this.#deferred)
|
|
149
|
+
this.#dedentLifted(from, to, dedent);
|
|
150
|
+
this.#deferred.length = 0;
|
|
151
|
+
}
|
|
152
|
+
// —— internals ——
|
|
153
|
+
/** Strip up to `dedent` leading-whitespace columns from each continuation line of `[from, to)`
|
|
154
|
+
* (blank lines skipped). Whitespace-only edits at line starts, so they compose with edits to the
|
|
155
|
+
* kept nodes' content; a line already consumed by another edit drops out via first-wins. */
|
|
156
|
+
#dedentLifted(from, to, dedent) {
|
|
157
|
+
for (let nl = this.#source.indexOf('\n', from); nl !== -1 && nl < to; nl = this.#source.indexOf('\n', nl + 1)) {
|
|
158
|
+
const lineStart = nl + 1;
|
|
159
|
+
let i = lineStart;
|
|
160
|
+
while (isHSpace(this.#source[i]))
|
|
161
|
+
i++;
|
|
162
|
+
if (i > lineStart)
|
|
163
|
+
this.#collector.remove(lineStart, lineStart + Math.min(dedent, i - lineStart));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** `text` as a fresh line at the indentation of the sibling at `index`. */
|
|
167
|
+
#line(text, index) {
|
|
168
|
+
const indent = indentOf(this.#source, index);
|
|
169
|
+
return this.#style.eol + indent + reindent(text, indent, this.#style.eol);
|
|
170
|
+
}
|
|
171
|
+
/** Open an empty `{}` block onto its own indented line — `{}` → `{⏎ text⏎}`. */
|
|
172
|
+
#fillBlock(node, text) {
|
|
173
|
+
const blockIndent = indentOf(this.#source, node.documentStartIndex);
|
|
174
|
+
const indent = blockIndent + this.#style.indentUnit;
|
|
175
|
+
const eol = this.#style.eol;
|
|
176
|
+
this.#collector.insertRight(openDelimiter(node).documentEndIndex, eol + indent + reindent(text, indent, eol) + eol + blockIndent);
|
|
177
|
+
}
|
|
178
|
+
/** Fill an empty delimited container with its sole element: a brace container is padded
|
|
179
|
+
* (`{}` → `{ text }`); an array / argument list is not (`[]` → `[text]`). */
|
|
180
|
+
#fillContainer(node, text) {
|
|
181
|
+
const open = openDelimiter(node);
|
|
182
|
+
const pad = open.text === '{' ? ' ' : '';
|
|
183
|
+
this.#collector.insertRight(open.documentEndIndex, pad + text + pad);
|
|
184
|
+
}
|
|
185
|
+
/** Append `text` after the last element `last` of a delimited container. A multi-line container
|
|
186
|
+
* places it on a fresh line at the elements' indent, mirroring the trailing-separator style —
|
|
187
|
+
* extend an existing trailing `sep`, add the mandatory `,` a comma list omits, or rely on the
|
|
188
|
+
* newline alone for a `;` list (where the separator between members is optional); an inline one
|
|
189
|
+
* stays on one line. */
|
|
190
|
+
#appendElement(node, last, text) {
|
|
191
|
+
const sep = this.#separatorFor(node);
|
|
192
|
+
if (!isMultiline(node)) {
|
|
193
|
+
this.#collector.insertRight(last.documentEndIndex, `${sep} ${text}`);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const trailing = trailingSeparator(last, sep);
|
|
197
|
+
const line = this.#line(text, last.documentStartIndex);
|
|
198
|
+
if (trailing)
|
|
199
|
+
this.#collector.insertRight(trailing.documentEndIndex, line + sep);
|
|
200
|
+
else if (sep === ';')
|
|
201
|
+
this.#collector.insertRight(last.documentEndIndex, line);
|
|
202
|
+
else
|
|
203
|
+
this.#collector.insertRight(last.documentEndIndex, sep + line);
|
|
204
|
+
}
|
|
205
|
+
/** Prepend `text` before the first element `first` — the mirror of {@link #appendElement}. The new
|
|
206
|
+
* element is always followed by the old first, so a multi-line container puts it on a fresh line
|
|
207
|
+
* after the open delimiter, separator-terminated (`;` only where the body terminates members with
|
|
208
|
+
* one); an inline one inserts before the first element so brace padding stays intact
|
|
209
|
+
* (`{ a }` → `{ x, a }`). */
|
|
210
|
+
#prependElement(node, first, text) {
|
|
211
|
+
const sep = this.#separatorFor(node);
|
|
212
|
+
if (!isMultiline(node)) {
|
|
213
|
+
this.#collector.insertLeft(first.documentStartIndex, `${text}${sep} `);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const terminate = sep === ',' || trailingSeparator(first, sep) !== null;
|
|
217
|
+
this.#collector.insertRight(openDelimiter(node).documentEndIndex, this.#line(text, first.documentStartIndex) + (terminate ? sep : ''));
|
|
218
|
+
}
|
|
219
|
+
/** The token that separates a container's elements — `;` for a TS interface / object type, else
|
|
220
|
+
* `,`. The seam a future `semi`/separator option would hook into. */
|
|
221
|
+
#separatorFor(node) {
|
|
222
|
+
return SEMI_CONTAINERS.has(node.type) ? ';' : ',';
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.js","sourceRoot":"","sources":["../src/formatter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAoB,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACrI,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,WAAW,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEpH;;;;;;;;;GASG;AACH,MAAM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