@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
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { parseHashline } from "./render.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convert the anchored grep output (grouped, `LINE#HASH│`) into a human-readable
|
|
6
|
+
* form for the TUI: drop the hash, keep file headers and line numbers. Within each
|
|
7
|
+
* file group, the common leading whitespace shared by all matched lines is folded
|
|
8
|
+
* into a single marker (›) so deep, repeated indentation doesn't eat display width;
|
|
9
|
+
* each line's indentation relative to that common base is preserved. The model still
|
|
10
|
+
* receives the anchored `content` text verbatim — this only affects what the user sees.
|
|
11
|
+
*/
|
|
12
|
+
function countLeading(s: string): number {
|
|
13
|
+
const m = s.match(/^[ \t]*/);
|
|
14
|
+
return m ? m[0].length : 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function toDisplayLines(raw: string, theme: Theme): string[] {
|
|
18
|
+
const out: string[] = [];
|
|
19
|
+
const lines = raw.split("\n");
|
|
20
|
+
const lineNoWidth = lines.reduce(
|
|
21
|
+
(width, line) => Math.max(width, parseHashline(line)?.lineNo.length ?? 0),
|
|
22
|
+
0,
|
|
23
|
+
);
|
|
24
|
+
let i = 0;
|
|
25
|
+
while (i < lines.length) {
|
|
26
|
+
const line = lines[i];
|
|
27
|
+
const h = line.match(/^(.+?) · (\d+ match(?:es)?)$/);
|
|
28
|
+
if (h) {
|
|
29
|
+
out.push(theme.fg("success", h[1]) + theme.fg("dim", ` · ${h[2]}`));
|
|
30
|
+
// collect the anchor lines in this file group
|
|
31
|
+
const group: { lineNo: string; content: string }[] = [];
|
|
32
|
+
let j = i + 1;
|
|
33
|
+
while (j < lines.length) {
|
|
34
|
+
const a = parseHashline(lines[j]);
|
|
35
|
+
if (!a) break;
|
|
36
|
+
group.push({ lineNo: a.lineNo, content: a.content });
|
|
37
|
+
j++;
|
|
38
|
+
}
|
|
39
|
+
// common base = min leading whitespace across the group; fold it into a marker
|
|
40
|
+
const base = group.length ? Math.min(...group.map((g) => countLeading(g.content))) : 0;
|
|
41
|
+
const marker = base > 0 ? theme.fg("dim", "›") + " " : "";
|
|
42
|
+
for (const g of group) {
|
|
43
|
+
const body = g.content.slice(base);
|
|
44
|
+
out.push(
|
|
45
|
+
theme.fg("dim", ` ${g.lineNo.padStart(lineNoWidth)}: `) +
|
|
46
|
+
marker +
|
|
47
|
+
theme.fg("toolOutput", body),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
i = j;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (line.startsWith("[")) out.push(theme.fg("warning", line));
|
|
54
|
+
else out.push(theme.fg("toolOutput", line));
|
|
55
|
+
i++;
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import { canonicalPath } from "./path.ts";
|
|
4
|
+
import { COMMON_RG_ARGS } from "./rg-line-filter.ts";
|
|
5
|
+
import {
|
|
6
|
+
recordSearchDiagnostics,
|
|
7
|
+
scopeArgs,
|
|
8
|
+
type GrepBackend,
|
|
9
|
+
type SearchScope,
|
|
10
|
+
} from "./grep-search.ts";
|
|
11
|
+
import { throwIfCancelled } from "./error-text.ts";
|
|
12
|
+
|
|
13
|
+
export const REGEX_SYNTAX = /[.*+?^${}()|[\]\\]/;
|
|
14
|
+
const REGEX_PARSE_ERROR = /^(?:rg: )?regex parse error:/m;
|
|
15
|
+
const LITERAL_FALLBACK_NOTICE = "Invalid regex; searched the pattern as literal text";
|
|
16
|
+
/**
|
|
17
|
+
* Lookaround and backreferences: valid in JavaScript/PCRE, rejected by ripgrep's default engine.
|
|
18
|
+
* The construct must be unescaped: an even run of backslashes (escaped backslashes) may precede it.
|
|
19
|
+
*/
|
|
20
|
+
const NON_RUST_REGEX_SYNTAX = /(?:^|[^\\])(?:\\\\)*(?:\(\?<?[=!]|\\[1-9]|\\k<)/;
|
|
21
|
+
|
|
22
|
+
/** Explain a parse failure caused by another regex dialect's syntax, if the query contains any. */
|
|
23
|
+
function regexDialectHint(patterns: readonly string[]): string | undefined {
|
|
24
|
+
return patterns.some((pattern) => NON_RUST_REGEX_SYNTAX.test(pattern))
|
|
25
|
+
? "ripgrep's Rust regex has no lookaround or backreferences; rewrite the pattern, or use replace for a JavaScript regex within one file"
|
|
26
|
+
: undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Notice for an invalid single pattern that was searched literally. */
|
|
30
|
+
export function literalFallbackNotice(patterns: readonly string[]): string {
|
|
31
|
+
const hint = regexDialectHint(patterns);
|
|
32
|
+
return hint ? `${LITERAL_FALLBACK_NOTICE}; ${hint}` : LITERAL_FALLBACK_NOTICE;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function withDialectHint(message: string, patterns: readonly string[]): string {
|
|
36
|
+
const hint = regexDialectHint(patterns);
|
|
37
|
+
return hint ? `${message}\n${hint}.` : message;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function resolveLiteralMode(
|
|
41
|
+
patterns: readonly string[],
|
|
42
|
+
explicit: boolean | undefined,
|
|
43
|
+
multiline: boolean,
|
|
44
|
+
rgPath: string,
|
|
45
|
+
backend: GrepBackend,
|
|
46
|
+
signal: AbortSignal | undefined,
|
|
47
|
+
allowFallback: boolean,
|
|
48
|
+
): Promise<boolean> {
|
|
49
|
+
if (explicit === true) return true;
|
|
50
|
+
if (explicit === undefined && !patterns.some((pattern) => REGEX_SYNTAX.test(pattern)))
|
|
51
|
+
return true;
|
|
52
|
+
|
|
53
|
+
const result = await backend.runRg(
|
|
54
|
+
rgPath,
|
|
55
|
+
[
|
|
56
|
+
...COMMON_RG_ARGS,
|
|
57
|
+
"--engine=default",
|
|
58
|
+
multiline ? "--multiline" : "--no-multiline",
|
|
59
|
+
"--quiet",
|
|
60
|
+
...patterns.flatMap((pattern) => ["-e", pattern]),
|
|
61
|
+
"--",
|
|
62
|
+
"-",
|
|
63
|
+
],
|
|
64
|
+
signal,
|
|
65
|
+
() => true,
|
|
66
|
+
);
|
|
67
|
+
throwIfCancelled(signal);
|
|
68
|
+
if (result.code === 0 || result.code === 1) return false;
|
|
69
|
+
if (result.code === 2 && REGEX_PARSE_ERROR.test(result.stderr)) {
|
|
70
|
+
if (explicit === false) throw new Error(withDialectHint(result.stderr.trim(), patterns));
|
|
71
|
+
if (!allowFallback)
|
|
72
|
+
throw new Error(
|
|
73
|
+
withDialectHint(
|
|
74
|
+
`Invalid regex in compound query; automatic literal fallback is disabled. Fix the regex or set literal:true for all patterns.\n${result.stderr.trim()}`,
|
|
75
|
+
patterns,
|
|
76
|
+
),
|
|
77
|
+
);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
throw new Error(result.stderr.trim() || `ripgrep exited with code ${result.code}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Normalize a `string | string[]` param to an array (`undefined` → `[]`). */
|
|
84
|
+
export function toArray(v: string | string[] | undefined): string[] {
|
|
85
|
+
if (v === undefined) return [];
|
|
86
|
+
return Array.isArray(v) ? v : [v];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface SearchPathInfo {
|
|
90
|
+
path: string;
|
|
91
|
+
isFile: boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function resolveSearchPaths(cwd: string, path: string | string[] | undefined) {
|
|
95
|
+
const searchPaths = (() => {
|
|
96
|
+
const values = toArray(path);
|
|
97
|
+
return (values.length ? values : ["."]).map((value) => canonicalPath(cwd, value));
|
|
98
|
+
})();
|
|
99
|
+
const pathInfo: SearchPathInfo[] = [];
|
|
100
|
+
for (const searchPath of searchPaths) {
|
|
101
|
+
try {
|
|
102
|
+
pathInfo.push({ path: searchPath, isFile: (await stat(searchPath)).isFile() });
|
|
103
|
+
} catch (error) {
|
|
104
|
+
const missing =
|
|
105
|
+
typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
106
|
+
const hint =
|
|
107
|
+
missing && /[*?]/.test(searchPath)
|
|
108
|
+
? " Use an existing directory as path and a filename wildcard as glob."
|
|
109
|
+
: "";
|
|
110
|
+
throw new Error(`Path not found: ${searchPath}${hint}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return { searchPaths, pathInfo };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function fileKey(path: string): string {
|
|
117
|
+
const absolute = resolve(path);
|
|
118
|
+
return process.platform === "win32" ? absolute.toLowerCase() : absolute;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export async function filterExplicitFilesByGlob(
|
|
122
|
+
backend: GrepBackend,
|
|
123
|
+
rgPath: string,
|
|
124
|
+
scope: SearchScope,
|
|
125
|
+
paths: readonly SearchPathInfo[],
|
|
126
|
+
signal: AbortSignal | undefined,
|
|
127
|
+
warnings: string[],
|
|
128
|
+
): Promise<string[]> {
|
|
129
|
+
const explicitFiles = paths.filter(({ isFile }) => isFile);
|
|
130
|
+
if (scope.globs.length === 0 || explicitFiles.length === 0) return paths.map(({ path }) => path);
|
|
131
|
+
|
|
132
|
+
const parents = [...new Set(explicitFiles.map(({ path }) => dirname(path)))];
|
|
133
|
+
const allowed = new Set<string>();
|
|
134
|
+
// Direct file arguments bypass ignore and symlink traversal; let only the ordered globs decide admission.
|
|
135
|
+
const args = [
|
|
136
|
+
...COMMON_RG_ARGS,
|
|
137
|
+
...scopeArgs({ ...scope, noIgnore: true, follow: true }),
|
|
138
|
+
"--files",
|
|
139
|
+
"--null",
|
|
140
|
+
"--max-depth=1",
|
|
141
|
+
"--",
|
|
142
|
+
...parents,
|
|
143
|
+
];
|
|
144
|
+
const listed = await backend.runRgPaths(rgPath, args, signal, async (path) => {
|
|
145
|
+
allowed.add(fileKey(path));
|
|
146
|
+
return true;
|
|
147
|
+
});
|
|
148
|
+
recordSearchDiagnostics(listed, warnings);
|
|
149
|
+
return paths
|
|
150
|
+
.filter(({ path, isFile }) => !isFile || allowed.has(fileKey(path)))
|
|
151
|
+
.map(({ path }) => path);
|
|
152
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { UNSUPPORTED_TEXT_NUL } from "../core/text.ts";
|
|
4
|
+
import { scanTextFile } from "./text-stream.ts";
|
|
5
|
+
import {
|
|
6
|
+
type RgRunResult,
|
|
7
|
+
matcherArgs,
|
|
8
|
+
resolveIgnoreCase,
|
|
9
|
+
rgBytes,
|
|
10
|
+
rgText,
|
|
11
|
+
runRg,
|
|
12
|
+
runRgPaths,
|
|
13
|
+
type SearchModes,
|
|
14
|
+
} from "./rg-line-filter.ts";
|
|
15
|
+
import { submatchesToLineRanges } from "./rg-line-ranges.ts";
|
|
16
|
+
import { throwIfCancelled } from "./error-text.ts";
|
|
17
|
+
|
|
18
|
+
/** Surface incomplete search diagnostics without discarding confirmed matches. */
|
|
19
|
+
export function recordSearchDiagnostics(result: RgRunResult, warnings?: string[]): void {
|
|
20
|
+
const message =
|
|
21
|
+
result.stderr.trim() ||
|
|
22
|
+
(!result.stopped && result.code !== 0 && result.code !== 1
|
|
23
|
+
? `ripgrep exited with code ${result.code}`
|
|
24
|
+
: "");
|
|
25
|
+
if (!message) return;
|
|
26
|
+
if (!warnings) throw new Error(message);
|
|
27
|
+
warnings.push(message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RgMatch {
|
|
31
|
+
filePath: string;
|
|
32
|
+
lineNumber: number;
|
|
33
|
+
column?: number;
|
|
34
|
+
matchedText?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface RgJsonEvent {
|
|
38
|
+
type: string;
|
|
39
|
+
data?: {
|
|
40
|
+
path?: Parameters<typeof rgText>[0];
|
|
41
|
+
lines?: Parameters<typeof rgBytes>[0];
|
|
42
|
+
line_number?: number;
|
|
43
|
+
submatches?: { start: number; end: number }[];
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @internal — injectable process boundary for deterministic tests. */
|
|
48
|
+
export interface GrepBackend {
|
|
49
|
+
runRg: typeof runRg;
|
|
50
|
+
runRgPaths: typeof runRgPaths;
|
|
51
|
+
resolveIgnoreCase: typeof resolveIgnoreCase;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SearchScope {
|
|
55
|
+
globs: readonly string[];
|
|
56
|
+
noIgnore: boolean;
|
|
57
|
+
follow: boolean;
|
|
58
|
+
searchPaths: readonly string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function scopeArgs(scope: SearchScope): string[] {
|
|
62
|
+
return [
|
|
63
|
+
"--hidden",
|
|
64
|
+
...(scope.noIgnore ? ["--no-ignore"] : []),
|
|
65
|
+
...(scope.follow ? ["--follow"] : []),
|
|
66
|
+
...scope.globs.flatMap((glob) => ["--glob", glob]),
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface SearchMatchesOptions {
|
|
71
|
+
backend: GrepBackend;
|
|
72
|
+
rgPath: string;
|
|
73
|
+
scope: SearchScope;
|
|
74
|
+
patterns: readonly string[];
|
|
75
|
+
modes: SearchModes;
|
|
76
|
+
limit: number;
|
|
77
|
+
outputMode: "content" | "files" | "count";
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
warnings: string[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function scanFileRevision(filePath: string, signal?: AbortSignal) {
|
|
83
|
+
const hash = createHash("sha256");
|
|
84
|
+
const stats = await scanTextFile(filePath, undefined, signal, (bytes) => hash.update(bytes));
|
|
85
|
+
return { ...stats, revision: hash.digest("hex") };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function fileReadWarning(
|
|
89
|
+
filePath: string,
|
|
90
|
+
error: unknown,
|
|
91
|
+
signal?: AbortSignal,
|
|
92
|
+
): string | undefined {
|
|
93
|
+
if (signal?.aborted || !(error instanceof Error) || !("code" in error)) return undefined;
|
|
94
|
+
return `Could not read ${filePath}: ${error.message}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function searchMatches(options: SearchMatchesOptions) {
|
|
98
|
+
const { backend, rgPath, scope, patterns, modes, limit, outputMode, signal, warnings } = options;
|
|
99
|
+
const raw: RgMatch[] = [];
|
|
100
|
+
const revisions = new Map<string, string>();
|
|
101
|
+
const lineCounts = new Map<string, number>();
|
|
102
|
+
let matchLimitReached = false;
|
|
103
|
+
const unreadableFiles = new Set<string>();
|
|
104
|
+
const seenMatches = new Set<string>();
|
|
105
|
+
// rg reports non-overlapping spans, so overlapping multiline OR patterns need separate scans.
|
|
106
|
+
const patternGroups = modes.multiline ? patterns.map((pattern) => [pattern]) : [patterns];
|
|
107
|
+
for (const group of patternGroups) {
|
|
108
|
+
const args = [
|
|
109
|
+
...matcherArgs(modes),
|
|
110
|
+
"--json",
|
|
111
|
+
"--line-number",
|
|
112
|
+
...scopeArgs(scope),
|
|
113
|
+
...group.flatMap((pattern) => ["-e", pattern]),
|
|
114
|
+
"--",
|
|
115
|
+
...scope.searchPaths,
|
|
116
|
+
];
|
|
117
|
+
const run = await backend.runRg(rgPath, args, signal, async (line) => {
|
|
118
|
+
if (raw.length >= limit) return false;
|
|
119
|
+
let event: RgJsonEvent;
|
|
120
|
+
try {
|
|
121
|
+
event = JSON.parse(line);
|
|
122
|
+
} catch {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
if (event.type !== "match") return true;
|
|
126
|
+
const data = event.data;
|
|
127
|
+
if (
|
|
128
|
+
!data?.path ||
|
|
129
|
+
!data.lines ||
|
|
130
|
+
typeof data.line_number !== "number" ||
|
|
131
|
+
!Number.isSafeInteger(data.line_number)
|
|
132
|
+
)
|
|
133
|
+
return true;
|
|
134
|
+
const filePath = resolve(rgText(data.path));
|
|
135
|
+
const startLine = data.line_number;
|
|
136
|
+
const bytes = rgBytes(data.lines);
|
|
137
|
+
const addMatch = async (match: RgMatch) => {
|
|
138
|
+
if (unreadableFiles.has(filePath)) return true;
|
|
139
|
+
const matchKey = `${filePath}\0${match.lineNumber}`;
|
|
140
|
+
if (seenMatches.has(matchKey)) return true;
|
|
141
|
+
seenMatches.add(matchKey);
|
|
142
|
+
if (outputMode === "content" && !revisions.has(filePath)) {
|
|
143
|
+
try {
|
|
144
|
+
const stats = await scanFileRevision(filePath, signal);
|
|
145
|
+
if (stats.hasNul) throw new Error(UNSUPPORTED_TEXT_NUL);
|
|
146
|
+
revisions.set(filePath, stats.revision);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const warning = fileReadWarning(filePath, error, signal);
|
|
149
|
+
if (!warning) throw error;
|
|
150
|
+
warnings.push(warning);
|
|
151
|
+
unreadableFiles.add(filePath);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
raw.push(match);
|
|
156
|
+
if (raw.length >= limit) {
|
|
157
|
+
matchLimitReached = true;
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
return true;
|
|
161
|
+
};
|
|
162
|
+
if (!modes.multiline) {
|
|
163
|
+
return addMatch({
|
|
164
|
+
filePath,
|
|
165
|
+
lineNumber: startLine,
|
|
166
|
+
column: bytes.subarray(0, data.submatches?.[0]?.start ?? 0).toString("utf8").length,
|
|
167
|
+
matchedText: bytes.toString("utf8").replace(/\n$/, ""),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (unreadableFiles.has(filePath)) return true;
|
|
171
|
+
if (!Array.isArray(data.submatches)) throw new Error("Invalid rg multiline match event");
|
|
172
|
+
let lineCount = lineCounts.get(filePath);
|
|
173
|
+
if (lineCount === undefined) {
|
|
174
|
+
try {
|
|
175
|
+
const stats =
|
|
176
|
+
outputMode === "content"
|
|
177
|
+
? await scanFileRevision(filePath, signal)
|
|
178
|
+
: await scanTextFile(filePath, undefined, signal);
|
|
179
|
+
if (stats.hasNul) throw new Error(UNSUPPORTED_TEXT_NUL);
|
|
180
|
+
lineCount = stats.totalLines;
|
|
181
|
+
lineCounts.set(filePath, lineCount);
|
|
182
|
+
if ("revision" in stats && typeof stats.revision === "string") {
|
|
183
|
+
revisions.set(filePath, stats.revision);
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
const warning = fileReadWarning(filePath, error, signal);
|
|
187
|
+
if (!warning) throw error;
|
|
188
|
+
warnings.push(warning);
|
|
189
|
+
unreadableFiles.add(filePath);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const columns = new Map<number, number>();
|
|
194
|
+
const submatches = data.submatches.length ? data.submatches : [{ start: 0, end: 0 }];
|
|
195
|
+
const ranges = submatchesToLineRanges(bytes, startLine, submatches, lineCount, columns);
|
|
196
|
+
const texts = bytes.toString("utf8").replace(/\n$/, "").split("\n");
|
|
197
|
+
for (const [start, end] of ranges) {
|
|
198
|
+
for (let lineNumber = start; lineNumber < end; lineNumber++) {
|
|
199
|
+
if (
|
|
200
|
+
!(await addMatch({
|
|
201
|
+
filePath,
|
|
202
|
+
lineNumber,
|
|
203
|
+
column: columns.get(lineNumber),
|
|
204
|
+
matchedText: texts[lineNumber - startLine],
|
|
205
|
+
}))
|
|
206
|
+
)
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
});
|
|
212
|
+
throwIfCancelled(signal);
|
|
213
|
+
recordSearchDiagnostics(run, warnings);
|
|
214
|
+
if (matchLimitReached) break;
|
|
215
|
+
}
|
|
216
|
+
return { raw, matchLimitReached, revisions };
|
|
217
|
+
}
|