make apply_diff can deduce when line number in search part fix #2990 (#3329)

This commit is contained in:
Sam Hoang Van 2025-05-13 10:11:01 +07:00 committed by GitHub
parent 86a03798d5
commit 6db30afc74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 0 deletions

View file

@ -2332,6 +2332,54 @@ function two() {
function three() {
return "three";
}`)
}
})
it("should deduce start_line when include line number in search and replace content", async () => {
const originalContent = `
function one() {
return 1;
}
function process() {
return "target";
}
function process() {
return "target";
}
function two() {
return 2;
}
`.trim()
const diffContent = `test.ts
<<<<<<< SEARCH
9 | function process() {
10 | return "target";
=======
9 | function process2() {
10 | return "target222";
>>>>>>> REPLACE`
const result = await strategy.applyDiff(originalContent, diffContent)
expect(result.success).toBe(true)
if (result.success) {
expect(result.content).toBe(`function one() {
return 1;
}
function process() {
return "target";
}
function process2() {
return "target222";
}
function two() {
return 2;
}`)
}
})

View file

@ -380,6 +380,10 @@ Only use a single line of '=======' between search and replacement content, beca
(everyLineHasLineNumbers(searchContent) && everyLineHasLineNumbers(replaceContent)) ||
(everyLineHasLineNumbers(searchContent) && replaceContent.trim() === "")
if (hasAllLineNumbers && startLine === 0) {
startLine = parseInt(searchContent.split("\n")[0].split("|")[0])
}
if (hasAllLineNumbers) {
searchContent = stripLineNumbers(searchContent)
replaceContent = stripLineNumbers(replaceContent)