pub struct GapBuffer { /* private fields */ }Expand description
Gap buffer implementation for efficient text editing.
The buffer maintains text as a vector of characters with a gap at the current editing position. This makes insertions and deletions at the cursor position very efficient (O(1)), at the cost of having to move the gap when editing at different positions.
§Example Structure
For text “Hello World” with gap after “Hello”:
['H', 'e', 'l', 'l', 'o', '\0', '\0', '\0', ' ', 'W', 'o', 'r', 'l', 'd']
^gap_start ^gap_endImplementations§
Source§impl GapBuffer
impl GapBuffer
Sourcepub fn from_lines(lines: Vec<String>) -> Self
pub fn from_lines(lines: Vec<String>) -> Self
Create a gap buffer from lines
Sourcepub fn move_gap_to(&mut self, position: usize)
pub fn move_gap_to(&mut self, position: usize)
Move the gap to a specific position in the text.
This is the core operation that enables efficient editing. When the gap is at the cursor position, insertions and deletions become O(1) operations.
§Complexity
O(n) where n is the distance between the current gap position and the target position
Sourcepub fn insert_char(&mut self, position: usize, ch: char)
pub fn insert_char(&mut self, position: usize, ch: char)
Insert a character at the specified position.
Moves the gap to the position first, then inserts the character by placing it at gap_start and incrementing gap_start.
Sourcepub fn delete_backward(&mut self, position: usize)
pub fn delete_backward(&mut self, position: usize)
Delete backward from the specified position
Sourcepub fn delete_forward(&mut self, position: usize)
pub fn delete_forward(&mut self, position: usize)
Delete forward from the specified position
Sourcepub fn delete_range(&mut self, start: usize, end: usize)
pub fn delete_range(&mut self, start: usize, end: usize)
Delete a range of text
Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Convert the buffer to a string.
Reconstructs the text by concatenating the content before the gap with the content after the gap, skipping the gap itself.
§Complexity
O(n) where n is the length of the text
Sourcepub fn cursor_to_position(&self, row: usize, col: usize) -> usize
pub fn cursor_to_position(&self, row: usize, col: usize) -> usize
Convert cursor position (row, col) to buffer position.
Translates a 2D cursor position (line and column) to a linear position in the buffer. This is used to map editor cursor positions to buffer positions for editing operations.
§Returns
The character index in the buffer corresponding to the cursor position
Sourcepub fn position_to_cursor(&self, position: usize) -> (usize, usize)
pub fn position_to_cursor(&self, position: usize) -> (usize, usize)
Convert buffer position to cursor position (row, col).
Translates a linear buffer position to a 2D cursor position
(line and column). This is the inverse of cursor_to_position.
§Returns
A tuple of (row, column) representing the cursor position
Trait Implementations§
Source§impl TextBuffer for GapBuffer
impl TextBuffer for GapBuffer
Source§fn line_count(&self) -> usize
fn line_count(&self) -> usize
Source§fn insert_at(&mut self, row: usize, col: usize, text: &str)
fn insert_at(&mut self, row: usize, col: usize, text: &str)
Source§fn delete_at(&mut self, row: usize, col: usize)
fn delete_at(&mut self, row: usize, col: usize)
Source§fn backspace_at(&mut self, row: usize, col: usize)
fn backspace_at(&mut self, row: usize, col: usize)
Auto Trait Implementations§
impl Freeze for GapBuffer
impl RefUnwindSafe for GapBuffer
impl Send for GapBuffer
impl Sync for GapBuffer
impl Unpin for GapBuffer
impl UnwindSafe for GapBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().