@ryuugg/stanza 0.3.3 → 0.4.1

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.
Files changed (59) hide show
  1. package/README.md +15 -13
  2. package/package.json +19 -11
  3. package/src/launcher +102 -8
  4. package/src/cli.ts +0 -684
  5. package/src/compile/darwin-arm64.ts +0 -3
  6. package/src/compile/darwin-x64.ts +0 -3
  7. package/src/compile/linux-arm64-musl.ts +0 -3
  8. package/src/compile/linux-arm64.ts +0 -3
  9. package/src/compile/linux-x64-musl.ts +0 -3
  10. package/src/compile/linux-x64.ts +0 -3
  11. package/src/compiled.ts +0 -9
  12. package/src/depth.ts +0 -372
  13. package/src/directories.ts +0 -77
  14. package/src/engine/attach.ts +0 -62
  15. package/src/engine/directives.ts +0 -157
  16. package/src/engine/doc.ts +0 -93
  17. package/src/engine/edits.ts +0 -79
  18. package/src/engine/explain.ts +0 -337
  19. package/src/engine/gaps.ts +0 -361
  20. package/src/engine/hunks.ts +0 -319
  21. package/src/engine/index.ts +0 -273
  22. package/src/engine/model.ts +0 -103
  23. package/src/engine/rules.ts +0 -104
  24. package/src/engine/types.ts +0 -38
  25. package/src/files.ts +0 -1186
  26. package/src/format.ts +0 -159
  27. package/src/gitignore.ts +0 -280
  28. package/src/hook.ts +0 -380
  29. package/src/languages/index.ts +0 -34
  30. package/src/languages/javascript/ast.ts +0 -261
  31. package/src/languages/javascript/braces.ts +0 -374
  32. package/src/languages/javascript/config/biome.ts +0 -112
  33. package/src/languages/javascript/config/eslint.ts +0 -128
  34. package/src/languages/javascript/config/evaluate.ts +0 -278
  35. package/src/languages/javascript/config/find.ts +0 -105
  36. package/src/languages/javascript/config/glob.ts +0 -149
  37. package/src/languages/javascript/config/index.ts +0 -48
  38. package/src/languages/javascript/config/layers.ts +0 -468
  39. package/src/languages/javascript/config/module.ts +0 -327
  40. package/src/languages/javascript/config/oxlint.ts +0 -170
  41. package/src/languages/javascript/config/width.ts +0 -723
  42. package/src/languages/javascript/deserializers.ts +0 -17
  43. package/src/languages/javascript/embedded.ts +0 -7
  44. package/src/languages/javascript/entry.ts +0 -42
  45. package/src/languages/javascript/facts.ts +0 -294
  46. package/src/languages/javascript/index.ts +0 -20
  47. package/src/languages/javascript/lists.ts +0 -64
  48. package/src/languages/javascript/oracle.ts +0 -191
  49. package/src/languages/javascript/oxc-raw.d.ts +0 -31
  50. package/src/languages/javascript/parse.ts +0 -131
  51. package/src/languages/javascript/raw.ts +0 -95
  52. package/src/languages/javascript/scan.ts +0 -68
  53. package/src/languages/language.ts +0 -109
  54. package/src/native-file.d.ts +0 -4
  55. package/src/pool.ts +0 -473
  56. package/src/start.ts +0 -49
  57. package/src/step.ts +0 -113
  58. package/src/usage.ts +0 -35
  59. package/src/worker.ts +0 -5
