Skip to content

Commit aecf968

Browse files
committed
fix: improve diff matching by handling invisible whitespace
Before normalization: - Only collapsed multiple whitespace characters into a single space - Only trimmed leading/trailing whitespace - Treated different line endings (\r\n vs \n) as different - Treated tabs and spaces as different characters - Zero-width spaces and other invisible characters were preserved After normalization: - Standardizes all line endings to \n - Converts tabs to spaces for consistent comparison - Still collapses multiple whitespace into single space - Removes zero-width spaces and other invisible Unicode characters - Still trims leading/trailing whitespace This fix applies to both: - Single search-replace functionality (search-replace.ts) - Multi-block diff functionality (multi-search-replace.ts) Users will encounter fewer "No sufficiently similar match found" errors across all diff operations when the content is semantically the same but has minor formatting differences.
1 parent 70a5d2b commit aecf968

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ function getSimilarity(original: string, search: string): number {
1212
}
1313

1414
// Normalize strings by removing extra whitespace but preserve case
15-
const normalizeStr = (str: string) => str.replace(/\s+/g, " ").trim()
15+
const normalizeStr = (str: string) =>
16+
str
17+
.replace(/\r\n/g, "\n") // Standardize line endings
18+
.replace(/\t/g, " ") // Convert tabs to spaces
19+
.replace(/\s+/g, " ") // Replace all whitespace sequences with a single space
20+
.replace(/\u200B/g, "") // Remove zero-width spaces
21+
.trim() // Remove leading/trailing whitespace
1622

1723
const normalizedOriginal = normalizeStr(original)
1824
const normalizedSearch = normalizeStr(search)

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ function getSimilarity(original: string, search: string): number {
1010
}
1111

1212
// Normalize strings by removing extra whitespace but preserve case
13-
const normalizeStr = (str: string) => str.replace(/\s+/g, " ").trim()
13+
const normalizeStr = (str: string) =>
14+
str
15+
.replace(/\r\n/g, "\n") // Standardize line endings
16+
.replace(/\t/g, " ") // Convert tabs to spaces
17+
.replace(/\s+/g, " ") // Replace all whitespace sequences with a single space
18+
.replace(/\u200B/g, "") // Remove zero-width spaces
19+
.trim() // Remove leading/trailing whitespace
1420

1521
const normalizedOriginal = normalizeStr(original)
1622
const normalizedSearch = normalizeStr(search)

0 commit comments

Comments
 (0)