MCPcopy
hub / github.com/continuedev/continue / applyUnifiedDiff

Function applyUnifiedDiff

core/edit/lazy/unifiedDiffApply.ts:43–93  ·  view source on GitHub ↗
(
  sourceCode: string,
  unifiedDiffText: string,
)

Source from the content-addressed store, hash-verified

41 * @throws Error if the diff cannot be cleanly applied to the source
42 */
43export function applyUnifiedDiff(
44 sourceCode: string,
45 unifiedDiffText: string,
46): DiffLine[] {
47 const sourceLines = sourceCode.split(/\r?\n/);
48 const hunks = parseUnifiedDiff(unifiedDiffText);
49 const diffResult: DiffLine[] = [];
50 let currentPos = 0; // pointer in sourceLines
51
52 for (const hunk of hunks) {
53 const hunkBeforeLines = extractBeforeLines(hunk.lines);
54 const hunkStart = findHunkInSource(
55 sourceLines,
56 hunkBeforeLines,
57 currentPos,
58 );
59 if (hunkStart === -1) {
60 // All hunks must be found in the source code. If not, throw an error.
61 throw new Error("Hunk could not be applied cleanly to source code.");
62 }
63
64 // Emit any unchanged lines that come before this hunk.
65 for (let i = currentPos; i < hunkStart; i++) {
66 diffResult.push({ type: "same", line: sourceLines[i] });
67 }
68 let hunkSourcePos = hunkStart;
69
70 for (const dline of hunk.lines) {
71 const srcLine = sourceLines[hunkSourcePos];
72 if (dline.startsWith("+")) {
73 // Insertion: output new line (strip the '+' marker)
74 diffResult.push({ type: "new", line: dline.substring(1) });
75 } else if (dline.startsWith("-")) {
76 // Removal: output the removed (old) line and advance the pointer.
77 diffResult.push({ type: "old", line: srcLine });
78 hunkSourcePos++;
79 } else {
80 // Context line: use the source line (in case the diff’s context has a minor whitespace error)
81 // and advance the pointer.
82 diffResult.push({ type: "same", line: srcLine });
83 hunkSourcePos++;
84 }
85 }
86 currentPos = hunkSourcePos;
87 }
88
89 for (let i = currentPos; i < sourceLines.length; i++) {
90 diffResult.push({ type: "same", line: sourceLines[i] });
91 }
92 return diffResult;
93}
94
95interface Hunk {
96 lines: string[];

Callers 2

applyCodeBlockFunction · 0.90

Calls 4

parseUnifiedDiffFunction · 0.85
extractBeforeLinesFunction · 0.85
findHunkInSourceFunction · 0.85
pushMethod · 0.65

Tested by

no test coverage detected