From 475776da98b4d9434150e2ac5008bd900ca40052 Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Mon, 16 Dec 2024 17:40:36 -0500 Subject: [PATCH] Bugfix to strip line numbers with leading space --- .../__tests__/search-replace.test.ts | 20 +++++++++++++++++++ src/core/diff/strategies/search-replace.ts | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/core/diff/strategies/__tests__/search-replace.test.ts b/src/core/diff/strategies/__tests__/search-replace.test.ts index 99aff99..f96aa17 100644 --- a/src/core/diff/strategies/__tests__/search-replace.test.ts +++ b/src/core/diff/strategies/__tests__/search-replace.test.ts @@ -591,6 +591,26 @@ this.init(); expect(result.content).toBe('function test() {\n return false;\n}\n') } }) + + it('should strip line numbers with leading spaces', () => { + const originalContent = 'function test() {\n return true;\n}\n' + const diffContent = `test.ts +<<<<<<< SEARCH + 1 | function test() { + 2 | return true; + 3 | } +======= + 1 | function test() { + 2 | return false; + 3 | } +>>>>>>> REPLACE` + + const result = strategy.applyDiff(originalContent, diffContent) + expect(result.success).toBe(true) + if (result.success) { + expect(result.content).toBe('function test() {\n return false;\n}\n') + } + }) it('should not strip when not all lines have numbers in either section', () => { const originalContent = 'function test() {\n return true;\n}\n' diff --git a/src/core/diff/strategies/search-replace.ts b/src/core/diff/strategies/search-replace.ts index 660d364..9153c4e 100644 --- a/src/core/diff/strategies/search-replace.ts +++ b/src/core/diff/strategies/search-replace.ts @@ -193,12 +193,12 @@ Result: // Strip line numbers from search and replace content if every line starts with a line number const hasLineNumbers = (content: string) => { const lines = content.split(/\r?\n/); - return lines.length > 0 && lines.every(line => /^\d+\s+\|(?!\|)/.test(line)); + return lines.length > 0 && lines.every(line => /^\s*\d+\s+\|(?!\|)/.test(line)); }; if (hasLineNumbers(searchContent) && hasLineNumbers(replaceContent)) { const stripLineNumbers = (content: string) => { - return content.replace(/^\d+\s+\|(?!\|)/gm, ''); + return content.replace(/^\s*\d+\s+\|(?!\|)/gm, ''); }; searchContent = stripLineNumbers(searchContent);