MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / parseUpdateDiff

Function parseUpdateDiff

sdk/src/tools/apply-patch.ts:349–414  ·  view source on GitHub ↗
(
  lines: string[],
  input: string,
)

Source from the content-addressed store, hash-verified

347}
348
349function parseUpdateDiff(
350 lines: string[],
351 input: string,
352): { chunks: Chunk[]; fuzz: number } {
353 const parser: ParserState = {
354 lines: [...lines, END_PATCH],
355 index: 0,
356 fuzz: 0,
357 }
358
359 const inputLines = input.split('\n')
360 const chunks: Chunk[] = []
361 let cursor = 0
362
363 while (!isDone(parser, END_SECTION_MARKERS)) {
364 const current = parser.lines[parser.index]
365 const line = typeof current === 'string' ? current : ''
366
367 let anchor = ''
368 const hasBareHeader = line === '@@'
369 const hasWrappedHeader = isWrappedAtHeader(line)
370 const hasAnchorHeader = line.startsWith('@@ ') && !hasWrappedHeader
371 const hasAnyHeader = hasBareHeader || hasWrappedHeader || hasAnchorHeader
372
373 if (hasAnchorHeader) {
374 anchor = line.slice(3)
375 parser.index += 1
376 } else if (hasBareHeader || hasWrappedHeader) {
377 parser.index += 1
378 }
379
380 if (!(hasAnyHeader || cursor === 0)) {
381 throw new Error(`Invalid Line:\n${parser.lines[parser.index]}`)
382 }
383
384 if (anchor.trim()) {
385 cursor = advanceCursorToAnchor(anchor, inputLines, cursor, parser)
386 }
387
388 const { nextContext, sectionChunks, endIndex, eof } = readSection(
389 parser.lines,
390 parser.index,
391 )
392
393 const { newIndex, fuzz } = findContext(inputLines, nextContext, cursor, eof)
394
395 if (newIndex === -1) {
396 const nextContextText = nextContext.join('\n')
397 if (eof) {
398 throw new Error(`Invalid EOF Context ${cursor}:\n${nextContextText}`)
399 }
400
401 throw new Error(`Invalid Context ${cursor}:\n${nextContextText}`)
402 }
403
404 parser.fuzz += fuzz
405 for (const chunk of sectionChunks) {
406 chunks.push({ ...chunk, origIndex: chunk.origIndex + newIndex })

Callers 1

applyDiffFunction · 0.85

Calls 5

isDoneFunction · 0.85
isWrappedAtHeaderFunction · 0.85
advanceCursorToAnchorFunction · 0.85
readSectionFunction · 0.85
findContextFunction · 0.85

Tested by

no test coverage detected