* Performs a "middle-out" search of `lines` (between [startIndex, endIndex]) to find * the slice that is most similar to `searchChunk`. Returns the best score, index, and matched text.
(lines: string[], searchChunk: string, startIndex: number, endIndex: number)
| 35 | * the slice that is most similar to `searchChunk`. Returns the best score, index, and matched text. |
| 36 | */ |
| 37 | function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, endIndex: number) { |
| 38 | let bestScore = 0 |
| 39 | let bestMatchIndex = -1 |
| 40 | let bestMatchContent = "" |
| 41 | const searchLen = searchChunk.split(/\r?\n/).length |
| 42 | |
| 43 | // Middle-out from the midpoint |
| 44 | const midPoint = Math.floor((startIndex + endIndex) / 2) |
| 45 | let leftIndex = midPoint |
| 46 | let rightIndex = midPoint + 1 |
| 47 | |
| 48 | while (leftIndex >= startIndex || rightIndex <= endIndex - searchLen) { |
| 49 | if (leftIndex >= startIndex) { |
| 50 | const originalChunk = lines.slice(leftIndex, leftIndex + searchLen).join("\n") |
| 51 | const similarity = getSimilarity(originalChunk, searchChunk) |
| 52 | if (similarity > bestScore) { |
| 53 | bestScore = similarity |
| 54 | bestMatchIndex = leftIndex |
| 55 | bestMatchContent = originalChunk |
| 56 | } |
| 57 | leftIndex-- |
| 58 | } |
| 59 | |
| 60 | if (rightIndex <= endIndex - searchLen) { |
| 61 | const originalChunk = lines.slice(rightIndex, rightIndex + searchLen).join("\n") |
| 62 | const similarity = getSimilarity(originalChunk, searchChunk) |
| 63 | if (similarity > bestScore) { |
| 64 | bestScore = similarity |
| 65 | bestMatchIndex = rightIndex |
| 66 | bestMatchContent = originalChunk |
| 67 | } |
| 68 | rightIndex++ |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return { bestScore, bestMatchIndex, bestMatchContent } |
| 73 | } |
| 74 | |
| 75 | export class MultiSearchReplaceDiffStrategy implements DiffStrategy { |
| 76 | private fuzzyThreshold: number |
no test coverage detected