Higher is better; -1 means no match. Prefers basename prefix > basename > path > subsequence.
(target: string, query: string)
| 21 | |
| 22 | /** Higher is better; -1 means no match. Prefers basename prefix > basename > path > subsequence. */ |
| 23 | function fuzzyScore(target: string, query: string): number { |
| 24 | if (!query) return 1 |
| 25 | const t = target.toLowerCase() |
| 26 | const q = query.toLowerCase() |
| 27 | const base = (target.split("/").pop() ?? target).toLowerCase() |
| 28 | if (base.startsWith(q)) return 4000 - target.length |
| 29 | if (base.includes(q)) return 3000 - target.length |
| 30 | const idx = t.indexOf(q) |
| 31 | if (idx !== -1) return 2000 - idx - target.length |
| 32 | let qi = 0 |
| 33 | for (let i = 0; i < t.length && qi < q.length; i++) { |
| 34 | if (t[i] === q[qi]) qi++ |
| 35 | } |
| 36 | if (qi === q.length) return 1000 - target.length |
| 37 | return -1 |
| 38 | } |
| 39 | |
| 40 | /** The `@token` being typed at the cursor, if any. */ |
| 41 | function findAtToken(value: string, cursor: number): { query: string; start: number } | null { |