MCPcopy
hub / github.com/codeaashu/claude-code / getSnippetForPatch

Function getSnippetForPatch

src/tools/FileEditTool/utils.ts:417–457  ·  view source on GitHub ↗
(
  patch: StructuredPatchHunk[],
  newFile: string,
)

Source from the content-addressed store, hash-verified

415 * @returns The snippet text with line numbers and the starting line number
416 */
417export function getSnippetForPatch(
418 patch: StructuredPatchHunk[],
419 newFile: string,
420): { formattedSnippet: string; startLine: number } {
421 if (patch.length === 0) {
422 // No changes, return empty snippet
423 return { formattedSnippet: '', startLine: 1 }
424 }
425
426 // Find the first and last changed lines across all hunks
427 let minLine = Infinity
428 let maxLine = -Infinity
429
430 for (const hunk of patch) {
431 if (hunk.oldStart < minLine) {
432 minLine = hunk.oldStart
433 }
434 // For the end line, we need to consider the new lines count since we're showing the new file
435 const hunkEnd = hunk.oldStart + (hunk.newLines || 0) - 1
436 if (hunkEnd > maxLine) {
437 maxLine = hunkEnd
438 }
439 }
440
441 // Calculate the range with context
442 const startLine = Math.max(1, minLine - CONTEXT_LINES)
443 const endLine = maxLine + CONTEXT_LINES
444
445 // Split the new file into lines and get the snippet
446 const fileLines = newFile.split(/\r?\n/)
447 const snippetLines = fileLines.slice(startLine - 1, endLine)
448 const snippet = snippetLines.join('\n')
449
450 // Add line numbers
451 const formattedSnippet = addLineNumbers({
452 content: snippet,
453 startLine,
454 })
455
456 return { formattedSnippet, startLine }
457}
458
459/**
460 * Gets a snippet from a file showing the context around a single edit.

Callers

nothing calls this directly

Calls 2

addLineNumbersFunction · 0.85
maxMethod · 0.80

Tested by

no test coverage detected