(file_path: string, edits: FileEdit[])
| 62 | } |
| 63 | |
| 64 | async function loadDiffData(file_path: string, edits: FileEdit[]): Promise<DiffData> { |
| 65 | const valid = edits.filter(e => e.old_string != null && e.new_string != null); |
| 66 | const single = valid.length === 1 ? valid[0]! : undefined; |
| 67 | |
| 68 | // SedEditPermissionRequest passes the entire file as old_string. Scanning for |
| 69 | // a needle ≥ CHUNK_SIZE allocates O(needle) for the overlap buffer — skip the |
| 70 | // file read entirely and diff the inputs we already have. |
| 71 | if (single && single.old_string.length >= CHUNK_SIZE) { |
| 72 | return diffToolInputsOnly(file_path, [single]); |
| 73 | } |
| 74 | |
| 75 | try { |
| 76 | const handle = await openForScan(file_path); |
| 77 | if (handle === null) return diffToolInputsOnly(file_path, valid); |
| 78 | try { |
| 79 | // Multi-edit and empty old_string genuinely need full-file for sequential |
| 80 | // replacements — structuredPatch needs before/after strings. replace_all |
| 81 | // routes through the chunked path below (shows first-occurrence window; |
| 82 | // matches within the slice still replace via edit.replace_all). |
| 83 | if (!single || single.old_string === '') { |
| 84 | const file = await readCapped(handle); |
| 85 | if (file === null) return diffToolInputsOnly(file_path, valid); |
| 86 | const normalized = valid.map(e => normalizeEdit(file, e)); |
| 87 | return { |
| 88 | patch: getPatchForDisplay({ |
| 89 | filePath: file_path, |
| 90 | fileContents: file, |
| 91 | edits: normalized, |
| 92 | }), |
| 93 | firstLine: firstLineOf(file), |
| 94 | fileContent: file, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | const ctx = await scanForContext(handle, single.old_string, CONTEXT_LINES); |
| 99 | if (ctx.truncated || ctx.content === '') { |
| 100 | return diffToolInputsOnly(file_path, [single]); |
| 101 | } |
| 102 | const normalized = normalizeEdit(ctx.content, single); |
| 103 | const hunks = getPatchForDisplay({ |
| 104 | filePath: file_path, |
| 105 | fileContents: ctx.content, |
| 106 | edits: [normalized], |
| 107 | }); |
| 108 | return { |
| 109 | patch: adjustHunkLineNumbers(hunks, ctx.lineOffset - 1), |
| 110 | firstLine: ctx.lineOffset === 1 ? firstLineOf(ctx.content) : null, |
| 111 | fileContent: ctx.content, |
| 112 | }; |
| 113 | } finally { |
| 114 | await handle.close(); |
| 115 | } |
| 116 | } catch (e) { |
| 117 | logError(e as Error); |
| 118 | return diffToolInputsOnly(file_path, valid); |
| 119 | } |
| 120 | } |
| 121 |
no test coverage detected