Skip to content

Commit 77341ed

Browse files
samhvw8hannesrudolph
authored andcommitted
make apply_diff can deduce when line number in search part fix #2990 (#3329)
1 parent bd66b4c commit 77341ed

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/core/diff/strategies/__tests__/multi-search-replace.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,54 @@ function two() {
23322332
23332333
function three() {
23342334
return "three";
2335+
}`)
2336+
}
2337+
})
2338+
2339+
it("should deduce start_line when include line number in search and replace content", async () => {
2340+
const originalContent = `
2341+
function one() {
2342+
return 1;
2343+
}
2344+
2345+
function process() {
2346+
return "target";
2347+
}
2348+
2349+
function process() {
2350+
return "target";
2351+
}
2352+
2353+
function two() {
2354+
return 2;
2355+
}
2356+
`.trim()
2357+
const diffContent = `test.ts
2358+
<<<<<<< SEARCH
2359+
9 | function process() {
2360+
10 | return "target";
2361+
=======
2362+
9 | function process2() {
2363+
10 | return "target222";
2364+
>>>>>>> REPLACE`
2365+
2366+
const result = await strategy.applyDiff(originalContent, diffContent)
2367+
expect(result.success).toBe(true)
2368+
if (result.success) {
2369+
expect(result.content).toBe(`function one() {
2370+
return 1;
2371+
}
2372+
2373+
function process() {
2374+
return "target";
2375+
}
2376+
2377+
function process2() {
2378+
return "target222";
2379+
}
2380+
2381+
function two() {
2382+
return 2;
23352383
}`)
23362384
}
23372385
})

src/core/diff/strategies/multi-search-replace.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ Only use a single line of '=======' between search and replacement content, beca
380380
(everyLineHasLineNumbers(searchContent) && everyLineHasLineNumbers(replaceContent)) ||
381381
(everyLineHasLineNumbers(searchContent) && replaceContent.trim() === "")
382382

383+
if (hasAllLineNumbers && startLine === 0) {
384+
startLine = parseInt(searchContent.split("\n")[0].split("|")[0])
385+
}
386+
383387
if (hasAllLineNumbers) {
384388
searchContent = stripLineNumbers(searchContent)
385389
replaceContent = stripLineNumbers(replaceContent)

0 commit comments

Comments
 (0)