-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheditHelper.ts
More file actions
477 lines (420 loc) · 12.6 KB
/
Copy patheditHelper.ts
File metadata and controls
477 lines (420 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Helpers for reconciling LLM-proposed edits with on-disk text.
*
* The normalization pipeline intentionally stays deterministic: we first try
* literal substring matches, then gradually relax comparison rules (smart
* quotes, em-dashes, trailing whitespace, etc.) until we either locate the
* exact slice from the file or conclude the edit cannot be applied.
*/
/* -------------------------------------------------------------------------- */
/* Character-level normalization */
/* -------------------------------------------------------------------------- */
const UNICODE_EQUIVALENT_MAP: Record<string, string> = {
// Hyphen variations → ASCII hyphen-minus.
'\u2010': '-',
'\u2011': '-',
'\u2012': '-',
'\u2013': '-',
'\u2014': '-',
'\u2015': '-',
'\u2212': '-',
// Curly single quotes → straight apostrophe.
'\u2018': "'",
'\u2019': "'",
'\u201A': "'",
'\u201B': "'",
// Curly double quotes → straight double quote.
'\u201C': '"',
'\u201D': '"',
'\u201E': '"',
'\u201F': '"',
// Whitespace variants → normal space.
'\u00A0': ' ',
'\u2002': ' ',
'\u2003': ' ',
'\u2004': ' ',
'\u2005': ' ',
'\u2006': ' ',
'\u2007': ' ',
'\u2008': ' ',
'\u2009': ' ',
'\u200A': ' ',
'\u202F': ' ',
'\u205F': ' ',
'\u3000': ' ',
};
function normalizeBasicCharacters(text: string): string {
if (text === '') {
return text;
}
let normalized = '';
for (const char of text) {
normalized += UNICODE_EQUIVALENT_MAP[char] ?? char;
}
return normalized;
}
/* -------------------------------------------------------------------------- */
/* Line-based search helpers */
/* -------------------------------------------------------------------------- */
interface MatchedSliceResult {
slice: string;
removedTrailingFinalEmptyLine: boolean;
}
/**
* Comparison passes become progressively more forgiving, making it possible to
* match when only trailing whitespace differs. Leading whitespace (indentation)
* is always preserved to avoid matching at incorrect scope levels.
*/
const LINE_COMPARISON_PASSES: Array<(value: string) => string> = [
(value) => value,
(value) => value.trimEnd(),
];
function normalizeLineForComparison(value: string): string {
return normalizeBasicCharacters(value).trimEnd();
}
/**
* Finds the first index where {@link pattern} appears within {@link lines} once
* both sequences are transformed in the same way.
*/
function seekSequenceWithTransform(
lines: string[],
pattern: string[],
transform: (value: string) => string,
): number | null {
if (pattern.length === 0) {
return 0;
}
if (pattern.length > lines.length) {
return null;
}
outer: for (let i = 0; i <= lines.length - pattern.length; i++) {
for (let p = 0; p < pattern.length; p++) {
if (transform(lines[i + p]) !== transform(pattern[p])) {
continue outer;
}
}
return i;
}
return null;
}
function buildLineIndex(text: string): {
lines: string[];
offsets: number[];
} {
const lines = text.split('\n');
const offsets = new Array<number>(lines.length + 1);
let cursor = 0;
for (let i = 0; i < lines.length; i++) {
offsets[i] = cursor;
cursor += lines[i].length;
if (i < lines.length - 1) {
cursor += 1; // Account for the newline that split() removed.
}
}
offsets[lines.length] = text.length;
return { lines, offsets };
}
/**
* Reconstructs the original characters for the matched lines, optionally
* preserving the newline that follows the final line.
*/
function sliceFromLines(
text: string,
offsets: number[],
lines: string[],
startLine: number,
lineCount: number,
includeTrailingNewline: boolean,
): string {
if (lineCount === 0) {
return includeTrailingNewline ? '\n' : '';
}
const startIndex = offsets[startLine] ?? 0;
const lastLineIndex = startLine + lineCount - 1;
const lastLineStart = offsets[lastLineIndex] ?? 0;
let endIndex = lastLineStart + (lines[lastLineIndex]?.length ?? 0);
if (includeTrailingNewline) {
const nextLineStart = offsets[startLine + lineCount];
if (nextLineStart !== undefined) {
endIndex = nextLineStart;
} else if (text.endsWith('\n')) {
endIndex = text.length;
}
}
return text.slice(startIndex, endIndex);
}
function findLineBasedMatch(
haystack: string,
needle: string,
): MatchedSliceResult | null {
const { lines, offsets } = buildLineIndex(haystack);
const patternLines = needle.split('\n');
const endsWithNewline = needle.endsWith('\n');
if (patternLines.length === 0) {
return null;
}
const attemptMatch = (candidate: string[]): number | null => {
for (const pass of LINE_COMPARISON_PASSES) {
const idx = seekSequenceWithTransform(lines, candidate, pass);
if (idx !== null) {
return idx;
}
}
return seekSequenceWithTransform(
lines,
candidate,
normalizeLineForComparison,
);
};
let matchIndex = attemptMatch(patternLines);
if (matchIndex !== null) {
return {
slice: sliceFromLines(
haystack,
offsets,
lines,
matchIndex,
patternLines.length,
endsWithNewline,
),
removedTrailingFinalEmptyLine: false,
};
}
if (patternLines.at(-1) === '') {
const trimmedPattern = patternLines.slice(0, -1);
if (trimmedPattern.length === 0) {
return null;
}
matchIndex = attemptMatch(trimmedPattern);
if (matchIndex !== null) {
return {
slice: sliceFromLines(
haystack,
offsets,
lines,
matchIndex,
trimmedPattern.length,
false,
),
removedTrailingFinalEmptyLine: true,
};
}
}
return null;
}
/* -------------------------------------------------------------------------- */
/* Slice discovery */
/* -------------------------------------------------------------------------- */
function findMatchedSlice(
haystack: string,
needle: string,
): MatchedSliceResult | null {
if (needle === '') {
return null;
}
const literalIndex = haystack.indexOf(needle);
if (literalIndex !== -1) {
return {
slice: haystack.slice(literalIndex, literalIndex + needle.length),
removedTrailingFinalEmptyLine: false,
};
}
const normalizedHaystack = normalizeBasicCharacters(haystack);
const normalizedNeedleChars = normalizeBasicCharacters(needle);
const normalizedIndex = normalizedHaystack.indexOf(normalizedNeedleChars);
if (normalizedIndex !== -1) {
return {
slice: haystack.slice(normalizedIndex, normalizedIndex + needle.length),
removedTrailingFinalEmptyLine: false,
};
}
return findLineBasedMatch(haystack, needle);
}
/**
* Returns the literal slice from {@link haystack} that best corresponds to the
* provided {@link needle}, or {@code null} when no match is found.
*/
/* -------------------------------------------------------------------------- */
/* Replacement helpers */
/* -------------------------------------------------------------------------- */
function removeTrailingNewline(text: string): string {
if (text.endsWith('\r\n')) {
return text.slice(0, -2);
}
if (text.endsWith('\n') || text.endsWith('\r')) {
return text.slice(0, -1);
}
return text;
}
function adjustNewStringForTrailingLine(
newString: string,
removedTrailingLine: boolean,
): string {
return removedTrailingLine ? removeTrailingNewline(newString) : newString;
}
export interface NormalizedEditStrings {
oldString: string;
newString: string;
}
/**
* Runs the core normalization pipeline:
* 1. Attempt to find the literal text inside {@link fileContent}.
* 2. If found through a relaxed match (smart quotes, line trims, etc.),
* return the canonical slice from disk so later replacements operate on
* exact bytes.
* 3. Preserve newString as-is (it represents the LLM's intent).
*
* Note: Trailing whitespace in newString is intentionally NOT stripped.
* While LLMs may sometimes accidentally add trailing whitespace, stripping it
* unconditionally breaks legitimate use cases where trailing whitespace is
* intentional (e.g., multi-line strings, heredocs). See issue #1618.
*/
export function normalizeEditStrings(
fileContent: string | null,
oldString: string,
newString: string,
): NormalizedEditStrings {
if (fileContent === null || oldString === '') {
return {
oldString,
newString,
};
}
const canonicalOriginal = findMatchedSlice(fileContent, oldString);
if (canonicalOriginal !== null) {
return {
oldString: canonicalOriginal.slice,
newString: adjustNewStringForTrailingLine(
newString,
canonicalOriginal.removedTrailingFinalEmptyLine,
),
};
}
return {
oldString,
newString,
};
}
/**
* When deleting text and the on-disk content contains the same substring with a
* trailing newline, automatically consume that newline so the removal does not
* leave a blank line behind.
*/
export function maybeAugmentOldStringForDeletion(
fileContent: string | null,
oldString: string,
newString: string,
): string {
if (
fileContent === null ||
oldString === '' ||
newString !== '' ||
oldString.endsWith('\n')
) {
return oldString;
}
const candidate = `${oldString}\n`;
return fileContent.includes(candidate) ? candidate : oldString;
}
/**
* Counts the number of non-overlapping occurrences of {@link substr} inside
* {@link source}. Returns 0 when the substring is empty.
*/
export function countOccurrences(source: string, substr: string): number {
if (substr === '') {
return 0;
}
let count = 0;
let index = source.indexOf(substr);
while (index !== -1) {
count++;
index = source.indexOf(substr, index + substr.length);
}
return count;
}
/**
* Result from extracting a snippet showing the edited region.
*/
export interface EditSnippetResult {
/** Starting line number (1-indexed) of the snippet */
startLine: number;
/** Ending line number (1-indexed) of the snippet */
endLine: number;
/** Total number of lines in the new content */
totalLines: number;
/** The snippet content (subset of lines from newContent) */
content: string;
}
const SNIPPET_CONTEXT_LINES = 4;
const SNIPPET_MAX_LINES = 1000;
/**
* Extracts a snippet from the edited file showing the changed region with
* surrounding context. This compares the old and new content line-by-line
* from both ends to locate the changed region.
*
* @param oldContent The original file content before the edit (null for new files)
* @param newContent The new file content after the edit
* @returns Snippet information, or null if no meaningful snippet can be extracted
*/
export function extractEditSnippet(
oldContent: string | null,
newContent: string,
): EditSnippetResult | null {
const newLines = newContent.split('\n');
const totalLines = newLines.length;
if (oldContent === null) {
return {
startLine: 1,
endLine: totalLines,
totalLines,
content: newContent,
};
}
// No changes case
if (oldContent === newContent || !newContent) {
return null;
}
const oldLines = oldContent.split('\n');
// Find the first line that differs from the start
let firstDiffLine = 0;
const minLength = Math.min(oldLines.length, newLines.length);
while (firstDiffLine < minLength) {
if (oldLines[firstDiffLine] !== newLines[firstDiffLine]) {
break;
}
firstDiffLine++;
}
// Find the first line that differs from the end
let oldEndIndex = oldLines.length - 1;
let newEndIndex = newLines.length - 1;
while (oldEndIndex >= firstDiffLine && newEndIndex >= firstDiffLine) {
if (oldLines[oldEndIndex] !== newLines[newEndIndex]) {
break;
}
oldEndIndex--;
newEndIndex--;
}
// The changed region in the new content is from firstDiffLine to newEndIndex (inclusive)
// Convert to 1-indexed line numbers
const changeStart = firstDiffLine + 1;
const changeEnd = newEndIndex + 1;
// If the change region is too large, don't generate a snippet
if (changeEnd - changeStart > SNIPPET_MAX_LINES) {
return null;
}
// Calculate snippet bounds with context
const snippetStart = Math.max(1, changeStart - SNIPPET_CONTEXT_LINES);
const snippetEnd = Math.min(totalLines, changeEnd + SNIPPET_CONTEXT_LINES);
const snippetLines = newLines.slice(snippetStart - 1, snippetEnd);
return {
startLine: snippetStart,
endLine: snippetEnd,
totalLines,
content: snippetLines.join('\n'),
};
}