Changesets · Branches · Tags · Compare
| Changeset | 5af0242772151593c23e4a307e097dc5cb52bcc3cddf5a131c72f5b367190781 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-08-19 10:47 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-08-19 10:47 -07:00 |
| Parents | 9bb5e7aa9f0c |
3 files changed
modified core/edit/searchAndReplace/findSearchMatch.strategy.test.ts +0 −95
| @@ -426,101 +426,6 @@ | ||
| 426 | 426 | }); |
| 427 | 427 | }); |
| 428 | 428 | |
| 429 | - describe("jaroWinklerFuzzyMatch strategy", () => { | |
| 430 | - it("should match similar code with variable name differences", () => { | |
| 431 | - const fileContent = `function calculateTotal(items) { | |
| 432 | - let sum = 0; | |
| 433 | - for (const item of items) { | |
| 434 | - sum += item.price; | |
| 435 | - } | |
| 436 | - return sum; | |
| 437 | -}`; | |
| 438 | - const searchContent = `function calculateTotal(products) { | |
| 439 | - let total = 0; | |
| 440 | - for (const product of products) { | |
| 441 | - total += product.price; | |
| 442 | - } | |
| 443 | - return total; | |
| 444 | -}`; | |
| 445 | - | |
| 446 | - const result = findSearchMatch(fileContent, searchContent); | |
| 447 | - expect(result).not.toBeNull(); | |
| 448 | - expect(result!.strategyName).toBe("jaroWinklerFuzzyMatch"); | |
| 449 | - }); | |
| 450 | - | |
| 451 | - it("should match code with minor typos", () => { | |
| 452 | - const testCases = [ | |
| 453 | - { | |
| 454 | - name: "typo in function name", | |
| 455 | - fileContent: "function getUserName() { return this.name; }", | |
| 456 | - searchContent: "function getUserNaem() { return this.name; }", | |
| 457 | - }, | |
| 458 | - { | |
| 459 | - name: "typo in variable", | |
| 460 | - fileContent: "const message = 'Hello World';", | |
| 461 | - searchContent: "const mesage = 'Hello World';", | |
| 462 | - }, | |
| 463 | - { | |
| 464 | - name: "missing character", | |
| 465 | - fileContent: "console.log('Debug info');", | |
| 466 | - searchContent: "console.log('Debug nfo');", | |
| 467 | - }, | |
| 468 | - ]; | |
| 469 | - | |
| 470 | - testCases.forEach(({ name, fileContent, searchContent }) => { | |
| 471 | - const result = findSearchMatch(fileContent, searchContent); | |
| 472 | - expect(result).not.toBeNull(); | |
| 473 | - expect(result!.strategyName).toBe("jaroWinklerFuzzyMatch"); | |
| 474 | - }); | |
| 475 | - }); | |
| 476 | - | |
| 477 | - it("should not match when similarity is too low", () => { | |
| 478 | - const fileContent = "function add(a, b) { return a + b; }"; | |
| 479 | - const searchContent = | |
| 480 | - "class Calculator { multiply(x, y) { return x * y; } }"; | |
| 481 | - | |
| 482 | - const result = findSearchMatch(fileContent, searchContent); | |
| 483 | - expect(result).toBeNull(); | |
| 484 | - }); | |
| 485 | - | |
| 486 | - it("should handle multiline fuzzy matches", () => { | |
| 487 | - const fileContent = `if (userIsLoggedIn) { | |
| 488 | - showDashboard(); | |
| 489 | - loadUserData(); | |
| 490 | -}`; | |
| 491 | - const searchContent = `if (isUserLoggedIn) { | |
| 492 | - displayDashboard(); | |
| 493 | - fetchUserData(); | |
| 494 | -}`; | |
| 495 | - | |
| 496 | - const result = findSearchMatch(fileContent, searchContent); | |
| 497 | - expect(result).not.toBeNull(); | |
| 498 | - expect(result!.strategyName).toBe("jaroWinklerFuzzyMatch"); | |
| 499 | - }); | |
| 500 | - | |
| 501 | - it("should verify replacement with fuzzy matches", () => { | |
| 502 | - const fileContent = "const userName = 'John';"; | |
| 503 | - const searchContent = "const usrName = 'John';"; // Typo in variable name | |
| 504 | - const replacement = "const userName = 'Jane';"; | |
| 505 | - | |
| 506 | - const match = findSearchMatch(fileContent, searchContent); | |
| 507 | - expect(match).not.toBeNull(); | |
| 508 | - expect(match!.strategyName).toBe("jaroWinklerFuzzyMatch"); | |
| 509 | - | |
| 510 | - const result = performReplacement(fileContent, match!, replacement); | |
| 511 | - expect(result).toBe(replacement); | |
| 512 | - }); | |
| 513 | - | |
| 514 | - it("should handle edge case with very short strings", () => { | |
| 515 | - const fileContent = "ab"; | |
| 516 | - const searchContent = "ac"; | |
| 517 | - | |
| 518 | - const result = findSearchMatch(fileContent, searchContent); | |
| 519 | - // Should not match due to low similarity on short strings | |
| 520 | - expect(result).toBeNull(); | |
| 521 | - }); | |
| 522 | - }); | |
| 523 | - | |
| 524 | 429 | describe("Empty search content", () => { |
| 525 | 430 | it("should always return position 0 for empty search", () => { |
| 526 | 431 | const testCases = [ |
modified core/edit/searchAndReplace/findSearchMatch.test.ts +0 −181
| @@ -191,34 +191,6 @@ | ||
| 191 | 191 | }); |
| 192 | 192 | }); |
| 193 | 193 | |
| 194 | -describe("No matches", () => { | |
| 195 | - it("should return null when search content is not found", () => { | |
| 196 | - const fileContent = `function hello() { | |
| 197 | - console.log("world"); | |
| 198 | -}`; | |
| 199 | - const searchContent = `function goodbye() { | |
| 200 | - console.log("world"); | |
| 201 | -}`; | |
| 202 | - | |
| 203 | - const result = findSearchMatch(fileContent, searchContent); | |
| 204 | - | |
| 205 | - expect(result?.strategyName).toEqual("jaroWinklerFuzzyMatch"); | |
| 206 | - }); | |
| 207 | - | |
| 208 | - it("should return only fuzzy result when trimmed search content is not found", () => { | |
| 209 | - const fileContent = `function hello() { | |
| 210 | - console.log("world"); | |
| 211 | -}`; | |
| 212 | - const searchContent = `\n\nfunction goodbye() { | |
| 213 | - console.log("world"); | |
| 214 | -}\n\n`; | |
| 215 | - | |
| 216 | - const result = findSearchMatch(fileContent, searchContent); | |
| 217 | - | |
| 218 | - expect(result?.strategyName).toEqual("jaroWinklerFuzzyMatch"); | |
| 219 | - }); | |
| 220 | -}); | |
| 221 | - | |
| 222 | 194 | describe("Edge cases", () => { |
| 223 | 195 | it("should handle empty file with non-empty search", () => { |
| 224 | 196 | const fileContent = ""; |
| @@ -612,157 +584,4 @@ | ||
| 612 | 584 | strategyName: "exactMatch", |
| 613 | 585 | }); |
| 614 | 586 | }); |
| 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 | - }); | |
| 768 | 587 | }); |
modified core/edit/searchAndReplace/findSearchMatch.ts +0 −156
| @@ -144,161 +144,6 @@ | ||
| 144 | 144 | }; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | -/** | |
| 148 | - * Calculate the Jaro similarity between two strings | |
| 149 | - */ | |
| 150 | -function jaroSimilarity(s1: string, s2: string): number { | |
| 151 | - if (s1 === s2) return 1.0; | |
| 152 | - if (s1.length === 0 || s2.length === 0) return 0.0; | |
| 153 | - | |
| 154 | - const matchDistance = Math.floor(Math.max(s1.length, s2.length) / 2) - 1; | |
| 155 | - if (matchDistance < 0) return 0.0; | |
| 156 | - | |
| 157 | - const s1Matches = new Array(s1.length).fill(false); | |
| 158 | - const s2Matches = new Array(s2.length).fill(false); | |
| 159 | - | |
| 160 | - let matches = 0; | |
| 161 | - let transpositions = 0; | |
| 162 | - | |
| 163 | - // Find matches | |
| 164 | - for (let i = 0; i < s1.length; i++) { | |
| 165 | - const start = Math.max(0, i - matchDistance); | |
| 166 | - const end = Math.min(i + matchDistance + 1, s2.length); | |
| 167 | - | |
| 168 | - for (let j = start; j < end; j++) { | |
| 169 | - if (s2Matches[j] || s1[i] !== s2[j]) continue; | |
| 170 | - s1Matches[i] = true; | |
| 171 | - s2Matches[j] = true; | |
| 172 | - matches++; | |
| 173 | - break; | |
| 174 | - } | |
| 175 | - } | |
| 176 | - | |
| 177 | - if (matches === 0) return 0.0; | |
| 178 | - | |
| 179 | - // Count transpositions | |
| 180 | - let k = 0; | |
| 181 | - for (let i = 0; i < s1.length; i++) { | |
| 182 | - if (!s1Matches[i]) continue; | |
| 183 | - while (!s2Matches[k]) k++; | |
| 184 | - if (s1[i] !== s2[k]) transpositions++; | |
| 185 | - k++; | |
| 186 | - } | |
| 187 | - | |
| 188 | - return ( | |
| 189 | - (matches / s1.length + | |
| 190 | - matches / s2.length + | |
| 191 | - (matches - transpositions / 2) / matches) / | |
| 192 | - 3.0 | |
| 193 | - ); | |
| 194 | -} | |
| 195 | - | |
| 196 | -/** | |
| 197 | - * Calculate the Jaro-Winkler similarity between two strings | |
| 198 | - */ | |
| 199 | -function jaroWinklerSimilarity( | |
| 200 | - s1: string, | |
| 201 | - s2: string, | |
| 202 | - prefixScale = 0.1, | |
| 203 | -): number { | |
| 204 | - const jaroSim = jaroSimilarity(s1, s2); | |
| 205 | - | |
| 206 | - if (jaroSim < 0.7) return jaroSim; | |
| 207 | - | |
| 208 | - // Calculate common prefix length (up to 4 characters) | |
| 209 | - let prefixLength = 0; | |
| 210 | - const maxPrefix = Math.min(4, Math.min(s1.length, s2.length)); | |
| 211 | - | |
| 212 | - for (let i = 0; i < maxPrefix; i++) { | |
| 213 | - if (s1[i] === s2[i]) { | |
| 214 | - prefixLength++; | |
| 215 | - } else { | |
| 216 | - break; | |
| 217 | - } | |
| 218 | - } | |
| 219 | - | |
| 220 | - return jaroSim + prefixLength * prefixScale * (1 - jaroSim); | |
| 221 | -} | |
| 222 | - | |
| 223 | -/** | |
| 224 | - * Find the best fuzzy match for search content in file content using Jaro-Winkler | |
| 225 | - */ | |
| 226 | -function findFuzzyMatch( | |
| 227 | - fileContent: string, | |
| 228 | - searchContent: string, | |
| 229 | - threshold: number = 0.9, | |
| 230 | -): BasicMatchResult | null { | |
| 231 | - const searchLines = searchContent.split("\n"); | |
| 232 | - const fileLines = fileContent.split("\n"); | |
| 233 | - | |
| 234 | - let bestMatch: BasicMatchResult | null = null; | |
| 235 | - let bestSimilarity = 0; | |
| 236 | - | |
| 237 | - // Try matching the search content as a whole block | |
| 238 | - const searchBlock = searchContent.trim(); | |
| 239 | - if (searchBlock.length > 5) { | |
| 240 | - // Require minimum length for meaningful matches | |
| 241 | - // Use sliding window approach for multi-line search | |
| 242 | - for (let i = 0; i <= fileLines.length - searchLines.length; i++) { | |
| 243 | - const candidateLines = fileLines.slice(i, i + searchLines.length); | |
| 244 | - const candidateBlock = candidateLines.join("\n").trim(); | |
| 245 | - | |
| 246 | - if (candidateBlock.length < 5) continue; // Skip very short blocks | |
| 247 | - | |
| 248 | - const similarity = jaroWinklerSimilarity(searchBlock, candidateBlock); | |
| 249 | - | |
| 250 | - if (similarity >= threshold && similarity > bestSimilarity) { | |
| 251 | - // Calculate character positions | |
| 252 | - const linesBeforeMatch = fileLines.slice(0, i); | |
| 253 | - const startIndex = | |
| 254 | - linesBeforeMatch.join("\n").length + | |
| 255 | - (linesBeforeMatch.length > 0 ? 1 : 0); | |
| 256 | - const endIndex = startIndex + candidateBlock.length; | |
| 257 | - | |
| 258 | - bestMatch = { | |
| 259 | - startIndex, | |
| 260 | - endIndex, | |
| 261 | - }; | |
| 262 | - bestSimilarity = similarity; | |
| 263 | - } | |
| 264 | - } | |
| 265 | - } | |
| 266 | - | |
| 267 | - // Also try line-by-line matching for better granularity | |
| 268 | - for ( | |
| 269 | - let searchLineIdx = 0; | |
| 270 | - searchLineIdx < searchLines.length; | |
| 271 | - searchLineIdx++ | |
| 272 | - ) { | |
| 273 | - const searchLine = searchLines[searchLineIdx].trim(); | |
| 274 | - if (searchLine.length === 0 || searchLine.length < 3) continue; // Skip very short lines | |
| 275 | - | |
| 276 | - for (let fileLineIdx = 0; fileLineIdx < fileLines.length; fileLineIdx++) { | |
| 277 | - const fileLine = fileLines[fileLineIdx].trim(); | |
| 278 | - if (fileLine.length === 0 || fileLine.length < 3) continue; // Skip very short lines | |
| 279 | - | |
| 280 | - const similarity = jaroWinklerSimilarity(searchLine, fileLine); | |
| 281 | - | |
| 282 | - if (similarity >= threshold && similarity > bestSimilarity) { | |
| 283 | - // Calculate character positions for the line | |
| 284 | - const linesBeforeMatch = fileLines.slice(0, fileLineIdx); | |
| 285 | - const startIndex = | |
| 286 | - linesBeforeMatch.join("\n").length + | |
| 287 | - (linesBeforeMatch.length > 0 ? 1 : 0); | |
| 288 | - const endIndex = startIndex + fileLines[fileLineIdx].length; | |
| 289 | - | |
| 290 | - bestMatch = { | |
| 291 | - startIndex, | |
| 292 | - endIndex, | |
| 293 | - }; | |
| 294 | - bestSimilarity = similarity; | |
| 295 | - } | |
| 296 | - } | |
| 297 | - } | |
| 298 | - | |
| 299 | - return bestMatch; | |
| 300 | -} | |
| 301 | - | |
| 302 | 147 | /** |
| 303 | 148 | * Ordered list of matching strategies to try with their names |
| 304 | 149 | */ |
| @@ -306,7 +151,6 @@ | ||
| 306 | 151 | { strategy: exactMatch, name: "exactMatch" }, |
| 307 | 152 | { strategy: trimmedMatch, name: "trimmedMatch" }, |
| 308 | 153 | { strategy: whitespaceIgnoredMatch, name: "whitespaceIgnoredMatch" }, |
| 309 | - { strategy: findFuzzyMatch, name: "jaroWinklerFuzzyMatch" }, | |
| 310 | 154 | ]; |
| 311 | 155 | |
| 312 | 156 | /** |