pub struct EditorBuffer { /* private fields */ }Expand description
A text buffer that manages document contents, cursor position, and undo/redo history.
EditorBuffer stores its text in a Rope, making insertion,
deletion, and line-based operations efficient for an editor.
Cursor positions are represented internally as byte offsets.
§Examples
use twrite_core::EditorBuffer;
let buffer = EditorBuffer::new("Hello, world!");
assert_eq!(buffer.len_bytes(), 13);
assert_eq!(buffer.len_lines(), 1);
assert_eq!(buffer.cursor_offset(), 0);Implementations§
Source§impl EditorBuffer
impl EditorBuffer
Sourcepub fn new(initial_text: &str) -> Self
pub fn new(initial_text: &str) -> Self
Creates a new editor buffer containing initial_text.
The cursor is initially positioned at byte offset 0, and the
undo/redo history starts empty.
Sourcepub fn version(&self) -> usize
pub fn version(&self) -> usize
Returns the monotonic document version, incremented on every text modification.
Sourcepub fn text(&self) -> &Rope
pub fn text(&self) -> &Rope
Returns a reference to the underlying text.
The returned Rope can be used to inspect the document without
copying its contents.
Sourcepub fn cursor_offset(&self) -> usize
pub fn cursor_offset(&self) -> usize
Returns the current cursor position as a byte offset.
The cursor is always maintained at a valid UTF-8 character boundary.
Sourcepub fn line_to_string(&self, line_idx: usize) -> String
pub fn line_to_string(&self, line_idx: usize) -> String
Returns the contents of a line as a String.
Returns an empty string if line_idx is outside the document.
Sourcepub fn offset_to_point(&self, offset: usize) -> Point
pub fn offset_to_point(&self, offset: usize) -> Point
Converts a byte offset into a Point.
The returned point contains a zero-based row and a byte-based
column. If offset is beyond the end of the document, it is
clamped to the document’s end.
Sourcepub fn point_to_offset(&self, point: Point) -> usize
pub fn point_to_offset(&self, point: Point) -> usize
Converts a Point into a byte offset.
If the row is outside the document, the returned offset points to the end of the document. If the column exceeds the length of the line, it is clamped to the end of that line.
Sourcepub fn cursor_point(&self) -> Point
pub fn cursor_point(&self) -> Point
Returns the current cursor position as a Point.
Sourcepub fn set_cursor_offset(&mut self, offset: usize)
pub fn set_cursor_offset(&mut self, offset: usize)
Sets the cursor to the given byte offset.
The offset is clamped to the document’s bounds.
The resulting cursor position is kept on a valid UTF-8 character boundary.
Sourcepub fn set_cursor_point(&mut self, point: Point)
pub fn set_cursor_point(&mut self, point: Point)
Sets the cursor to the given document position.
The row and column are clamped to the document’s bounds.
Sourcepub fn move_cursor_right(&mut self)
pub fn move_cursor_right(&mut self)
Moves the cursor one character to the right.
Does nothing if the cursor is already at the end of the document.
Sourcepub fn move_cursor_up(&mut self)
pub fn move_cursor_up(&mut self)
Moves the cursor one line upward.
The column is preserved when possible. If the target line is shorter, the cursor is placed at the end of that line.
Sourcepub fn move_cursor_down(&mut self)
pub fn move_cursor_down(&mut self)
Moves the cursor one line downward.
The column is preserved when possible. If the target line is shorter, the cursor is placed at the end of that line.
Sourcepub fn move_cursor_left(&mut self)
pub fn move_cursor_left(&mut self)
Moves the cursor one character to the left.
Does nothing if the cursor is already at the beginning of the document.
Sourcepub fn prev_word_offset(&self) -> usize
pub fn prev_word_offset(&self) -> usize
Returns the byte offset of the previous word start relative to current cursor.
Sourcepub fn next_word_offset(&self) -> usize
pub fn next_word_offset(&self) -> usize
Returns the byte offset of the next word end relative to current cursor.
Sourcepub fn line_start_offset(&self) -> usize
pub fn line_start_offset(&self) -> usize
Returns the byte offset of the start of the current line.
Sourcepub fn line_end_offset(&self) -> usize
pub fn line_end_offset(&self) -> usize
Returns the byte offset of the end of the current line (excluding trailing newline).
Sourcepub fn word_range_at(&self, offset: usize) -> Range<usize> ⓘ
pub fn word_range_at(&self, offset: usize) -> Range<usize> ⓘ
Returns the byte range of the word, punctuation token, or whitespace run containing offset.
Sourcepub fn line_range_at(&self, offset: usize) -> Range<usize> ⓘ
pub fn line_range_at(&self, offset: usize) -> Range<usize> ⓘ
Returns the byte range of the full line containing offset, including
any trailing line terminator.
Sourcepub fn move_cursor_prev_word(&mut self)
pub fn move_cursor_prev_word(&mut self)
Moves the cursor to the start of the previous word.
Sourcepub fn move_cursor_next_word(&mut self)
pub fn move_cursor_next_word(&mut self)
Moves the cursor to the end of the next word.
Sourcepub fn move_cursor_line_start(&mut self)
pub fn move_cursor_line_start(&mut self)
Moves the cursor to the beginning of the current line.
Sourcepub fn move_cursor_line_end(&mut self)
pub fn move_cursor_line_end(&mut self)
Moves the cursor to the end of the current line.
Sourcepub fn delete_prev_word(&mut self) -> bool
pub fn delete_prev_word(&mut self) -> bool
Deletes the text from the previous word boundary up to the cursor.
Returns true if text was deleted, or false if the cursor was already at the beginning.
Sourcepub fn delete_next_word(&mut self) -> bool
pub fn delete_next_word(&mut self) -> bool
Deletes the text from the cursor up to the next word boundary.
Returns true if text was deleted, or false if the cursor was already at the end.
Sourcepub fn insert(&mut self, text: &str)
pub fn insert(&mut self, text: &str)
Inserts text at the current cursor position.
The inserted text becomes a single undoable transaction, and the cursor is moved to the end of the inserted text.
Inserting new text after undoing clears the redo history.
Sourcepub fn backspace(&mut self)
pub fn backspace(&mut self)
Deletes the character immediately before the cursor.
If the cursor is at the beginning of the document, this method does nothing.
The deleted character is recorded as an undoable transaction and the cursor moves to the beginning of the deleted character.
Inserting new text after undoing clears the redo history.
Sourcepub fn delete(&mut self)
pub fn delete(&mut self)
Deletes the character at the current cursor position.
If the cursor is at the end of the document, this method does nothing. The cursor remains at the same byte offset after the deletion.
The deleted text is recorded as a transaction so the operation can be undone and redone.
Sourcepub fn delete_range(&mut self, range: Range<usize>)
pub fn delete_range(&mut self, range: Range<usize>)
Deletes the text within range.
The deletion is recorded as an undoable transaction and the cursor
is set to the start of range.
Sourcepub fn replace_range(&mut self, range: Range<usize>, text: &str)
pub fn replace_range(&mut self, range: Range<usize>, text: &str)
Replaces the text within range with text.
If range is empty, this is equivalent to Self::insert.
Sourcepub fn replace_many(
&mut self,
replacements: Vec<(Range<usize>, String)>,
) -> usize
pub fn replace_many( &mut self, replacements: Vec<(Range<usize>, String)>, ) -> usize
Applies multiple non-overlapping replacements as a single undoable transaction.
replacements holds (range, replacement_text) pairs. They are applied
back-to-front so earlier byte offsets stay valid, recorded as one
Transaction, and undone/redone together.
Returns the number of replacements applied. Overlapping, empty, or
out-of-bounds ranges are skipped. A no-op leaves the version untouched.
Sourcepub fn undo(&mut self)
pub fn undo(&mut self)
Undoes the most recent transaction.
If there is no transaction to undo, this method does nothing. The undone transaction is moved to the redo stack.
Sourcepub fn redo(&mut self)
pub fn redo(&mut self)
Redoes the most recently undone transaction.
If there is no transaction to redo, this method does nothing. The redone transaction is moved back to the undo stack.
Sourcepub fn is_char_boundary(&self, offset: usize) -> bool
pub fn is_char_boundary(&self, offset: usize) -> bool
Checks whether offset falls on a valid UTF-8 character boundary.
Sourcepub fn validate_offset(&self, offset: usize) -> Result<()>
pub fn validate_offset(&self, offset: usize) -> Result<()>
Validates that offset is within bounds and lies on a UTF-8 character boundary.
Sourcepub fn validate_range(&self, range: &Range<usize>) -> Result<()>
pub fn validate_range(&self, range: &Range<usize>) -> Result<()>
Validates that range is well-formed, within bounds, and on UTF-8 character boundaries.
Sourcepub fn try_line_to_string(&self, row: usize) -> Result<String>
pub fn try_line_to_string(&self, row: usize) -> Result<String>
Attempts to read the text of the given row, returning an error if out of bounds.
Sourcepub fn try_replace_range(
&mut self,
range: Range<usize>,
text: &str,
) -> Result<()>
pub fn try_replace_range( &mut self, range: Range<usize>, text: &str, ) -> Result<()>
Attempts to replace the text within range, validating bounds and UTF-8 boundaries.
Sourcepub fn try_delete_range(&mut self, range: Range<usize>) -> Result<()>
pub fn try_delete_range(&mut self, range: Range<usize>) -> Result<()>
Attempts to delete the text within range, validating bounds and UTF-8 boundaries.