| 1 | /** Scores `query` as a subsequence of `text`: consecutive characters and characters that start a word score extra. `null` when `text` does not hold it. */ |
| 2 | function subsequenceScore(text: |
| 3 | let score = |
| 4 | let at = |
| 5 | for (const char |
| 6 | const next = |
| 7 | if (next === |
| 8 | if (next === |
| 9 | if (next === |
| 10 | at = next; |
| 11 | } |
| 12 | return score; |
| 13 | } |
| 14 | |
| 15 | /** Paths that fuzzy-match `query`, case-insensitively, best first: a match inside the file name outranks one that needs the directories, then tighter matches, then shorter paths. */ |
| 16 | export function rankPaths(paths |
| 17 | const needle = |
| 18 | if (!needle) |
| 19 | |
| 20 | return paths |
| 21 | .flatMap((path) |
| 22 | const lower = |
| 23 | const inName = |
| 24 | lower.slice(lower |
| 25 | needle, |
| 26 | ); |
| 27 | const score = |
| 28 | inName === null ? |
| 29 | return score === |
| 30 | }) |
| 31 | .sort( |
| 32 | (a, b) |
| 33 | b.score - |
| 34 | a.path.length |
| 35 | a.path.localeCompare |
| 36 | ) |
| 37 | .slice(0, limit |
| 38 | .map(({ path |
| 39 | } |