Skip to main content

TextFieldBuffer

Struct TextFieldBuffer 

Source
pub struct TextFieldBuffer { /* private fields */ }
Expand description

A mutable text buffer that can be edited.

This provides methods for changing text content:

  • replace - Replace a range with new text
  • append - Add text at the end
  • insert - Insert text at cursor position
  • delete - Delete a range of text

And for manipulating cursor/selection:

§Example

use cranpose_foundation::text::{TextFieldBuffer, TextRange};

let mut buffer = TextFieldBuffer::new("Hello");
buffer.place_cursor_at_end();
buffer.insert(", World!");
assert_eq!(buffer.text(), "Hello, World!");

Implementations§

Source§

impl TextFieldBuffer

Source

pub fn new(initial_text: impl Into<String>) -> Self

Creates a new buffer with the given initial text. Cursor is placed at the end of the text.

Source

pub fn with_selection(text: impl Into<String>, selection: TextRange) -> Self

Creates a buffer with text and specified selection.

Source

pub fn text(&self) -> &str

Returns the current text content.

Source

pub fn len(&self) -> usize

Returns the length of the text in bytes.

Source

pub fn is_empty(&self) -> bool

Returns true if the buffer is empty.

Source

pub fn selection(&self) -> TextRange

Returns the current selection range.

Source

pub fn composition(&self) -> Option<TextRange>

Returns the current composition (IME) range, if any.

Source

pub fn has_selection(&self) -> bool

Returns true if there’s a non-collapsed selection.

Source

pub fn has_changes(&self) -> bool

Returns true if any changes have been made.

Source

pub fn replace(&mut self, range: TextRange, replacement: &str)

Replaces text in the given range with new text.

The selection is adjusted based on the replacement:

  • If replacing before selection, selection shifts
  • If replacing within selection, cursor moves to end of replacement
Source

pub fn insert(&mut self, text: &str)

Inserts text at the current cursor position (or replaces selection).

Source

pub fn append(&mut self, text: &str)

Appends text at the end of the buffer.

Source

pub fn delete(&mut self, range: TextRange)

Deletes text in the given range.

Source

pub fn delete_before_cursor(&mut self)

Deletes the character before the cursor (backspace).

Source

pub fn delete_after_cursor(&mut self)

Deletes the character after the cursor (delete key).

Source

pub fn delete_surrounding(&mut self, before_bytes: usize, after_bytes: usize)

Deletes text surrounding the cursor or selection.

before_bytes and after_bytes are byte counts in UTF-8. The deletion respects character boundaries and preserves any IME composition range.

Source

pub fn clear(&mut self)

Clears all text.

Source

pub fn place_cursor_at_end(&mut self)

Places the cursor at the end of the text.

Source

pub fn place_cursor_at_start(&mut self)

Places the cursor at the start of the text.

Source

pub fn place_cursor_before_char(&mut self, index: usize)

Places the cursor before the character at the given index.

Source

pub fn place_cursor_after_char(&mut self, index: usize)

Places the cursor after the character at the given index.

Source

pub fn select_all(&mut self)

Selects all text.

Source

pub fn extend_selection_left(&mut self)

Extends the selection to the left by one character. If no selection exists, starts selection from current cursor position. The anchor (end) stays fixed while the cursor (start) moves left.

Source

pub fn extend_selection_right(&mut self)

Extends the selection to the right by one character. If no selection exists, starts selection from current cursor position. The anchor (start stays at origin) while cursor (end) moves right.

Source

pub fn select(&mut self, range: TextRange)

Selects the given range.

Source

pub fn set_composition(&mut self, range: Option<TextRange>)

Sets the composition (IME) range.

Source

pub fn copy_selection(&self) -> Option<String>

Returns the selected text for copy operations. Returns None if no selection.

Source

pub fn cut_selection(&mut self) -> Option<String>

Cuts the selected text (returns it and deletes from buffer). Returns the cut text, or None if no selection.

Trait Implementations§

Source§

impl Clone for TextFieldBuffer

Source§

fn clone(&self) -> TextFieldBuffer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TextFieldBuffer

Source§

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

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

impl Default for TextFieldBuffer

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.