@@ -1,361 +0,0 @@
1
- import { blankLines, commentIndex, finding, lineAt } from "./doc.ts";
2
- import { within, type Region } from "./directives.ts";
3
- import type {
4
- Binding,
5
- Doc,
6
- Gap,
7
- GapDecision,
8
- JoinRule,
9
- Kind,
10
- LineEdits,
11
- Path,
12
- StatementList,
13
- Stmt,
14
- } from "./model.ts";
15
- import type { Finding } from "./types.ts";
16
-
17
- const JUMP_KINDS = new Set<Kind>(["return", "throw", "continue", "break"]);
18
-
19
- const USE_KINDS = new Set<Kind>(["loop", "try", "function"]);
20
-
21
- const BLOCK_KINDS = new Set<Kind>(["if", "loop", "try", "switch"]);
22
-
23
- function pathName(path: Path): string {
24
- return path.reduce((name, segment) =>
25
- segment.startsWith("[") ? name + segment : `${name}.${segment}`,
26
- );
27
- }
28
-
29
- function readBy(bound: Binding, stmt: Stmt): string | undefined {
30
- if (bound instanceof Set) return stmt.references(bound)[Symbol.iterator]().next().value;
31
- return stmt.readsPath(bound) ? pathName(bound) : undefined;
32
- }
33
-
34
- function joinRule(prev: Stmt, next: Stmt): JoinRule | null {
35
- if (next.kind === "if") return "guard-join";
36
- if (next.kind === "return" || next.kind === "switch") return "consume-join";
37
- if (USE_KINDS.has(next.kind) && prev.kind === "declaration") return "use-join";
38
-
39
- return null;
40
- }
41
-
42
- function declarationJoin(prev: Stmt, next: Stmt): Ruled | null {
43
- const name = prev.binds === null ? undefined : readBy(prev.binds, next);
44
- if (name === undefined) return null;
45
-
46
- const rule = joinRule(prev, next);
47
- if (rule === null || next.word === null) return null;
48
-
49
- return { want: "none", rule, name, reader: next.word };
50
- }
51
-
52
- function shortBody(list: StatementList): boolean {
53
- return (
54
- list.kind === "block" &&
55
- list.stmts.length >= 2 &&
56
- list.stmts.length <= 3 &&
57
- list.stmts.every((stmt) => stmt.compact)
58
- );
59
- }
60
-
61
- export type Ruled = Exclude<GapDecision, { want: "keep" | "frozen" }>;
62
-
63
- type Step = (list: StatementList, prev: Stmt, next: Stmt) => Ruled | null;
64
-
65
- const LADDER: Step[] = [
66
- (list) => (shortBody(list) ? { want: "none", rule: "short-body" } : null),
67
- (_list, prev, next) =>
68
- prev.guard && next.guard && prev.compact && next.compact
69
- ? { want: "none", rule: "guard-chain" }
70
- : null,
71
- (_list, prev, next) =>
72
- prev.multiline && (prev.kind !== "import" || next.kind !== "import")
73
- ? { want: "at-least-one", rule: "after-multiline" }
74
- : null,
75
- (list, prev) =>
76
- list.kind === "switch" && prev.kind === "case" && prev.caseBody
77
- ? { want: "at-least-one", rule: "switch-clauses" }
78
- : null,
79
- (_list, prev, next) => declarationJoin(prev, next),
80
- (_list, prev, next) =>
81
- prev.guard?.jump && next.kind !== "if" && !JUMP_KINDS.has(next.kind)
82
- ? { want: "at-least-one", rule: "after-guard" }
83
- : null,
84
- ];
85
-
86
- export function* matches(list: StatementList, prev: Stmt, next: Stmt): Generator<Ruled> {
87
- for (const step of LADDER) {
88
- const decision = step(list, prev, next);
89
- if (decision) yield decision;
90
- }
91
- }
92
-
93
- export function decide(list: StatementList, prev: Stmt, next: Stmt): GapDecision {
94
- if (prev.frozen || next.frozen) return { want: "frozen" };
95
- for (const decision of matches(list, prev, next)) return decision;
96
- return { want: "keep" };
97
- }
98
-
99
- function mayJoinRun(gap: Gap): boolean {
100
- return (
101
- gap.decision.want === "keep" &&
102
- !gap.next.detached &&
103
- gap.prev.declaration?.letLike === true &&
104
- !gap.prev.multiline &&
105
- gap.prev.binds !== null
106
- );
107
- }
108
-
109
- function referenceRanks(block: Stmt, bindings: Binding[]): Map<string, number> {
110
- const names = new Set<string>();
111
- for (const bound of bindings) if (bound instanceof Set) for (const name of bound) names.add(name);
112
-
113
- const ranks = new Map<string, number>();
114
- if (names.size === 0) return ranks;
115
-
116
- for (const name of block.references(names)) {
117
- if (!ranks.has(name)) ranks.set(name, ranks.size);
118
- if (ranks.size === names.size) break;
119
- }
120
-
121
- return ranks;
122
- }
123
-
124
- function firstRead(bound: Binding, block: Stmt, ranks: Map<string, number>): string | undefined {
125
- if (!(bound instanceof Set)) return readBy(bound, block);
126
-
127
- let first: string | undefined;
128
- for (const name of bound) {
129
- const rank = ranks.get(name);
130
- if (rank !== undefined && (first === undefined || rank < ranks.get(first)!)) first = name;
131
- }
132
-
133
- return first;
134
- }
135
-
136
- function letSteps(gaps: Gap[]): void {
137
- for (let index = 1; index < gaps.length; index++) {
138
- const joined = gaps[index]!;
139
- const { decision } = joined;
140
- if (
141
- !joined.prev.declaration?.letLike ||
142
- decision.want !== "none" ||
143
- decision.rule === "short-body"
144
- )
145
- continue;
146
-
147
- let start = index;
148
- while (start > 0 && mayJoinRun(gaps[start - 1]!)) start--;
149
-
150
- const run = gaps.slice(start, index).toReversed();
151
- const ranks = referenceRanks(
152
- joined.next,
153
- run.map((gap) => gap.prev.binds!),
154
- );
155
-
156
- let first = index;
157
- for (const gap of run) {
158
- const name = firstRead(gap.prev.binds!, joined.next, ranks);
159
- if (name === undefined) break;
160
-
161
- first--;
162
- gap.decision = "name" in decision ? { ...decision, name } : decision;
163
- }
164
-
165
- const above = gaps[first - 1];
166
- if (above?.decision.want === "keep")
167
- above.decision = { want: "at-least-one", rule: "let-step" };
168
- }
169
- }
170
-
171
- function bracketedTry(prev: Stmt, next: Stmt): boolean {
172
- return prev.operation !== null && next.finallyRepeats(prev.operation);
173
- }
174
-
175
- export function blockSpacing(doc: Doc, gap: Gap): Finding[] {
176
- const { prev, next, blank, decision } = gap;
177
- if (
178
- decision.want !== "keep" ||
179
- blank !== 0 ||
180
- prev.multiline ||
181
- !next.multiline ||
182
- !BLOCK_KINDS.has(next.kind)
183
- )
184
- return [];
185
-
186
- if (prev.kind === "if" && next.kind === "if") return [];
187
- if (bracketedTry(prev, next)) return [];
188
-
189
- return [finding(doc, next.start, "block-spacing")];
190
- }
191
-
192
- function fixKeepsBlank(gap: Gap, underComment = gap.next.detached): boolean {
193
- return gap.decision.want !== "none" || underComment;
194
- }
195
-
196
- function commentAbove(doc: Doc, stmt: Stmt): boolean {
197
- return stmt.detached || lineAt(doc, stmt.start) > stmt.startLine;
198
- }
199
-
200
- function gapEdits(doc: Doc, gap: Gap, edits: LineEdits): Finding[] {
201
- const { prev, next, blank, decision } = gap;
202
- if (decision.want === "keep" || decision.want === "frozen") return [];
203
-
204
- if (decision.want === "none") {
205
- if (blank === 0 || fixKeepsBlank(gap)) return [];
206
- for (const line of blankLines(doc, prev.endLine, next.startLine)) edits.deleteLines.add(line);
207
- } else {
208
- if (blank > 0 || next.startLine <= prev.endLine) return [];
209
- edits.insertAfter.add(next.startLine - 1);
210
- }
211
-
212
- if ("name" in decision)
213
- return [finding(doc, next.start, decision.rule, decision.name, decision.reader)];
214
-
215
- return [finding(doc, next.start, decision.rule)];
216
- }
217
-
218
- function edgeEdits(
219
- doc: Doc,
220
- list: StatementList,
221
- regions: Region[],
222
- edits: LineEdits,
223
- touches: (first: number, last: number) => boolean,
224
- ): Finding[] {
225
- const { openLine, closeLine } = list;
226
- if (openLine === null || closeLine === null || openLine === closeLine) return [];
227
-
228
- const to = commentIndex(doc, list.end);
229
-
230
- let opened = openLine;
231
- let from = commentIndex(doc, list.start + 1);
232
- while (from < to && lineAt(doc, doc.comments[from]!.start) === openLine) {
233
- opened = lineAt(doc, doc.comments[from]!.end - 1);
234
- from++;
235
- }
236
-
237
- const firstComment = from < to ? lineAt(doc, doc.comments[from]!.start) : closeLine;
238
- const lastComment = from < to ? lineAt(doc, doc.comments[to - 1]!.end - 1) : opened;
239
-
240
- const first = Math.min(list.stmts[0]?.startLine ?? closeLine, firstComment);
241
- const last = Math.max(list.stmts.at(-1)?.endLine ?? opened, lastComment);
242
-
243
- const findings: Finding[] = [];
244
- for (const [after, before, side, frozen] of [
245
- [opened, first, "after {", list.stmts[0]?.frozen],
246
- [last, closeLine, "before }", list.stmts.at(-1)?.frozen],
247
- ] as const) {
248
- if (frozen || !touches(after, before)) continue;
249
-
250
- for (const line of blankLines(doc, after, before)) {
251
- if (edits.deleteLines.has(line) || within(regions, doc.lineStarts[line - 1]!)) continue;
252
- edits.deleteLines.add(line);
253
- findings.push(finding(doc, doc.lineStarts[line - 1]!, "edge-blank", side));
254
- }
255
- }
256
-
257
- return findings;
258
- }
259
-
260
- function separatedAfterFix(gap: Gap, touched: boolean): boolean {
261
- if (gap.decision.want === "frozen") return true;
262
- if (!touched) return gap.blank > 0;
263
- return gap.decision.want === "at-least-one" || (gap.blank > 0 && fixKeepsBlank(gap));
264
- }
265
-
266
- function walls(
267
- doc: Doc,
268
- list: StatementList,
269
- gaps: Gap[],
270
- touches: (first: number, last: number) => boolean,
271
- ): Finding[] {
272
- const findings: Finding[] = [];
273
- if (list.kind === "switch") return findings;
274
-
275
- const excluded = new Set<number>();
276
-
277
- let groupStart = 0;
278
- for (let index = 0; index <= list.stmts.length; index++) {
279
- const stmt = list.stmts[index];
280
- const gap = gaps[index - 1];
281
- const counted = stmt && !stmt.multiline && stmt.kind !== "import";
282
- const forcedJoined =
283
- gap &&
284
- !fixKeepsBlank(gap, commentAbove(doc, gap.next)) &&
285
- (gap.blank === 0 || touches(gap.prev.startLine, gap.next.endLine));
286
-
287
- if (!counted || !forcedJoined) {
288
- if (index - groupStart >= 6)
289
- for (let forcedIndex = groupStart; forcedIndex < index; forcedIndex++)
290
- excluded.add(forcedIndex);
291
-
292
- groupStart = index;
293
- }
294
-
295
- if (!counted) groupStart = index + 1;
296
- }
297
-
298
- let runStart: Stmt | undefined;
299
- let length = 0;
300
- for (let index = 0; index <= list.stmts.length; index++) {
301
- const stmt = list.stmts[index];
302
- const gap = gaps[index - 1];
303
- const separated = gap && separatedAfterFix(gap, touches(gap.prev.startLine, gap.next.endLine));
304
-
305
- const counted = stmt && !stmt.multiline && stmt.kind !== "import" && !excluded.has(index);
306
- if (!counted || separated) {
307
- if (runStart && length >= 6 && touches(runStart.startLine, list.stmts[index - 1]!.endLine))
308
- findings.push(finding(doc, runStart.start, "wall"));
309
-
310
- runStart = undefined;
311
- length = 0;
312
- }
313
-
314
- if (!counted) continue;
315
-
316
- runStart ??= stmt;
317
- length++;
318
- }
319
-
320
- return findings;
321
- }
322
-
323
- export function listGaps(doc: Doc, list: StatementList): Gap[] {
324
- const gaps: Gap[] = [];
325
- for (let index = 1; index < list.stmts.length; index++) {
326
- const prev = list.stmts[index - 1]!;
327
- const next = list.stmts[index]!;
328
- gaps.push({
329
- prev,
330
- next,
331
- blank: blankLines(doc, prev.endLine, next.startLine).length,
332
- decision: decide(list, prev, next),
333
- });
334
- }
335
-
336
- letSteps(gaps);
337
- return gaps;
338
- }
339
-
340
- export function spacing(
341
- doc: Doc,
342
- lists: StatementList[],
343
- regions: Region[],
344
- touches: (first: number, last: number) => boolean = () => true,
345
- ): { edits: LineEdits; findings: Finding[] } {
346
- const edits: LineEdits = { deleteLines: new Set(), insertAfter: new Set() };
347
- const findings: Finding[] = [];
348
- for (const list of lists) {
349
- const gaps = listGaps(doc, list);
350
- for (const gap of gaps)
351
- if (touches(gap.prev.startLine, gap.next.endLine))
352
- findings.push(...gapEdits(doc, gap, edits), ...blockSpacing(doc, gap));
353
-
354
- findings.push(
355
- ...edgeEdits(doc, list, regions, edits, touches),
356
- ...walls(doc, list, gaps, touches),
357
- );
358
- }
359
-
360
- return { edits, findings };
361
- }
@@ -1,319 +0,0 @@
1
- import { lineAt } from "./doc.ts";
2
- import { lowerBound, offsetMap, type OffsetEdit } from "./edits.ts";
3
- import type { Changed, Doc, LineEdits } from "./model.ts";
4
-
5
- export function touching(changed: Changed | undefined): (first: number, last: number) => boolean {
6
- if (changed === undefined) return () => true;
7
-
8
- const lines = [...changed.lines].sort((left, right) => left - right);
9
- const deletedAfter = [...changed.deletedAfter].sort((left, right) => left - right);
10
- return (first, last) =>
11
- (lines[lowerBound(lines, first)] ?? Infinity) <= last ||
12
- (deletedAfter[lowerBound(deletedAfter, first)] ?? Infinity) < last;
13
- }
14
-
15
- const braces = (line: string) => line.replace(/[^{}]/g, "");
16
- const braceOnly = (line: string) => /^[\s{}]*$/.test(line) && braces(line) !== "";
17
-
18
- function literals(line: string): { code: string; text: string } {
19
- let code = "";
20
- let text = "";
21
- for (let index = 0; index < line.length; index++) {
22
- const char = line[index]!;
23
- if (char === "/" && (line[index + 1] === "/" || line[index + 1] === "*"))
24
- return { code: `${code}${char}${line[index + 1]}`, text: text + line.slice(index + 2) };
25
-
26
- code += char;
27
- if (char !== '"' && char !== "'" && char !== "`") continue;
28
-
29
- let end = index + 1;
30
- while (end < line.length && line[end] !== char) end += line[end] === "\\" ? 2 : 1;
31
-
32
- text += `${line.slice(index + 1, end)}\u0000`;
33
- code += line[end] ?? "";
34
- index = end;
35
- }
36
-
37
- return { code, text };
38
- }
39
-
40
- function unbraced(line: string): string {
41
- const { code, text } = literals(line.replace(/\r$/, ""));
42
- return `${unbracedCode(code)}\u0000${text}`;
43
- }
44
-
45
- function unbracedCode(line: string): string {
46
- return line
47
- .replace(/^(\s*)\}\s*/, "$1")
48
- .replace(/(\)|\belse|\bdo)\s*\{(?=\s*(?:\/\/|\/\*|$)|.*\}\s*$)\s*/g, "$1 ")
49
- .replace(/\s*\}\s*(?=else\b|while\b|\/\/|\/\*|\}|$)/g, " ")
50
- .trimEnd();
51
- }
52
-
53
- const editLimit = 2000;
54
- function shortestEdit(before: number[], after: number[]): [number, number][] | undefined {
55
- const offset = editLimit + 1;
56
- const reach = new Int32Array(2 * offset + 1);
57
- const trace: Int32Array[] = [];
58
- for (let edits = 0; edits <= editLimit; edits++) {
59
- for (let diagonal = -edits; diagonal <= edits; diagonal += 2) {
60
- const down =
61
- diagonal === -edits ||
62
- (diagonal !== edits && reach[offset + diagonal - 1]! < reach[offset + diagonal + 1]!);
63
-
64
- let x = down ? reach[offset + diagonal + 1]! : reach[offset + diagonal - 1]! + 1;
65
- let y = x - diagonal;
66
- while (x < before.length && y < after.length && before[x] === after[y]) {
67
- x++;
68
- y++;
69
- }
70
-
71
- reach[offset + diagonal] = x;
72
- if (x < before.length || y < after.length) continue;
73
-
74
- trace.push(reach.slice(offset - edits, offset + edits + 1));
75
- return backtrack(trace, before.length, after.length);
76
- }
77
-
78
- trace.push(reach.slice(offset - edits, offset + edits + 1));
79
- }
80
-
81
- return undefined;
82
- }
83
-
84
- function matches(before: number[], after: number[]): [number, number][] | undefined {
85
- let head = 0;
86
- while (head < before.length && head < after.length && before[head] === after[head]) head++;
87
-
88
- let tail = 0;
89
- while (
90
- tail < before.length - head &&
91
- tail < after.length - head &&
92
- before[before.length - 1 - tail] === after[after.length - 1 - tail]
93
- )
94
- tail++;
95
-
96
- const middle = shortestEdit(
97
- before.slice(head, before.length - tail),
98
- after.slice(head, after.length - tail),
99
- );
100
-
101
- if (!middle) return undefined;
102
-
103
- return [
104
- ...Array.from({ length: head }, (_, index): [number, number] => [index, index]),
105
- ...middle.map(([x, y]): [number, number] => [x + head, y + head]),
106
- ...Array.from({ length: tail }, (_, index): [number, number] => [
107
- before.length - tail + index,
108
- after.length - tail + index,
109
- ]),
110
- ];
111
- }
112
-
113
- function backtrack(trace: Int32Array[], width: number, height: number): [number, number][] {
114
- const pairs: [number, number][] = [];
115
-
116
- let x = width;
117
- let y = height;
118
- for (let edits = trace.length - 1; edits >= 0; edits--) {
119
- const diagonal = x - y;
120
-
121
- let startX = 0;
122
- let startY = 0;
123
- if (edits > 0) {
124
- const previous = trace[edits - 1]!;
125
- const at = (index: number) => previous[index + edits - 1]!;
126
- const from =
127
- diagonal === -edits || (diagonal !== edits && at(diagonal - 1) < at(diagonal + 1))
128
- ? diagonal + 1
129
- : diagonal - 1;
130
-
131
- startX = at(from);
132
- startY = startX - from;
133
- }
134
-
135
- const snakeX = edits > 0 && startX - startY === diagonal - 1 ? startX + 1 : startX;
136
- const snakeY = edits > 0 && startX - startY === diagonal + 1 ? startY + 1 : startY;
137
- while (x > snakeX && y > snakeY) pairs.push([--x, --y]);
138
-
139
- x = startX;
140
- y = startY;
141
- }
142
-
143
- return pairs.reverse();
144
- }
145
-
146
- interface Lines {
147
- before: string[];
148
- after: { text: string; line: number }[];
149
- at: number;
150
- }
151
-
152
- function sides(section: string[]): { file: Lines; blocks: Lines[] } {
153
- const file: Lines = { before: [], after: [], at: 0 };
154
- const blocks: Lines[] = [];
155
-
156
- let line = 0;
157
- let beforeLeft = 0;
158
- let afterLeft = 0;
159
- let block: Lines | undefined;
160
- for (const row of section) {
161
- const header = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(row);
162
- if (header) {
163
- beforeLeft = header[1] === undefined ? 1 : Number(header[1]);
164
- afterLeft = header[3] === undefined ? 1 : Number(header[3]);
165
- line = afterLeft === 0 ? Number(header[2]) : Number(header[2]) - 1;
166
- block = undefined;
167
- continue;
168
- }
169
-
170
- if (beforeLeft === 0 && afterLeft === 0) continue;
171
-
172
- const text = row.slice(1);
173
- if (row.startsWith(" ")) {
174
- beforeLeft--;
175
- afterLeft--;
176
-
177
- file.before.push(text);
178
- file.after.push({ text, line: ++line });
179
- block = undefined;
180
- continue;
181
- }
182
-
183
- if (!row.startsWith("-") && !row.startsWith("+")) continue;
184
- if (!block) blocks.push((block = { before: [], after: [], at: line }));
185
- if (row.startsWith("+")) {
186
- afterLeft--;
187
- file.after.push({ text, line: ++line });
188
- block.after.push({ text, line });
189
- } else {
190
- beforeLeft--;
191
- if (braceOnly(text)) continue;
192
-
193
- file.before.push(text);
194
- block.before.push(text);
195
- }
196
- }
197
-
198
- file.at = (file.after[0]?.line ?? 1) - 1;
199
- return { file, blocks };
200
- }
201
-
202
- function classify(
203
- { before, after, at }: Lines,
204
- changed: { lines: Set<number>; deletedAfter: Set<number> },
205
- ): boolean {
206
- const ids = new Map<string, number>();
207
- const id = (text: string) => {
208
- const key = braceOnly(text) ? text : unbraced(text);
209
-
210
- let value = ids.get(key);
211
- if (value === undefined) ids.set(key, (value = ids.size));
212
- return value;
213
- };
214
-
215
- const pairs = matches(
216
- before.map(id),
217
- after.map(({ text }) => id(text)),
218
- );
219
-
220
- if (!pairs) return false;
221
-
222
- let x = 0;
223
- let y = 0;
224
- for (const [pairX, pairY] of [...pairs, [before.length, after.length] as const]) {
225
- if (x < pairX) changed.deletedAfter.add(at);
226
-
227
- for (; y < pairY; y++) changed.lines.add((at = after[y]!.line));
228
- if (pairX === before.length) break;
229
-
230
- const was = before[pairX]!;
231
- const now = after[pairY]!;
232
- if (was !== now.text && braces(now.text).length >= braces(was).length)
233
- changed.lines.add(now.line);
234
-
235
- at = now.line;
236
- x = pairX + 1;
237
- y = pairY + 1;
238
- }
239
-
240
- return true;
241
- }
242
-
243
- export function diffChanges(section: string[]): Changed {
244
- const { file, blocks } = sides(section);
245
- const changed = { lines: new Set<number>(), deletedAfter: new Set<number>() };
246
- if (!classify(file, changed))
247
- for (const block of blocks) {
248
- if (classify(block, changed)) continue;
249
- if (block.before.length > 0) changed.deletedAfter.add(block.at);
250
- for (const { line } of block.after) changed.lines.add(line);
251
- }
252
-
253
- for (const line of changed.deletedAfter)
254
- if (changed.lines.has(line) || changed.lines.has(line + 1)) changed.deletedAfter.delete(line);
255
-
256
- return changed;
257
- }
258
-
259
- function lineBefore(next: Doc, offset: number): number {
260
- const line = lineAt(next, Math.min(offset, next.text.length));
261
- return next.lineStarts[line - 1] === offset ? line - 1 : line;
262
- }
263
-
264
- export function afterOffsets(
265
- doc: Doc,
266
- changed: Changed | undefined,
267
- edits: OffsetEdit[],
268
- next: Doc,
269
- ): Changed | undefined {
270
- if (changed === undefined) return undefined;
271
-
272
- const shift = offsetMap(edits).forward;
273
- const lines = new Set<number>();
274
- const deletedAfter = new Set<number>();
275
- for (const line of changed.lines) {
276
- const from = doc.lineStarts[line - 1];
277
- if (from === undefined) continue;
278
-
279
- const start = shift(from);
280
- const end = shift(doc.lineStarts[line] ?? doc.text.length);
281
- if (start === end) {
282
- deletedAfter.add(lineBefore(next, start));
283
- continue;
284
- }
285
-
286
- const last = lineAt(next, Math.min(end, next.text.length) - 1);
287
- for (let number = lineAt(next, start); number <= last; number++) lines.add(number);
288
- }
289
-
290
- for (const line of changed.deletedAfter)
291
- deletedAfter.add(lineBefore(next, shift(doc.lineStarts[line] ?? doc.text.length)));
292
-
293
- return { lines, deletedAfter };
294
- }
295
-
296
- export function afterLines(
297
- doc: Doc,
298
- changed: Changed | undefined,
299
- edits: LineEdits,
300
- ): Changed | undefined {
301
- if (changed === undefined) return undefined;
302
-
303
- const lines = new Set<number>();
304
- const deletedAfter = new Set<number>();
305
- if (changed.deletedAfter.has(0)) deletedAfter.add(0);
306
-
307
- let output = 0;
308
- for (let line = 1; line <= doc.lines.length; line++) {
309
- if (!edits.deleteLines.has(line)) {
310
- output++;
311
- if (changed.lines.has(line)) lines.add(output);
312
- } else if (changed.lines.has(line)) deletedAfter.add(output);
313
-
314
- if (changed.deletedAfter.has(line)) deletedAfter.add(output);
315
- if (edits.insertAfter.has(line)) output++;
316
- }
317
-
318
- return { lines, deletedAfter };
319
- }