@criogaid/pi-hashline-edit 0.3.1 → 0.5.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 +166 -61
- package/package.json +8 -3
- package/src/core/apply.test.ts +459 -313
- package/src/core/apply.ts +251 -215
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +49 -20
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/replace.ts +109 -0
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +26 -9
- package/src/core/types.ts +56 -41
- package/src/index.ts +23 -19
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +879 -251
- package/src/integration/large-text.test.ts +63 -0
- package/src/integration/replace-package.test.ts +41 -0
- package/src/pi/action-fusion.test.ts +612 -207
- package/src/pi/action-fusion.ts +442 -254
- package/src/pi/anchor-format.test.ts +53 -21
- package/src/pi/anchor-format.ts +48 -20
- package/src/pi/budgets.ts +25 -0
- package/src/pi/config.test.ts +178 -35
- package/src/pi/config.ts +96 -37
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +360 -0
- package/src/pi/edit-tool.ts +307 -279
- package/src/pi/error-text.ts +26 -0
- package/src/pi/execute.test.ts +1718 -713
- package/src/pi/failure-context.test.ts +110 -83
- package/src/pi/failure-context.ts +232 -73
- package/src/pi/failure-path.test.ts +180 -66
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +348 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +203 -87
- package/src/pi/grep-output.ts +225 -0
- package/src/pi/grep-render.ts +59 -0
- package/src/pi/grep-scope.ts +139 -0
- package/src/pi/grep-search.ts +231 -0
- package/src/pi/grep-tool.ts +215 -774
- package/src/pi/grep.test.ts +825 -376
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +86 -0
- package/src/pi/mutation-result.ts +118 -63
- package/src/pi/mutation-runner.ts +151 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +100 -52
- package/src/pi/read-tool.ts +245 -134
- package/src/pi/registration.test.ts +271 -114
- package/src/pi/render.test.ts +227 -84
- package/src/pi/render.ts +86 -116
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +247 -241
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +778 -390
- package/src/pi/result-safety.test.ts +821 -344
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +45 -82
- package/src/pi/rg-process.test.ts +113 -0
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +72 -125
- package/src/pi/rg-text-view.ts +102 -48
- package/src/pi/schema.ts +19 -0
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +114 -52
- package/src/pi/write.test.ts +250 -97
- package/src/core/index.ts +0 -14
- package/src/pi/rg-line-filter.test.ts +0 -85
- package/src/pi/state.ts +0 -26
|
@@ -1,40 +1,66 @@
|
|
|
1
1
|
import { test } from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import {
|
|
3
|
+
import { submatchesToLineRanges } from "./rg-line-ranges.ts";
|
|
4
4
|
|
|
5
|
-
test("
|
|
6
|
-
assert.deepEqual(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
assert.deepEqual(submatchesToLineRanges(bytes, 1, [{ start: 1, end: 1 }, { start: 3, end: 3 }], 2), [[1, 3]]);
|
|
23
|
-
assert.deepEqual(submatchesToLineRanges(Buffer.from("b"), 2, [{ start: 1, end: 1 }], 2), [[2, 3]]);
|
|
5
|
+
test("multiline submatches map to distinct physical lines without inventing boundary lines", () => {
|
|
6
|
+
assert.deepEqual(
|
|
7
|
+
submatchesToLineRanges(
|
|
8
|
+
Buffer.from("alpha\nbeta\ngamma\n"),
|
|
9
|
+
1,
|
|
10
|
+
[
|
|
11
|
+
{ start: 0, end: 10 },
|
|
12
|
+
{ start: 6, end: 16 },
|
|
13
|
+
],
|
|
14
|
+
3,
|
|
15
|
+
),
|
|
16
|
+
[[1, 4]],
|
|
17
|
+
);
|
|
18
|
+
assert.deepEqual(
|
|
19
|
+
submatchesToLineRanges(Buffer.from("alpha\nbeta\n"), 1, [{ start: 0, end: 6 }], 2),
|
|
20
|
+
[[1, 2]],
|
|
21
|
+
);
|
|
24
22
|
assert.deepEqual(submatchesToLineRanges(Buffer.alloc(0), 1, [{ start: 0, end: 0 }], 0), []);
|
|
25
23
|
});
|
|
26
24
|
|
|
27
|
-
test("
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
test("zero-width EOF and UTF-8 offsets retain real lines and UTF-16 columns", () => {
|
|
26
|
+
const bytes = Buffer.from("\u{1F600}\u754Cneedle\ntail");
|
|
27
|
+
const columns = new Map<number, number>();
|
|
28
|
+
assert.deepEqual(
|
|
29
|
+
submatchesToLineRanges(
|
|
30
|
+
bytes,
|
|
31
|
+
1,
|
|
32
|
+
[
|
|
33
|
+
{ start: 7, end: 13 },
|
|
34
|
+
{ start: bytes.length, end: bytes.length },
|
|
35
|
+
],
|
|
36
|
+
2,
|
|
37
|
+
columns,
|
|
38
|
+
),
|
|
39
|
+
[[1, 3]],
|
|
40
|
+
);
|
|
41
|
+
assert.deepEqual(
|
|
42
|
+
[...columns],
|
|
43
|
+
[
|
|
44
|
+
[1, 3],
|
|
45
|
+
[2, 4],
|
|
46
|
+
],
|
|
47
|
+
);
|
|
48
|
+
assert.deepEqual(submatchesToLineRanges(Buffer.from("last"), 1, [{ start: 4, end: 4 }], 1), [
|
|
49
|
+
[1, 2],
|
|
50
|
+
]);
|
|
30
51
|
});
|
|
31
52
|
|
|
32
|
-
test("
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
53
|
+
test("invalid multiline match offsets fail before anchoring", () => {
|
|
54
|
+
assert.throws(
|
|
55
|
+
() => submatchesToLineRanges(Buffer.from("a"), 1, [{ start: 0, end: 2 }], 1),
|
|
56
|
+
/Invalid rg submatch byte offsets/,
|
|
57
|
+
);
|
|
58
|
+
assert.throws(
|
|
59
|
+
() => submatchesToLineRanges(Buffer.from("alpha\nbeta\n"), 1, [{ start: 0, end: 10 }], 1),
|
|
60
|
+
/File changed during search/,
|
|
61
|
+
);
|
|
62
|
+
assert.throws(
|
|
63
|
+
() => submatchesToLineRanges(Buffer.from("last\n"), 1, [{ start: 5, end: 5 }], 1),
|
|
64
|
+
/File changed during search/,
|
|
65
|
+
);
|
|
40
66
|
});
|
package/src/pi/rg-line-ranges.ts
CHANGED
|
@@ -1,112 +1,75 @@
|
|
|
1
1
|
import { mergeRanges, type HalfOpenRange } from "../core/ranges.ts";
|
|
2
|
+
import { searchChangedError } from "./error-text.ts";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export interface RgSubmatch {
|
|
4
|
+
interface RgSubmatch {
|
|
6
5
|
start: number;
|
|
7
6
|
end: number;
|
|
8
7
|
}
|
|
9
8
|
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
for (const range of ranges) assertRange(range);
|
|
18
|
-
return mergeRanges(ranges);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function unionRanges(left: readonly LineRange[], right: readonly LineRange[]): LineRange[] {
|
|
22
|
-
return normalizeRanges([...left, ...right]);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function intersectRanges(left: readonly LineRange[], right: readonly LineRange[]): LineRange[] {
|
|
26
|
-
const a = normalizeRanges(left);
|
|
27
|
-
const b = normalizeRanges(right);
|
|
28
|
-
const result: LineRange[] = [];
|
|
29
|
-
let i = 0;
|
|
30
|
-
let j = 0;
|
|
31
|
-
while (i < a.length && j < b.length) {
|
|
32
|
-
const start = Math.max(a[i][0], b[j][0]);
|
|
33
|
-
const end = Math.min(a[i][1], b[j][1]);
|
|
34
|
-
if (start < end) result.push([start, end]);
|
|
35
|
-
if (a[i][1] < b[j][1]) i++;
|
|
36
|
-
else j++;
|
|
9
|
+
function countLfBefore(lineBreaks: readonly number[], offset: number): number {
|
|
10
|
+
let low = 0;
|
|
11
|
+
let high = lineBreaks.length;
|
|
12
|
+
while (low < high) {
|
|
13
|
+
const middle = Math.floor((low + high) / 2);
|
|
14
|
+
if (lineBreaks[middle] < offset) low = middle + 1;
|
|
15
|
+
else high = middle;
|
|
37
16
|
}
|
|
38
|
-
return
|
|
17
|
+
return low;
|
|
39
18
|
}
|
|
40
19
|
|
|
41
|
-
|
|
42
|
-
const source = normalizeRanges(left);
|
|
43
|
-
const removed = normalizeRanges(right);
|
|
44
|
-
const result: LineRange[] = [];
|
|
45
|
-
let j = 0;
|
|
46
|
-
for (const [start, end] of source) {
|
|
47
|
-
let cursor = start;
|
|
48
|
-
while (j < removed.length && removed[j][1] <= cursor) j++;
|
|
49
|
-
let k = j;
|
|
50
|
-
while (k < removed.length && removed[k][0] < end) {
|
|
51
|
-
if (removed[k][0] > cursor) result.push([cursor, Math.min(removed[k][0], end)]);
|
|
52
|
-
cursor = Math.max(cursor, removed[k][1]);
|
|
53
|
-
if (cursor >= end) break;
|
|
54
|
-
k++;
|
|
55
|
-
}
|
|
56
|
-
if (cursor < end) result.push([cursor, end]);
|
|
57
|
-
}
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function countLfBefore(bytes: Buffer, offset: number): number {
|
|
62
|
-
let count = 0;
|
|
63
|
-
for (let i = 0; i < offset; i++) if (bytes[i] === 10) count++;
|
|
64
|
-
return count;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function zeroWidthLine(bytes: Buffer, offset: number, eventStartLine: number, fileLineCount: number): number | undefined {
|
|
68
|
-
if (fileLineCount === 0) return undefined;
|
|
69
|
-
const candidate = eventStartLine + countLfBefore(bytes, offset);
|
|
70
|
-
return Math.min(candidate, fileLineCount);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Convert rg byte offsets to physical line ranges; optionally record a match's UTF-16 column on its starting line. */
|
|
20
|
+
/** Map rg's byte offsets in a multiline JSON event to existing physical lines. */
|
|
74
21
|
export function submatchesToLineRanges(
|
|
75
22
|
bytes: Buffer,
|
|
76
23
|
eventStartLine: number,
|
|
77
24
|
submatches: readonly RgSubmatch[],
|
|
78
25
|
fileLineCount: number,
|
|
79
26
|
columns?: Map<number, number>,
|
|
80
|
-
):
|
|
81
|
-
if (
|
|
27
|
+
): HalfOpenRange[] {
|
|
28
|
+
if (
|
|
29
|
+
!Number.isSafeInteger(eventStartLine) ||
|
|
30
|
+
eventStartLine < 1 ||
|
|
31
|
+
!Number.isSafeInteger(fileLineCount) ||
|
|
32
|
+
fileLineCount < 0
|
|
33
|
+
) {
|
|
82
34
|
throw new Error("Invalid rg physical line metadata");
|
|
83
35
|
}
|
|
84
|
-
const ranges:
|
|
36
|
+
const ranges: HalfOpenRange[] = [];
|
|
37
|
+
const lineBreaks: number[] = [];
|
|
38
|
+
let at = 0;
|
|
39
|
+
while ((at = bytes.indexOf(10, at)) !== -1) {
|
|
40
|
+
lineBreaks.push(at);
|
|
41
|
+
at++;
|
|
42
|
+
}
|
|
85
43
|
const recordColumn = (line: number, offset: number) => {
|
|
86
44
|
if (!columns || columns.has(line)) return;
|
|
87
45
|
const lineStart = offset === 0 ? 0 : bytes.lastIndexOf(10, offset - 1) + 1;
|
|
88
46
|
columns.set(line, bytes.subarray(lineStart, offset).toString("utf8").length);
|
|
89
47
|
};
|
|
90
|
-
for (const
|
|
91
|
-
|
|
92
|
-
|
|
48
|
+
for (const { start, end } of submatches) {
|
|
49
|
+
if (
|
|
50
|
+
!Number.isSafeInteger(start) ||
|
|
51
|
+
!Number.isSafeInteger(end) ||
|
|
52
|
+
start < 0 ||
|
|
53
|
+
end < start ||
|
|
54
|
+
end > bytes.length
|
|
55
|
+
) {
|
|
93
56
|
throw new Error("Invalid rg submatch byte offsets");
|
|
94
57
|
}
|
|
95
58
|
if (start === end) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
59
|
+
if (fileLineCount === 0) continue;
|
|
60
|
+
const line = eventStartLine + countLfBefore(lineBreaks, start);
|
|
61
|
+
if (line > fileLineCount) throw searchChangedError();
|
|
62
|
+
ranges.push([line, line + 1]);
|
|
63
|
+
recordColumn(line, start);
|
|
102
64
|
continue;
|
|
103
65
|
}
|
|
104
|
-
const first = eventStartLine + countLfBefore(
|
|
105
|
-
const last = eventStartLine + countLfBefore(
|
|
106
|
-
if (first
|
|
107
|
-
|
|
108
|
-
recordColumn(first, start);
|
|
66
|
+
const first = eventStartLine + countLfBefore(lineBreaks, start);
|
|
67
|
+
const last = eventStartLine + countLfBefore(lineBreaks, end - 1);
|
|
68
|
+
if (first > fileLineCount || last > fileLineCount) {
|
|
69
|
+
throw searchChangedError();
|
|
109
70
|
}
|
|
71
|
+
ranges.push([first, last + 1]);
|
|
72
|
+
recordColumn(first, start);
|
|
110
73
|
}
|
|
111
|
-
return
|
|
74
|
+
return mergeRanges(ranges);
|
|
112
75
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { assertRgSucceeded, resolveIgnoreCase, type RunText } from "./rg-process.ts";
|
|
4
|
+
|
|
5
|
+
const lineModes = { literal: false, multiline: false } as const;
|
|
6
|
+
|
|
7
|
+
test("explicit case settings bypass the smart-case probe", async () => {
|
|
8
|
+
const run: RunText = async () => {
|
|
9
|
+
throw new Error("probe must not run");
|
|
10
|
+
};
|
|
11
|
+
const modes = { literal: false, multiline: true } as const;
|
|
12
|
+
assert.equal(await resolveIgnoreCase("rg", ["Foo"], modes, true, undefined, run), true);
|
|
13
|
+
assert.equal(await resolveIgnoreCase("rg", ["foo"], modes, false, undefined, run), false);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("standard smart-case probe distinguishes its sensor from user matches", async () => {
|
|
17
|
+
const calls: { args: readonly string[]; input: Buffer }[] = [];
|
|
18
|
+
const insensitive: RunText = async (_path, args, input) => {
|
|
19
|
+
calls.push({ args, input });
|
|
20
|
+
return { code: 0, stdout: "a\n\n", stderr: "" };
|
|
21
|
+
};
|
|
22
|
+
assert.equal(
|
|
23
|
+
await resolveIgnoreCase("rg", ["(a)|Foo"], lineModes, undefined, undefined, insensitive),
|
|
24
|
+
true,
|
|
25
|
+
);
|
|
26
|
+
assert.deepEqual(calls[0].input, Buffer.from("a\n"));
|
|
27
|
+
assert.ok(calls[0].args.includes("--smart-case"));
|
|
28
|
+
assert.ok(calls[0].args.includes("--no-config"));
|
|
29
|
+
assert.ok(calls[0].args.includes("--no-multiline"));
|
|
30
|
+
|
|
31
|
+
const sensitive: RunText = async () => ({ code: 0, stdout: "\n", stderr: "" });
|
|
32
|
+
assert.equal(
|
|
33
|
+
await resolveIgnoreCase("rg", ["(a)|Foo"], lineModes, undefined, undefined, sensitive),
|
|
34
|
+
false,
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("literal smart-case escapes user patterns before probing", async () => {
|
|
39
|
+
let args: readonly string[] = [];
|
|
40
|
+
const run: RunText = async (_path, received) => {
|
|
41
|
+
args = received;
|
|
42
|
+
return { code: 1, stdout: "", stderr: "" };
|
|
43
|
+
};
|
|
44
|
+
await resolveIgnoreCase(
|
|
45
|
+
"rg",
|
|
46
|
+
["foo\\S*"],
|
|
47
|
+
{ ...lineModes, literal: true },
|
|
48
|
+
undefined,
|
|
49
|
+
undefined,
|
|
50
|
+
run,
|
|
51
|
+
);
|
|
52
|
+
const patterns = args.flatMap((arg, index) => (args[index - 1] === "-e" ? [arg] : []));
|
|
53
|
+
assert.deepEqual(patterns, ["(\\p{Lu})", "foo\\\\S\\*"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("multiline smart-case probe uses the same mode as matching", async () => {
|
|
57
|
+
let args: readonly string[] = [];
|
|
58
|
+
const run: RunText = async (_path, received) => {
|
|
59
|
+
args = received;
|
|
60
|
+
return { code: 0, stdout: "a\n\n", stderr: "" };
|
|
61
|
+
};
|
|
62
|
+
assert.equal(
|
|
63
|
+
await resolveIgnoreCase(
|
|
64
|
+
"rg",
|
|
65
|
+
["foo\\nbar"],
|
|
66
|
+
{ literal: false, multiline: true },
|
|
67
|
+
undefined,
|
|
68
|
+
undefined,
|
|
69
|
+
run,
|
|
70
|
+
),
|
|
71
|
+
true,
|
|
72
|
+
);
|
|
73
|
+
assert.ok(args.includes("--multiline"));
|
|
74
|
+
assert.ok(!args.includes("--no-multiline"));
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("smart-case probe propagates parser errors and rejects unknown output", async () => {
|
|
78
|
+
const failed: RunText = async () => ({ code: 2, stdout: "", stderr: "regex parse error" });
|
|
79
|
+
await assert.rejects(
|
|
80
|
+
resolveIgnoreCase("rg", ["("], lineModes, undefined, undefined, failed),
|
|
81
|
+
/regex parse error/,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const unexpected: RunText = async () => ({ code: 0, stdout: "unexpected\n", stderr: "" });
|
|
85
|
+
await assert.rejects(
|
|
86
|
+
resolveIgnoreCase("rg", ["foo"], lineModes, undefined, undefined, unexpected),
|
|
87
|
+
/Unexpected smart-case probe output/,
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("smart-case probe normalizes cancellation without fallback", async () => {
|
|
92
|
+
const controller = new AbortController();
|
|
93
|
+
const run: RunText = async () => {
|
|
94
|
+
controller.abort();
|
|
95
|
+
return { code: 1, stdout: "", stderr: "" };
|
|
96
|
+
};
|
|
97
|
+
await assert.rejects(
|
|
98
|
+
resolveIgnoreCase("rg", ["foo"], lineModes, undefined, controller.signal, run),
|
|
99
|
+
/Operation aborted/,
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("search exit status accepts no matches and preserves failure diagnostics", () => {
|
|
104
|
+
for (const code of [0, 1]) assert.doesNotThrow(() => assertRgSucceeded({ code, stderr: "" }));
|
|
105
|
+
assert.throws(
|
|
106
|
+
() => assertRgSucceeded({ code: 2, stderr: " regex parse error\n" }),
|
|
107
|
+
/^Error: regex parse error$/,
|
|
108
|
+
);
|
|
109
|
+
assert.throws(
|
|
110
|
+
() => assertRgSucceeded({ code: null, stderr: "" }),
|
|
111
|
+
/ripgrep exited with code null/,
|
|
112
|
+
);
|
|
113
|
+
});
|