MCPcopy Index your code
hub / github.com/anomalyco/opencode / deriveNewContentsFromChunks

Function deriveNewContentsFromChunks

packages/opencode/src/patch/index.ts:307–340  ·  view source on GitHub ↗
(
  filePath: string,
  chunks: UpdateFileChunk[],
  originalText: string,
)

Source from the content-addressed store, hash-verified

305}
306
307export function deriveNewContentsFromChunks(
308 filePath: string,
309 chunks: UpdateFileChunk[],
310 originalText: string,
311): ApplyPatchFileUpdate {
312 const originalContent = Bom.split(originalText)
313
314 let originalLines = originalContent.text.split("\n")
315
316 // Drop trailing empty element for consistent line counting
317 if (originalLines.length > 0 && originalLines[originalLines.length - 1] === "") {
318 originalLines.pop()
319 }
320
321 const replacements = computeReplacements(originalLines, filePath, chunks)
322 let newLines = applyReplacements(originalLines, replacements)
323
324 // Ensure trailing newline
325 if (newLines.length === 0 || newLines[newLines.length - 1] !== "") {
326 newLines.push("")
327 }
328
329 const next = Bom.split(newLines.join("\n"))
330 const newContent = next.text
331
332 // Generate unified diff
333 const unifiedDiff = generateUnifiedDiff(originalContent.text, newContent)
334
335 return {
336 unified_diff: unifiedDiff,
337 content: newContent,
338 bom: originalContent.bom || next.bom,
339 }
340}
341
342function computeReplacements(
343 originalLines: string[],

Callers 1

index.tsFile · 0.85

Calls 4

applyReplacementsFunction · 0.85
generateUnifiedDiffFunction · 0.85
pushMethod · 0.80
computeReplacementsFunction · 0.70

Tested by

no test coverage detected