Skip to content

internals fuzzy matching

github-actions[bot] edited this page Dec 1, 2026 · 1 revision

Fuzzy matching internals

User guide: Overlays and pickers. File picker indexing, caching and ranking: File search internals.

Package role

internal/fuzzymatch is the only package that imports fzf (github.com/junegunn/fzf). The file picker (internal/filesearch) and every TUI picker with a search query score through it.

Symbol Role
Text, NewText, Text.RuneLen A candidate string prepared once for repeated matching
Pattern, NewPattern, Pattern.Empty, Pattern.Key A prepared query; Key returns the prepared runes as a string for cache keys
Scratch, GetScratch, PutScratch Pooled per-goroutine match scratch space; never shared between goroutines
Pattern.Score(t, s) Score without positions; does not allocate per call, so it is the hot path for file search workers
Pattern.Match(text) Score plus matched positions, for highlighting
Positions(text, query) Matched positions for a raw query; nil for an empty query or no match

Matching uses fzf FuzzyMatchV2, case-insensitive and normalized.

Scheme

The package calls algo.Init("path") once, guarded by the initOnce sync.Once, before the first match. The path scheme treats / as the delimiter, which fits file paths, and the same scheme applies to every picker. algo.Init mutates fzf package globals (delimiter classes and the bonus matrix), so the scheme is never switched at runtime: a switch would race with concurrent matching and change file search scores. No other package may initialize fzf.

Query preparation

NewPattern trims the query, caps it at 256 runes (maxQueryRunes), lowercases it and normalizes it with algo.NormalizeRunes. A blank query yields an empty Pattern. Positions returns nil for an empty pattern; Pattern.Match on an empty pattern matches with score 0 and no positions.

Positions

Pattern.Match and Positions return byte offsets into the candidate text, in ascending order. fzf reports rune indexes; byteOffsets converts them. renderMatchedText (internal/tui/fuzzy_match.go) highlights the runes at those offsets. When a row is truncated, clipMatchIndexes (internal/tui/search_picker.go) drops offsets at or beyond the kept text so the ellipsis is not highlighted.

TUI ranking

fuzzyRank (internal/tui/fuzzy_match.go) matches one Pattern against each entry's fields independently and returns a fuzzyHit per matching entry with the matched field index, score and positions. An empty or whitespace query returns all entries in input order with no positions.

  • Untiered: an entry keeps its best-scoring field. Hits sort by score descending, then lower field index, then input order. Used by the model, profile, plan, session, oneshot resume, accent and reasoning pickers.
  • Tiered: an entry keeps its first matching field. Any match on field 0 ranks above any match on field 1, and so on; within a field, score descending, then input order. Used by the slash overlay (fuzzyMatchSlashItems) with fields command, name and description; a leading / in the query is ignored.

Matched fields per picker:

Picker Fields
Model (fuzzyMatchModelEntries) Display, or Ref when Display is empty
Profile (fuzzyMatchProfileNames), accent Name
Plan (fuzzyMatchPlanNames) Base name only; positions are shifted onto the full path
Session Title
Oneshot resume Task, run ID, slug; only a task match is highlighted
Reasoning Label

The sub-agent picker (subAgentPickerRows in internal/tui/subagent_picker_rows.go) does not use fuzzyRank. It keeps a member when Pattern.Match matches its joined search text (type label, agent type, agent ID, group and task preview), and leaves sections, groups and order unchanged. It does not highlight matches.

Clone this wiki locally