kimun_notes/components/autocomplete/host.rs
1use std::num::NonZeroU64;
2
3use crate::components::text_editor::snapshot::EditorSnapshot;
4
5/// Read-only view of the input surface (editor or search box) for the
6/// autocomplete controller. The controller derives byte offsets and
7/// the joined buffer text from the snapshot — neither needs to be
8/// pre-computed by the host.
9///
10/// The trait is intentionally read-only: the controller computes what
11/// to insert and returns an `AcceptAction` (see `controller`), and the
12/// host applies it. That split keeps borrow-checker contention out of
13/// the way when the controller is held as a field of the host itself.
14pub trait AutocompleteHost {
15 /// Atomic borrow of `(lines, cursor, content_revision)`. The
16 /// snapshot's lifetime ties to `&self`, so the editor's textarea
17 /// or the search-box buffer is borrowed without an intermediate
18 /// `Vec<String>` clone in the common case — perf #8.
19 fn buffer_snapshot(&self) -> EditorSnapshot<'_>;
20
21 /// Host opt-in to the controller's per-text-revision cache for
22 /// `(joined_text, ExclusionZones)`. Returning `None` makes every
23 /// reconcile rebuild both; use for hosts whose buffer is tiny
24 /// enough that the rebuild cost is negligible (e.g. the
25 /// single-line search box). Hosts that participate must bump
26 /// this value on every content change — sharing the editor's
27 /// `content_revision` is the easy way (see `EditorSnapshot`).
28 fn cache_key(&self) -> Option<NonZeroU64>;
29
30 /// Screen position adjacent to the trigger byte (the byte right
31 /// after `[[` or `#`). Returned as `(col, row)` cells. `None`
32 /// when the byte is currently off-screen; the controller hides
33 /// the popup in that case.
34 fn screen_anchor_for(&self, byte_offset: usize) -> Option<(u16, u16)>;
35}