Browse Source

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

Sam Hoang Van 9 months ago
parent
commit
6db30afc74

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

@@ -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;
 }`)
 			}
 		})

+ 4 - 0
src/core/diff/strategies/multi-search-replace.ts

@@ -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)