@criogaid/pi-hashline-edit 0.7.0 → 0.9.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 +76 -220
- package/README.zh-CN.md +166 -0
- package/package.json +2 -1
- package/src/core/AGENTS.md +1 -1
- package/src/core/apply.test.ts +171 -133
- package/src/core/apply.ts +34 -31
- package/src/core/case-fold.test.ts +11 -7
- package/src/core/case-fold.ts +2 -6
- package/src/core/character-set.ts +14 -0
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +25 -7
- package/src/core/ranges.test.ts +49 -9
- package/src/core/ranges.ts +15 -4
- package/src/core/regex.ts +36 -6
- package/src/core/replace-schema.ts +18 -11
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +6 -5
- package/src/core/search-analysis.test.ts +9 -0
- package/src/core/search-analysis.ts +35 -31
- package/src/core/search.ts +1 -1
- package/src/core/types.ts +6 -8
- package/src/index.ts +2 -4
- package/src/integration/config-registration.test.ts +18 -10
- package/src/integration/fusion-dispatch.test.ts +41 -22
- package/src/integration/grep-flags.test.ts +6 -13
- package/src/integration/grep-js-completeness.test.ts +41 -31
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +4 -3
- package/src/integration/grep-regex.test.ts +13 -16
- package/src/integration/grep-rg.test.ts +132 -92
- package/src/integration/grep-scan-budget.test.ts +27 -5
- package/src/integration/large-text.test.ts +4 -5
- package/src/integration/model-budget-lifecycle.test.ts +26 -8
- package/src/integration/mutation-file-types.test.ts +0 -1
- package/src/integration/mutation-file-types.testing.ts +2 -2
- package/src/integration/npm-package.testing.ts +7 -33
- package/src/integration/publication-read-changes.test.ts +60 -28
- package/src/integration/replace-package.testing.ts +4 -1
- package/src/integration/replace-worker-boundary.test.ts +34 -24
- package/src/integration/report-replay.test.ts +3 -3
- package/src/integration/utf8-errors.test.ts +3 -3
- package/src/pi/AGENTS.md +13 -14
- package/src/pi/action-fusion.test.ts +75 -220
- package/src/pi/action-fusion.ts +37 -74
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +7 -3
- package/src/pi/argument-diagnostics.ts +35 -186
- package/src/pi/argument-error.testing.ts +7 -4
- package/src/pi/argument-error.ts +5 -7
- package/src/pi/argument-format.test.ts +25 -38
- package/src/pi/argument-validation.test.ts +446 -420
- package/src/pi/argument-validation.ts +4 -5
- package/src/pi/budgets.ts +5 -7
- package/src/pi/config.test.ts +15 -5
- package/src/pi/config.ts +6 -6
- package/src/pi/diagnostic-buffer.test.ts +2 -3
- package/src/pi/diagnostic-buffer.ts +2 -2
- package/src/pi/edit-tool.ts +175 -121
- package/src/pi/execute.test.ts +170 -227
- package/src/pi/failure-context.test.ts +61 -16
- package/src/pi/failure-context.ts +13 -10
- package/src/pi/failure-path.test.ts +115 -50
- package/src/pi/file-commit.test.ts +30 -46
- package/src/pi/file-commit.ts +11 -40
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +193 -37
- package/src/pi/fusion-card.ts +186 -116
- package/src/pi/grep-candidates.ts +25 -27
- package/src/pi/grep-matrix.test.ts +1 -1
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +62 -69
- package/src/pi/grep-render.ts +4 -1
- package/src/pi/grep-scope.ts +1 -1
- package/src/pi/grep-search.ts +85 -56
- package/src/pi/grep-tool.ts +17 -48
- package/src/pi/grep.test.ts +271 -170
- package/src/pi/mutation-outcome.testing.ts +2 -3
- package/src/pi/mutation-result.test.ts +26 -19
- package/src/pi/mutation-result.ts +25 -40
- package/src/pi/mutation-runner.ts +9 -17
- package/src/pi/output-budgets.test.ts +27 -18
- package/src/pi/path.ts +3 -1
- package/src/pi/pi.test.ts +7 -6
- package/src/pi/read-tool.ts +32 -80
- package/src/pi/read.test.ts +145 -92
- package/src/pi/regex-worker-contract.ts +5 -23
- package/src/pi/regex-worker.ts +4 -10
- package/src/pi/registration.test.ts +106 -67
- package/src/pi/render.test.ts +239 -109
- package/src/pi/render.ts +42 -22
- package/src/pi/replace-regex.ts +3 -5
- package/src/pi/replace-tool.ts +29 -27
- package/src/pi/replace-worker-contract.ts +3 -12
- package/src/pi/replace.test.ts +144 -226
- package/src/pi/report-cause.ts +23 -19
- package/src/pi/report-failure.testing.ts +2 -2
- package/src/pi/report-schema.ts +60 -45
- package/src/pi/report.test.ts +138 -71
- package/src/pi/report.ts +40 -43
- package/src/pi/result-safety.test.ts +186 -131
- package/src/pi/rg-process.test.ts +36 -10
- package/src/pi/rg-process.ts +9 -3
- package/src/pi/rg-regex.test.ts +17 -0
- package/src/pi/rg-regex.ts +28 -22
- package/src/pi/text-stream.test.ts +35 -36
- package/src/pi/text-stream.ts +14 -14
- package/src/pi/tool-call.testing.ts +17 -7
- package/src/pi/write-tool.ts +9 -11
- package/src/pi/write.test.ts +43 -75
- package/src/testing/regex-translation-corpus.ts +16 -0
- package/src/testing/rg-line-listing.fixture.mjs +1 -1
- package/src/integration/forget-grep.test.ts +0 -148
- package/src/pi/forget-tool.ts +0 -367
- package/src/pi/forget.test.ts +0 -630
- package/src/pi/forget.testing.ts +0 -201
package/src/core/apply.ts
CHANGED
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
* deferred until anchors are corrected.
|
|
22
22
|
*
|
|
23
23
|
* Other strict semantics:
|
|
24
|
-
* - Operation ranges must not overlap
|
|
24
|
+
* - Operation ranges must not overlap, and each position takes at most one insertion,
|
|
25
|
+
* copy/move destinations included. An insertion at the start of a replaced or deleted
|
|
26
|
+
* range applies before that range.
|
|
25
27
|
* - Byte-identical output is a success; the commit layer reports it as a no-op.
|
|
26
28
|
*
|
|
27
29
|
* @module pi-hashline-edit/core
|
|
@@ -31,6 +33,7 @@ import { computeLineHash } from "./hash.ts";
|
|
|
31
33
|
import {
|
|
32
34
|
detectLineEnding,
|
|
33
35
|
hasFinalNewline,
|
|
36
|
+
joinTextFragments,
|
|
34
37
|
lineSeparators,
|
|
35
38
|
replacementSeparator,
|
|
36
39
|
splitLines,
|
|
@@ -113,10 +116,7 @@ function verifyAnchor(
|
|
|
113
116
|
recovery = { kind: "none" };
|
|
114
117
|
}
|
|
115
118
|
|
|
116
|
-
const current =
|
|
117
|
-
line >= 1 && line <= lines.length
|
|
118
|
-
? { hash: computeLineHash(line, lines[line - 1], hashLen), content: lines[line - 1] }
|
|
119
|
-
: null;
|
|
119
|
+
const current = line >= 1 && line <= lines.length ? { content: lines[line - 1] } : null;
|
|
120
120
|
|
|
121
121
|
return { opIndex, which, op, cited, recovery, current };
|
|
122
122
|
}
|
|
@@ -129,13 +129,11 @@ type TranslateResult =
|
|
|
129
129
|
type RangeError = Pick<Extract<ApplyFailure, { kind: "range" }>, "code" | "message">;
|
|
130
130
|
|
|
131
131
|
function checkedAnchor(
|
|
132
|
-
edit: Edit,
|
|
133
132
|
opIndex: number,
|
|
134
133
|
which: AnchorCheck["which"],
|
|
135
|
-
cited: Anchor,
|
|
136
134
|
failure: AnchorFailure | null,
|
|
137
135
|
): AnchorCheck {
|
|
138
|
-
return { opIndex, which,
|
|
136
|
+
return { opIndex, which, status: failure ? "mismatched" : "matched" };
|
|
139
137
|
}
|
|
140
138
|
|
|
141
139
|
/** Verify an Edit against the snapshot and plan its mutation spans and captured source. */
|
|
@@ -153,13 +151,13 @@ function translateEdit(
|
|
|
153
151
|
case "move": {
|
|
154
152
|
const failures: AnchorFailure[] = [];
|
|
155
153
|
const checks: AnchorCheck[] = [];
|
|
156
|
-
const
|
|
157
|
-
checks.push(checkedAnchor(
|
|
158
|
-
if (
|
|
159
|
-
let endLine = edit.
|
|
154
|
+
const anchorF = verifyAnchor(lines, edit.anchor, "anchor", opIndex, edit.op, hashLen, radius);
|
|
155
|
+
checks.push(checkedAnchor(opIndex, "anchor", anchorF));
|
|
156
|
+
if (anchorF) failures.push(anchorF);
|
|
157
|
+
let endLine = edit.anchor.line;
|
|
160
158
|
if (edit.end) {
|
|
161
159
|
const endF = verifyAnchor(lines, edit.end, "end", opIndex, edit.op, hashLen, radius);
|
|
162
|
-
checks.push(checkedAnchor(
|
|
160
|
+
checks.push(checkedAnchor(opIndex, "end", endF));
|
|
163
161
|
if (endF) failures.push(endF);
|
|
164
162
|
endLine = edit.end.line;
|
|
165
163
|
}
|
|
@@ -179,28 +177,28 @@ function translateEdit(
|
|
|
179
177
|
hashLen,
|
|
180
178
|
radius,
|
|
181
179
|
);
|
|
182
|
-
checks.push(checkedAnchor(
|
|
180
|
+
checks.push(checkedAnchor(opIndex, destination.which, failure));
|
|
183
181
|
if (failure) failures.push(failure);
|
|
184
182
|
}
|
|
185
183
|
if (failures.length > 0) return { ok: false, anchorFailures: failures, checks };
|
|
186
|
-
if (endLine < edit.
|
|
184
|
+
if (endLine < edit.anchor.line)
|
|
187
185
|
return {
|
|
188
186
|
ok: false,
|
|
189
187
|
rangeError: {
|
|
190
188
|
code: "INVALID_RANGE",
|
|
191
|
-
message: `Range ${edit.
|
|
189
|
+
message: `Range ${edit.anchor.line}..${endLine} ends before it starts.`,
|
|
192
190
|
},
|
|
193
191
|
checks,
|
|
194
192
|
};
|
|
195
193
|
if (destination) {
|
|
196
|
-
const sourceLo = edit.
|
|
194
|
+
const sourceLo = edit.anchor.line - 1;
|
|
197
195
|
const gap = destination.cited.line - (destination.which === "before" ? 1 : 0);
|
|
198
196
|
if (edit.op === "move" && sourceLo < gap && gap < endLine) {
|
|
199
197
|
return {
|
|
200
198
|
ok: false,
|
|
201
199
|
rangeError: {
|
|
202
200
|
code: "INVALID_RANGE",
|
|
203
|
-
message: `Destination ${destination.which} line ${destination.cited.line} lies inside moved range ${edit.
|
|
201
|
+
message: `Destination ${destination.which} line ${destination.cited.line} lies inside moved range ${edit.anchor.line}..${endLine}.`,
|
|
204
202
|
},
|
|
205
203
|
checks,
|
|
206
204
|
};
|
|
@@ -229,7 +227,7 @@ function translateEdit(
|
|
|
229
227
|
checks,
|
|
230
228
|
ops: [
|
|
231
229
|
{
|
|
232
|
-
lo: edit.
|
|
230
|
+
lo: edit.anchor.line - 1,
|
|
233
231
|
hi: endLine,
|
|
234
232
|
kind: "body",
|
|
235
233
|
newLines: edit.op === "replace" ? edit.body : [],
|
|
@@ -240,7 +238,7 @@ function translateEdit(
|
|
|
240
238
|
case "insert_after":
|
|
241
239
|
case "insert_before": {
|
|
242
240
|
const failure = verifyAnchor(lines, edit.anchor, "anchor", opIndex, edit.op, hashLen, radius);
|
|
243
|
-
const checks = [checkedAnchor(
|
|
241
|
+
const checks = [checkedAnchor(opIndex, "anchor", failure)];
|
|
244
242
|
if (failure) return { ok: false, anchorFailures: [failure], checks };
|
|
245
243
|
const line = edit.anchor.line - (edit.op === "insert_before" ? 1 : 0);
|
|
246
244
|
return { ok: true, checks, ops: [{ lo: line, hi: line, kind: "body", newLines: edit.body }] };
|
|
@@ -306,14 +304,22 @@ export function applyEdits(
|
|
|
306
304
|
}
|
|
307
305
|
|
|
308
306
|
const sorted = [...ops].sort((a, b) => a.lo - b.lo || a.hi - b.hi);
|
|
309
|
-
const conflict = findSortedRangeConflict(
|
|
307
|
+
const conflict = findSortedRangeConflict(
|
|
308
|
+
sorted.map((op) => [op.lo, op.hi]),
|
|
309
|
+
true,
|
|
310
|
+
);
|
|
310
311
|
if (conflict !== undefined) {
|
|
312
|
+
const { lo, hi } = sorted[conflict];
|
|
313
|
+
const previous = sorted[conflict - 1];
|
|
314
|
+
const repeatedInsertion = lo === hi && previous.lo === lo && previous.hi === hi;
|
|
311
315
|
return {
|
|
312
316
|
ok: false,
|
|
313
317
|
failure: {
|
|
314
318
|
kind: "range",
|
|
315
319
|
code: "OVERLAPPING_EDITS",
|
|
316
|
-
message:
|
|
320
|
+
message: repeatedInsertion
|
|
321
|
+
? `Edits insert more than once at one position near line ${lo + 1}.`
|
|
322
|
+
: `Edits overlap near line ${lo + 1}.`,
|
|
317
323
|
checks: anchorChecks,
|
|
318
324
|
},
|
|
319
325
|
};
|
|
@@ -372,14 +378,11 @@ export function applyEdits(
|
|
|
372
378
|
hasFinalNewline(text) || (result.at(-1)?.content === "" && (result.length > 1 || !bom));
|
|
373
379
|
const newText =
|
|
374
380
|
bom +
|
|
375
|
-
|
|
376
|
-
.
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
: ""),
|
|
382
|
-
)
|
|
383
|
-
.join("");
|
|
381
|
+
joinTextFragments(
|
|
382
|
+
result.flatMap(({ content, separator: current }, i) => [
|
|
383
|
+
content,
|
|
384
|
+
i < result.length - 1 || finalNewline ? current || separator : "",
|
|
385
|
+
]),
|
|
386
|
+
);
|
|
384
387
|
return { ok: true, text: newText };
|
|
385
388
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { test } from "node:test";
|
|
3
|
-
import { caseVariants
|
|
3
|
+
import { caseVariants } from "./case-fold.ts";
|
|
4
|
+
import { MAX_UNIT } from "./character-set.ts";
|
|
4
5
|
|
|
5
6
|
for (const [char, unicode, expected] of [
|
|
6
7
|
["k", false, ["K", "k"]],
|
|
@@ -30,11 +31,14 @@ for (const [char, unicode, expected] of [
|
|
|
30
31
|
});
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
test("non-Unicode
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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);
|
|
39
43
|
}
|
|
40
44
|
});
|
package/src/core/case-fold.ts
CHANGED
|
@@ -8,11 +8,8 @@
|
|
|
8
8
|
import { MAX_UNIT, MAX_SCALAR, nativeCharacterRanges } from "./character-set.ts";
|
|
9
9
|
const MAX_ASCII = 0x7f;
|
|
10
10
|
|
|
11
|
-
let unitGroups: readonly (readonly number[])[] | undefined;
|
|
12
|
-
|
|
13
11
|
/** Code units that ignoreCase treats as equal: ECMA-262 Canonicalize without the u flag. */
|
|
14
|
-
|
|
15
|
-
if (unitGroups) return unitGroups;
|
|
12
|
+
function equivalentCases(): readonly (readonly number[])[] {
|
|
16
13
|
const groups = new Map<number, number[]>();
|
|
17
14
|
for (let unit = 0; unit <= MAX_UNIT; unit++) {
|
|
18
15
|
const upper = String.fromCharCode(unit).toUpperCase();
|
|
@@ -24,8 +21,7 @@ export function equivalentCases(): readonly (readonly number[])[] {
|
|
|
24
21
|
if (group) group.push(unit);
|
|
25
22
|
else groups.set(canonical, [unit]);
|
|
26
23
|
}
|
|
27
|
-
|
|
28
|
-
return unitGroups;
|
|
24
|
+
return [...groups.values()].filter((group) => group.length > 1);
|
|
29
25
|
}
|
|
30
26
|
|
|
31
27
|
let unitVariants: ReadonlyMap<number, readonly string[]> | undefined;
|
|
@@ -139,6 +139,20 @@ export function foldRanges(ranges: Ranges, unicode: boolean): Ranges {
|
|
|
139
139
|
return nativeCharacterRanges(source, unicode ? "iu" : "i");
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
+
/**
|
|
143
|
+
* The multi-character strings a class node matches under `flags`; undefined when a string
|
|
144
|
+
* property cannot be enumerated here.
|
|
145
|
+
*/
|
|
146
|
+
export function acceptedClassStrings(
|
|
147
|
+
node: CharacterNode,
|
|
148
|
+
flags: string,
|
|
149
|
+
): readonly string[] | undefined {
|
|
150
|
+
const strings = classStrings(node);
|
|
151
|
+
if (!strings?.length) return strings;
|
|
152
|
+
const accepts = membership(node.raw, flags);
|
|
153
|
+
return strings.filter((value) => accepts.test(value));
|
|
154
|
+
}
|
|
155
|
+
|
|
142
156
|
/** String representatives of a class; undefined means a string property is not enumerable here. */
|
|
143
157
|
export function classStrings(node: ClassNode): readonly string[] | undefined {
|
|
144
158
|
switch (node.type) {
|
package/src/core/hash.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { test } from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import { computeLineHash
|
|
3
|
+
import { computeLineHash } from "./hash.ts";
|
|
4
4
|
|
|
5
5
|
const ALLOWED = new Set("0123456789ABCDEFGHJKMNPQRSTVWXYZ");
|
|
6
6
|
|
|
@@ -21,10 +21,6 @@ test("different content → different hash", () => {
|
|
|
21
21
|
assert.notEqual(computeLineHash(1, "a", 4), computeLineHash(1, "b", 4));
|
|
22
22
|
});
|
|
23
23
|
|
|
24
|
-
test("same (line, content) → same hash", () => {
|
|
25
|
-
assert.equal(computeLineHash(7, "x", 4), computeLineHash(7, "x", 4));
|
|
26
|
-
});
|
|
27
|
-
|
|
28
24
|
test("base32 alphabet (without I/L/O/U) in bulk", () => {
|
|
29
25
|
for (let i = 0; i < 2000; i++) {
|
|
30
26
|
const h = computeLineHash(i + 1, `line ${i}`, 4);
|
|
@@ -32,31 +28,11 @@ test("base32 alphabet (without I/L/O/U) in bulk", () => {
|
|
|
32
28
|
}
|
|
33
29
|
});
|
|
34
30
|
|
|
35
|
-
test("hashFileLines length equals line count", () => {
|
|
36
|
-
assert.equal(hashFileLines(["a", "b", "c"], 4).length, 3);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("hashFileLines empty file", () => {
|
|
40
|
-
assert.deepEqual(hashFileLines([], 4), []);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("hashFileLines normally disambiguates repeated content without changing hash length", () => {
|
|
44
|
-
const lines = ["", "", "", "", "", "}", "}", "}", "return", "return", ",", ","];
|
|
45
|
-
const hashes = hashFileLines(lines, 4);
|
|
46
|
-
assert.equal(new Set(hashes).size, hashes.length, "unexpected collision in fixture");
|
|
47
|
-
for (const h of hashes) assert.equal(h.length, 4, `hash ${h} is not 4 chars`);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
31
|
test("truncated checksums can collide", () => {
|
|
51
32
|
assert.equal(computeLineHash(1, "const value = 558;", 4), "TM02");
|
|
52
33
|
assert.equal(computeLineHash(1, "const value = 9344;", 4), "TM02");
|
|
53
34
|
});
|
|
54
35
|
|
|
55
|
-
test("hashFileLines respects the length parameter", () => {
|
|
56
|
-
assert.equal(hashFileLines(["a", "b"], 6)[0].length, 6);
|
|
57
|
-
assert.equal(hashFileLines(["a", "b"], 4)[0].length, 4);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
36
|
test("hashLen 8 has a leading zero due to the 32-bit hash limit", () => {
|
|
61
37
|
for (let i = 1; i <= 20; i++) {
|
|
62
38
|
const h = computeLineHash(i, `content ${i}`, 8);
|
package/src/core/hash.ts
CHANGED
|
@@ -86,8 +86,3 @@ export const HASH_LEN_MAX = 8;
|
|
|
86
86
|
export function computeLineHash(line: number, content: string, len: number): string {
|
|
87
87
|
return toBase32(fnv1a32Line(line, content), len);
|
|
88
88
|
}
|
|
89
|
-
|
|
90
|
-
/** Compute compact per-line checksums; callers must treat collisions as possible. */
|
|
91
|
-
export function hashFileLines(lines: readonly string[], len: number): string[] {
|
|
92
|
-
return lines.map((content, i) => computeLineHash(i + 1, content, len));
|
|
93
|
-
}
|
package/src/core/lines.ts
CHANGED
|
@@ -37,11 +37,33 @@ export function replacementSeparator(
|
|
|
37
37
|
return separators[index] || separators[separators.length - 1] || fallback;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
/** Keep a trailing CR as content when the following fragment starts with an LF separator. */
|
|
41
|
+
function preserveBoundaryCarriageReturn(previous: string, next: string): string {
|
|
42
|
+
return previous.endsWith("\r") && next.startsWith("\n") ? `\r${next}` : next;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Join independent raw fragments without absorbing trailing CR content into the next LF. */
|
|
46
|
+
export function joinTextFragments(parts: readonly string[]): string {
|
|
47
|
+
const joined: string[] = [];
|
|
48
|
+
let previous = "";
|
|
49
|
+
for (const part of parts) {
|
|
50
|
+
if (!part) continue;
|
|
51
|
+
joined.push(preserveBoundaryCarriageReturn(previous, part));
|
|
52
|
+
previous = part;
|
|
53
|
+
}
|
|
54
|
+
return joined.join("");
|
|
55
|
+
}
|
|
56
|
+
|
|
40
57
|
/** Restore a logical replacement without normalizing it a second time. */
|
|
41
58
|
export function restoreLineEndings(logical: string, original: string, fallback: string): string {
|
|
42
59
|
const separators = lineSeparators(original);
|
|
43
60
|
let index = 0;
|
|
44
|
-
return logical.replace(/\n/g, (
|
|
61
|
+
return logical.replace(/\n/g, (_match, offset: number) =>
|
|
62
|
+
preserveBoundaryCarriageReturn(
|
|
63
|
+
logical[offset - 1] ?? "",
|
|
64
|
+
replacementSeparator(separators, index++, fallback),
|
|
65
|
+
),
|
|
66
|
+
);
|
|
45
67
|
}
|
|
46
68
|
|
|
47
69
|
/** Build an LF matching view with UTF-16 boundary offsets back into the original text. */
|
|
@@ -76,12 +98,8 @@ export function createLfTextView(source: string) {
|
|
|
76
98
|
*/
|
|
77
99
|
export function splitLines(text: string): string[] {
|
|
78
100
|
if (text === "") return [];
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
const normalized = text.replaceAll("\r\n", "\n");
|
|
83
|
-
const lines = (normalized.endsWith("\n") ? normalized.slice(0, -1) : normalized).split("\n");
|
|
84
|
-
return lines;
|
|
101
|
+
const normalized = normalizeLineEndings(text);
|
|
102
|
+
return (normalized.endsWith("\n") ? normalized.slice(0, -1) : normalized).split("\n");
|
|
85
103
|
}
|
|
86
104
|
|
|
87
105
|
/** Whether the text uses CRLF at all (any `\r\n` counts; mixed files report "crlf"). */
|
package/src/core/ranges.test.ts
CHANGED
|
@@ -2,15 +2,16 @@ import assert from "node:assert/strict";
|
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import { findSortedRangeConflict, mergeRanges, type HalfOpenRange } from "./ranges.ts";
|
|
4
4
|
|
|
5
|
-
test("
|
|
6
|
-
const cases: [HalfOpenRange[], number | undefined][] = [
|
|
7
|
-
[[], undefined],
|
|
5
|
+
test("sorted ranges sharing or crossing boundaries → report conflicts according to insertion policy", () => {
|
|
6
|
+
const cases: [HalfOpenRange[], number | undefined, number | undefined][] = [
|
|
7
|
+
[[], undefined, undefined],
|
|
8
8
|
[
|
|
9
9
|
[
|
|
10
10
|
[0, 2],
|
|
11
11
|
[2, 4],
|
|
12
12
|
],
|
|
13
13
|
undefined,
|
|
14
|
+
undefined,
|
|
14
15
|
],
|
|
15
16
|
[
|
|
16
17
|
[
|
|
@@ -18,6 +19,7 @@ test("conflicts distinguish adjacent ranges from insertions at starts and interi
|
|
|
18
19
|
[2, 2],
|
|
19
20
|
],
|
|
20
21
|
undefined,
|
|
22
|
+
undefined,
|
|
21
23
|
],
|
|
22
24
|
[
|
|
23
25
|
[
|
|
@@ -25,13 +27,24 @@ test("conflicts distinguish adjacent ranges from insertions at starts and interi
|
|
|
25
27
|
[0, 0],
|
|
26
28
|
],
|
|
27
29
|
1,
|
|
30
|
+
1,
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
[
|
|
34
|
+
[0, 0],
|
|
35
|
+
[0, 2],
|
|
36
|
+
],
|
|
37
|
+
1,
|
|
38
|
+
undefined,
|
|
28
39
|
],
|
|
29
40
|
[
|
|
30
41
|
[
|
|
42
|
+
[0, 0],
|
|
31
43
|
[0, 0],
|
|
32
44
|
[0, 2],
|
|
33
45
|
],
|
|
34
46
|
1,
|
|
47
|
+
1,
|
|
35
48
|
],
|
|
36
49
|
[
|
|
37
50
|
[
|
|
@@ -39,6 +52,7 @@ test("conflicts distinguish adjacent ranges from insertions at starts and interi
|
|
|
39
52
|
[1, 1],
|
|
40
53
|
],
|
|
41
54
|
1,
|
|
55
|
+
1,
|
|
42
56
|
],
|
|
43
57
|
[
|
|
44
58
|
[
|
|
@@ -46,6 +60,23 @@ test("conflicts distinguish adjacent ranges from insertions at starts and interi
|
|
|
46
60
|
[1, 2],
|
|
47
61
|
],
|
|
48
62
|
1,
|
|
63
|
+
1,
|
|
64
|
+
],
|
|
65
|
+
[
|
|
66
|
+
[
|
|
67
|
+
[0, 2],
|
|
68
|
+
[0, 3],
|
|
69
|
+
],
|
|
70
|
+
1,
|
|
71
|
+
1,
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
[
|
|
75
|
+
[0, 2],
|
|
76
|
+
[0, 2],
|
|
77
|
+
],
|
|
78
|
+
1,
|
|
79
|
+
1,
|
|
49
80
|
],
|
|
50
81
|
[
|
|
51
82
|
[
|
|
@@ -54,14 +85,23 @@ test("conflicts distinguish adjacent ranges from insertions at starts and interi
|
|
|
54
85
|
[1, 3],
|
|
55
86
|
],
|
|
56
87
|
2,
|
|
88
|
+
2,
|
|
57
89
|
],
|
|
58
90
|
];
|
|
59
|
-
for (const [ranges,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
91
|
+
for (const [ranges, strictConflict, insertionBeforeRangeConflict] of cases) {
|
|
92
|
+
for (const insertBeforeRange of [false, true]) {
|
|
93
|
+
const expected = insertBeforeRange ? insertionBeforeRangeConflict : strictConflict;
|
|
94
|
+
for (const shift of [0, 10]) {
|
|
95
|
+
assert.equal(
|
|
96
|
+
findSortedRangeConflict(
|
|
97
|
+
ranges.map(([start, end]) => [start + shift, end + shift]),
|
|
98
|
+
insertBeforeRange,
|
|
99
|
+
),
|
|
100
|
+
expected,
|
|
101
|
+
JSON.stringify({ ranges, insertBeforeRange, shift }),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
65
105
|
}
|
|
66
106
|
});
|
|
67
107
|
|
package/src/core/ranges.ts
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
/** Half-open bounds in a caller-selected unit (line indices or string offsets). */
|
|
2
2
|
export type HalfOpenRange = readonly [start: number, end: number];
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Find the first conflicting pair's second index. Input must be sorted by start, then end.
|
|
6
|
+
* A range starting inside an earlier non-empty range conflicts, and so do two insertions
|
|
7
|
+
* (empty ranges) at one position. An insertion at the start of a non-empty range also
|
|
8
|
+
* conflicts unless `insertBeforeRange` is set; then it applies before that range.
|
|
9
|
+
*/
|
|
10
|
+
export function findSortedRangeConflict(
|
|
11
|
+
ranges: readonly HalfOpenRange[],
|
|
12
|
+
insertBeforeRange = false,
|
|
13
|
+
): number | undefined {
|
|
6
14
|
for (let i = 1; i < ranges.length; i++) {
|
|
7
15
|
const previous = ranges[i - 1];
|
|
8
16
|
const current = ranges[i];
|
|
9
|
-
|
|
10
|
-
|
|
17
|
+
if (
|
|
18
|
+
current[0] < previous[1] ||
|
|
19
|
+
(current[0] === previous[0] && (!insertBeforeRange || current[1] === previous[1]))
|
|
20
|
+
)
|
|
21
|
+
return i;
|
|
11
22
|
}
|
|
12
23
|
return undefined;
|
|
13
24
|
}
|
package/src/core/regex.ts
CHANGED
|
@@ -1,21 +1,51 @@
|
|
|
1
1
|
/** JavaScript regex construction on the shared LF view, used by both grep and replace. */
|
|
2
|
+
import { errorMessage } from "./errors.ts";
|
|
2
3
|
import { normalizeLineEndings } from "./lines.ts";
|
|
3
4
|
import { escapeRegex } from "./text.ts";
|
|
4
5
|
|
|
5
6
|
/** JavaScript regex source on the shared LF view; literal text matches exactly. */
|
|
6
|
-
export function regexSource(find: string,
|
|
7
|
+
export function regexSource(find: string, literal: boolean): string {
|
|
7
8
|
const logical = normalizeLineEndings(find);
|
|
8
|
-
return
|
|
9
|
+
return literal ? escapeRegex(logical) : logical;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
|
-
* Both matcher modes operate on the shared LF view.
|
|
13
|
-
*
|
|
13
|
+
* Both matcher modes operate on the shared LF view. Replace's and grep's prepareArguments
|
|
14
|
+
* validate flags and compile regexes through checkRegexArguments before any file is read.
|
|
14
15
|
*/
|
|
15
|
-
export function buildRegex(find: string,
|
|
16
|
+
export function buildRegex(find: string, literal: boolean, flagsRaw: string | undefined): RegExp {
|
|
16
17
|
const set = new Set((flagsRaw ?? "").split(""));
|
|
17
18
|
set.add("g");
|
|
18
19
|
const flagStr = [...set].join("");
|
|
19
20
|
// The engine's SyntaxError already names the pattern and flags it compiled.
|
|
20
|
-
return new RegExp(regexSource(find,
|
|
21
|
+
return new RegExp(regexSource(find, literal), flagStr);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Report V8's diagnostics for a rule's flags and, in regex mode, for each pattern that
|
|
26
|
+
* buildRegex would reject. Invalid flags stop the check, since every pattern compiles with
|
|
27
|
+
* them; values that are not non-empty strings are left to schema validation.
|
|
28
|
+
*/
|
|
29
|
+
export function checkRegexArguments(
|
|
30
|
+
flagsField: string,
|
|
31
|
+
flags: string | undefined,
|
|
32
|
+
literal: boolean,
|
|
33
|
+
patterns: readonly (readonly [field: string, value: unknown])[],
|
|
34
|
+
report: (field: string, reason: string) => void,
|
|
35
|
+
): void {
|
|
36
|
+
try {
|
|
37
|
+
buildRegex("", true, flags);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
report(flagsField, errorMessage(error));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (literal) return;
|
|
43
|
+
for (const [field, value] of patterns) {
|
|
44
|
+
if (typeof value !== "string" || value === "") continue;
|
|
45
|
+
try {
|
|
46
|
+
buildRegex(value, false, flags);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
report(field, errorMessage(error));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
21
51
|
}
|
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
/** Tool-validated replacement rules and their
|
|
1
|
+
/** Tool-validated replacement rules, the engine's result, and their types; no Pi runtime imports. */
|
|
2
2
|
import { Type, type Static } from "typebox";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export const REPLACEMENT_SCHEMA = Type.Object(
|
|
4
|
+
const REPLACEMENT_SCHEMA = Type.Object(
|
|
7
5
|
{
|
|
8
6
|
find: Type.String({
|
|
9
7
|
minLength: 1,
|
|
10
8
|
description:
|
|
11
|
-
"
|
|
9
|
+
"What to match: plain text when literal is true, a JavaScript regex when false. CRLF in the file reads as LF, so a line break is \\n; in literal mode a backslash followed by n matches those two characters. In regex mode, ^ and $ need the m flag to match per line; \\d, \\w, and \\b are ASCII-based.",
|
|
12
10
|
}),
|
|
13
|
-
|
|
11
|
+
literal: Type.Boolean({
|
|
14
12
|
description:
|
|
15
|
-
"
|
|
13
|
+
"true: find matches exactly, including regex punctuation, and replace keeps $ as is. false: find is a JavaScript regex and replace expands $1, $<name>, and $&; use it only for an intentional regex.",
|
|
14
|
+
}),
|
|
15
|
+
replace: Type.String({
|
|
16
|
+
description: "Replacement text; the file's line endings are kept.",
|
|
16
17
|
}),
|
|
17
|
-
regex: Type.Optional(
|
|
18
|
-
Type.Boolean({ description: "Treat find as a JavaScript regex (default false)." }),
|
|
19
|
-
),
|
|
20
18
|
flags: Type.Optional(
|
|
21
19
|
Type.String({
|
|
22
|
-
pattern: REGEX_FLAGS_PATTERN,
|
|
23
20
|
description: "Extra regex flags, also applied in literal mode; g is always added.",
|
|
24
21
|
}),
|
|
25
22
|
),
|
|
@@ -32,3 +29,13 @@ export const REPLACEMENTS_SCHEMA = Type.Array(REPLACEMENT_SCHEMA, {
|
|
|
32
29
|
description: "Rules applied to the original content; inserted text is not searched again.",
|
|
33
30
|
});
|
|
34
31
|
export type Replacement = Readonly<Static<typeof REPLACEMENT_SCHEMA>>;
|
|
32
|
+
|
|
33
|
+
/** A replacement batch's new text and match count; the host checks it again across the worker boundary. */
|
|
34
|
+
export const REPLACEMENT_RESULT_SCHEMA = Type.Object(
|
|
35
|
+
{
|
|
36
|
+
text: Type.String(),
|
|
37
|
+
count: Type.Integer({ minimum: 1 }),
|
|
38
|
+
},
|
|
39
|
+
{ additionalProperties: false },
|
|
40
|
+
);
|
|
41
|
+
export type ReplacementResult = Static<typeof REPLACEMENT_RESULT_SCHEMA>;
|