( originalFile: string, oldString: string, newString: string, contextLines: number = 4, )
| 466 | * @returns The snippet and the starting line number |
| 467 | */ |
| 468 | export function getSnippet( |
| 469 | originalFile: string, |
| 470 | oldString: string, |
| 471 | newString: string, |
| 472 | contextLines: number = 4, |
| 473 | ): { snippet: string; startLine: number } { |
| 474 | // Use the original algorithm from FileEditTool.tsx |
| 475 | const before = originalFile.split(oldString)[0] ?? '' |
| 476 | const replacementLine = before.split(/\r?\n/).length - 1 |
| 477 | const newFileLines = applyEditToFile( |
| 478 | originalFile, |
| 479 | oldString, |
| 480 | newString, |
| 481 | ).split(/\r?\n/) |
| 482 | |
| 483 | // Calculate the start and end line numbers for the snippet |
| 484 | const startLine = Math.max(0, replacementLine - contextLines) |
| 485 | const endLine = |
| 486 | replacementLine + contextLines + newString.split(/\r?\n/).length |
| 487 | |
| 488 | // Get snippet |
| 489 | const snippetLines = newFileLines.slice(startLine, endLine) |
| 490 | const snippet = snippetLines.join('\n') |
| 491 | |
| 492 | return { snippet, startLine: startLine + 1 } |
| 493 | } |
| 494 | |
| 495 | export function getEditsForPatch(patch: StructuredPatchHunk[]): FileEdit[] { |
| 496 | return patch.map(hunk => { |
nothing calls this directly
no test coverage detected