@criogaid/pi-hashline-edit 0.6.0 → 0.7.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 +161 -304
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +10 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +189 -60
- package/src/core/apply.ts +110 -74
- package/src/core/case-fold.test.ts +40 -0
- package/src/core/case-fold.ts +81 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +170 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/lines.ts +7 -0
- package/src/core/regex.ts +21 -0
- package/src/core/replace-schema.ts +34 -0
- package/src/core/replace.ts +37 -54
- package/src/core/search-analysis.test.ts +199 -0
- package/src/core/search-analysis.ts +375 -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 +16 -11
- package/src/index.ts +25 -15
- package/src/integration/config-registration.test.ts +30 -24
- package/src/integration/forget-grep.test.ts +52 -2
- package/src/integration/fusion-dispatch.test.ts +21 -105
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +280 -0
- package/src/integration/grep-js-completeness.test.ts +208 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-path-matrix.test.ts +132 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +410 -0
- package/src/integration/grep-rg.test.ts +220 -402
- package/src/integration/grep-scan-budget.test.ts +89 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +57 -1
- package/src/integration/model-budget-lifecycle.test.ts +128 -0
- package/src/integration/mutation-file-types.test.ts +12 -25
- 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 +123 -0
- package/src/integration/publication-read-changes.test.ts +344 -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 +33 -0
- package/src/integration/replace-worker-boundary.test.ts +107 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +55 -0
- package/src/pi/action-fusion.test.ts +253 -103
- package/src/pi/action-fusion.ts +116 -290
- package/src/pi/anchor-format.ts +9 -0
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +43 -0
- package/src/pi/argument-error.ts +78 -0
- package/src/pi/argument-format.test.ts +208 -0
- package/src/pi/argument-validation.test.ts +742 -45
- package/src/pi/argument-validation.ts +56 -59
- package/src/pi/budgets.ts +45 -16
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +14 -10
- package/src/pi/config.ts +21 -7
- package/src/pi/diagnostic-buffer.test.ts +52 -16
- package/src/pi/diagnostic-buffer.ts +11 -14
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +100 -93
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +362 -315
- package/src/pi/extension-loading.test.ts +14 -0
- package/src/pi/extension-loading.testing.ts +3 -0
- package/src/pi/failure-context.test.ts +220 -88
- package/src/pi/failure-context.ts +161 -180
- package/src/pi/failure-path.test.ts +85 -88
- package/src/pi/file-commit.test.ts +78 -45
- package/src/pi/file-commit.ts +218 -151
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/forget-tool.ts +189 -90
- package/src/pi/forget.test.ts +309 -22
- package/src/pi/forget.testing.ts +41 -113
- package/src/pi/fusion-card.test.ts +40 -14
- package/src/pi/fusion-card.ts +84 -50
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +193 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-output.ts +181 -138
- package/src/pi/grep-render.ts +28 -25
- package/src/pi/grep-scope.ts +61 -53
- package/src/pi/grep-search.ts +299 -242
- package/src/pi/grep-tool.ts +150 -140
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +631 -632
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +5 -5
- package/src/pi/mutation-result.test.ts +21 -74
- package/src/pi/mutation-result.ts +74 -87
- package/src/pi/mutation-runner.ts +83 -75
- package/src/pi/output-budgets.test.ts +166 -0
- package/src/pi/path.ts +7 -0
- package/src/pi/read-tool.ts +151 -137
- package/src/pi/read.test.ts +39 -33
- package/src/pi/regex-worker-contract.ts +73 -0
- package/src/pi/regex-worker.ts +101 -0
- package/src/pi/registration.test.ts +111 -92
- package/src/pi/render.test.ts +193 -19
- package/src/pi/render.ts +137 -29
- package/src/pi/replace-regex.ts +11 -44
- package/src/pi/replace-tool.ts +38 -145
- package/src/pi/replace-worker-contract.ts +19 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +93 -160
- package/src/pi/report-cause.ts +99 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +512 -0
- package/src/pi/report.test.ts +276 -0
- package/src/pi/report.ts +288 -0
- package/src/pi/result-safety.test.ts +122 -427
- package/src/pi/rg-process.test.ts +78 -79
- package/src/pi/rg-process.ts +124 -177
- package/src/pi/rg-regex.test.ts +50 -0
- package/src/pi/rg-regex.ts +709 -0
- package/src/pi/text-stream.test.ts +67 -38
- package/src/pi/text-stream.ts +96 -75
- package/src/pi/tool-call.testing.ts +3 -39
- package/src/pi/write-tool.ts +45 -34
- package/src/pi/write.test.ts +45 -37
- 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 +308 -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/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
|
*
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
*
|
|
23
23
|
* Other strict semantics:
|
|
24
24
|
* - Operation ranges must not overlap (including the same insertion point).
|
|
25
|
-
* - Byte-identical output
|
|
25
|
+
* - Byte-identical output is a success; the commit layer reports it as a no-op.
|
|
26
26
|
*
|
|
27
27
|
* @module pi-hashline-edit/core
|
|
28
28
|
*/
|
|
@@ -40,17 +40,17 @@ import type {
|
|
|
40
40
|
AnchorCheck,
|
|
41
41
|
AnchorFailure,
|
|
42
42
|
AnchorRecovery,
|
|
43
|
+
ApplyFailure,
|
|
43
44
|
ApplyResult,
|
|
44
45
|
Edit,
|
|
45
46
|
} from "./types.ts";
|
|
46
47
|
import { findSortedRangeConflict } from "./ranges.ts";
|
|
47
48
|
|
|
48
|
-
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
49
|
+
/** Replace `[lo, hi)` with caller text or a range captured from the verified original lines. */
|
|
50
|
+
type SpanOp = { readonly lo: number; readonly hi: number } & (
|
|
51
|
+
| { readonly kind: "body"; readonly newLines: readonly string[] }
|
|
52
|
+
| { readonly kind: "capture"; readonly sourceLo: number; readonly sourceHi: number }
|
|
53
|
+
);
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
56
|
* Verify an anchor against the live content; on mismatch, attempt shifted
|
|
@@ -67,7 +67,7 @@ interface SpanOp {
|
|
|
67
67
|
function verifyAnchor(
|
|
68
68
|
lines: readonly string[],
|
|
69
69
|
cited: Anchor,
|
|
70
|
-
which: "
|
|
70
|
+
which: AnchorCheck["which"],
|
|
71
71
|
opIndex: number,
|
|
72
72
|
op: Edit["op"],
|
|
73
73
|
hashLen: number,
|
|
@@ -122,21 +122,23 @@ function verifyAnchor(
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
type TranslateResult =
|
|
125
|
-
| { readonly ok: true; readonly
|
|
125
|
+
| { readonly ok: true; readonly ops: readonly SpanOp[]; readonly checks: AnchorCheck[] }
|
|
126
126
|
| { readonly ok: false; readonly anchorFailures: AnchorFailure[]; readonly checks: AnchorCheck[] }
|
|
127
|
-
| { readonly ok: false; readonly rangeError:
|
|
127
|
+
| { readonly ok: false; readonly rangeError: RangeError; readonly checks: AnchorCheck[] };
|
|
128
|
+
|
|
129
|
+
type RangeError = Pick<Extract<ApplyFailure, { kind: "range" }>, "code" | "message">;
|
|
128
130
|
|
|
129
131
|
function checkedAnchor(
|
|
130
132
|
edit: Edit,
|
|
131
133
|
opIndex: number,
|
|
132
|
-
which: "
|
|
134
|
+
which: AnchorCheck["which"],
|
|
133
135
|
cited: Anchor,
|
|
134
136
|
failure: AnchorFailure | null,
|
|
135
137
|
): AnchorCheck {
|
|
136
138
|
return { opIndex, which, op: edit.op, cited, status: failure ? "mismatched" : "matched" };
|
|
137
139
|
}
|
|
138
140
|
|
|
139
|
-
/**
|
|
141
|
+
/** Verify an Edit against the snapshot and plan its mutation spans and captured source. */
|
|
140
142
|
function translateEdit(
|
|
141
143
|
edit: Edit,
|
|
142
144
|
opIndex: number,
|
|
@@ -146,7 +148,9 @@ function translateEdit(
|
|
|
146
148
|
): TranslateResult {
|
|
147
149
|
switch (edit.op) {
|
|
148
150
|
case "replace":
|
|
149
|
-
case "delete":
|
|
151
|
+
case "delete":
|
|
152
|
+
case "copy":
|
|
153
|
+
case "move": {
|
|
150
154
|
const failures: AnchorFailure[] = [];
|
|
151
155
|
const checks: AnchorCheck[] = [];
|
|
152
156
|
const startF = verifyAnchor(lines, edit.start, "anchor", opIndex, edit.op, hashLen, radius);
|
|
@@ -159,21 +163,78 @@ function translateEdit(
|
|
|
159
163
|
if (endF) failures.push(endF);
|
|
160
164
|
endLine = edit.end.line;
|
|
161
165
|
}
|
|
166
|
+
const destination =
|
|
167
|
+
edit.op === "copy" || edit.op === "move"
|
|
168
|
+
? edit.before !== undefined
|
|
169
|
+
? { which: "before" as const, cited: edit.before }
|
|
170
|
+
: { which: "after" as const, cited: edit.after }
|
|
171
|
+
: undefined;
|
|
172
|
+
if (destination) {
|
|
173
|
+
const failure = verifyAnchor(
|
|
174
|
+
lines,
|
|
175
|
+
destination.cited,
|
|
176
|
+
destination.which,
|
|
177
|
+
opIndex,
|
|
178
|
+
edit.op,
|
|
179
|
+
hashLen,
|
|
180
|
+
radius,
|
|
181
|
+
);
|
|
182
|
+
checks.push(checkedAnchor(edit, opIndex, destination.which, destination.cited, failure));
|
|
183
|
+
if (failure) failures.push(failure);
|
|
184
|
+
}
|
|
162
185
|
if (failures.length > 0) return { ok: false, anchorFailures: failures, checks };
|
|
163
186
|
if (endLine < edit.start.line)
|
|
164
187
|
return {
|
|
165
188
|
ok: false,
|
|
166
|
-
rangeError:
|
|
189
|
+
rangeError: {
|
|
190
|
+
code: "INVALID_RANGE",
|
|
191
|
+
message: `Range ${edit.start.line}..${endLine} ends before it starts.`,
|
|
192
|
+
},
|
|
167
193
|
checks,
|
|
168
194
|
};
|
|
195
|
+
if (destination) {
|
|
196
|
+
const sourceLo = edit.start.line - 1;
|
|
197
|
+
const gap = destination.cited.line - (destination.which === "before" ? 1 : 0);
|
|
198
|
+
if (edit.op === "move" && sourceLo < gap && gap < endLine) {
|
|
199
|
+
return {
|
|
200
|
+
ok: false,
|
|
201
|
+
rangeError: {
|
|
202
|
+
code: "INVALID_RANGE",
|
|
203
|
+
message: `Destination ${destination.which} line ${destination.cited.line} lies inside moved range ${edit.start.line}..${endLine}.`,
|
|
204
|
+
},
|
|
205
|
+
checks,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (edit.op === "move" && (gap === sourceLo || gap === endLine)) {
|
|
209
|
+
return { ok: true, ops: [], checks };
|
|
210
|
+
}
|
|
211
|
+
const insertion: SpanOp = {
|
|
212
|
+
lo: gap,
|
|
213
|
+
hi: gap,
|
|
214
|
+
kind: "capture",
|
|
215
|
+
sourceLo,
|
|
216
|
+
sourceHi: endLine,
|
|
217
|
+
};
|
|
218
|
+
return {
|
|
219
|
+
ok: true,
|
|
220
|
+
checks,
|
|
221
|
+
ops:
|
|
222
|
+
edit.op === "move"
|
|
223
|
+
? [insertion, { lo: sourceLo, hi: endLine, kind: "body", newLines: [] }]
|
|
224
|
+
: [insertion],
|
|
225
|
+
};
|
|
226
|
+
}
|
|
169
227
|
return {
|
|
170
228
|
ok: true,
|
|
171
229
|
checks,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
230
|
+
ops: [
|
|
231
|
+
{
|
|
232
|
+
lo: edit.start.line - 1,
|
|
233
|
+
hi: endLine,
|
|
234
|
+
kind: "body",
|
|
235
|
+
newLines: edit.op === "replace" ? edit.body : [],
|
|
236
|
+
},
|
|
237
|
+
],
|
|
177
238
|
};
|
|
178
239
|
}
|
|
179
240
|
case "insert_after":
|
|
@@ -182,25 +243,23 @@ function translateEdit(
|
|
|
182
243
|
const checks = [checkedAnchor(edit, opIndex, "anchor", edit.anchor, failure)];
|
|
183
244
|
if (failure) return { ok: false, anchorFailures: [failure], checks };
|
|
184
245
|
const line = edit.anchor.line - (edit.op === "insert_before" ? 1 : 0);
|
|
185
|
-
return { ok: true, checks,
|
|
246
|
+
return { ok: true, checks, ops: [{ lo: line, hi: line, kind: "body", newLines: edit.body }] };
|
|
186
247
|
}
|
|
187
248
|
case "append":
|
|
188
249
|
return {
|
|
189
250
|
ok: true,
|
|
190
251
|
checks: [],
|
|
191
|
-
|
|
252
|
+
ops: [{ lo: lines.length, hi: lines.length, kind: "body", newLines: edit.body }],
|
|
192
253
|
};
|
|
193
254
|
case "prepend":
|
|
194
|
-
return { ok: true, checks: [],
|
|
255
|
+
return { ok: true, checks: [], ops: [{ lo: 0, hi: 0, kind: "body", newLines: edit.body }] };
|
|
195
256
|
}
|
|
196
257
|
}
|
|
197
258
|
|
|
198
259
|
/**
|
|
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.
|
|
260
|
+
* Apply edits to `text`. Anchors are verified against the current content.
|
|
261
|
+
* On any anchor mismatch, all failures (with shifted recovery) are collected
|
|
262
|
+
* and returned together. Every failure includes per-input anchor checks.
|
|
204
263
|
*
|
|
205
264
|
* @param text current full file text
|
|
206
265
|
* @param edits validated edit operations; each body element is one logical line
|
|
@@ -220,13 +279,13 @@ export function applyEdits(
|
|
|
220
279
|
const ops: SpanOp[] = [];
|
|
221
280
|
const anchorFailures: AnchorFailure[] = [];
|
|
222
281
|
const anchorChecks: AnchorCheck[] = [];
|
|
223
|
-
let rangeError:
|
|
282
|
+
let rangeError: RangeError | null = null;
|
|
224
283
|
|
|
225
284
|
for (let i = 0; i < edits.length; i++) {
|
|
226
285
|
const t = translateEdit(edits[i], i, lines, hashLen, shiftRadius);
|
|
227
286
|
anchorChecks.push(...t.checks);
|
|
228
287
|
if (t.ok) {
|
|
229
|
-
ops.push(
|
|
288
|
+
for (const op of t.ops) ops.push(op);
|
|
230
289
|
} else if ("anchorFailures" in t) {
|
|
231
290
|
anchorFailures.push(...t.anchorFailures);
|
|
232
291
|
} else if (rangeError === null) {
|
|
@@ -243,7 +302,7 @@ export function applyEdits(
|
|
|
243
302
|
};
|
|
244
303
|
}
|
|
245
304
|
if (rangeError !== null) {
|
|
246
|
-
return { ok: false, failure: { kind: "range",
|
|
305
|
+
return { ok: false, failure: { kind: "range", ...rangeError, checks: anchorChecks } };
|
|
247
306
|
}
|
|
248
307
|
|
|
249
308
|
const sorted = [...ops].sort((a, b) => a.lo - b.lo || a.hi - b.hi);
|
|
@@ -253,7 +312,8 @@ export function applyEdits(
|
|
|
253
312
|
ok: false,
|
|
254
313
|
failure: {
|
|
255
314
|
kind: "range",
|
|
256
|
-
|
|
315
|
+
code: "OVERLAPPING_EDITS",
|
|
316
|
+
message: `Edits overlap near line ${sorted[conflict].lo + 1}.`,
|
|
257
317
|
checks: anchorChecks,
|
|
258
318
|
},
|
|
259
319
|
};
|
|
@@ -272,19 +332,23 @@ export function applyEdits(
|
|
|
272
332
|
// function arguments, and later operations keep their original coordinates.
|
|
273
333
|
const result: { content: string; separator: string }[] = [];
|
|
274
334
|
let cursor = 0;
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
const content = lines[
|
|
278
|
-
//
|
|
335
|
+
const appendSource = (start: number, end: number) => {
|
|
336
|
+
for (let index = start; index < end; index++) {
|
|
337
|
+
const content = lines[index];
|
|
338
|
+
// Keep the verified first-line BOM at byte zero, including for captured ranges.
|
|
279
339
|
result.push({
|
|
280
|
-
content: bom &&
|
|
281
|
-
separator: separators?.[
|
|
340
|
+
content: bom && index === 0 ? content.slice(1) : content,
|
|
341
|
+
separator: separators?.[index] ?? "",
|
|
282
342
|
});
|
|
283
|
-
cursor++;
|
|
284
343
|
}
|
|
285
344
|
};
|
|
286
345
|
for (const op of sorted) {
|
|
287
|
-
|
|
346
|
+
appendSource(cursor, op.lo);
|
|
347
|
+
if (op.kind === "capture") {
|
|
348
|
+
appendSource(op.sourceLo, op.sourceHi);
|
|
349
|
+
cursor = op.hi;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
288
352
|
// The trailing gap is outside the logical replacement, just as in substring replacement.
|
|
289
353
|
const removedSeparators = separators?.slice(op.lo, Math.max(op.lo, op.hi - 1)) ?? [];
|
|
290
354
|
for (let i = 0; i < op.newLines.length; i++) {
|
|
@@ -301,7 +365,7 @@ export function applyEdits(
|
|
|
301
365
|
}
|
|
302
366
|
cursor = op.hi;
|
|
303
367
|
}
|
|
304
|
-
|
|
368
|
+
appendSource(cursor, lines.length);
|
|
305
369
|
// An empty final logical line needs its own terminator to survive splitLines.
|
|
306
370
|
// A sole BOM-backed line already has a representation without a terminator.
|
|
307
371
|
const finalNewline =
|
|
@@ -311,39 +375,11 @@ export function applyEdits(
|
|
|
311
375
|
result
|
|
312
376
|
.map(
|
|
313
377
|
({ content, separator: current }, i) =>
|
|
314
|
-
content +
|
|
378
|
+
content +
|
|
379
|
+
(i < result.length - 1 || finalNewline
|
|
380
|
+
? current || (content.endsWith("\r") ? "\r\n" : separator)
|
|
381
|
+
: ""),
|
|
315
382
|
)
|
|
316
383
|
.join("");
|
|
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
|
-
};
|
|
384
|
+
return { ok: true, text: newText };
|
|
349
385
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { caseVariants, equivalentCases } from "./case-fold.ts";
|
|
4
|
+
|
|
5
|
+
for (const [char, unicode, expected] of [
|
|
6
|
+
["k", false, ["K", "k"]],
|
|
7
|
+
["s", false, ["S", "s"]],
|
|
8
|
+
["k", true, ["K", "k", "K"]],
|
|
9
|
+
["s", true, ["S", "s", "ſ"]],
|
|
10
|
+
["σ", false, ["Σ", "ς", "σ"]],
|
|
11
|
+
["ß", false, ["ß"]],
|
|
12
|
+
["😀", false, ["😀"]],
|
|
13
|
+
["é", true, ["É", "é"]],
|
|
14
|
+
["K", true, ["K", "k", "K"]],
|
|
15
|
+
["σ", true, ["Σ", "ς", "σ"]],
|
|
16
|
+
["𐐀", true, ["𐐀", "𐐨"]],
|
|
17
|
+
["ß", true, ["ß", "ẞ"]],
|
|
18
|
+
] as const) {
|
|
19
|
+
test(`${JSON.stringify(char)} unicode=${unicode} → native case variants`, () => {
|
|
20
|
+
assert.deepEqual(caseVariants(char, unicode), expected);
|
|
21
|
+
for (const flags of unicode ? ["iu", "iv"] : ["i"]) {
|
|
22
|
+
const native = new RegExp(`^${char}$`, flags);
|
|
23
|
+
for (const candidate of ["K", "k", "K", "S", "s", "ſ", "Σ", "σ", "ς", "ß", "é", "😀"])
|
|
24
|
+
assert.equal(
|
|
25
|
+
expected.some((value) => value === candidate),
|
|
26
|
+
native.test(candidate),
|
|
27
|
+
`${flags}: ${candidate}`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
test("non-Unicode equivalence groups → every member agrees with native RegExp", () => {
|
|
34
|
+
for (const group of equivalentCases()) {
|
|
35
|
+
const source = `^\\u${group[0].toString(16).padStart(4, "0")}$`;
|
|
36
|
+
const native = new RegExp(source, "i");
|
|
37
|
+
for (const unit of group)
|
|
38
|
+
assert.ok(native.test(String.fromCharCode(unit)), `${source}: ${unit}`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
let unitGroups: readonly (readonly number[])[] | undefined;
|
|
12
|
+
|
|
13
|
+
/** Code units that ignoreCase treats as equal: ECMA-262 Canonicalize without the u flag. */
|
|
14
|
+
export function equivalentCases(): readonly (readonly number[])[] {
|
|
15
|
+
if (unitGroups) return unitGroups;
|
|
16
|
+
const groups = new Map<number, number[]>();
|
|
17
|
+
for (let unit = 0; unit <= MAX_UNIT; unit++) {
|
|
18
|
+
const upper = String.fromCharCode(unit).toUpperCase();
|
|
19
|
+
const canonical =
|
|
20
|
+
upper.length === 1 && !(unit >= 128 && upper.charCodeAt(0) < 128)
|
|
21
|
+
? upper.charCodeAt(0)
|
|
22
|
+
: unit;
|
|
23
|
+
const group = groups.get(canonical);
|
|
24
|
+
if (group) group.push(unit);
|
|
25
|
+
else groups.set(canonical, [unit]);
|
|
26
|
+
}
|
|
27
|
+
unitGroups = [...groups.values()].filter((group) => group.length > 1);
|
|
28
|
+
return unitGroups;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let unitVariants: ReadonlyMap<number, readonly string[]> | undefined;
|
|
32
|
+
|
|
33
|
+
function variantsWithoutUnicode(unit: number): readonly string[] | undefined {
|
|
34
|
+
if (!unitVariants) {
|
|
35
|
+
const variants = new Map<number, readonly string[]>();
|
|
36
|
+
for (const group of equivalentCases()) {
|
|
37
|
+
const chars = group.map((member) => String.fromCharCode(member));
|
|
38
|
+
for (const member of group) variants.set(member, chars);
|
|
39
|
+
}
|
|
40
|
+
unitVariants = variants;
|
|
41
|
+
}
|
|
42
|
+
return unitVariants.get(unit);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let asciiVariants: readonly (readonly string[])[] | undefined;
|
|
46
|
+
|
|
47
|
+
/** With u/v, every character that RegExp matches caselessly against each ASCII character. */
|
|
48
|
+
function variantsOfAscii(code: number): readonly string[] {
|
|
49
|
+
if (!asciiVariants) {
|
|
50
|
+
const probe = /^[\0-\x7f]$/iu;
|
|
51
|
+
const hits: string[] = [];
|
|
52
|
+
for (let point = 0; point <= MAX_SCALAR; point++) {
|
|
53
|
+
if (point === 0xd800) point = 0xe000;
|
|
54
|
+
const char = String.fromCodePoint(point);
|
|
55
|
+
if (probe.test(char)) hits.push(char);
|
|
56
|
+
}
|
|
57
|
+
asciiVariants = Array.from({ length: MAX_ASCII + 1 }, (_, ascii) => {
|
|
58
|
+
const exact = new RegExp(`^\\x${ascii.toString(16).padStart(2, "0")}$`, "iu");
|
|
59
|
+
return hits.filter((char) => exact.test(char));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return asciiVariants[code];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The characters JavaScript's ignoreCase matches for one character, itself included,
|
|
67
|
+
* derived from V8 for Unicode and ECMAScript Canonicalize otherwise.
|
|
68
|
+
*/
|
|
69
|
+
export function caseVariants(char: string, unicode: boolean): readonly string[] {
|
|
70
|
+
const point = char.codePointAt(0)!;
|
|
71
|
+
if (unicode) {
|
|
72
|
+
if (point <= MAX_ASCII) return variantsOfAscii(point);
|
|
73
|
+
const result: string[] = [];
|
|
74
|
+
for (const [low, high] of nativeCharacterRanges(`\\u{${point.toString(16)}}`, "iu"))
|
|
75
|
+
for (let code = low; code <= high; code++) result.push(String.fromCodePoint(code));
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
// Without u, ignoreCase never folds a supplementary character's surrogates.
|
|
79
|
+
if (point > MAX_UNIT) return [char];
|
|
80
|
+
return variantsWithoutUnicode(point) ?? [char];
|
|
81
|
+
}
|
|
@@ -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
|
+
});
|