Merge pull request #136 from RooVetGit/fix_line_number_stripping

Bugfix to strip line numbers with leading space
This commit is contained in:
Matt Rubens
2024-12-16 17:49:19 -05:00
committed by GitHub
2 changed files with 22 additions and 2 deletions

View File

@@ -591,6 +591,26 @@ this.init();
expect(result.content).toBe('function test() {\n return false;\n}\n') 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', () => { it('should not strip when not all lines have numbers in either section', () => {
const originalContent = 'function test() {\n return true;\n}\n' const originalContent = 'function test() {\n return true;\n}\n'

View File

@@ -193,12 +193,12 @@ Result:
// Strip line numbers from search and replace content if every line starts with a line number // Strip line numbers from search and replace content if every line starts with a line number
const hasLineNumbers = (content: string) => { const hasLineNumbers = (content: string) => {
const lines = content.split(/\r?\n/); 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)) { if (hasLineNumbers(searchContent) && hasLineNumbers(replaceContent)) {
const stripLineNumbers = (content: string) => { const stripLineNumbers = (content: string) => {
return content.replace(/^\d+\s+\|(?!\|)/gm, ''); return content.replace(/^\s*\d+\s+\|(?!\|)/gm, '');
}; };
searchContent = stripLineNumbers(searchContent); searchContent = stripLineNumbers(searchContent);