@criogaid/pi-hashline-edit 0.3.0 → 0.4.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 +59 -50
- package/package.json +8 -3
- package/src/core/apply.test.ts +366 -310
- package/src/core/apply.ts +269 -202
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +45 -16
- 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/text.test.ts +3 -3
- package/src/core/text.ts +28 -9
- package/src/core/types.ts +54 -36
- package/src/index.ts +22 -16
- package/src/integration/grep-rg.test.ts +834 -219
- package/src/integration/large-text.test.ts +62 -0
- package/src/integration/replace-package.test.ts +37 -0
- package/src/pi/action-fusion.test.ts +535 -206
- package/src/pi/action-fusion.ts +469 -244
- package/src/pi/anchor-format.test.ts +30 -19
- package/src/pi/anchor-format.ts +27 -14
- package/src/pi/budgets.ts +20 -0
- package/src/pi/config.test.ts +60 -30
- package/src/pi/config.ts +45 -32
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +359 -0
- package/src/pi/edit-tool.ts +426 -262
- package/src/pi/error-text.ts +21 -0
- package/src/pi/execute.test.ts +1594 -694
- package/src/pi/failure-context.test.ts +105 -83
- package/src/pi/failure-context.ts +83 -71
- package/src/pi/failure-path.test.ts +171 -64
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +347 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +120 -85
- package/src/pi/grep-output.ts +208 -0
- package/src/pi/grep-render.ts +58 -0
- package/src/pi/grep-scope.ts +152 -0
- package/src/pi/grep-search.ts +217 -0
- package/src/pi/grep-tool.ts +191 -766
- package/src/pi/grep.test.ts +562 -150
- package/src/pi/mutation-result.test.ts +37 -0
- package/src/pi/mutation-result.ts +107 -59
- package/src/pi/mutation-runner.ts +168 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +95 -52
- package/src/pi/read-tool.ts +219 -130
- package/src/pi/registration.test.ts +274 -113
- package/src/pi/render.test.ts +218 -83
- package/src/pi/render.ts +152 -95
- package/src/pi/replace-apply.ts +109 -0
- package/src/pi/replace-tool.ts +280 -240
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +773 -389
- package/src/pi/result-safety.test.ts +779 -342
- package/src/pi/rg-line-filter.test.ts +64 -36
- package/src/pi/rg-line-filter.ts +40 -124
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +44 -82
- package/src/pi/rg-text-view.ts +106 -36
- package/src/pi/schema.ts +27 -0
- package/src/pi/state.ts +7 -7
- 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 +115 -52
- package/src/pi/write.test.ts +248 -96
|
@@ -2,13 +2,15 @@ import { test } from "node:test";
|
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import { assertRgSucceeded, resolveIgnoreCase, type RunText } from "./rg-line-filter.ts";
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
const literalMode = { ...regexMode, literal: true } as const;
|
|
5
|
+
const lineModes = { literal: false, multiline: false } as const;
|
|
7
6
|
|
|
8
|
-
test("explicit case settings
|
|
9
|
-
const run: RunText = async () => {
|
|
10
|
-
|
|
11
|
-
|
|
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);
|
|
12
14
|
});
|
|
13
15
|
|
|
14
16
|
test("standard smart-case probe distinguishes its sensor from user matches", async () => {
|
|
@@ -17,13 +19,20 @@ test("standard smart-case probe distinguishes its sensor from user matches", asy
|
|
|
17
19
|
calls.push({ args, input });
|
|
18
20
|
return { code: 0, stdout: "a\n\n", stderr: "" };
|
|
19
21
|
};
|
|
20
|
-
assert.equal(
|
|
22
|
+
assert.equal(
|
|
23
|
+
await resolveIgnoreCase("rg", ["(a)|Foo"], lineModes, undefined, undefined, insensitive),
|
|
24
|
+
true,
|
|
25
|
+
);
|
|
21
26
|
assert.deepEqual(calls[0].input, Buffer.from("a\n"));
|
|
22
27
|
assert.ok(calls[0].args.includes("--smart-case"));
|
|
23
28
|
assert.ok(calls[0].args.includes("--no-config"));
|
|
29
|
+
assert.ok(calls[0].args.includes("--no-multiline"));
|
|
24
30
|
|
|
25
31
|
const sensitive: RunText = async () => ({ code: 0, stdout: "\n", stderr: "" });
|
|
26
|
-
assert.equal(
|
|
32
|
+
assert.equal(
|
|
33
|
+
await resolveIgnoreCase("rg", ["(a)|Foo"], lineModes, undefined, undefined, sensitive),
|
|
34
|
+
false,
|
|
35
|
+
);
|
|
27
36
|
});
|
|
28
37
|
|
|
29
38
|
test("literal smart-case escapes user patterns before probing", async () => {
|
|
@@ -32,41 +41,51 @@ test("literal smart-case escapes user patterns before probing", async () => {
|
|
|
32
41
|
args = received;
|
|
33
42
|
return { code: 1, stdout: "", stderr: "" };
|
|
34
43
|
};
|
|
35
|
-
await resolveIgnoreCase(
|
|
36
|
-
|
|
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] : []));
|
|
37
53
|
assert.deepEqual(patterns, ["(\\p{Lu})", "foo\\\\S\\*"]);
|
|
38
54
|
});
|
|
39
55
|
|
|
40
|
-
test("
|
|
41
|
-
|
|
42
|
-
const run: RunText = async (_path,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return { code: 0, stdout: "ripgrep 15.0.0\nfeatures:+pcre2\nPCRE2 10.45 is available (JIT is available)\n", stderr: "" };
|
|
46
|
-
}
|
|
47
|
-
return { code: 0, stdout: "", stderr: "" };
|
|
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: "" };
|
|
48
61
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
assert.
|
|
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"));
|
|
62
75
|
});
|
|
63
76
|
|
|
64
77
|
test("smart-case probe propagates parser errors and rejects unknown output", async () => {
|
|
65
78
|
const failed: RunText = async () => ({ code: 2, stdout: "", stderr: "regex parse error" });
|
|
66
|
-
await assert.rejects(
|
|
79
|
+
await assert.rejects(
|
|
80
|
+
resolveIgnoreCase("rg", ["("], lineModes, undefined, undefined, failed),
|
|
81
|
+
/regex parse error/,
|
|
82
|
+
);
|
|
67
83
|
|
|
68
84
|
const unexpected: RunText = async () => ({ code: 0, stdout: "unexpected\n", stderr: "" });
|
|
69
|
-
await assert.rejects(
|
|
85
|
+
await assert.rejects(
|
|
86
|
+
resolveIgnoreCase("rg", ["foo"], lineModes, undefined, undefined, unexpected),
|
|
87
|
+
/Unexpected smart-case probe output/,
|
|
88
|
+
);
|
|
70
89
|
});
|
|
71
90
|
|
|
72
91
|
test("smart-case probe normalizes cancellation without fallback", async () => {
|
|
@@ -75,11 +94,20 @@ test("smart-case probe normalizes cancellation without fallback", async () => {
|
|
|
75
94
|
controller.abort();
|
|
76
95
|
return { code: 1, stdout: "", stderr: "" };
|
|
77
96
|
};
|
|
78
|
-
await assert.rejects(
|
|
97
|
+
await assert.rejects(
|
|
98
|
+
resolveIgnoreCase("rg", ["foo"], lineModes, undefined, controller.signal, run),
|
|
99
|
+
/Operation aborted/,
|
|
100
|
+
);
|
|
79
101
|
});
|
|
80
102
|
|
|
81
103
|
test("search exit status accepts no matches and preserves failure diagnostics", () => {
|
|
82
104
|
for (const code of [0, 1]) assert.doesNotThrow(() => assertRgSucceeded({ code, stderr: "" }));
|
|
83
|
-
assert.throws(
|
|
84
|
-
|
|
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
|
+
);
|
|
85
113
|
});
|
package/src/pi/rg-line-filter.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
2
|
import type { Readable } from "node:stream";
|
|
3
3
|
import { escapeRegex } from "../core/text.ts";
|
|
4
|
+
import { throwIfCancelled } from "./error-text.ts";
|
|
4
5
|
|
|
5
6
|
export const COMMON_RG_ARGS = ["--no-config", "--color=never", "--no-crlf"];
|
|
6
7
|
export const MAX_RG_RECORD_BYTES = 16 * 1024 * 1024;
|
|
8
|
+
/** Retained ripgrep stderr; diagnostics beyond this are dropped. */
|
|
9
|
+
export const MAX_RG_STDERR_BYTES = 64 * 1024;
|
|
10
|
+
/** Probe runs read only a short stdout; more means an unexpected rg mode. */
|
|
11
|
+
const MAX_RG_PROBE_OUTPUT_BYTES = 64 * 1024;
|
|
7
12
|
|
|
8
13
|
export interface SearchModes {
|
|
9
|
-
engine: "default" | "pcre2";
|
|
10
|
-
multiline: boolean;
|
|
11
14
|
literal: boolean;
|
|
12
15
|
ignoreCase: boolean;
|
|
16
|
+
multiline: boolean;
|
|
13
17
|
}
|
|
14
18
|
|
|
15
|
-
export type LinePredicate = (lines: readonly Buffer[]) => Promise<boolean[]>;
|
|
16
|
-
|
|
17
19
|
interface ExitResult {
|
|
18
20
|
code: number | null;
|
|
19
21
|
stderr: string;
|
|
@@ -32,12 +34,8 @@ export interface RgRunResult {
|
|
|
32
34
|
stopped: boolean;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
function checkAbort(signal?: AbortSignal): void {
|
|
36
|
-
if (signal?.aborted) throw new Error("Operation aborted");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
37
|
function startRg(rgPath: string, args: readonly string[], signal?: AbortSignal): RunningProcess {
|
|
40
|
-
|
|
38
|
+
throwIfCancelled(signal);
|
|
41
39
|
const env = { ...process.env };
|
|
42
40
|
delete env.RIPGREP_CONFIG_PATH;
|
|
43
41
|
const child = spawn(rgPath, [...args], {
|
|
@@ -59,7 +57,8 @@ function startRg(rgPath: string, args: readonly string[], signal?: AbortSignal):
|
|
|
59
57
|
};
|
|
60
58
|
|
|
61
59
|
child.stderr.on("data", (chunk: Buffer) => {
|
|
62
|
-
if (stderr.length <
|
|
60
|
+
if (stderr.length < MAX_RG_STDERR_BYTES)
|
|
61
|
+
stderr += chunk.toString("utf8").slice(0, MAX_RG_STDERR_BYTES - stderr.length);
|
|
63
62
|
});
|
|
64
63
|
child.on("error", (cause) => {
|
|
65
64
|
error = cause;
|
|
@@ -87,14 +86,17 @@ async function* delimitedRecords(stream: Readable, delimiter: number): AsyncGene
|
|
|
87
86
|
let pending = Buffer.alloc(0);
|
|
88
87
|
for await (const value of stream) {
|
|
89
88
|
const chunk = Buffer.isBuffer(value) ? value : Buffer.from(value);
|
|
90
|
-
|
|
89
|
+
// Fast path: no pending data — search the chunk directly.
|
|
90
|
+
let buf = pending.length ? Buffer.concat([pending, chunk]) : chunk;
|
|
91
|
+
pending = Buffer.alloc(0);
|
|
91
92
|
let at: number;
|
|
92
|
-
while ((at =
|
|
93
|
+
while ((at = buf.indexOf(delimiter)) !== -1) {
|
|
93
94
|
if (at > MAX_RG_RECORD_BYTES) throw new Error("ripgrep output record exceeds 16 MiB");
|
|
94
|
-
yield
|
|
95
|
-
|
|
95
|
+
yield buf.subarray(0, at);
|
|
96
|
+
buf = buf.subarray(at + 1);
|
|
96
97
|
}
|
|
97
|
-
if (
|
|
98
|
+
if (buf.length > MAX_RG_RECORD_BYTES) throw new Error("ripgrep output record exceeds 16 MiB");
|
|
99
|
+
pending = buf;
|
|
98
100
|
}
|
|
99
101
|
if (pending.length) yield pending;
|
|
100
102
|
}
|
|
@@ -112,15 +114,15 @@ async function runDelimited(
|
|
|
112
114
|
process.child.stdin.end(input);
|
|
113
115
|
try {
|
|
114
116
|
for await (const record of delimitedRecords(process.child.stdout, delimiter)) {
|
|
115
|
-
|
|
116
|
-
if (!await onRecord(record)) {
|
|
117
|
+
throwIfCancelled(signal);
|
|
118
|
+
if (!(await onRecord(record))) {
|
|
117
119
|
stopped = true;
|
|
118
120
|
process.kill();
|
|
119
121
|
break;
|
|
120
122
|
}
|
|
121
123
|
}
|
|
122
124
|
const result = await process.done;
|
|
123
|
-
|
|
125
|
+
throwIfCancelled(signal);
|
|
124
126
|
if (result.error) throw new Error(`Failed to run ripgrep: ${result.error.message}`);
|
|
125
127
|
return { code: result.code, stderr: result.stderr, stopped };
|
|
126
128
|
} finally {
|
|
@@ -137,7 +139,7 @@ export function runRg(
|
|
|
137
139
|
onLine: (line: string) => boolean | Promise<boolean>,
|
|
138
140
|
): Promise<RgRunResult> {
|
|
139
141
|
return runDelimited(rgPath, args, undefined, 10, signal, (record) =>
|
|
140
|
-
record.length === 0 ? true : onLine(record.toString("utf8"))
|
|
142
|
+
record.length === 0 ? true : onLine(record.toString("utf8")),
|
|
141
143
|
);
|
|
142
144
|
}
|
|
143
145
|
|
|
@@ -150,7 +152,8 @@ export function runRgPaths(
|
|
|
150
152
|
): Promise<RgRunResult> {
|
|
151
153
|
return runDelimited(rgPath, args, undefined, 0, signal, (record) => {
|
|
152
154
|
const path = record.toString("utf8");
|
|
153
|
-
if (!Buffer.from(path, "utf8").equals(record))
|
|
155
|
+
if (!Buffer.from(path, "utf8").equals(record))
|
|
156
|
+
throw new Error("Non-UTF-8 search paths are not supported");
|
|
154
157
|
return onPath(path);
|
|
155
158
|
});
|
|
156
159
|
}
|
|
@@ -181,14 +184,15 @@ export const runText: RunText = async (rgPath, args, input, signal) => {
|
|
|
181
184
|
let bytes = 0;
|
|
182
185
|
process.child.stdout.on("data", (chunk: Buffer) => {
|
|
183
186
|
bytes += chunk.length;
|
|
184
|
-
if (bytes <=
|
|
187
|
+
if (bytes <= MAX_RG_PROBE_OUTPUT_BYTES) chunks.push(chunk);
|
|
185
188
|
else process.kill();
|
|
186
189
|
});
|
|
187
190
|
process.child.stdin.end(input);
|
|
188
191
|
const result = await process.done;
|
|
189
|
-
|
|
192
|
+
throwIfCancelled(signal);
|
|
190
193
|
if (result.error) throw result.error;
|
|
191
|
-
if (bytes >
|
|
194
|
+
if (bytes > MAX_RG_PROBE_OUTPUT_BYTES)
|
|
195
|
+
throw new Error("Unexpected ripgrep probe output overflow");
|
|
192
196
|
return {
|
|
193
197
|
code: result.code,
|
|
194
198
|
stderr: result.stderr,
|
|
@@ -196,63 +200,36 @@ export const runText: RunText = async (rgPath, args, input, signal) => {
|
|
|
196
200
|
};
|
|
197
201
|
};
|
|
198
202
|
|
|
199
|
-
|
|
200
|
-
return [
|
|
201
|
-
...COMMON_RG_ARGS,
|
|
202
|
-
`--engine=${modes.engine}`,
|
|
203
|
-
modes.multiline ? "--multiline" : "--no-multiline",
|
|
204
|
-
];
|
|
205
|
-
}
|
|
203
|
+
const DEFAULT_RG_MODE_ARGS = [...COMMON_RG_ARGS, "--engine=default"];
|
|
206
204
|
|
|
207
|
-
export function matcherArgs(modes: SearchModes
|
|
205
|
+
export function matcherArgs(modes: SearchModes): string[] {
|
|
208
206
|
return [
|
|
209
|
-
...
|
|
207
|
+
...DEFAULT_RG_MODE_ARGS,
|
|
208
|
+
modes.multiline ? "--multiline" : "--no-multiline",
|
|
210
209
|
modes.ignoreCase ? "--ignore-case" : "--case-sensitive",
|
|
211
210
|
...(modes.literal ? ["--fixed-strings"] : []),
|
|
212
|
-
...(word ? ["--word-regexp"] : []),
|
|
213
211
|
];
|
|
214
212
|
}
|
|
215
213
|
|
|
216
|
-
function pcre2CaseCarrier(patterns: readonly string[]): string {
|
|
217
|
-
return ["(?x)", ...patterns.flatMap((pattern) => pattern.split("\n").map((line) => `#${line}`)), "\\x{41}", ""].join("\n");
|
|
218
|
-
}
|
|
219
|
-
|
|
220
214
|
/** Resolve rg's query-level default case flag; inline regex flags still apply normally. */
|
|
221
215
|
export async function resolveIgnoreCase(
|
|
222
216
|
rgPath: string,
|
|
223
217
|
patterns: readonly string[],
|
|
224
|
-
modes: Pick<SearchModes, "
|
|
218
|
+
modes: Pick<SearchModes, "literal" | "multiline">,
|
|
225
219
|
explicit: boolean | undefined,
|
|
226
220
|
signal?: AbortSignal,
|
|
227
221
|
run: RunText = runText,
|
|
228
222
|
): Promise<boolean> {
|
|
229
|
-
|
|
230
|
-
if (explicit !== undefined) return explicit;
|
|
223
|
+
throwIfCancelled(signal);
|
|
231
224
|
if (patterns.length === 0) throw new Error("pattern is required (got an empty array)");
|
|
225
|
+
if (explicit !== undefined) return explicit;
|
|
232
226
|
|
|
233
|
-
|
|
234
|
-
const version = await run(rgPath, ["--version"], Buffer.alloc(0), signal);
|
|
235
|
-
if (version.code !== 0 || !/^ripgrep 15\.0\.0\b/m.test(version.stdout) || !/^features:\+pcre2$/m.test(version.stdout) || !/^PCRE2 10\.45 is available/m.test(version.stdout)) {
|
|
236
|
-
throw new Error("PCRE2 smart-case is not validated for this bundled ripgrep build; set ignoreCase explicitly");
|
|
237
|
-
}
|
|
238
|
-
const result = await run(
|
|
239
|
-
rgPath,
|
|
240
|
-
[...modeArgs(modes), "--smart-case", "--quiet", "-e", pcre2CaseCarrier(patterns), "--", "-"],
|
|
241
|
-
Buffer.from("a\n"),
|
|
242
|
-
signal,
|
|
243
|
-
);
|
|
244
|
-
checkAbort(signal);
|
|
245
|
-
if (result.code !== 0 && result.code !== 1) {
|
|
246
|
-
throw new Error(result.stderr.trim() || `ripgrep exited with code ${result.code}`);
|
|
247
|
-
}
|
|
248
|
-
return result.code === 0;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const sources = patterns.map((pattern) => modes.literal ? escapeRegex(pattern) : pattern);
|
|
227
|
+
const sources = patterns.map((pattern) => (modes.literal ? escapeRegex(pattern) : pattern));
|
|
252
228
|
const result = await run(
|
|
253
229
|
rgPath,
|
|
254
230
|
[
|
|
255
|
-
...
|
|
231
|
+
...DEFAULT_RG_MODE_ARGS,
|
|
232
|
+
modes.multiline ? "--multiline" : "--no-multiline",
|
|
256
233
|
"--smart-case",
|
|
257
234
|
"--encoding=none",
|
|
258
235
|
"--no-heading",
|
|
@@ -270,34 +247,14 @@ export async function resolveIgnoreCase(
|
|
|
270
247
|
Buffer.from("a\n"),
|
|
271
248
|
signal,
|
|
272
249
|
);
|
|
273
|
-
|
|
250
|
+
throwIfCancelled(signal);
|
|
274
251
|
assertRgSucceeded(result);
|
|
275
252
|
const lines = result.stdout.replace(/\r\n/g, "\n").split("\n");
|
|
276
|
-
if (lines.some((line) => line !== "" && line !== "a"))
|
|
253
|
+
if (lines.some((line) => line !== "" && line !== "a"))
|
|
254
|
+
throw new Error("Unexpected smart-case probe output");
|
|
277
255
|
return lines.includes("a");
|
|
278
256
|
}
|
|
279
257
|
|
|
280
|
-
export async function validatePatterns(
|
|
281
|
-
rgPath: string,
|
|
282
|
-
patterns: readonly string[],
|
|
283
|
-
modes: SearchModes,
|
|
284
|
-
word: boolean,
|
|
285
|
-
signal?: AbortSignal,
|
|
286
|
-
run: RunText = runText,
|
|
287
|
-
): Promise<void> {
|
|
288
|
-
if (modes.literal) return;
|
|
289
|
-
for (const pattern of patterns) {
|
|
290
|
-
const result = await run(
|
|
291
|
-
rgPath,
|
|
292
|
-
[...matcherArgs(modes, word), "--quiet", "-e", pattern, "--", "-"],
|
|
293
|
-
Buffer.alloc(0),
|
|
294
|
-
signal,
|
|
295
|
-
);
|
|
296
|
-
checkAbort(signal);
|
|
297
|
-
assertRgSucceeded(result);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
258
|
interface RgString {
|
|
302
259
|
text?: string;
|
|
303
260
|
bytes?: string;
|
|
@@ -313,44 +270,3 @@ export function rgText(value: RgString): string {
|
|
|
313
270
|
if (typeof value.text === "string") return value.text;
|
|
314
271
|
throw new Error("Non-UTF-8 search paths are not supported");
|
|
315
272
|
}
|
|
316
|
-
|
|
317
|
-
export function createLinePredicate(
|
|
318
|
-
rgPath: string,
|
|
319
|
-
patterns: readonly string[],
|
|
320
|
-
modes: SearchModes,
|
|
321
|
-
word: boolean,
|
|
322
|
-
signal?: AbortSignal,
|
|
323
|
-
): LinePredicate {
|
|
324
|
-
const args = [
|
|
325
|
-
...matcherArgs(modes, word),
|
|
326
|
-
"--json",
|
|
327
|
-
"--line-number",
|
|
328
|
-
"--passthru",
|
|
329
|
-
"--text",
|
|
330
|
-
"--encoding=none",
|
|
331
|
-
...patterns.flatMap((pattern) => ["-e", pattern]),
|
|
332
|
-
"--",
|
|
333
|
-
"-",
|
|
334
|
-
];
|
|
335
|
-
return async (lines) => {
|
|
336
|
-
checkAbort(signal);
|
|
337
|
-
if (lines.length === 0) return [];
|
|
338
|
-
const records = lines.map((line) => {
|
|
339
|
-
const firstLf = line.indexOf(10);
|
|
340
|
-
if (firstLf >= 0 && firstLf !== line.length - 1) throw new Error("Expected exactly one physical candidate line");
|
|
341
|
-
return firstLf >= 0 ? line : Buffer.concat([line, Buffer.from("\n")]);
|
|
342
|
-
});
|
|
343
|
-
const matches: boolean[] = [];
|
|
344
|
-
const result = await runDelimited(rgPath, args, Buffer.concat(records), 10, signal, (record) => {
|
|
345
|
-
if (record.length === 0) return true;
|
|
346
|
-
const event = JSON.parse(record.toString("utf8"));
|
|
347
|
-
if (event.type !== "match" && event.type !== "context") return true;
|
|
348
|
-
if (event.data.line_number !== matches.length + 1) throw new Error("rg predicate line-number protocol mismatch");
|
|
349
|
-
matches.push(event.type === "match");
|
|
350
|
-
return true;
|
|
351
|
-
});
|
|
352
|
-
assertRgSucceeded(result);
|
|
353
|
-
if (matches.length !== lines.length) throw new Error("rg predicate ended before all responses");
|
|
354
|
-
return matches;
|
|
355
|
-
};
|
|
356
|
-
}
|
|
@@ -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
|
});
|