pub struct Buffer { /* private fields */ }Expand description
A bounded record of recently typed text in the focused element.
Carries a caret byte offset into text. Char/Backspace operate
at the caret; MoveLeft/MoveRight slide the caret without changing
the text. last_word / last_sentence extract from the text
behind the caret, so navigating left into already-typed text and
hitting the correction chord still operates on the right region.
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a buffer holding at most capacity characters (at least 1).
Sourcepub fn text_before_caret(&self) -> &str
pub fn text_before_caret(&self) -> &str
The buffered text up to the caret — the part the daemon treats as “what sits behind the cursor right now.”
Sourcepub fn sentence_at_caret(&self) -> Option<SentenceAtCaret>
pub fn sentence_at_caret(&self) -> Option<SentenceAtCaret>
The last sentence in the buffer with the whitespace that
follows it, or None when the buffer holds no sentence
(empty / only whitespace).
“Sentence” means the run of text bounded by sentence-enders
(./!/?). The buffer’s final sentence-ender, if any, is
included — so pressing the chord right after typing
"The quick brown fox." operates on "The quick brown fox."
rather than no-opping. If the buffer doesn’t end with an
ender the sentence is the in-progress text after the previous
one.
Sourcepub fn sentence_containing(&self, byte_offset: usize) -> Option<Sentence>
pub fn sentence_containing(&self, byte_offset: usize) -> Option<Sentence>
The sentence in the buffer that contains byte_offset, plus
the sentence’s byte range within self.text — caller can
subtract buffer_byte_start from any in-buffer byte position
to land in sentence-relative bytes.
Same “split on .!?, trim whitespace, prefer the later range
at a boundary” semantics as Self::sentence_at_caret; that
method projects this result onto the caret.
Sourcepub fn word_at_caret(&self) -> Option<WordAtCaret>
pub fn word_at_caret(&self) -> Option<WordAtCaret>
The word at (or immediately left of) the caret. Decides by looking at the char immediately BEFORE the caret:
- If it’s a word char, the caret is in / at the end of a word; expand both directions.
- If it’s whitespace (or the caret is at position 0), pick the previous word — matches the “fix the word your cursor just passed” mental model.
Returns None when there’s no word to operate on.
Sourcepub fn apply_around_caret(
&mut self,
backspaces: usize,
deletes: usize,
insert: &str,
)
pub fn apply_around_caret( &mut self, backspaces: usize, deletes: usize, insert: &str, )
Mirror an external edit that happens AROUND the caret: delete
backspaces characters going LEFT and deletes characters
going RIGHT, then insert at the caret. Called after the
emulation layer fires BackSpace × N + Delete × M + the
replacement text.
Sourcepub fn apply(&mut self, backspaces: usize, insert: &str)
pub fn apply(&mut self, backspaces: usize, insert: &str)
Mirror an end-of-caret edit (no right-side deletes). Shim
over [apply_around_caret] so end-of-text call-sites stay
readable.
Sourcepub fn caret(&self) -> usize
pub fn caret(&self) -> usize
Current caret position as a byte offset into Self::text.
Used by the nearby-word fallback to compute the signed
distance from the caret to a candidate replacement target.
Sourcepub fn apply_at_word(
&mut self,
byte_start: usize,
byte_end: usize,
insert: &str,
)
pub fn apply_at_word( &mut self, byte_start: usize, byte_end: usize, insert: &str, )
Mirror an edit that happens at an arbitrary byte range —
byte_start..byte_end is replaced with insert, and the
caret lands at byte_start + insert.len(). Used by the
nearby-word fallback when the picked word_at_caret turns
out to be fine but a typo a few words away is what the
user actually wanted to fix.
Sourcepub fn words_near_caret(&self) -> Vec<NearbyWord>
pub fn words_near_caret(&self) -> Vec<NearbyWord>
Enumerate every word in the buffer, ordered by char distance
from the caret (nearest first). The corrector uses this as
a fallback when word_at_caret’s primary pick comes back
“fine” — buffer caret can drift a few chars from the
visible cursor on long auto-repeats or mouse clicks, so
scanning a small window around the caret recovers the
real typo the user meant to fix.