Files
archived-orca/src/shared/quick-open-separator-match.ts
T
Brennan Benson 5f314d05c3 @file mentions and Quick Open keep up with typing in large repositories (#34820)
* perf(quick-open): rescore only the previous matches while a file query is typed

Each keystroke in a native chat @file mention or Quick Open ranked the whole
workspace listing (up to 20,001 paths) on the UI thread and rebuilt a set of
every path. A path that matches a longer term always matches its prefix, so
when the new query only extends the old terms, score just the old matches;
cache the listing's membership set per listing. Results are identical.

* refactor(quick-open): keep separator-variant matching beside the other separator matching

* test: cover mention ranking reuse and inventory replacement

* test: type mention query props for rerenders
2026-10-28 12:55:08 -07:00

64 lines
2.1 KiB
TypeScript

/** Keeps alternative character positions without recursively retrying path prefixes. */
export function matchQuickOpenSeparatorAlternatives(
query: string,
path: string,
boundaries: ReadonlySet<number> | undefined
): number | null {
if (!boundaries?.size && !/[-_ ]/.test(path)) {
return null
}
let previous = new Float64Array(path.length + 1).fill(Infinity)
let current = new Float64Array(path.length + 1)
previous[0] = 0
for (let qi = 0; qi < query.length; qi++) {
current.fill(Infinity)
const separator = query[qi] === '-' || query[qi] === '_'
let best = Infinity
for (let ti = 0; ti < path.length; ti++) {
if (ti > 0) {
best = Math.min(best, previous[ti] - ti)
}
const actual = path[ti]
const matches = separator
? actual === '-' || actual === '_' || actual === ' '
: actual === query[qi]
if (matches) {
const prefix = Math.min(previous[0], best + ti)
const boundary = ti > 0 && '/.-_'.includes(path[ti - 1]) ? 5 : 0
current[ti + 1] = Math.min(current[ti + 1], prefix - boundary)
}
if (separator && ti > 0 && boundaries?.has(ti)) {
current[ti] = Math.min(current[ti], previous[ti] + 2)
}
}
;[previous, current] = [current, previous]
}
const score = previous.reduce((best, value) => Math.min(best, value), Infinity)
return Number.isFinite(score) ? score : null
}
/** Whether `filename` contains `query` with any of -, _ or space standing in for each separator. */
export function filenameContainsSeparatorVariant(filename: string, query: string): boolean {
for (let start = 0; start <= filename.length - query.length; start++) {
let offset = 0
while (offset < query.length) {
const expected = query[offset]
const actual = filename[start + offset]
if (
expected !== actual &&
!(
(expected === '-' || expected === '_') &&
(actual === '-' || actual === '_' || actual === ' ')
)
) {
break
}
offset++
}
if (offset === query.length) {
return true
}
}
return false
}