Expand description
Insert-mode completion – the editor surface that turns the existing pipeline (cmdline today) into a buffer-level input flow.
Behavioural spec lives in
docs/dev/architecture/insert-completion.md.
This module is the data-flow layer: state types, trigger
enum, sync source trait, fuzzy matcher tuned for code
completion, and the per-buffer aggregator that holds it all
together. The host (lattice-ui-tui) owns the async glue
(LSP request fan-out, tokio spawn, the popup widget); this
crate stays sync + pure-data so plugins can target it
without pulling in tokio.
§Two kinds of source
- Sync (
InsertSource). Buffer-words, snippets, path, tree-sitter – everything that runs in microseconds. The aggregator callsproduce()directly when the popup query changes. - Async (host-orchestrated). LSP, plugin generators,
anything that round-trips. The host spawns a tokio task
that pushes
RawCandidates through a channel intoInsertCompletionState::raw; the aggregator coalesces pushes on a 16 ms tick and re-runs matcher / ranker.
The split keeps this crate dependency-light. Hosts add the async dance; we don’t.
Structs§
- Buffer
Words Source - Sync source emitting word-completions from a buffer’s text.
Cheap enough to walk the rope once per query change. Words
shorter than
min_word_lengthare skipped; duplicates are deduped. - DocPopup
State - One side popup showing the focused candidate’s full
documentation. Lazy: not opened until
<C-d>/cmd:completion-toggle-docsflips it on. - Fuzzy
Insert Matcher - Built-in fuzzy matcher tuned for Insert-mode completion.
- Insert
Completion State - Live state for an in-flight Insert-mode completion. Held by
the host (
App.insert_completion: Option<…>) while the popup is up; dropped on dismiss. Per-source channels and cancellation tokens live host-side – they pull in tokio types this crate avoids – so this struct stays dependency-light enough that any host (TUI today, GPU later) can reuse it. - Insert
Context - Snapshot of editor state a source reads when producing
candidates. Held by reference so the aggregator borrows from
the surrounding frame for the duration of
produce(). - Insert
Ranker - Built-in ranker tuned for Insert-mode completion. Sorts by
final_scoredescending, where: - PerLanguage
Overrides - Per-language overrides for the insert-completion popup. Each
field is
Optionso a TOML override at[completion.per-language.<lang>]can flip exactly the keys it cares about; unset fields fall back to the global typed option (or, forsources, “every enabled source contributes”). - Source
Id - Stable identifier for a source. Strings keep the registry
transparent (
"gen:lsp-completion","gen:buffer-words","plugin:foo"); the host’s per-source priority / enable config keys off this string so users see the same name in:set completion.source.<id>.priority=…and in:help completion-sources.
Enums§
- Completion
Trigger - What opened the popup. Stays constant for the popup’s
lifetime; rides on LSP completion requests as
CompletionTriggerKind.
Constants§
- LSP_
COMPLETION_ SOURCE_ ID - Source id for the host-orchestrated LSP completion source.
- PATH_
SOURCE_ ID - Source id for the host-orchestrated path-completion source (Phase 4.2.g.6 (2/2)). Triggered when the cursor sits inside a string literal (per tree-sitter scope detection); walks the directory of the partial path and emits filesystem entries.
- SNIPPET_
SOURCE_ ID - Source id for the host-orchestrated snippet completion source.
- TREE_
SITTER_ SYMBOL_ SOURCE_ ID - Source id for the host-orchestrated tree-sitter local-symbol
source (Phase 4.2.g.6 (1/2)). Walks the buffer’s syntax tree
per popup-trigger via
lattice_syntax::Syntax::collect_symbols.
Traits§
- Insert
Source - A sync candidate source. Implementations are cheap to call
– the aggregator invokes
produce()once per query change (a re-filter on each keystroke). Async sources don’t implement this trait; they are orchestrated host-side viatokio::spawn+ a channel that pushes intoInsertCompletionState.rawdirectly.
Functions§
- canonical_
source_ id - Map a user-friendly source label (
"lsp","snippet","buffer-words") to its canonical source id. Unknown labels pass through as-is, so plugin sources can be referenced by their full id ("plugin:my-source"). - fuzzy_
match - Free-function fuzzy match – the algorithm beneath
FuzzyInsertMatcher. Exposed so surfaces that need to match on a string other thanRawCandidate.text(e.g. the vertico picker, which matches on the user-visibledisplaywhiletextcarries a routing payload) get the same 5-tier scoring without duplicating the algorithm. - per_
language_ defaults - Spec-driven defaults shipping with v1
(
docs/dev/architecture/insert-completion.md§9). Markdown / text restrict to snippet + buffer-words (no LSP for prose); rust enables auto-fire + auto-insert-single since rust-analyzer’s items are precise.