Skip to main content

Buffer

Struct Buffer 

Source
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

Source

pub fn with_capacity(capacity: usize) -> Self

Create a buffer holding at most capacity characters (at least 1).

Source

pub fn push(&mut self, key: Key)

Feed one unit of input to the buffer.

Source

pub fn clear(&mut self)

Clear the buffer.

Source

pub fn is_empty(&self) -> bool

true when the buffer holds no text.

Source

pub fn text(&self) -> &str

The full buffered text, oldest character first.

Source

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.”

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Buffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Buffer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.