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 365a36bc7d..37edcccb62 100644 --- a/src/core/diff/strategies/__tests__/multi-search-replace.test.ts +++ b/src/core/diff/strategies/__tests__/multi-search-replace.test.ts @@ -2253,6 +2253,85 @@ function process() { function two() { return 2; +}`) + } + }) + + it("should fail when line range is far outside file bounds", async () => { + const originalContent = ` +function one() { + return 1; +} + +function two() { + return 2; +} + +function three() { + return 3; +} +`.trim() + const diffContent = `test.ts +<<<<<<< SEARCH +:start_line:1000 +------- +function three() { + return 3; +} +======= +function three() { + return "three"; +} +>>>>>>> REPLACE` + + // Line 1000 is way outside the bounds of the file (10 lines) + // and outside of any reasonable buffer range, so it should fail + const result = await strategy.applyDiff(originalContent, diffContent, 1000) + expect(result.success).toBe(false) + }) + + it("should find match when line range is slightly out of bounds but within buffer zone", async () => { + const originalContent = ` +function one() { + return 1; +} + +function two() { + return 2; +} + +function three() { + return 3; +} +`.trim() + const diffContent = `test.ts +<<<<<<< SEARCH +:start_line:11 +------- +function three() { + return 3; +} +======= +function three() { + return "three"; +} +>>>>>>> REPLACE` + + // File only has 10 lines, but we specify line 11 + // It should still find the match since it's within the buffer zone (5 lines) + const result = await strategy.applyDiff(originalContent, diffContent, 11) + expect(result.success).toBe(true) + if (result.success) { + expect(result.content).toBe(`function one() { + return 1; +} + +function two() { + return 2; +} + +function three() { + return "three"; }`) } }) diff --git a/src/core/diff/strategies/multi-search-replace.ts b/src/core/diff/strategies/multi-search-replace.ts index 36de3c58ad..b7c2dffe8a 100644 --- a/src/core/diff/strategies/multi-search-replace.ts +++ b/src/core/diff/strategies/multi-search-replace.ts @@ -430,14 +430,6 @@ Only use a single line of '=======' between search and replacement content, beca const searchLen = searchLines.length const exactEndIndex = exactStartIndex + searchLen - 1 - if (exactStartIndex < 0 || exactEndIndex >= resultLines.length) { - diffResults.push({ - success: false, - error: `Line range ${startLine}-${startLine + searchLen - 1} is invalid (file has ${resultLines.length} lines)\n\nDebug Info:\n- Requested Range: lines ${startLine}-${startLine + searchLen - 1}\n- File Bounds: lines 1-${resultLines.length}`, - }) - continue - } - // Try exact match first const originalChunk = resultLines.slice(exactStartIndex, exactEndIndex + 1).join("\n") const similarity = getSimilarity(originalChunk, searchChunk)