Changesets · Branches · Tags · Compare
| Changeset | f4d5696d9d66f793d975e22b12b5ec191b3f1b9946af6a6afe58bcad63dc80bc |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-07-04 23:13 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-07-04 23:13 -07:00 |
| Parents | 778571452873 |
2 files changed
modified core/edit/searchAndReplace/findSearchMatch.test.ts +158 −31
| @@ -202,10 +202,10 @@ | ||
| 202 | 202 | |
| 203 | 203 | const result = findSearchMatch(fileContent, searchContent); |
| 204 | 204 | |
| 205 | - expect(result).toBeNull(); | |
| 205 | + expect(result?.strategyName).toEqual("jaroWinklerFuzzyMatch"); | |
| 206 | 206 | }); |
| 207 | 207 | |
| 208 | - it("should return null when trimmed search content is not found", () => { | |
| 208 | + it("should return only fuzzy result when trimmed search content is not found", () => { | |
| 209 | 209 | const fileContent = `function hello() { |
| 210 | 210 | console.log("world"); |
| 211 | 211 | }`; |
| @@ -215,31 +215,7 @@ | ||
| 215 | 215 | |
| 216 | 216 | const result = findSearchMatch(fileContent, searchContent); |
| 217 | 217 | |
| 218 | - expect(result).toBeNull(); | |
| 219 | - }); | |
| 220 | - | |
| 221 | - it("should return null for non-existent single line", () => { | |
| 222 | - const fileContent = `const a = 1; | |
| 223 | -const b = 2; | |
| 224 | -const c = 3;`; | |
| 225 | - const searchContent = `const d = 4;`; | |
| 226 | - | |
| 227 | - const result = findSearchMatch(fileContent, searchContent); | |
| 228 | - | |
| 229 | - expect(result).toBeNull(); | |
| 230 | - }); | |
| 231 | - | |
| 232 | - it("should return null for partial match", () => { | |
| 233 | - const fileContent = `function hello() { | |
| 234 | - console.log("world"); | |
| 235 | -}`; | |
| 236 | - const searchContent = `function hello() { | |
| 237 | - console.log("universe"); | |
| 238 | -}`; | |
| 239 | - | |
| 240 | - const result = findSearchMatch(fileContent, searchContent); | |
| 241 | - | |
| 242 | - expect(result).toBeNull(); | |
| 218 | + expect(result?.strategyName).toEqual("jaroWinklerFuzzyMatch"); | |
| 243 | 219 | }); |
| 244 | 220 | }); |
| 245 | 221 | |
| @@ -534,13 +510,11 @@ | ||
| 534 | 510 | }`); |
| 535 | 511 | }); |
| 536 | 512 | |
| 537 | - it("should return null when no whitespace-ignored match exists", () => { | |
| 513 | + it("should return null when no close match exists", () => { | |
| 538 | 514 | const fileContent = `function hello() { |
| 539 | 515 | console.log("world"); |
| 540 | 516 | }`; |
| 541 | - const searchContent = `function goodbye() { | |
| 542 | - console.log("universe"); | |
| 543 | -}`; | |
| 517 | + const searchContent = `this has nothing to do with the other option`; | |
| 544 | 518 | |
| 545 | 519 | const result = findSearchMatch(fileContent, searchContent); |
| 546 | 520 | |
| @@ -638,4 +612,157 @@ | ||
| 638 | 612 | strategyName: "exactMatch", |
| 639 | 613 | }); |
| 640 | 614 | }); |
| 615 | + | |
| 616 | + describe("Jaro-Winkler fuzzy matching", () => { | |
| 617 | + describe("Basic fuzzy matching", () => { | |
| 618 | + it("should find fuzzy match for similar strings", () => { | |
| 619 | + const fileContent = `function calculateSum(a, b) { | |
| 620 | + return a + b; | |
| 621 | +}`; | |
| 622 | + const searchContent = `function calculateSum(x, y) { | |
| 623 | + return x + y; | |
| 624 | +}`; | |
| 625 | + | |
| 626 | + const result = findSearchMatch(fileContent, searchContent); | |
| 627 | + | |
| 628 | + expect(result).not.toBeNull(); | |
| 629 | + }); | |
| 630 | + | |
| 631 | + it("should find fuzzy match with minor typos", () => { | |
| 632 | + const fileContent = `const message = "Hello World";`; | |
| 633 | + const searchContent = `const mesage = "Hello World";`; // Missing 's' in 'message' | |
| 634 | + | |
| 635 | + const result = findSearchMatch(fileContent, searchContent); | |
| 636 | + | |
| 637 | + expect(result).not.toBeNull(); | |
| 638 | + expect(result?.startIndex).toBe(0); | |
| 639 | + }); | |
| 640 | + | |
| 641 | + it("should find fuzzy match with different variable names", () => { | |
| 642 | + const fileContent = `let userAge = 25; | |
| 643 | +let userName = "John";`; | |
| 644 | + const searchContent = `let age = 25; | |
| 645 | +let name = "John";`; | |
| 646 | + | |
| 647 | + const result = findSearchMatch(fileContent, searchContent); | |
| 648 | + | |
| 649 | + expect(result).not.toBeNull(); | |
| 650 | + }); | |
| 651 | + | |
| 652 | + it("should find fuzzy match for similar function signature", () => { | |
| 653 | + const fileContent = `function processUserData(userData) { | |
| 654 | + validateInput(userData); | |
| 655 | + return formatOutput(userData); | |
| 656 | +}`; | |
| 657 | + const searchContent = `function processData(data) { | |
| 658 | + validateInput(data); | |
| 659 | + return formatOutput(data); | |
| 660 | +}`; | |
| 661 | + | |
| 662 | + const result = findSearchMatch(fileContent, searchContent); | |
| 663 | + | |
| 664 | + expect(result).not.toBeNull(); | |
| 665 | + }); | |
| 666 | + }); | |
| 667 | + | |
| 668 | + describe("Multi-line fuzzy matching", () => { | |
| 669 | + it("should find fuzzy match for multi-line blocks", () => { | |
| 670 | + const fileContent = `class Calculator { | |
| 671 | + constructor() { | |
| 672 | + this.result = 0; | |
| 673 | + } | |
| 674 | + | |
| 675 | + add(value) { | |
| 676 | + this.result += value; | |
| 677 | + return this; | |
| 678 | + } | |
| 679 | +}`; | |
| 680 | + const searchContent = `class Calculator { | |
| 681 | + constructor() { | |
| 682 | + this.value = 0; | |
| 683 | + } | |
| 684 | + | |
| 685 | + add(num) { | |
| 686 | + this.value += num; | |
| 687 | + return this; | |
| 688 | + } | |
| 689 | +}`; | |
| 690 | + | |
| 691 | + const result = findSearchMatch(fileContent, searchContent); | |
| 692 | + | |
| 693 | + expect(result).not.toBeNull(); | |
| 694 | + }); | |
| 695 | + | |
| 696 | + it("should find fuzzy match for partial blocks", () => { | |
| 697 | + const fileContent = `function processData(input) { | |
| 698 | + const validated = validateInput(input); | |
| 699 | + const processed = transformData(validated); | |
| 700 | + const result = formatOutput(processed); | |
| 701 | + return result; | |
| 702 | +}`; | |
| 703 | + const searchContent = `const validated = validateInput(input); | |
| 704 | + const processed = transformData(validated);`; | |
| 705 | + | |
| 706 | + const result = findSearchMatch(fileContent, searchContent); | |
| 707 | + | |
| 708 | + expect(result).not.toBeNull(); | |
| 709 | + }); | |
| 710 | + }); | |
| 711 | + | |
| 712 | + describe("Edge cases with fuzzy matching", () => { | |
| 713 | + it("should handle empty search content with fuzzy matching enabled", () => { | |
| 714 | + const fileContent = `function test() {}`; | |
| 715 | + const searchContent = ""; | |
| 716 | + | |
| 717 | + const result = findSearchMatch(fileContent, searchContent); | |
| 718 | + | |
| 719 | + expect(result).toEqual({ | |
| 720 | + startIndex: 0, | |
| 721 | + endIndex: 0, | |
| 722 | + strategyName: "emptySearch", | |
| 723 | + }); | |
| 724 | + }); | |
| 725 | + | |
| 726 | + it("should handle empty file content with fuzzy matching enabled", () => { | |
| 727 | + const fileContent = ""; | |
| 728 | + const searchContent = "function test() {}"; | |
| 729 | + | |
| 730 | + const result = findSearchMatch(fileContent, searchContent); | |
| 731 | + | |
| 732 | + expect(result).toBeNull(); | |
| 733 | + }); | |
| 734 | + | |
| 735 | + it("should prefer exact match over fuzzy match", () => { | |
| 736 | + const fileContent = `function test() { return true; } | |
| 737 | +function test() { return false; }`; | |
| 738 | + const searchContent = `function test() { return true; }`; | |
| 739 | + | |
| 740 | + const result = findSearchMatch(fileContent, searchContent); | |
| 741 | + | |
| 742 | + expect(result).not.toBeNull(); | |
| 743 | + expect(result?.startIndex).toBe(0); | |
| 744 | + }); | |
| 745 | + | |
| 746 | + it("should handle very short strings", () => { | |
| 747 | + const fileContent = `a b c`; | |
| 748 | + const searchContent = `a c`; | |
| 749 | + | |
| 750 | + const result = findSearchMatch(fileContent, searchContent); | |
| 751 | + | |
| 752 | + // Should not match due to low similarity | |
| 753 | + expect(result).toBeNull(); | |
| 754 | + }); | |
| 755 | + | |
| 756 | + it("should handle special characters in fuzzy matching", () => { | |
| 757 | + const fileContent = `const regex = /[a-zA-Z]+/g; | |
| 758 | +const symbols = !@#$%^&*();`; | |
| 759 | + const searchContent = `const regex = /[a-zA-Z0-9]+/g; | |
| 760 | +const symbols = !@#$%^&*();`; | |
| 761 | + | |
| 762 | + const result = findSearchMatch(fileContent, searchContent); | |
| 763 | + | |
| 764 | + expect(result).not.toBeNull(); | |
| 765 | + }); | |
| 766 | + }); | |
| 767 | + }); | |
| 641 | 768 | }); |
modified core/edit/searchAndReplace/findSearchMatch.ts +157 −0
| @@ -116,6 +116,161 @@ | ||
| 116 | 116 | }; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | +/** | |
| 120 | + * Calculate the Jaro similarity between two strings | |
| 121 | + */ | |
| 122 | +function jaroSimilarity(s1: string, s2: string): number { | |
| 123 | + if (s1 === s2) return 1.0; | |
| 124 | + if (s1.length === 0 || s2.length === 0) return 0.0; | |
| 125 | + | |
| 126 | + const matchDistance = Math.floor(Math.max(s1.length, s2.length) / 2) - 1; | |
| 127 | + if (matchDistance < 0) return 0.0; | |
| 128 | + | |
| 129 | + const s1Matches = new Array(s1.length).fill(false); | |
| 130 | + const s2Matches = new Array(s2.length).fill(false); | |
| 131 | + | |
| 132 | + let matches = 0; | |
| 133 | + let transpositions = 0; | |
| 134 | + | |
| 135 | + // Find matches | |
| 136 | + for (let i = 0; i < s1.length; i++) { | |
| 137 | + const start = Math.max(0, i - matchDistance); | |
| 138 | + const end = Math.min(i + matchDistance + 1, s2.length); | |
| 139 | + | |
| 140 | + for (let j = start; j < end; j++) { | |
| 141 | + if (s2Matches[j] || s1[i] !== s2[j]) continue; | |
| 142 | + s1Matches[i] = true; | |
| 143 | + s2Matches[j] = true; | |
| 144 | + matches++; | |
| 145 | + break; | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + if (matches === 0) return 0.0; | |
| 150 | + | |
| 151 | + // Count transpositions | |
| 152 | + let k = 0; | |
| 153 | + for (let i = 0; i < s1.length; i++) { | |
| 154 | + if (!s1Matches[i]) continue; | |
| 155 | + while (!s2Matches[k]) k++; | |
| 156 | + if (s1[i] !== s2[k]) transpositions++; | |
| 157 | + k++; | |
| 158 | + } | |
| 159 | + | |
| 160 | + return ( | |
| 161 | + (matches / s1.length + | |
| 162 | + matches / s2.length + | |
| 163 | + (matches - transpositions / 2) / matches) / | |
| 164 | + 3.0 | |
| 165 | + ); | |
| 166 | +} | |
| 167 | + | |
| 168 | +/** | |
| 169 | + * Calculate the Jaro-Winkler similarity between two strings | |
| 170 | + */ | |
| 171 | +function jaroWinklerSimilarity( | |
| 172 | + s1: string, | |
| 173 | + s2: string, | |
| 174 | + prefixScale = 0.1, | |
| 175 | +): number { | |
| 176 | + const jaroSim = jaroSimilarity(s1, s2); | |
| 177 | + | |
| 178 | + if (jaroSim < 0.7) return jaroSim; | |
| 179 | + | |
| 180 | + // Calculate common prefix length (up to 4 characters) | |
| 181 | + let prefixLength = 0; | |
| 182 | + const maxPrefix = Math.min(4, Math.min(s1.length, s2.length)); | |
| 183 | + | |
| 184 | + for (let i = 0; i < maxPrefix; i++) { | |
| 185 | + if (s1[i] === s2[i]) { | |
| 186 | + prefixLength++; | |
| 187 | + } else { | |
| 188 | + break; | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | + return jaroSim + prefixLength * prefixScale * (1 - jaroSim); | |
| 193 | +} | |
| 194 | + | |
| 195 | +/** | |
| 196 | + * Find the best fuzzy match for search content in file content using Jaro-Winkler | |
| 197 | + */ | |
| 198 | +function findFuzzyMatch( | |
| 199 | + fileContent: string, | |
| 200 | + searchContent: string, | |
| 201 | + threshold: number = 0.9, | |
| 202 | +): BasicMatchResult | null { | |
| 203 | + const searchLines = searchContent.split("\n"); | |
| 204 | + const fileLines = fileContent.split("\n"); | |
| 205 | + | |
| 206 | + let bestMatch: BasicMatchResult | null = null; | |
| 207 | + let bestSimilarity = 0; | |
| 208 | + | |
| 209 | + // Try matching the search content as a whole block | |
| 210 | + const searchBlock = searchContent.trim(); | |
| 211 | + if (searchBlock.length > 5) { | |
| 212 | + // Require minimum length for meaningful matches | |
| 213 | + // Use sliding window approach for multi-line search | |
| 214 | + for (let i = 0; i <= fileLines.length - searchLines.length; i++) { | |
| 215 | + const candidateLines = fileLines.slice(i, i + searchLines.length); | |
| 216 | + const candidateBlock = candidateLines.join("\n").trim(); | |
| 217 | + | |
| 218 | + if (candidateBlock.length < 5) continue; // Skip very short blocks | |
| 219 | + | |
| 220 | + const similarity = jaroWinklerSimilarity(searchBlock, candidateBlock); | |
| 221 | + | |
| 222 | + if (similarity >= threshold && similarity > bestSimilarity) { | |
| 223 | + // Calculate character positions | |
| 224 | + const linesBeforeMatch = fileLines.slice(0, i); | |
| 225 | + const startIndex = | |
| 226 | + linesBeforeMatch.join("\n").length + | |
| 227 | + (linesBeforeMatch.length > 0 ? 1 : 0); | |
| 228 | + const endIndex = startIndex + candidateBlock.length; | |
| 229 | + | |
| 230 | + bestMatch = { | |
| 231 | + startIndex, | |
| 232 | + endIndex, | |
| 233 | + }; | |
| 234 | + bestSimilarity = similarity; | |
| 235 | + } | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + // Also try line-by-line matching for better granularity | |
| 240 | + for ( | |
| 241 | + let searchLineIdx = 0; | |
| 242 | + searchLineIdx < searchLines.length; | |
| 243 | + searchLineIdx++ | |
| 244 | + ) { | |
| 245 | + const searchLine = searchLines[searchLineIdx].trim(); | |
| 246 | + if (searchLine.length === 0 || searchLine.length < 3) continue; // Skip very short lines | |
| 247 | + | |
| 248 | + for (let fileLineIdx = 0; fileLineIdx < fileLines.length; fileLineIdx++) { | |
| 249 | + const fileLine = fileLines[fileLineIdx].trim(); | |
| 250 | + if (fileLine.length === 0 || fileLine.length < 3) continue; // Skip very short lines | |
| 251 | + | |
| 252 | + const similarity = jaroWinklerSimilarity(searchLine, fileLine); | |
| 253 | + | |
| 254 | + if (similarity >= threshold && similarity > bestSimilarity) { | |
| 255 | + // Calculate character positions for the line | |
| 256 | + const linesBeforeMatch = fileLines.slice(0, fileLineIdx); | |
| 257 | + const startIndex = | |
| 258 | + linesBeforeMatch.join("\n").length + | |
| 259 | + (linesBeforeMatch.length > 0 ? 1 : 0); | |
| 260 | + const endIndex = startIndex + fileLines[fileLineIdx].length; | |
| 261 | + | |
| 262 | + bestMatch = { | |
| 263 | + startIndex, | |
| 264 | + endIndex, | |
| 265 | + }; | |
| 266 | + bestSimilarity = similarity; | |
| 267 | + } | |
| 268 | + } | |
| 269 | + } | |
| 270 | + | |
| 271 | + return bestMatch; | |
| 272 | +} | |
| 273 | + | |
| 119 | 274 | /** |
| 120 | 275 | * Ordered list of matching strategies to try with their names |
| 121 | 276 | */ |
| @@ -123,6 +278,7 @@ | ||
| 123 | 278 | { strategy: exactMatch, name: "exactMatch" }, |
| 124 | 279 | { strategy: trimmedMatch, name: "trimmedMatch" }, |
| 125 | 280 | { strategy: whitespaceIgnoredMatch, name: "whitespaceIgnoredMatch" }, |
| 281 | + { strategy: findFuzzyMatch, name: "jaroWinklerFuzzyMatch" }, | |
| 126 | 282 | ]; |
| 127 | 283 | |
| 128 | 284 | /** |
| @@ -135,6 +291,7 @@ | ||
| 135 | 291 | * |
| 136 | 292 | * @param fileContent - The complete content of the file to search in |
| 137 | 293 | * @param searchContent - The content to search for |
| 294 | + * @param config - Configuration options for matching behavior | |
| 138 | 295 | * @returns Match result with character positions, or null if no match found |
| 139 | 296 | */ |
| 140 | 297 | export function findSearchMatch( |