UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

39 lines • 2.05 kB
import { replacementsAroundContext, SourceText } from "./line-endings.js"; import { seekSequenceWithFuzz } from "./seek-sequence.js"; export function replaceChunks(content, filePath, chunks) { const source = SourceText.parse(content); const originalLines = source.texts; const replacements = []; let lineIndex = 0; let fuzz = 0; for (const chunk of chunks) { for (const changeContext of chunk.changeContexts) { const contextIndex = seekSequenceWithFuzz(originalLines, [changeContext], lineIndex, false); if (contextIndex === undefined) throw new Error(`Failed to find context '${changeContext}' in ${filePath}`); fuzz += contextIndex.fuzz; lineIndex = contextIndex.index + 1; } if (chunk.oldLines.length === 0) { const insertionIndex = originalLines[originalLines.length - 1] === "" ? originalLines.length - 1 : originalLines.length; replacements.push({ start: insertionIndex, oldLength: 0, newLines: chunk.newLines }); continue; } let pattern = chunk.oldLines; let newLines = chunk.newLines; let foundAt = seekSequenceWithFuzz(originalLines, pattern, lineIndex, chunk.isEndOfFile); if (foundAt === undefined && pattern[pattern.length - 1] === "") { pattern = pattern.slice(0, -1); if (newLines[newLines.length - 1] === "") newLines = newLines.slice(0, -1); foundAt = seekSequenceWithFuzz(originalLines, pattern, lineIndex, chunk.isEndOfFile); } if (foundAt === undefined) throw new Error(`Failed to find expected lines in ${filePath}:\n${chunk.oldLines.join("\n")}`); fuzz += foundAt.fuzz; replacements.push(...replacementsAroundContext(foundAt.index, pattern.length, newLines, chunk.contextLineIndices)); lineIndex = foundAt.index + pattern.length; } return { content: source.replace(replacements), fuzz }; } //# sourceMappingURL=patch-replace.js.map