diff --git a/src/core/diff/strategies/__tests__/multi-search-replace.test.ts b/src/core/diff/strategies/__tests__/multi-search-replace.test.ts index 63111ba9aa..365a36bc7d 100644 --- a/src/core/diff/strategies/__tests__/multi-search-replace.test.ts +++ b/src/core/diff/strategies/__tests__/multi-search-replace.test.ts @@ -159,6 +159,25 @@ function helloWorld() { } }) + it("should replace matching content when end_line is passed in", async () => { + const originalContent = 'function hello() {\n console.log("hello")\n}\n' + const diffContent = `test.ts +<<<<<<< SEARCH +:start_line:1 +:end_line:1 +------- +function hello() { +======= +function helloWorld() { +>>>>>>> REPLACE` + + const result = await strategy.applyDiff(originalContent, diffContent) + expect(result.success).toBe(true) + if (result.success) { + expect(result.content).toBe('function helloWorld() {\n console.log("hello")\n}\n') + } + }) + it("should match content with different surrounding whitespace", async () => { const originalContent = "\nfunction example() {\n return 42;\n}\n\n" const diffContent = `test.ts diff --git a/src/core/diff/strategies/multi-search-replace.ts b/src/core/diff/strategies/multi-search-replace.ts index a9d4ba6560..075a768fba 100644 --- a/src/core/diff/strategies/multi-search-replace.ts +++ b/src/core/diff/strategies/multi-search-replace.ts @@ -186,6 +186,7 @@ Only use a single line of '=======' between search and replacement content, beca .replace(/^\\=======/gm, "=======") .replace(/^\\>>>>>>>/gm, ">>>>>>>") .replace(/^\\-------/gm, "-------") + .replace(/^\\:end_line:/gm, ":end_line:") .replace(/^\\:start_line:/gm, ":start_line:") } @@ -322,25 +323,28 @@ Only use a single line of '=======' between search and replacement content, beca 3. ((?:\:start_line:\s*(\d+)\s*\n))?   Optionally matches a “:start_line:” line. The outer capturing group is group 1 and the inner (\d+) is group 2. - 4. ((?>>>>>> REPLACE)(?=\n|$) + 9. (?:(?<=\n)(?>>>>>> REPLACE)(?=\n|$)   Matches the final “>>>>>>> REPLACE” marker on its own line (and requires a following newline or the end of file). */ let matches = [ ...diffContent.matchAll( - /(?:^|\n)(?>>>>>> REPLACE)(?=\n|$)/g, + /(?:^|\n)(?>>>>>> REPLACE)(?=\n|$)/g, ), ] @@ -359,8 +363,8 @@ Only use a single line of '=======' between search and replacement content, beca const replacements = matches .map((match) => ({ startLine: Number(match[2] ?? 0), - searchContent: match[4], - replaceContent: match[5], + searchContent: match[6], + replaceContent: match[7], })) .sort((a, b) => a.startLine - b.startLine)