Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(palette): pure fuzzy ranker for quick-open file search

enes7ab699d22b9a
diff --git a/src/renderer/lib/quickOpenSearch.test.ts b/src/renderer/lib/quickOpenSearch.test.ts
new file mode 100644
index 000000000..8d92028b6
--- /dev/null
+++ b/src/renderer/lib/quickOpenSearch.test.ts
@@ -0,0 +1,30 @@
+import { describe, it, expect } from 'vitest'
+import { prepareQuickOpenFiles, rankQuickOpenFiles } from './quickOpenSearch'
+
+const files = prepareQuickOpenFiles([
+ 'src/main/index.ts',
+ 'src/renderer/components/CommandPalette.tsx',
+ 'dist/nodeterm-0.2.0-arm64.dmg',
+ 'README.md'
+])
+
+describe('rankQuickOpenFiles', () => {
+ it('empty query returns first N in input order', () => {
+ const r = rankQuickOpenFiles('', files, 2)
+ expect(r.map((x) => x.path)).toEqual(['src/main/index.ts', 'src/renderer/components/CommandPalette.tsx'])
+ })
+ it('finds a git-ignored artifact by name fragment', () => {
+ const r = rankQuickOpenFiles('dmg', files)
+ expect(r[0].path).toBe('dist/nodeterm-0.2.0-arm64.dmg')
+ })
+ it('a filename hit outranks a path-only subsequence hit', () => {
+ const r = rankQuickOpenFiles('index', files)
+ expect(r[0].path).toBe('src/main/index.ts')
+ })
+ it('returns nothing when the subsequence is absent', () => {
+ expect(rankQuickOpenFiles('zzzz', files)).toEqual([])
+ })
+ it('respects the limit', () => {
+ expect(rankQuickOpenFiles('s', files, 1).length).toBe(1)
+ })
+})
diff --git a/src/renderer/lib/quickOpenSearch.ts b/src/renderer/lib/quickOpenSearch.ts
new file mode 100644
index 000000000..c89358f14
--- /dev/null
+++ b/src/renderer/lib/quickOpenSearch.ts
@@ -0,0 +1,90 @@
+/** Pure fuzzy ranking for Quick Open (⌘K file search). Port of REF's quick-open-search.ts. */
+export const QUICK_OPEN_RESULT_LIMIT = 50
+
+export type QuickOpenIndexedFile = {
+ path: string
+ lowerPath: string
+ lowerFilename: string
+ inputIndex: number
+}
+
+export type QuickOpenSearchResult = {
+ path: string
+ score: number
+}
+
+export function prepareQuickOpenFiles(files: readonly string[]): QuickOpenIndexedFile[] {
+ return files.map((path, inputIndex) => {
+ const searchPath = path.replace(/\\/g, '/')
+ const lastSlash = searchPath.lastIndexOf('/')
+ return {
+ path,
+ lowerPath: searchPath.toLowerCase(),
+ lowerFilename: searchPath.slice(lastSlash + 1).toLowerCase(),
+ inputIndex
+ }
+ })
+}
+
+type QuickOpenRankedResult = QuickOpenSearchResult & { inputIndex: number }
+
+export function rankQuickOpenFiles(
+ query: string,
+ files: readonly QuickOpenIndexedFile[],
+ limit = QUICK_OPEN_RESULT_LIMIT
+): QuickOpenSearchResult[] {
+ if (limit <= 0) return []
+ const normalizedQuery = query.trim().replace(/\\/g, '/').toLowerCase()
+ if (!normalizedQuery) {
+ return files.slice(0, limit).map((file) => ({ path: file.path, score: 0 }))
+ }
+ const results: QuickOpenRankedResult[] = []
+ for (const file of files) {
+ const score = fuzzyMatchIndexedFile(normalizedQuery, file)
+ if (score === -1) continue
+ insertTopResult(results, { path: file.path, score, inputIndex: file.inputIndex }, limit)
+ }
+ return results.map(({ path, score }) => ({ path, score }))
+}
+
+// Lower score = better. Gaps between matched chars add; matching right after a /.- boundary
+// subtracts 5; a filename substring hit subtracts 100. -1 means "no subsequence match".
+function fuzzyMatchIndexedFile(query: string, file: QuickOpenIndexedFile): number {
+ let qi = 0
+ let score = 0
+ let lastMatchIdx = -1
+ for (let ti = 0; ti < file.lowerPath.length && qi < query.length; ti++) {
+ if (file.lowerPath[ti] === query[qi]) {
+ score += lastMatchIdx === -1 ? 0 : ti - lastMatchIdx - 1
+ const prev = file.lowerPath[ti - 1]
+ if (ti > 0 && (prev === '/' || prev === '.' || prev === '-')) score -= 5
+ lastMatchIdx = ti
+ qi++
+ }
+ }
+ if (qi < query.length) return -1
+ if (file.lowerFilename.includes(query)) score -= 100
+ return score
+}
+
+function compareRankedResult(a: QuickOpenRankedResult, b: QuickOpenRankedResult): number {
+ return a.score - b.score || a.inputIndex - b.inputIndex
+}
+
+function insertTopResult(
+ results: QuickOpenRankedResult[],
+ candidate: QuickOpenRankedResult,
+ limit: number
+): void {
+ const worst = results.at(-1)
+ if (results.length === limit && worst && compareRankedResult(candidate, worst) >= 0) return
+ let low = 0
+ let high = results.length
+ while (low < high) {
+ const mid = Math.floor((low + high) / 2)
+ if (compareRankedResult(candidate, results[mid]) < 0) high = mid
+ else low = mid + 1
+ }
+ results.splice(low, 0, candidate)
+ if (results.length > limit) results.pop()
+}