@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
package/src/pi/grep-output.ts
CHANGED
|
@@ -28,6 +28,12 @@ function formatFileHeader(path: string, matches: number): string {
|
|
|
28
28
|
}
|
|
29
29
|
const FILE_HEADER = /^(.+?) · (\d+ match(?:es)?)$/s;
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* The row between two groups of rows that are not adjacent lines of one file, as ripgrep prints
|
|
33
|
+
* it. Only a search with context rows has groups; grep-render reads this row back for the TUI.
|
|
34
|
+
*/
|
|
35
|
+
export const CONTEXT_GAP = "--";
|
|
36
|
+
|
|
31
37
|
export function parseFileHeader(line: string): { path: string; summary: string } | undefined {
|
|
32
38
|
const match = FILE_HEADER.exec(line);
|
|
33
39
|
return match ? { path: match[1], summary: match[2] } : undefined;
|
|
@@ -80,7 +86,7 @@ function previewLine(text: string, column = 0, maxUnits: number): Preview {
|
|
|
80
86
|
if (text.length <= maxUnits) return { text };
|
|
81
87
|
const lead = Math.min(GREP_PREVIEW_LEAD, Math.floor(maxUnits / 5));
|
|
82
88
|
let start = Math.max(0, Math.min(column - lead, text.length - maxUnits));
|
|
83
|
-
if (start > 0 && LOW_SURROGATE.test(text[start])) start
|
|
89
|
+
if (start > 0 && LOW_SURROGATE.test(text[start])) start--;
|
|
84
90
|
let end = Math.min(text.length, start + maxUnits);
|
|
85
91
|
if (end < text.length && LOW_SURROGATE.test(text[end])) end--;
|
|
86
92
|
if (end === start) return { text: "…", kept: 0 };
|
|
@@ -130,232 +136,174 @@ interface FormatMatchesOptions {
|
|
|
130
136
|
readonly outputMode: GrepOutputMode;
|
|
131
137
|
readonly context: number;
|
|
132
138
|
readonly anchors: AnchorFormatter;
|
|
133
|
-
readonly signal
|
|
139
|
+
readonly signal: AbortSignal;
|
|
134
140
|
readonly budget: OutputBudgets;
|
|
135
141
|
}
|
|
136
142
|
|
|
137
|
-
interface
|
|
143
|
+
interface PreviewMinimum {
|
|
144
|
+
readonly index: number;
|
|
145
|
+
readonly units: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface OutputBlock {
|
|
138
149
|
readonly lines: readonly string[];
|
|
139
|
-
readonly
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
150
|
+
readonly matches: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Row indices count from the header at 0. A gap and its following row fit together. */
|
|
154
|
+
interface ContentBlock extends OutputBlock {
|
|
155
|
+
readonly invalidPath?: string;
|
|
156
|
+
/** Rows that establish a narrower displayed preview. */
|
|
157
|
+
readonly previewMinimums: readonly PreviewMinimum[];
|
|
158
|
+
/** Indices of matching rows in the displayed prefix. */
|
|
159
|
+
readonly hitIndices: readonly number[];
|
|
160
|
+
/** The hits-only prefix, shared with `lines` when context is zero. */
|
|
161
|
+
readonly withoutContext: readonly string[];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function rowBytes(row: string, separator: string): number {
|
|
165
|
+
return Buffer.byteLength(row) + separator.length;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Keep the first overflowing row so the ordered fill still sees where this block ends. */
|
|
169
|
+
function filePrefix(header: string, maxBytes: number) {
|
|
170
|
+
const lines = [header];
|
|
171
|
+
let remaining = maxBytes - rowBytes(header, "");
|
|
172
|
+
return {
|
|
173
|
+
lines,
|
|
174
|
+
get open() {
|
|
175
|
+
return remaining >= 0;
|
|
176
|
+
},
|
|
177
|
+
append(row: string) {
|
|
178
|
+
remaining -= rowBytes(row, "\n");
|
|
179
|
+
lines.push(row);
|
|
180
|
+
},
|
|
181
|
+
};
|
|
146
182
|
}
|
|
147
183
|
|
|
148
184
|
/**
|
|
149
|
-
*
|
|
150
|
-
* first row
|
|
185
|
+
* Each prefix fits at most the full call budget. Earlier blocks can only reduce that space.
|
|
186
|
+
* Narrow each prefix's first row to the space remaining after its header.
|
|
151
187
|
*/
|
|
152
188
|
function contentBlock(
|
|
153
189
|
{ filePath, rows, snapshot }: GrepFileMatch,
|
|
154
190
|
{ cwd, anchors, budget, context }: FormatMatchesOptions,
|
|
155
|
-
): LineCollector<
|
|
191
|
+
): LineCollector<ContentBlock> {
|
|
156
192
|
const columns = new Map(rows.map((match) => [match.lineNumber, match.column]));
|
|
157
193
|
const displayPath = serializePath(cwd, filePath);
|
|
158
194
|
const header = formatFileHeader(displayPath, rows.length);
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
const
|
|
163
|
-
|
|
195
|
+
const content = filePrefix(header, budget.grepBytes);
|
|
196
|
+
const solo = context > 0 ? filePrefix(header, budget.grepBytes) : undefined;
|
|
197
|
+
const firstRowBytes = budget.grepBytes - rowBytes(header, "\n");
|
|
198
|
+
const previewMinimums: PreviewMinimum[] = [];
|
|
199
|
+
const hitIndices: number[] = [];
|
|
200
|
+
let previousLine = 0;
|
|
164
201
|
return {
|
|
202
|
+
select: (number) => content.open || (solo?.open === true && columns.has(number)),
|
|
165
203
|
onLine(number, text) {
|
|
166
204
|
// Rows make CR visible one unit for one, so preview columns stay source columns.
|
|
167
205
|
const column = columns.get(number);
|
|
206
|
+
const gap = context > 0 && previousLine !== 0 && number > previousLine + 1;
|
|
207
|
+
previousLine = number;
|
|
168
208
|
const row = (display: string) =>
|
|
169
209
|
snapshot.validUtf8 ? anchors.row(number, text, display) : plainRow(number, display);
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
210
|
+
const regular = previewLine(text, column, budget.grepPreviewUnits);
|
|
211
|
+
const firstContent = content.lines.length === 1;
|
|
212
|
+
const firstSolo = column !== undefined && solo?.lines.length === 1;
|
|
213
|
+
let first = regular;
|
|
214
|
+
if (firstContent || firstSolo) {
|
|
174
215
|
const prefixBytes = Buffer.byteLength(row(""));
|
|
175
|
-
const
|
|
176
|
-
prefixBytes + Buffer.byteLength(displayCarriageReturns(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
216
|
+
const fits = ({ text }: Preview) =>
|
|
217
|
+
prefixBytes + Buffer.byteLength(displayCarriageReturns(text)) <= firstRowBytes;
|
|
218
|
+
if (!fits(regular)) first = narrowPreview(text, column, budget.grepPreviewUnits, fits);
|
|
219
|
+
}
|
|
220
|
+
const preview = firstContent ? first : regular;
|
|
221
|
+
const shown = row(preview.text);
|
|
222
|
+
const gapPrefix = gap ? `${CONTEXT_GAP}\n` : "";
|
|
223
|
+
const soloPreview = firstSolo ? first : regular;
|
|
224
|
+
if (solo?.open && column !== undefined) {
|
|
225
|
+
solo.append(soloPreview === preview ? shown : row(soloPreview.text));
|
|
185
226
|
}
|
|
186
|
-
if (
|
|
227
|
+
if (!content.open) return;
|
|
228
|
+
if (solo && column !== undefined) hitIndices.push(content.lines.length);
|
|
187
229
|
if (
|
|
188
230
|
preview.kept !== undefined &&
|
|
189
231
|
preview.kept < (previewMinimums.at(-1)?.units ?? Infinity)
|
|
190
232
|
) {
|
|
191
|
-
previewMinimums.push({ index: lines.length, units: preview.kept });
|
|
233
|
+
previewMinimums.push({ index: content.lines.length, units: preview.kept });
|
|
192
234
|
}
|
|
193
|
-
|
|
235
|
+
content.append(gapPrefix + shown);
|
|
194
236
|
},
|
|
195
237
|
finish: () => ({
|
|
196
|
-
lines,
|
|
197
|
-
|
|
238
|
+
lines: content.lines,
|
|
239
|
+
matches: rows.length,
|
|
240
|
+
invalidPath: snapshot.validUtf8 ? undefined : displayPath,
|
|
198
241
|
previewMinimums,
|
|
199
242
|
hitIndices,
|
|
200
|
-
|
|
243
|
+
withoutContext: solo?.lines ?? content.lines,
|
|
201
244
|
}),
|
|
202
245
|
};
|
|
203
246
|
}
|
|
204
247
|
|
|
205
|
-
async function formatMatches(options: FormatMatchesOptions)
|
|
248
|
+
async function formatMatches(options: FormatMatchesOptions): Promise<
|
|
249
|
+
| {
|
|
250
|
+
readonly mode: "content";
|
|
251
|
+
readonly blocks: readonly ContentBlock[];
|
|
252
|
+
readonly warnings: readonly SearchWarning[];
|
|
253
|
+
}
|
|
254
|
+
| {
|
|
255
|
+
readonly mode: Exclude<GrepOutputMode, "content">;
|
|
256
|
+
readonly blocks: readonly OutputBlock[];
|
|
257
|
+
readonly warnings: readonly [];
|
|
258
|
+
}
|
|
259
|
+
> {
|
|
206
260
|
const { cwd, files, outputMode, context, signal } = options;
|
|
207
|
-
const formatPath = (filePath: string): string => serializePath(cwd, filePath);
|
|
208
|
-
const blocks: FormattedBlock[] = [];
|
|
209
|
-
/** Files left out after their second read, in result order. */
|
|
210
|
-
const warnings: SearchWarning[] = [];
|
|
211
261
|
// Paths and counts state the searched snapshot as is; content rows come from a second read.
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
if ("warning" in result) {
|
|
220
|
-
warnings.push(result.warning);
|
|
221
|
-
fileCount--;
|
|
222
|
-
matches -= files[index].rows.length;
|
|
223
|
-
} else {
|
|
224
|
-
blocks.push(result.value);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
262
|
+
if (outputMode !== "content") {
|
|
263
|
+
const blocks = files.map(({ filePath, rows }) => {
|
|
264
|
+
const path = serializePath(cwd, filePath);
|
|
265
|
+
const matches = rows.length;
|
|
266
|
+
return { lines: [outputMode === "files" ? path : `${path}: ${matches}`], matches };
|
|
267
|
+
});
|
|
268
|
+
return { mode: outputMode, blocks, warnings: [] };
|
|
227
269
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
270
|
+
const blocks: ContentBlock[] = [];
|
|
271
|
+
const warnings: SearchWarning[] = [];
|
|
272
|
+
const results = await rereadMatches(files, context, signal, (file) =>
|
|
273
|
+
contentBlock(file, options),
|
|
274
|
+
);
|
|
275
|
+
for (const result of results) {
|
|
276
|
+
if ("warning" in result) warnings.push(result.warning);
|
|
277
|
+
else blocks.push(result.value);
|
|
233
278
|
}
|
|
234
|
-
return {
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
interface AssembleGrepOutputOptions {
|
|
238
|
-
readonly formatted: Omit<Awaited<ReturnType<typeof formatMatches>>, "warnings">;
|
|
239
|
-
readonly cwd: string;
|
|
240
|
-
readonly warnings: readonly SearchWarning[];
|
|
241
|
-
readonly outputMode: GrepOutputMode;
|
|
242
|
-
readonly matchLimitReached: boolean;
|
|
243
|
-
readonly effectiveLimit: number;
|
|
244
|
-
readonly searchFacts: SearchFacts;
|
|
245
|
-
readonly budget: OutputBudgets;
|
|
279
|
+
return { mode: outputMode, blocks, warnings };
|
|
246
280
|
}
|
|
247
281
|
|
|
248
|
-
/**
|
|
249
|
-
|
|
250
|
-
* that does not fit; content blocks are separated by an empty line. `shown` counts each reached
|
|
251
|
-
* block's kept lines, its separator excluded.
|
|
252
|
-
*/
|
|
253
|
-
function fitPayload(blocks: readonly (readonly string[])[], separated: boolean, maxBytes: number) {
|
|
282
|
+
/** Fit whole rows, including any blank separator that belongs to a file header. */
|
|
283
|
+
function fitPayload(blocks: Iterable<Iterable<string>>, separator: string, maxBytes: number) {
|
|
254
284
|
const kept: string[] = [];
|
|
255
285
|
const shown: number[] = [];
|
|
256
286
|
let bytes = 0;
|
|
257
|
-
const keep = (line: string) => {
|
|
258
|
-
const cost = Buffer.byteLength(line) + (kept.length ? 1 : 0);
|
|
259
|
-
if (bytes + cost > maxBytes) return false;
|
|
260
|
-
kept.push(line);
|
|
261
|
-
bytes += cost;
|
|
262
|
-
return true;
|
|
263
|
-
};
|
|
264
287
|
for (const lines of blocks) {
|
|
265
|
-
|
|
266
|
-
|
|
288
|
+
let prefix = kept.length ? separator : "";
|
|
289
|
+
const start = kept.length;
|
|
267
290
|
for (const line of lines) {
|
|
268
|
-
|
|
269
|
-
|
|
291
|
+
bytes += rowBytes(line, prefix);
|
|
292
|
+
if (bytes > maxBytes) {
|
|
293
|
+
shown.push(kept.length - start);
|
|
294
|
+
return { kept, shown, truncated: true };
|
|
295
|
+
}
|
|
296
|
+
kept.push(prefix + line);
|
|
297
|
+
prefix = "\n";
|
|
270
298
|
}
|
|
299
|
+
shown.push(kept.length - start);
|
|
271
300
|
}
|
|
272
301
|
return { kept, shown, truncated: false };
|
|
273
302
|
}
|
|
274
303
|
|
|
275
|
-
/** Would the same byte budget show more hits if context rows were removed? */
|
|
276
|
-
function contextHidesHit(
|
|
277
|
-
blocks: readonly FormattedBlock[],
|
|
278
|
-
shown: readonly number[],
|
|
279
|
-
maxBytes: number,
|
|
280
|
-
): boolean {
|
|
281
|
-
let shownHits = 0;
|
|
282
|
-
const hitsOnly: string[][] = [];
|
|
283
|
-
for (const [i, { lines, hitIndices, firstHitRow }] of blocks.entries()) {
|
|
284
|
-
if (!hitIndices) return false;
|
|
285
|
-
shownHits += hitIndices.filter((index) => index < (shown[i] ?? 0)).length;
|
|
286
|
-
hitsOnly.push([
|
|
287
|
-
lines[0],
|
|
288
|
-
firstHitRow ?? lines[hitIndices[0]],
|
|
289
|
-
...hitIndices.slice(1).map((index) => lines[index]),
|
|
290
|
-
]);
|
|
291
|
-
}
|
|
292
|
-
const withoutContext = fitPayload(hitsOnly, true, maxBytes).shown;
|
|
293
|
-
return withoutContext.reduce((sum, count) => sum + Math.max(0, count - 1), 0) > shownHits;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/** Join the payload within the call's byte budget and state each limit, omission, and diagnostic once. */
|
|
297
|
-
function assembleGrepOutput(options: AssembleGrepOutputOptions): {
|
|
298
|
-
payload: string;
|
|
299
|
-
facts: SuccessFacts<"grep">;
|
|
300
|
-
next?: OutcomeVariant;
|
|
301
|
-
} {
|
|
302
|
-
const {
|
|
303
|
-
formatted,
|
|
304
|
-
cwd,
|
|
305
|
-
warnings,
|
|
306
|
-
outputMode,
|
|
307
|
-
matchLimitReached,
|
|
308
|
-
effectiveLimit,
|
|
309
|
-
searchFacts,
|
|
310
|
-
budget,
|
|
311
|
-
} = options;
|
|
312
|
-
const { blocks, matches, files } = formatted;
|
|
313
|
-
|
|
314
|
-
if (!blocks.length && warnings.length) {
|
|
315
|
-
throw searchIncompleteError(
|
|
316
|
-
"No matches could be displayed.",
|
|
317
|
-
warnings,
|
|
318
|
-
cwd,
|
|
319
|
-
budget,
|
|
320
|
-
searchFacts,
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
const { kept, shown, truncated } = fitPayload(
|
|
324
|
-
blocks.map((block) => block.lines),
|
|
325
|
-
outputMode === "content",
|
|
326
|
-
budget.grepBytes,
|
|
327
|
-
);
|
|
328
|
-
const invalidUtf8: string[] = [];
|
|
329
|
-
let previewLimit: number | undefined;
|
|
330
|
-
for (const [i, count] of shown.entries()) {
|
|
331
|
-
const block = blocks[i];
|
|
332
|
-
// A file shown only by its header has no preview rows.
|
|
333
|
-
if (block.invalidUtf8 && count > 1) invalidUtf8.push(block.invalidUtf8);
|
|
334
|
-
for (const { index, units } of block.previewMinimums ?? []) {
|
|
335
|
-
if (index >= count) break;
|
|
336
|
-
previewLimit = Math.min(previewLimit ?? Infinity, units);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
// One complete content or count block already states its path and match count.
|
|
340
|
-
const countsShown = outputMode !== "files" && blocks.length === 1 && !truncated;
|
|
341
|
-
return {
|
|
342
|
-
payload: kept.join("\n"),
|
|
343
|
-
facts: {
|
|
344
|
-
...(countsShown ? {} : { matches, files }),
|
|
345
|
-
...searchFacts,
|
|
346
|
-
matchLimit: matchLimitReached ? effectiveLimit : undefined,
|
|
347
|
-
outputLimit: truncated ? formatByteSize(budget.grepBytes) : undefined,
|
|
348
|
-
linePreviewLimit: previewLimit,
|
|
349
|
-
invalidUtf8: invalidUtf8.length ? invalidUtf8 : undefined,
|
|
350
|
-
diagnostics: warnings.length ? searchDiagnostics(warnings, cwd, budget) : undefined,
|
|
351
|
-
},
|
|
352
|
-
next: truncated && contextHidesHit(blocks, shown, budget.grepBytes) ? "contextRows" : undefined,
|
|
353
|
-
};
|
|
354
|
-
}
|
|
355
|
-
|
|
356
304
|
/**
|
|
357
|
-
* grep's payload and
|
|
358
|
-
*
|
|
305
|
+
* grep's payload and facts for a finished search. The payload is absent when nothing matched;
|
|
306
|
+
* a search that cannot confirm any result fails with SEARCH_INCOMPLETE instead.
|
|
359
307
|
*/
|
|
360
308
|
export async function grepOutput(
|
|
361
309
|
options: Omit<FormatMatchesOptions, "files"> & {
|
|
@@ -368,9 +316,13 @@ export async function grepOutput(
|
|
|
368
316
|
/** A literal pattern contains `|`, which matches only that character. */
|
|
369
317
|
readonly literalPipe: boolean;
|
|
370
318
|
},
|
|
371
|
-
): Promise<{
|
|
372
|
-
|
|
373
|
-
|
|
319
|
+
): Promise<{
|
|
320
|
+
payload?: string;
|
|
321
|
+
facts: SuccessFacts<"grep">;
|
|
322
|
+
payloadShowsCounts?: true;
|
|
323
|
+
next?: OutcomeVariant;
|
|
324
|
+
}> {
|
|
325
|
+
const { cwd, search, warnings, missing, context, effectiveLimit, literalPipe, budget } = options;
|
|
374
326
|
const searchFacts: SearchFacts = {
|
|
375
327
|
scanLimit: search.scanLimitReached ? formatByteSize(MAX_UNNARROWED_SCAN_BYTES) : undefined,
|
|
376
328
|
missing: missing.length ? [...missing] : undefined,
|
|
@@ -380,21 +332,76 @@ export async function grepOutput(
|
|
|
380
332
|
throw searchIncompleteError("No matches confirmed.", warnings, cwd, budget, searchFacts);
|
|
381
333
|
return {
|
|
382
334
|
// A literal `|` finding nothing most likely meant alternatives.
|
|
383
|
-
facts: {
|
|
335
|
+
facts: {
|
|
336
|
+
matches: 0,
|
|
337
|
+
files: 0,
|
|
338
|
+
...searchFacts,
|
|
339
|
+
literalPipe: literalPipe || undefined,
|
|
340
|
+
},
|
|
384
341
|
};
|
|
385
342
|
}
|
|
386
|
-
const {
|
|
343
|
+
const {
|
|
344
|
+
mode,
|
|
345
|
+
blocks,
|
|
346
|
+
warnings: rereadWarnings,
|
|
347
|
+
} = await formatMatches({
|
|
387
348
|
...options,
|
|
388
349
|
files: search.files,
|
|
389
350
|
});
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
351
|
+
const diagnostics = [...warnings, ...rereadWarnings];
|
|
352
|
+
if (!blocks.length && diagnostics.length)
|
|
353
|
+
throw searchIncompleteError(
|
|
354
|
+
"No matches could be displayed.",
|
|
355
|
+
diagnostics,
|
|
356
|
+
cwd,
|
|
357
|
+
budget,
|
|
358
|
+
searchFacts,
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
const separator = mode === "content" ? "\n\n" : "\n";
|
|
362
|
+
const { kept, shown, truncated } = fitPayload(
|
|
363
|
+
blocks.map(({ lines }) => lines),
|
|
364
|
+
separator,
|
|
365
|
+
budget.grepBytes,
|
|
366
|
+
);
|
|
367
|
+
const invalidUtf8: string[] = [];
|
|
368
|
+
let previewLimit = Infinity;
|
|
369
|
+
let shownHits = 0;
|
|
370
|
+
let next: OutcomeVariant | undefined;
|
|
371
|
+
if (mode === "content") {
|
|
372
|
+
for (const [index, count] of shown.entries()) {
|
|
373
|
+
// A file shown only by its header has no preview rows.
|
|
374
|
+
if (count <= 1) continue;
|
|
375
|
+
const block = blocks[index];
|
|
376
|
+
if (block.invalidPath !== undefined) invalidUtf8.push(block.invalidPath);
|
|
377
|
+
const minimum = block.previewMinimums.findLast(({ index }) => index < count);
|
|
378
|
+
previewLimit = Math.min(previewLimit, minimum?.units ?? Infinity);
|
|
379
|
+
shownHits += block.hitIndices.findLastIndex((hit) => hit < count) + 1;
|
|
380
|
+
}
|
|
381
|
+
if (truncated && context > 0) {
|
|
382
|
+
const withoutContext = fitPayload(
|
|
383
|
+
blocks.map((block) => block.withoutContext),
|
|
384
|
+
separator,
|
|
385
|
+
budget.grepBytes,
|
|
386
|
+
).shown;
|
|
387
|
+
const hitCount = withoutContext.reduce((sum, count) => sum + Math.max(0, count - 1), 0);
|
|
388
|
+
if (hitCount > shownHits) next = "contextRows";
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
payload: kept.join(""),
|
|
393
|
+
facts: {
|
|
394
|
+
matches: blocks.reduce((sum, block) => sum + block.matches, 0),
|
|
395
|
+
files: blocks.length,
|
|
396
|
+
...searchFacts,
|
|
397
|
+
matchLimit: search.matchLimitReached ? effectiveLimit : undefined,
|
|
398
|
+
outputLimit: truncated ? formatByteSize(budget.grepBytes) : undefined,
|
|
399
|
+
linePreviewLimit: previewLimit === Infinity ? undefined : previewLimit,
|
|
400
|
+
invalidUtf8: invalidUtf8.length ? invalidUtf8 : undefined,
|
|
401
|
+
diagnostics: diagnostics.length ? searchDiagnostics(diagnostics, cwd, budget) : undefined,
|
|
402
|
+
},
|
|
403
|
+
// One complete content or count block already states its path and match count.
|
|
404
|
+
payloadShowsCounts: (!truncated && blocks.length === 1 && mode !== "files") || undefined,
|
|
405
|
+
next,
|
|
406
|
+
};
|
|
400
407
|
}
|
package/src/pi/grep-render.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { parseDisplayRow } from "./anchor-format.ts";
|
|
8
|
-
import { parseFileHeader } from "./grep-output.ts";
|
|
8
|
+
import { CONTEXT_GAP, parseFileHeader } from "./grep-output.ts";
|
|
9
9
|
|
|
10
10
|
/** Leading spaces and tabs of a row's content. */
|
|
11
11
|
function indentation(text: string): number {
|
|
@@ -15,13 +15,15 @@ function indentation(text: string): number {
|
|
|
15
15
|
/**
|
|
16
16
|
* Render grouped grep results for the TUI. Anchored and plain preview rows share
|
|
17
17
|
* aligned line numbers and folded leading indentation; anchored rows lose their
|
|
18
|
-
* hashes
|
|
18
|
+
* hashes, and a gap row keeps its place between two groups of one file. Notices are
|
|
19
|
+
* report facts, which the card renders from the report.
|
|
19
20
|
*/
|
|
20
21
|
export function toDisplayLines(raw: string, theme: Theme): string[] {
|
|
21
22
|
const lines = raw.split("\n");
|
|
22
23
|
type Header = NonNullable<ReturnType<typeof parseFileHeader>>;
|
|
23
24
|
type Row = NonNullable<ReturnType<typeof parseDisplayRow>>;
|
|
24
|
-
const blocks: (string | { header: Header; rows: Row[] })[] =
|
|
25
|
+
const blocks: (string | { header: Header; rows: (Row | typeof CONTEXT_GAP)[]; base: number })[] =
|
|
26
|
+
[];
|
|
25
27
|
let lineNoWidth = 0;
|
|
26
28
|
// A filename can look like a row, and a source row can end like a header.
|
|
27
29
|
// Parse each group before measuring its rows so the header contributes no width.
|
|
@@ -32,15 +34,20 @@ export function toDisplayLines(raw: string, theme: Theme): string[] {
|
|
|
32
34
|
blocks.push(line);
|
|
33
35
|
continue;
|
|
34
36
|
}
|
|
35
|
-
const rows: Row[] = [];
|
|
37
|
+
const rows: (Row | typeof CONTEXT_GAP)[] = [];
|
|
38
|
+
let base = Infinity;
|
|
36
39
|
while (index + 1 < lines.length) {
|
|
37
|
-
|
|
40
|
+
// A gap row belongs to the group only when a row follows it.
|
|
41
|
+
const gap = rows.length > 0 && lines[index + 1] === CONTEXT_GAP ? 1 : 0;
|
|
42
|
+
const row = parseDisplayRow(lines[index + 1 + gap] ?? "");
|
|
38
43
|
if (!row) break;
|
|
44
|
+
if (gap) rows.push(CONTEXT_GAP);
|
|
39
45
|
rows.push(row);
|
|
46
|
+
base = Math.min(base, indentation(row.content));
|
|
40
47
|
lineNoWidth = Math.max(lineNoWidth, row.lineNo.length);
|
|
41
|
-
index
|
|
48
|
+
index += 1 + gap;
|
|
42
49
|
}
|
|
43
|
-
blocks.push({ header, rows });
|
|
50
|
+
blocks.push({ header, rows, base });
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
const out: string[] = [];
|
|
@@ -51,12 +58,14 @@ export function toDisplayLines(raw: string, theme: Theme): string[] {
|
|
|
51
58
|
}
|
|
52
59
|
const { path, summary } = group.header;
|
|
53
60
|
out.push(theme.fg("success", path) + theme.fg("dim", ` · ${summary}`));
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
const { base, rows } = group;
|
|
62
|
+
const marker = base > 0 && rows.length ? theme.fg("dim", "›") + " " : "";
|
|
63
|
+
for (const row of rows) {
|
|
64
|
+
if (row === CONTEXT_GAP) {
|
|
65
|
+
out.push(theme.fg("dim", ` ${CONTEXT_GAP}`));
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const { lineNo, content } = row;
|
|
60
69
|
out.push(
|
|
61
70
|
theme.fg("dim", ` ${lineNo.padStart(lineNoWidth)}: `) +
|
|
62
71
|
marker +
|
package/src/pi/grep-reread.ts
CHANGED
|
@@ -15,8 +15,10 @@ import { scanTextLines } from "./text-stream.ts";
|
|
|
15
15
|
|
|
16
16
|
const MAX_CONCURRENT_FILE_READS = 16;
|
|
17
17
|
|
|
18
|
-
/**
|
|
18
|
+
/** Selects and collects output rows during the revision scan. */
|
|
19
19
|
export interface LineCollector<T> {
|
|
20
|
+
/** Whether the collector still needs this physical line if it is in the match window. */
|
|
21
|
+
select(number: number): boolean;
|
|
20
22
|
onLine(number: number, text: string): void;
|
|
21
23
|
/** Runs only after the read's revision matched the search. */
|
|
22
24
|
finish(): T;
|
|
@@ -42,9 +44,8 @@ async function rereadFile<T>(
|
|
|
42
44
|
const shown = shownLines(file, context);
|
|
43
45
|
const stats = await scanTextLines(
|
|
44
46
|
file.filePath,
|
|
45
|
-
(number) => shown.has(number),
|
|
46
|
-
|
|
47
|
-
(line) => collector.onLine(line.number, line.text!),
|
|
47
|
+
(number) => shown.has(number) && collector.select(number),
|
|
48
|
+
(line) => collector.onLine(line.number, line.text),
|
|
48
49
|
{ signal, decoding: "lossy" },
|
|
49
50
|
);
|
|
50
51
|
// A NUL the search did not see also changes the revision.
|
|
@@ -59,13 +60,13 @@ async function rereadFile<T>(
|
|
|
59
60
|
export async function rereadMatches<T>(
|
|
60
61
|
files: readonly GrepFileMatch[],
|
|
61
62
|
context: number,
|
|
62
|
-
signal: AbortSignal
|
|
63
|
+
signal: AbortSignal,
|
|
63
64
|
collect: (file: GrepFileMatch) => LineCollector<T>,
|
|
64
65
|
): Promise<Reread<T>[]> {
|
|
65
66
|
const results = new Array<Reread<T>>(files.length);
|
|
66
67
|
let nextIndex = 0;
|
|
67
68
|
const workerAbort = new AbortController();
|
|
68
|
-
const scanSignal =
|
|
69
|
+
const scanSignal = AbortSignal.any([signal, workerAbort.signal]);
|
|
69
70
|
let failure: { error: unknown } | undefined;
|
|
70
71
|
const workers = Array.from(
|
|
71
72
|
{ length: Math.min(MAX_CONCURRENT_FILE_READS, files.length) },
|