| 1 | /** |
| 2 | * Represents a basic match result with start and end character positions |
| 3 | */ |
| 4 | interface BasicMatchResult { |
| 5 | /** The starting character index of the match in the file content */ |
| 6 | startIndex: number; |
| 7 | /** The ending character index of the match in the file content */ |
| 8 | endIndex: number; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Represents a match result with start and end character positions |
| 13 | */ |
| 14 | export interface SearchMatchResult extends BasicMatchResult { |
| 15 | /** The name of the strategy that successfully matched */ |
| 16 | strategyName: string; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Strategy function type for finding matches |
| 21 | */ |
| 22 | type MatchStrategy = ( |
| 23 | fileContent: string, |
| 24 | searchContent: string, |
| 25 | ) => BasicMatchResult | null; |
| 26 | |
| 27 | /** |
| 28 | * Exact string matching strategy |
| 29 | */ |
| 30 | function exactMatch( |
| 31 | fileContent: string, |
| 32 | searchContent: string, |
| 33 | ): BasicMatchResult | null { |
| 34 | const exactIndex = fileContent.indexOf(searchContent); |
| 35 | if (exactIndex !== -1) { |
| 36 | return { |
| 37 | startIndex: exactIndex, |
| 38 | endIndex: exactIndex + searchContent.length, |
| 39 | }; |
| 40 | } |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Trimmed content matching strategy |
| 46 | */ |
| 47 | function trimmedMatch( |
| 48 | fileContent: string, |
| 49 | searchContent: string, |
| 50 | ): BasicMatchResult | null { |
| 51 | const trimmedSearchContent = searchContent.trim(); |
| 52 | const trimmedIndex = fileContent.indexOf(trimmedSearchContent); |
| 53 | if (trimmedIndex !== -1) { |
| 54 | return { |
| 55 | startIndex: trimmedIndex, |
| 56 | endIndex: trimmedIndex + trimmedSearchContent.length, |
| 57 | }; |
| 58 | } |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Whitespace-ignored matching strategy |
| 64 | * Removes all whitespace from both content and search, then finds the match |
| 65 | */ |
| 66 | function whitespaceIgnoredMatch( |
| 67 | fileContent: string, |
| 68 | searchContent: string, |
| 69 | ): BasicMatchResult | null { |
| 70 | // Remove all whitespace (spaces, tabs, newlines, etc.) |
| 71 | const strippedFileContent = fileContent.replace(/\s/g, ""); |
| 72 | const strippedSearchContent = searchContent.replace(/\s/g, ""); |
| 73 | |
| 74 | if (strippedSearchContent === "") { |
| 75 | return null; // Empty search after stripping whitespace |
| 76 | } |
| 77 | |
| 78 | const strippedIndex = strippedFileContent.indexOf(strippedSearchContent); |
| 79 | if (strippedIndex === -1) { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | // Map the stripped position back to the original file content |
| 84 | let originalStartIndex = 0; |
| 85 | let strippedCharCount = 0; |
| 86 | |
| 87 | // Find the original position by counting non-whitespace characters |
| 88 | for (let i = 0; i < fileContent.length; i++) { |
| 89 | if (strippedCharCount === strippedIndex) { |
| 90 | originalStartIndex = i; |
| 91 | break; |
| 92 | } |
| 93 | if (!/\s/.test(fileContent[i])) { |
| 94 | strippedCharCount++; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Find the end position by counting the length of the search content |
| 99 | let originalEndIndex = originalStartIndex; |
| 100 | let matchedChars = 0; |
| 101 | |
| 102 | for ( |
| 103 | let i = originalStartIndex; |
| 104 | i < fileContent.length && matchedChars < strippedSearchContent.length; |
| 105 | i++ |
| 106 | ) { |
| 107 | if (!/\s/.test(fileContent[i])) { |
| 108 | matchedChars++; |
| 109 | } |
| 110 | originalEndIndex = i + 1; |
| 111 | } |
| 112 | |
| 113 | return { |
| 114 | startIndex: originalStartIndex, |
| 115 | endIndex: originalEndIndex, |
| 116 | }; |
| 117 | } |
| 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 | |
| 274 | /** |
| 275 | * Ordered list of matching strategies to try with their names |
| 276 | */ |
| 277 | const matchingStrategies: Array<{ strategy: MatchStrategy; name: string }> = [ |
| 278 | { strategy: exactMatch, name: "exactMatch" }, |
| 279 | { strategy: trimmedMatch, name: "trimmedMatch" }, |
| 280 | { strategy: whitespaceIgnoredMatch, name: "whitespaceIgnoredMatch" }, |
| 281 | { strategy: findFuzzyMatch, name: "jaroWinklerFuzzyMatch" }, |
| 282 | ]; |
| 283 | |
| 284 | /** |
| 285 | * Find the exact match position for search content in file content. |
| 286 | * Uses multiple matching strategies in order of preference. |
| 287 | * |
| 288 | * Matching Strategy: |
| 289 | * 1. If search content is empty, matches at the beginning of file (position 0) |
| 290 | * 2. Try each matching strategy in order until one succeeds |
| 291 | * |
| 292 | * @param fileContent - The complete content of the file to search in |
| 293 | * @param searchContent - The content to search for |
| 294 | * @param config - Configuration options for matching behavior |
| 295 | * @returns Match result with character positions, or null if no match found |
| 296 | */ |
| 297 | export function findSearchMatch( |
| 298 | fileContent: string, |
| 299 | searchContent: string, |
| 300 | ): SearchMatchResult | null { |
| 301 | const trimmedSearchContent = searchContent.trim(); |
| 302 | |
| 303 | if (trimmedSearchContent === "") { |
| 304 | // Empty search content matches the beginning of the file |
| 305 | return { startIndex: 0, endIndex: 0, strategyName: "emptySearch" }; |
| 306 | } |
| 307 | |
| 308 | // Try each matching strategy in order |
| 309 | for (const { strategy, name } of matchingStrategies) { |
| 310 | const result = strategy(fileContent, searchContent); |
| 311 | if (result !== null) { |
| 312 | return { ...result, strategyName: name }; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return null; |
| 317 | } |