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 textappend- Add text at the endinsert- Insert text at cursor positiondelete- 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
impl TextFieldBuffer
Sourcepub fn new(initial_text: impl Into<String>) -> Self
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.
Sourcepub fn with_selection(text: impl Into<String>, selection: TextRange) -> Self
pub fn with_selection(text: impl Into<String>, selection: TextRange) -> Self
Creates a buffer with text and specified selection.
Sourcepub fn composition(&self) -> Option<TextRange>
pub fn composition(&self) -> Option<TextRange>
Returns the current composition (IME) range, if any.
Sourcepub fn has_selection(&self) -> bool
pub fn has_selection(&self) -> bool
Returns true if there’s a non-collapsed selection.
Sourcepub fn has_changes(&self) -> bool
pub fn has_changes(&self) -> bool
Returns true if any changes have been made.
Sourcepub fn replace(&mut self, range: TextRange, replacement: &str)
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
Sourcepub fn insert(&mut self, text: &str)
pub fn insert(&mut self, text: &str)
Inserts text at the current cursor position (or replaces selection).
Sourcepub fn delete_before_cursor(&mut self)
pub fn delete_before_cursor(&mut self)
Deletes the character before the cursor (backspace).
Sourcepub fn delete_after_cursor(&mut self)
pub fn delete_after_cursor(&mut self)
Deletes the character after the cursor (delete key).
Sourcepub fn delete_surrounding(&mut self, before_bytes: usize, after_bytes: usize)
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.
Sourcepub fn place_cursor_at_end(&mut self)
pub fn place_cursor_at_end(&mut self)
Places the cursor at the end of the text.
Sourcepub fn place_cursor_at_start(&mut self)
pub fn place_cursor_at_start(&mut self)
Places the cursor at the start of the text.
Sourcepub fn place_cursor_before_char(&mut self, index: usize)
pub fn place_cursor_before_char(&mut self, index: usize)
Places the cursor before the character at the given index.
Sourcepub fn select_all(&mut self)
pub fn select_all(&mut self)
Selects all text.
Sourcepub fn extend_selection_left(&mut self)
pub fn extend_selection_left(&mut self)
Extends the selection one character to the left.
start is the anchor and end is the moving cursor, which is what
makes this and Self::extend_selection_right inverses of each other:
shift-left followed by shift-right returns to where it began. Moving
whichever end happens to be smaller instead would leave the two growing
from opposite ends, and shift-right after shift-left would grow the
selection rather than shrink it.
A reverse range (start > end) is how an anchor to the right of the
cursor is represented; see TextRange.
Sourcepub fn extend_selection_right(&mut self)
pub fn extend_selection_right(&mut self)
Extends the selection one character to the right.
The anchor stays where it is and the cursor moves; see
Self::extend_selection_left.
Sourcepub fn set_composition(&mut self, range: Option<TextRange>)
pub fn set_composition(&mut self, range: Option<TextRange>)
Sets the composition (IME) range.
Sourcepub fn copy_selection(&self) -> Option<String>
pub fn copy_selection(&self) -> Option<String>
Returns the selected text for copy operations. Returns None if no selection.
Sourcepub fn cut_selection(&mut self) -> Option<String>
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.