Skip to content

Commit c842d3b

Browse files
authored
Merge pull request #136 from RooVetGit/fix_line_number_stripping
Bugfix to strip line numbers with leading space
2 parents 8391111 + 475776d commit c842d3b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,26 @@ this.init();
591591
expect(result.content).toBe('function test() {\n return false;\n}\n')
592592
}
593593
})
594+
595+
it('should strip line numbers with leading spaces', () => {
596+
const originalContent = 'function test() {\n return true;\n}\n'
597+
const diffContent = `test.ts
598+
<<<<<<< SEARCH
599+
1 | function test() {
600+
2 | return true;
601+
3 | }
602+
=======
603+
1 | function test() {
604+
2 | return false;
605+
3 | }
606+
>>>>>>> REPLACE`
607+
608+
const result = strategy.applyDiff(originalContent, diffContent)
609+
expect(result.success).toBe(true)
610+
if (result.success) {
611+
expect(result.content).toBe('function test() {\n return false;\n}\n')
612+
}
613+
})
594614

595615
it('should not strip when not all lines have numbers in either section', () => {
596616
const originalContent = 'function test() {\n return true;\n}\n'

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ Result:
193193
// Strip line numbers from search and replace content if every line starts with a line number
194194
const hasLineNumbers = (content: string) => {
195195
const lines = content.split(/\r?\n/);
196-
return lines.length > 0 && lines.every(line => /^\d+\s+\|(?!\|)/.test(line));
196+
return lines.length > 0 && lines.every(line => /^\s*\d+\s+\|(?!\|)/.test(line));
197197
};
198198

199199
if (hasLineNumbers(searchContent) && hasLineNumbers(replaceContent)) {
200200
const stripLineNumbers = (content: string) => {
201-
return content.replace(/^\d+\s+\|(?!\|)/gm, '');
201+
return content.replace(/^\s*\d+\s+\|(?!\|)/gm, '');
202202
};
203203

204204
searchContent = stripLineNumbers(searchContent);

0 commit comments

Comments
 (0)