11import { DiffStrategy , DiffResult } from "../types"
22import { addLineNumbers , everyLineHasLineNumbers , stripLineNumbers } from "../../../integrations/misc/extract-text"
3+ import { distance } from "fastest-levenshtein"
34
45const BUFFER_LINES = 20 // Number of extra context lines to show before and after matches
56
6- function levenshteinDistance ( a : string , b : string ) : number {
7- const matrix : number [ ] [ ] = [ ]
8-
9- // Initialize matrix
10- for ( let i = 0 ; i <= a . length ; i ++ ) {
11- matrix [ i ] = [ i ]
12- }
13- for ( let j = 0 ; j <= b . length ; j ++ ) {
14- matrix [ 0 ] [ j ] = j
15- }
16-
17- // Fill matrix
18- for ( let i = 1 ; i <= a . length ; i ++ ) {
19- for ( let j = 1 ; j <= b . length ; j ++ ) {
20- if ( a [ i - 1 ] === b [ j - 1 ] ) {
21- matrix [ i ] [ j ] = matrix [ i - 1 ] [ j - 1 ]
22- } else {
23- matrix [ i ] [ j ] = Math . min (
24- matrix [ i - 1 ] [ j - 1 ] + 1 , // substitution
25- matrix [ i ] [ j - 1 ] + 1 , // insertion
26- matrix [ i - 1 ] [ j ] + 1 , // deletion
27- )
28- }
29- }
30- }
31-
32- return matrix [ a . length ] [ b . length ]
33- }
34-
357function getSimilarity ( original : string , search : string ) : number {
368 if ( search === "" ) {
379 return 1
@@ -47,12 +19,12 @@ function getSimilarity(original: string, search: string): number {
4719 return 1
4820 }
4921
50- // Calculate Levenshtein distance
51- const distance = levenshteinDistance ( normalizedOriginal , normalizedSearch )
22+ // Calculate Levenshtein distance using fastest-levenshtein's distance function
23+ const dist = distance ( normalizedOriginal , normalizedSearch )
5224
53- // Calculate similarity ratio (0 to 1, where 1 is exact match)
25+ // Calculate similarity ratio (0 to 1, where 1 is an exact match)
5426 const maxLength = Math . max ( normalizedOriginal . length , normalizedSearch . length )
55- return 1 - distance / maxLength
27+ return 1 - dist / maxLength
5628}
5729
5830export class SearchReplaceDiffStrategy implements DiffStrategy {
0 commit comments