Skip to main content

EditorBuffer

Struct EditorBuffer 

Source
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

Source

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.

Source

pub fn version(&self) -> usize

Returns the monotonic document version, incremented on every text modification.

Source

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.

Source

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.

Source

pub fn len_bytes(&self) -> usize

Returns the total number of bytes in the document.

Source

pub fn len_lines(&self) -> usize

Returns the number of lines in the document.

Source

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.

Source

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.

Source

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.

Source

pub fn cursor_point(&self) -> Point

Returns the current cursor position as a Point.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn prev_word_offset(&self) -> usize

Returns the byte offset of the previous word start relative to current cursor.

Source

pub fn next_word_offset(&self) -> usize

Returns the byte offset of the next word end relative to current cursor.

Source

pub fn line_start_offset(&self) -> usize

Returns the byte offset of the start of the current line.

Source

pub fn line_end_offset(&self) -> usize

Returns the byte offset of the end of the current line (excluding trailing newline).

Source

pub fn word_range_at(&self, offset: usize) -> Range<usize>

Returns the byte range of the word, punctuation token, or whitespace run containing offset.

Source

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.

Source

pub fn move_cursor_prev_word(&mut self)

Moves the cursor to the start of the previous word.

Source

pub fn move_cursor_next_word(&mut self)

Moves the cursor to the end of the next word.

Source

pub fn move_cursor_line_start(&mut self)

Moves the cursor to the beginning of the current line.

Source

pub fn move_cursor_line_end(&mut self)

Moves the cursor to the end of the current line.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn can_undo(&self) -> bool

Returns whether an undo transaction is available.

Source

pub fn can_redo(&self) -> bool

Returns whether a redo transaction is available.

Source

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.

Source

pub fn is_char_boundary(&self, offset: usize) -> bool

Checks whether offset falls on a valid UTF-8 character boundary.

Source

pub fn validate_offset(&self, offset: usize) -> Result<()>

Validates that offset is within bounds and lies on a UTF-8 character boundary.

Source

pub fn validate_range(&self, range: &Range<usize>) -> Result<()>

Validates that range is well-formed, within bounds, and on UTF-8 character boundaries.

Source

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.

Source

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.

Source

pub fn try_delete_range(&mut self, range: Range<usize>) -> Result<()>

Attempts to delete the text within range, validating bounds and UTF-8 boundaries.

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self>

Loads document text directly from a file path.

Source

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>

Saves the current buffer contents to a file path.

Trait Implementations§

Source§

impl Debug for EditorBuffer

Source§

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

Formats the value using the given formatter. 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.