@criogaid/pi-hashline-edit 0.6.1 → 0.8.0
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/README.md +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
package/src/core/apply.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Verification is live and surgical: each anchor's hash is recomputed from the
|
|
5
5
|
* CURRENT line content at the cited line number and compared to the cited hash.
|
|
6
|
-
* No
|
|
6
|
+
* No prior-read revision guard — a line that changed (or was
|
|
7
7
|
* misremembered) fails its own anchor; unchanged lines elsewhere never block
|
|
8
8
|
* the edit.
|
|
9
9
|
*
|
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
* deferred until anchors are corrected.
|
|
22
22
|
*
|
|
23
23
|
* Other strict semantics:
|
|
24
|
-
* - Operation ranges must not overlap
|
|
25
|
-
*
|
|
24
|
+
* - Operation ranges must not overlap. Insertions at one position, or at the start of a
|
|
25
|
+
* replaced or deleted range, apply in batch order before that range.
|
|
26
|
+
* - Byte-identical output is a success; the commit layer reports it as a no-op.
|
|
26
27
|
*
|
|
27
28
|
* @module pi-hashline-edit/core
|
|
28
29
|
*/
|
|
@@ -31,6 +32,7 @@ import { computeLineHash } from "./hash.ts";
|
|
|
31
32
|
import {
|
|
32
33
|
detectLineEnding,
|
|
33
34
|
hasFinalNewline,
|
|
35
|
+
joinTextFragments,
|
|
34
36
|
lineSeparators,
|
|
35
37
|
replacementSeparator,
|
|
36
38
|
splitLines,
|
|
@@ -40,17 +42,17 @@ import type {
|
|
|
40
42
|
AnchorCheck,
|
|
41
43
|
AnchorFailure,
|
|
42
44
|
AnchorRecovery,
|
|
45
|
+
ApplyFailure,
|
|
43
46
|
ApplyResult,
|
|
44
47
|
Edit,
|
|
45
48
|
} from "./types.ts";
|
|
46
49
|
import { findSortedRangeConflict } from "./ranges.ts";
|
|
47
50
|
|
|
48
|
-
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
51
|
+
/** Replace `[lo, hi)` with caller text or a range captured from the verified original lines. */
|
|
52
|
+
type SpanOp = { readonly lo: number; readonly hi: number } & (
|
|
53
|
+
| { readonly kind: "body"; readonly newLines: readonly string[] }
|
|
54
|
+
| { readonly kind: "capture"; readonly sourceLo: number; readonly sourceHi: number }
|
|
55
|
+
);
|
|
54
56
|
|
|
55
57
|
/**
|
|
56
58
|
* Verify an anchor against the live content; on mismatch, attempt shifted
|
|
@@ -67,7 +69,7 @@ interface SpanOp {
|
|
|
67
69
|
function verifyAnchor(
|
|
68
70
|
lines: readonly string[],
|
|
69
71
|
cited: Anchor,
|
|
70
|
-
which: "
|
|
72
|
+
which: AnchorCheck["which"],
|
|
71
73
|
opIndex: number,
|
|
72
74
|
op: Edit["op"],
|
|
73
75
|
hashLen: number,
|
|
@@ -113,30 +115,27 @@ function verifyAnchor(
|
|
|
113
115
|
recovery = { kind: "none" };
|
|
114
116
|
}
|
|
115
117
|
|
|
116
|
-
const current =
|
|
117
|
-
line >= 1 && line <= lines.length
|
|
118
|
-
? { hash: computeLineHash(line, lines[line - 1], hashLen), content: lines[line - 1] }
|
|
119
|
-
: null;
|
|
118
|
+
const current = line >= 1 && line <= lines.length ? { content: lines[line - 1] } : null;
|
|
120
119
|
|
|
121
120
|
return { opIndex, which, op, cited, recovery, current };
|
|
122
121
|
}
|
|
123
122
|
|
|
124
123
|
type TranslateResult =
|
|
125
|
-
| { readonly ok: true; readonly
|
|
124
|
+
| { readonly ok: true; readonly ops: readonly SpanOp[]; readonly checks: AnchorCheck[] }
|
|
126
125
|
| { readonly ok: false; readonly anchorFailures: AnchorFailure[]; readonly checks: AnchorCheck[] }
|
|
127
|
-
| { readonly ok: false; readonly rangeError:
|
|
126
|
+
| { readonly ok: false; readonly rangeError: RangeError; readonly checks: AnchorCheck[] };
|
|
127
|
+
|
|
128
|
+
type RangeError = Pick<Extract<ApplyFailure, { kind: "range" }>, "code" | "message">;
|
|
128
129
|
|
|
129
130
|
function checkedAnchor(
|
|
130
|
-
edit: Edit,
|
|
131
131
|
opIndex: number,
|
|
132
|
-
which: "
|
|
133
|
-
cited: Anchor,
|
|
132
|
+
which: AnchorCheck["which"],
|
|
134
133
|
failure: AnchorFailure | null,
|
|
135
134
|
): AnchorCheck {
|
|
136
|
-
return { opIndex, which,
|
|
135
|
+
return { opIndex, which, status: failure ? "mismatched" : "matched" };
|
|
137
136
|
}
|
|
138
137
|
|
|
139
|
-
/**
|
|
138
|
+
/** Verify an Edit against the snapshot and plan its mutation spans and captured source. */
|
|
140
139
|
function translateEdit(
|
|
141
140
|
edit: Edit,
|
|
142
141
|
opIndex: number,
|
|
@@ -146,61 +145,118 @@ function translateEdit(
|
|
|
146
145
|
): TranslateResult {
|
|
147
146
|
switch (edit.op) {
|
|
148
147
|
case "replace":
|
|
149
|
-
case "delete":
|
|
148
|
+
case "delete":
|
|
149
|
+
case "copy":
|
|
150
|
+
case "move": {
|
|
150
151
|
const failures: AnchorFailure[] = [];
|
|
151
152
|
const checks: AnchorCheck[] = [];
|
|
152
|
-
const
|
|
153
|
-
checks.push(checkedAnchor(
|
|
154
|
-
if (
|
|
155
|
-
let endLine = edit.
|
|
153
|
+
const anchorF = verifyAnchor(lines, edit.anchor, "anchor", opIndex, edit.op, hashLen, radius);
|
|
154
|
+
checks.push(checkedAnchor(opIndex, "anchor", anchorF));
|
|
155
|
+
if (anchorF) failures.push(anchorF);
|
|
156
|
+
let endLine = edit.anchor.line;
|
|
156
157
|
if (edit.end) {
|
|
157
158
|
const endF = verifyAnchor(lines, edit.end, "end", opIndex, edit.op, hashLen, radius);
|
|
158
|
-
checks.push(checkedAnchor(
|
|
159
|
+
checks.push(checkedAnchor(opIndex, "end", endF));
|
|
159
160
|
if (endF) failures.push(endF);
|
|
160
161
|
endLine = edit.end.line;
|
|
161
162
|
}
|
|
163
|
+
const destination =
|
|
164
|
+
edit.op === "copy" || edit.op === "move"
|
|
165
|
+
? edit.before !== undefined
|
|
166
|
+
? { which: "before" as const, cited: edit.before }
|
|
167
|
+
: { which: "after" as const, cited: edit.after }
|
|
168
|
+
: undefined;
|
|
169
|
+
if (destination) {
|
|
170
|
+
const failure = verifyAnchor(
|
|
171
|
+
lines,
|
|
172
|
+
destination.cited,
|
|
173
|
+
destination.which,
|
|
174
|
+
opIndex,
|
|
175
|
+
edit.op,
|
|
176
|
+
hashLen,
|
|
177
|
+
radius,
|
|
178
|
+
);
|
|
179
|
+
checks.push(checkedAnchor(opIndex, destination.which, failure));
|
|
180
|
+
if (failure) failures.push(failure);
|
|
181
|
+
}
|
|
162
182
|
if (failures.length > 0) return { ok: false, anchorFailures: failures, checks };
|
|
163
|
-
if (endLine < edit.
|
|
183
|
+
if (endLine < edit.anchor.line)
|
|
164
184
|
return {
|
|
165
185
|
ok: false,
|
|
166
|
-
rangeError:
|
|
186
|
+
rangeError: {
|
|
187
|
+
code: "INVALID_RANGE",
|
|
188
|
+
message: `Range ${edit.anchor.line}..${endLine} ends before it starts.`,
|
|
189
|
+
},
|
|
167
190
|
checks,
|
|
168
191
|
};
|
|
192
|
+
if (destination) {
|
|
193
|
+
const sourceLo = edit.anchor.line - 1;
|
|
194
|
+
const gap = destination.cited.line - (destination.which === "before" ? 1 : 0);
|
|
195
|
+
if (edit.op === "move" && sourceLo < gap && gap < endLine) {
|
|
196
|
+
return {
|
|
197
|
+
ok: false,
|
|
198
|
+
rangeError: {
|
|
199
|
+
code: "INVALID_RANGE",
|
|
200
|
+
message: `Destination ${destination.which} line ${destination.cited.line} lies inside moved range ${edit.anchor.line}..${endLine}.`,
|
|
201
|
+
},
|
|
202
|
+
checks,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
if (edit.op === "move" && (gap === sourceLo || gap === endLine)) {
|
|
206
|
+
return { ok: true, ops: [], checks };
|
|
207
|
+
}
|
|
208
|
+
const insertion: SpanOp = {
|
|
209
|
+
lo: gap,
|
|
210
|
+
hi: gap,
|
|
211
|
+
kind: "capture",
|
|
212
|
+
sourceLo,
|
|
213
|
+
sourceHi: endLine,
|
|
214
|
+
};
|
|
215
|
+
return {
|
|
216
|
+
ok: true,
|
|
217
|
+
checks,
|
|
218
|
+
ops:
|
|
219
|
+
edit.op === "move"
|
|
220
|
+
? [insertion, { lo: sourceLo, hi: endLine, kind: "body", newLines: [] }]
|
|
221
|
+
: [insertion],
|
|
222
|
+
};
|
|
223
|
+
}
|
|
169
224
|
return {
|
|
170
225
|
ok: true,
|
|
171
226
|
checks,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
227
|
+
ops: [
|
|
228
|
+
{
|
|
229
|
+
lo: edit.anchor.line - 1,
|
|
230
|
+
hi: endLine,
|
|
231
|
+
kind: "body",
|
|
232
|
+
newLines: edit.op === "replace" ? edit.body : [],
|
|
233
|
+
},
|
|
234
|
+
],
|
|
177
235
|
};
|
|
178
236
|
}
|
|
179
237
|
case "insert_after":
|
|
180
238
|
case "insert_before": {
|
|
181
239
|
const failure = verifyAnchor(lines, edit.anchor, "anchor", opIndex, edit.op, hashLen, radius);
|
|
182
|
-
const checks = [checkedAnchor(
|
|
240
|
+
const checks = [checkedAnchor(opIndex, "anchor", failure)];
|
|
183
241
|
if (failure) return { ok: false, anchorFailures: [failure], checks };
|
|
184
242
|
const line = edit.anchor.line - (edit.op === "insert_before" ? 1 : 0);
|
|
185
|
-
return { ok: true, checks,
|
|
243
|
+
return { ok: true, checks, ops: [{ lo: line, hi: line, kind: "body", newLines: edit.body }] };
|
|
186
244
|
}
|
|
187
245
|
case "append":
|
|
188
246
|
return {
|
|
189
247
|
ok: true,
|
|
190
248
|
checks: [],
|
|
191
|
-
|
|
249
|
+
ops: [{ lo: lines.length, hi: lines.length, kind: "body", newLines: edit.body }],
|
|
192
250
|
};
|
|
193
251
|
case "prepend":
|
|
194
|
-
return { ok: true, checks: [],
|
|
252
|
+
return { ok: true, checks: [], ops: [{ lo: 0, hi: 0, kind: "body", newLines: edit.body }] };
|
|
195
253
|
}
|
|
196
254
|
}
|
|
197
255
|
|
|
198
256
|
/**
|
|
199
|
-
* Apply edits to `text`. Anchors are verified against the current content
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
* are collected and returned together — nothing is written. Every failure includes
|
|
203
|
-
* per-input anchor checks.
|
|
257
|
+
* Apply edits to `text`. Anchors are verified against the current content.
|
|
258
|
+
* On any anchor mismatch, all failures (with shifted recovery) are collected
|
|
259
|
+
* and returned together. Every failure includes per-input anchor checks.
|
|
204
260
|
*
|
|
205
261
|
* @param text current full file text
|
|
206
262
|
* @param edits validated edit operations; each body element is one logical line
|
|
@@ -220,13 +276,13 @@ export function applyEdits(
|
|
|
220
276
|
const ops: SpanOp[] = [];
|
|
221
277
|
const anchorFailures: AnchorFailure[] = [];
|
|
222
278
|
const anchorChecks: AnchorCheck[] = [];
|
|
223
|
-
let rangeError:
|
|
279
|
+
let rangeError: RangeError | null = null;
|
|
224
280
|
|
|
225
281
|
for (let i = 0; i < edits.length; i++) {
|
|
226
282
|
const t = translateEdit(edits[i], i, lines, hashLen, shiftRadius);
|
|
227
283
|
anchorChecks.push(...t.checks);
|
|
228
284
|
if (t.ok) {
|
|
229
|
-
ops.push(
|
|
285
|
+
for (const op of t.ops) ops.push(op);
|
|
230
286
|
} else if ("anchorFailures" in t) {
|
|
231
287
|
anchorFailures.push(...t.anchorFailures);
|
|
232
288
|
} else if (rangeError === null) {
|
|
@@ -243,17 +299,22 @@ export function applyEdits(
|
|
|
243
299
|
};
|
|
244
300
|
}
|
|
245
301
|
if (rangeError !== null) {
|
|
246
|
-
return { ok: false, failure: { kind: "range",
|
|
302
|
+
return { ok: false, failure: { kind: "range", ...rangeError, checks: anchorChecks } };
|
|
247
303
|
}
|
|
248
304
|
|
|
305
|
+
// A stable sort keeps batch order among insertions at one position.
|
|
249
306
|
const sorted = [...ops].sort((a, b) => a.lo - b.lo || a.hi - b.hi);
|
|
250
|
-
const conflict = findSortedRangeConflict(
|
|
307
|
+
const conflict = findSortedRangeConflict(
|
|
308
|
+
sorted.map((op) => [op.lo, op.hi]),
|
|
309
|
+
true,
|
|
310
|
+
);
|
|
251
311
|
if (conflict !== undefined) {
|
|
252
312
|
return {
|
|
253
313
|
ok: false,
|
|
254
314
|
failure: {
|
|
255
315
|
kind: "range",
|
|
256
|
-
|
|
316
|
+
code: "OVERLAPPING_EDITS",
|
|
317
|
+
message: `Edits overlap near line ${sorted[conflict].lo + 1}.`,
|
|
257
318
|
checks: anchorChecks,
|
|
258
319
|
},
|
|
259
320
|
};
|
|
@@ -272,19 +333,23 @@ export function applyEdits(
|
|
|
272
333
|
// function arguments, and later operations keep their original coordinates.
|
|
273
334
|
const result: { content: string; separator: string }[] = [];
|
|
274
335
|
let cursor = 0;
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
const content = lines[
|
|
278
|
-
//
|
|
336
|
+
const appendSource = (start: number, end: number) => {
|
|
337
|
+
for (let index = start; index < end; index++) {
|
|
338
|
+
const content = lines[index];
|
|
339
|
+
// Keep the verified first-line BOM at byte zero, including for captured ranges.
|
|
279
340
|
result.push({
|
|
280
|
-
content: bom &&
|
|
281
|
-
separator: separators?.[
|
|
341
|
+
content: bom && index === 0 ? content.slice(1) : content,
|
|
342
|
+
separator: separators?.[index] ?? "",
|
|
282
343
|
});
|
|
283
|
-
cursor++;
|
|
284
344
|
}
|
|
285
345
|
};
|
|
286
346
|
for (const op of sorted) {
|
|
287
|
-
|
|
347
|
+
appendSource(cursor, op.lo);
|
|
348
|
+
if (op.kind === "capture") {
|
|
349
|
+
appendSource(op.sourceLo, op.sourceHi);
|
|
350
|
+
cursor = op.hi;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
288
353
|
// The trailing gap is outside the logical replacement, just as in substring replacement.
|
|
289
354
|
const removedSeparators = separators?.slice(op.lo, Math.max(op.lo, op.hi - 1)) ?? [];
|
|
290
355
|
for (let i = 0; i < op.newLines.length; i++) {
|
|
@@ -301,49 +366,18 @@ export function applyEdits(
|
|
|
301
366
|
}
|
|
302
367
|
cursor = op.hi;
|
|
303
368
|
}
|
|
304
|
-
|
|
369
|
+
appendSource(cursor, lines.length);
|
|
305
370
|
// An empty final logical line needs its own terminator to survive splitLines.
|
|
306
371
|
// A sole BOM-backed line already has a representation without a terminator.
|
|
307
372
|
const finalNewline =
|
|
308
373
|
hasFinalNewline(text) || (result.at(-1)?.content === "" && (result.length > 1 || !bom));
|
|
309
374
|
const newText =
|
|
310
375
|
bom +
|
|
311
|
-
|
|
312
|
-
.
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
)
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
return { ok: true, text, changed: false, touchedLines: [], contextLines: [] };
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// touchedLines: new-file indices worth re-anchoring — each produced line,
|
|
322
|
-
// and for a pure delete the line that shifted into the gap (so the model
|
|
323
|
-
// gets a fresh anchor for the shifted region).
|
|
324
|
-
const touched: number[] = [];
|
|
325
|
-
const contextLines = new Set<number>();
|
|
326
|
-
let delta = 0;
|
|
327
|
-
for (const op of sorted) {
|
|
328
|
-
const newLo = op.lo + delta;
|
|
329
|
-
if (op.newLines.length > 0) {
|
|
330
|
-
for (let i = 0; i < op.newLines.length; i++) {
|
|
331
|
-
touched.push(newLo + i);
|
|
332
|
-
// An adjacent replacement may supply the deletion successor itself.
|
|
333
|
-
contextLines.delete(newLo + i);
|
|
334
|
-
}
|
|
335
|
-
} else if (op.hi > op.lo && newLo < result.length) {
|
|
336
|
-
touched.push(newLo);
|
|
337
|
-
contextLines.add(newLo);
|
|
338
|
-
}
|
|
339
|
-
delta += op.newLines.length - (op.hi - op.lo);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return {
|
|
343
|
-
ok: true,
|
|
344
|
-
text: newText,
|
|
345
|
-
changed: true,
|
|
346
|
-
touchedLines: touched,
|
|
347
|
-
contextLines: [...contextLines],
|
|
348
|
-
};
|
|
376
|
+
joinTextFragments(
|
|
377
|
+
result.flatMap(({ content, separator: current }, i) => [
|
|
378
|
+
content,
|
|
379
|
+
i < result.length - 1 || finalNewline ? current || separator : "",
|
|
380
|
+
]),
|
|
381
|
+
);
|
|
382
|
+
return { ok: true, text: newText };
|
|
349
383
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { caseVariants } from "./case-fold.ts";
|
|
4
|
+
import { MAX_UNIT } from "./character-set.ts";
|
|
5
|
+
|
|
6
|
+
for (const [char, unicode, expected] of [
|
|
7
|
+
["k", false, ["K", "k"]],
|
|
8
|
+
["s", false, ["S", "s"]],
|
|
9
|
+
["k", true, ["K", "k", "K"]],
|
|
10
|
+
["s", true, ["S", "s", "ſ"]],
|
|
11
|
+
["σ", false, ["Σ", "ς", "σ"]],
|
|
12
|
+
["ß", false, ["ß"]],
|
|
13
|
+
["😀", false, ["😀"]],
|
|
14
|
+
["é", true, ["É", "é"]],
|
|
15
|
+
["K", true, ["K", "k", "K"]],
|
|
16
|
+
["σ", true, ["Σ", "ς", "σ"]],
|
|
17
|
+
["𐐀", true, ["𐐀", "𐐨"]],
|
|
18
|
+
["ß", true, ["ß", "ẞ"]],
|
|
19
|
+
] as const) {
|
|
20
|
+
test(`${JSON.stringify(char)} unicode=${unicode} → native case variants`, () => {
|
|
21
|
+
assert.deepEqual(caseVariants(char, unicode), expected);
|
|
22
|
+
for (const flags of unicode ? ["iu", "iv"] : ["i"]) {
|
|
23
|
+
const native = new RegExp(`^${char}$`, flags);
|
|
24
|
+
for (const candidate of ["K", "k", "K", "S", "s", "ſ", "Σ", "σ", "ς", "ß", "é", "😀"])
|
|
25
|
+
assert.equal(
|
|
26
|
+
expected.some((value) => value === candidate),
|
|
27
|
+
native.test(candidate),
|
|
28
|
+
`${flags}: ${candidate}`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
test("Cartesian non-Unicode code units → case variants agree with native RegExp", () => {
|
|
35
|
+
const allUnits = Array.from({ length: MAX_UNIT + 1 }, (_, unit) =>
|
|
36
|
+
String.fromCharCode(unit),
|
|
37
|
+
).join("");
|
|
38
|
+
for (let unit = 0; unit <= MAX_UNIT; unit++) {
|
|
39
|
+
const char = String.fromCharCode(unit);
|
|
40
|
+
const source = `\\u${unit.toString(16).padStart(4, "0")}`;
|
|
41
|
+
const expected = allUnits.match(new RegExp(source, "gi"));
|
|
42
|
+
assert.deepEqual(caseVariants(char, false), expected, source);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JavaScript ignoreCase equivalence, derived from the engine's own definitions:
|
|
3
|
+
* without u/v, ECMA-262 Canonicalize through toUpperCase; with u/v, RegExp itself.
|
|
4
|
+
*
|
|
5
|
+
* @module pi-hashline-edit/core
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { MAX_UNIT, MAX_SCALAR, nativeCharacterRanges } from "./character-set.ts";
|
|
9
|
+
const MAX_ASCII = 0x7f;
|
|
10
|
+
|
|
11
|
+
/** Code units that ignoreCase treats as equal: ECMA-262 Canonicalize without the u flag. */
|
|
12
|
+
function equivalentCases(): readonly (readonly number[])[] {
|
|
13
|
+
const groups = new Map<number, number[]>();
|
|
14
|
+
for (let unit = 0; unit <= MAX_UNIT; unit++) {
|
|
15
|
+
const upper = String.fromCharCode(unit).toUpperCase();
|
|
16
|
+
const canonical =
|
|
17
|
+
upper.length === 1 && !(unit >= 128 && upper.charCodeAt(0) < 128)
|
|
18
|
+
? upper.charCodeAt(0)
|
|
19
|
+
: unit;
|
|
20
|
+
const group = groups.get(canonical);
|
|
21
|
+
if (group) group.push(unit);
|
|
22
|
+
else groups.set(canonical, [unit]);
|
|
23
|
+
}
|
|
24
|
+
return [...groups.values()].filter((group) => group.length > 1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let unitVariants: ReadonlyMap<number, readonly string[]> | undefined;
|
|
28
|
+
|
|
29
|
+
function variantsWithoutUnicode(unit: number): readonly string[] | undefined {
|
|
30
|
+
if (!unitVariants) {
|
|
31
|
+
const variants = new Map<number, readonly string[]>();
|
|
32
|
+
for (const group of equivalentCases()) {
|
|
33
|
+
const chars = group.map((member) => String.fromCharCode(member));
|
|
34
|
+
for (const member of group) variants.set(member, chars);
|
|
35
|
+
}
|
|
36
|
+
unitVariants = variants;
|
|
37
|
+
}
|
|
38
|
+
return unitVariants.get(unit);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let asciiVariants: readonly (readonly string[])[] | undefined;
|
|
42
|
+
|
|
43
|
+
/** With u/v, every character that RegExp matches caselessly against each ASCII character. */
|
|
44
|
+
function variantsOfAscii(code: number): readonly string[] {
|
|
45
|
+
if (!asciiVariants) {
|
|
46
|
+
const probe = /^[\0-\x7f]$/iu;
|
|
47
|
+
const hits: string[] = [];
|
|
48
|
+
for (let point = 0; point <= MAX_SCALAR; point++) {
|
|
49
|
+
if (point === 0xd800) point = 0xe000;
|
|
50
|
+
const char = String.fromCodePoint(point);
|
|
51
|
+
if (probe.test(char)) hits.push(char);
|
|
52
|
+
}
|
|
53
|
+
asciiVariants = Array.from({ length: MAX_ASCII + 1 }, (_, ascii) => {
|
|
54
|
+
const exact = new RegExp(`^\\x${ascii.toString(16).padStart(2, "0")}$`, "iu");
|
|
55
|
+
return hits.filter((char) => exact.test(char));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return asciiVariants[code];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The characters JavaScript's ignoreCase matches for one character, itself included,
|
|
63
|
+
* derived from V8 for Unicode and ECMAScript Canonicalize otherwise.
|
|
64
|
+
*/
|
|
65
|
+
export function caseVariants(char: string, unicode: boolean): readonly string[] {
|
|
66
|
+
const point = char.codePointAt(0)!;
|
|
67
|
+
if (unicode) {
|
|
68
|
+
if (point <= MAX_ASCII) return variantsOfAscii(point);
|
|
69
|
+
const result: string[] = [];
|
|
70
|
+
for (const [low, high] of nativeCharacterRanges(`\\u{${point.toString(16)}}`, "iu"))
|
|
71
|
+
for (let code = low; code <= high; code++) result.push(String.fromCodePoint(code));
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
// Without u, ignoreCase never folds a supplementary character's surrogates.
|
|
75
|
+
if (point > MAX_UNIT) return [char];
|
|
76
|
+
return variantsWithoutUnicode(point) ?? [char];
|
|
77
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { RegExpParser } from "@eslint-community/regexpp";
|
|
4
|
+
import {
|
|
5
|
+
contains,
|
|
6
|
+
intersect,
|
|
7
|
+
subtract,
|
|
8
|
+
membership,
|
|
9
|
+
nativeCharacterRanges,
|
|
10
|
+
surrogatePair,
|
|
11
|
+
} from "./character-set.ts";
|
|
12
|
+
import { unsupportedByHost } from "../testing/regex-host.testing.ts";
|
|
13
|
+
|
|
14
|
+
for (const [source, flags] of [
|
|
15
|
+
["\\p{Letter}", "u"],
|
|
16
|
+
["[^\\p{Letter}]", "v"],
|
|
17
|
+
["[\\p{Letter}--[a-z]]", "iv"],
|
|
18
|
+
["(?i:[kſΣ])", "u"],
|
|
19
|
+
["[\\q{ab|😀|}]", "v"],
|
|
20
|
+
["\\uD83D", ""],
|
|
21
|
+
[".", ""],
|
|
22
|
+
[".", "s"],
|
|
23
|
+
] as const) {
|
|
24
|
+
test(`native character intervals ${source} /${flags} → agree with the host engine`, {
|
|
25
|
+
skip: unsupportedByHost([source], flags),
|
|
26
|
+
}, () => {
|
|
27
|
+
const ranges = nativeCharacterRanges(source, flags);
|
|
28
|
+
const native = membership(source, flags);
|
|
29
|
+
for (const char of [
|
|
30
|
+
"a",
|
|
31
|
+
"A",
|
|
32
|
+
"1",
|
|
33
|
+
"\n",
|
|
34
|
+
"\r",
|
|
35
|
+
"\u2028",
|
|
36
|
+
"\u0558",
|
|
37
|
+
"ſ",
|
|
38
|
+
"K",
|
|
39
|
+
"Σ",
|
|
40
|
+
"ς",
|
|
41
|
+
"é",
|
|
42
|
+
"𐐀",
|
|
43
|
+
"😀",
|
|
44
|
+
"\ud83d",
|
|
45
|
+
"�",
|
|
46
|
+
]) {
|
|
47
|
+
const isUnit = flags.includes("u") || flags.includes("v") || char.length === 1;
|
|
48
|
+
assert.equal(
|
|
49
|
+
contains(ranges, char.codePointAt(0)!),
|
|
50
|
+
isUnit && native.test(char),
|
|
51
|
+
`${source}: ${JSON.stringify(char)}`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
test("interval intersection and difference → preserve included and excluded endpoints", () => {
|
|
58
|
+
assert.deepEqual(
|
|
59
|
+
intersect(
|
|
60
|
+
[
|
|
61
|
+
[1, 4],
|
|
62
|
+
[8, 10],
|
|
63
|
+
],
|
|
64
|
+
[[3, 8]],
|
|
65
|
+
),
|
|
66
|
+
[
|
|
67
|
+
[3, 4],
|
|
68
|
+
[8, 8],
|
|
69
|
+
],
|
|
70
|
+
);
|
|
71
|
+
assert.deepEqual(
|
|
72
|
+
subtract(
|
|
73
|
+
[[1, 10]],
|
|
74
|
+
[
|
|
75
|
+
[0, 1],
|
|
76
|
+
[4, 7],
|
|
77
|
+
[10, 12],
|
|
78
|
+
],
|
|
79
|
+
),
|
|
80
|
+
[
|
|
81
|
+
[2, 3],
|
|
82
|
+
[8, 9],
|
|
83
|
+
],
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("adjacent surrogate Characters → decode a code point without joining other elements", () => {
|
|
88
|
+
const parser = new RegExpParser();
|
|
89
|
+
for (const [pattern, decoded] of [
|
|
90
|
+
["\\uD800\\uDC00", "\u{10000}"],
|
|
91
|
+
["\\uDBFF\\uDFFF", "\u{10ffff}"],
|
|
92
|
+
["😀", "😀"],
|
|
93
|
+
["\\uD83D\\uDE00", "😀"],
|
|
94
|
+
["\\uDE00\\uD83D", undefined],
|
|
95
|
+
["\\uD83D", undefined],
|
|
96
|
+
["\\uD83Dx", undefined],
|
|
97
|
+
["x\\uDE00", undefined],
|
|
98
|
+
["\\uD83D(?:)\\uDE00", undefined],
|
|
99
|
+
] as const) {
|
|
100
|
+
const [element, next] = parser.parsePattern(pattern).alternatives[0].elements;
|
|
101
|
+
assert.equal(surrogatePair(element, next), decoded?.codePointAt(0), pattern);
|
|
102
|
+
}
|
|
103
|
+
});
|