pub struct GapBuffer { /* private fields */ }
Expand description
An implementation of a gap buffer that tracks internal meta-data to help with accessing sub-regions of the text such as character ranges and lines.
Implementations§
Source§impl GapBuffer
impl GapBuffer
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The current length of “active” data in the buffer (i.e. not including the gap)
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether or not the visible buffer contents are empty or not. This can return true while there is “deleted” data in the gap.
Sourcepub fn iter_lines(&self) -> impl Iterator<Item = Slice<'_>>
pub fn iter_lines(&self) -> impl Iterator<Item = Slice<'_>>
Iterate over the lines of the buffer
pub fn byte_line_endings(&self) -> Vec<usize>
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the contents of the buffer.
§Note
This does not actually zero out the data currently within the buffer or truncate the allocation in any way. It simply resets internal state so that it behaves like an empty initial buffer.
Sourcepub fn char(&self, char_idx: usize) -> char
pub fn char(&self, char_idx: usize) -> char
The character at the specified character index.
§Panics
This method will panic if the given character index is out of bounds
Sourcepub fn get_char(&self, char_idx: usize) -> Option<char>
pub fn get_char(&self, char_idx: usize) -> Option<char>
The character at the specified character index.
Sourcepub fn line_len_chars(&self, line_idx: usize) -> usize
pub fn line_len_chars(&self, line_idx: usize) -> usize
The number of characters in the requested line.
§Panics
This method will panic if the given line index is out of bounds
Sourcepub fn maximal_slice_from_offset(&self, byte_offset: usize) -> &[u8] ⓘ
pub fn maximal_slice_from_offset(&self, byte_offset: usize) -> &[u8] ⓘ
Primarily intended for supplying contiguous ranges of bytes to tree-sitter when parsing. Returns a byte slice from the underlying data buffer without entering the gap.
Sourcepub fn slice_from_byte_offsets(
&self,
byte_from: usize,
byte_to: usize,
) -> Slice<'_>
pub fn slice_from_byte_offsets( &self, byte_from: usize, byte_to: usize, ) -> Slice<'_>
An exclusive range of characters from the buffer
Sourcepub fn slice(&self, char_from: usize, char_to: usize) -> Slice<'_>
pub fn slice(&self, char_from: usize, char_to: usize) -> Slice<'_>
An exclusive range of characters from the buffer
Sourcepub fn raw_byte_to_char(&self, byte_idx: usize) -> usize
pub fn raw_byte_to_char(&self, byte_idx: usize) -> usize
Convert a byte index to a character index
Sourcepub fn char_to_line(&self, char_idx: usize) -> usize
pub fn char_to_line(&self, char_idx: usize) -> usize
Convert a character index to the index of the line containing it
§Panics
This method will panic if the given char index is out of bounds
Sourcepub fn try_char_to_line(&self, char_idx: usize) -> Option<usize>
pub fn try_char_to_line(&self, char_idx: usize) -> Option<usize>
Convert a character index to the index of the line containing it
Sourcepub fn line_to_char(&self, line_idx: usize) -> usize
pub fn line_to_char(&self, line_idx: usize) -> usize
Convert a line index to the character index of its first character
§Panics
This method will panic if the given char index is out of bounds
Sourcepub fn try_line_to_char(&self, line_idx: usize) -> Option<usize>
pub fn try_line_to_char(&self, line_idx: usize) -> Option<usize>
Convert a line index to the character index of its first character
Sourcepub fn insert_char(&mut self, char_idx: usize, ch: char)
pub fn insert_char(&mut self, char_idx: usize, ch: char)
Insert a single character at the specifified byte index.
This is O(1) if idx is at the current gap start and the gap is large enough to accomodate the new text, otherwise data will need to be copied in order to relocate the gap.
Sourcepub fn insert_str(&mut self, char_idx: usize, s: &str)
pub fn insert_str(&mut self, char_idx: usize, s: &str)
Insert a string at the specifified byte index.
This is O(1) if idx is at the current gap start and the gap is large enough to accomodate the new text, otherwise data will need to be copied in order to relocate the gap.
Sourcepub fn remove_char(&mut self, char_idx: usize)
pub fn remove_char(&mut self, char_idx: usize)
Remove the requested character index from the visible region of the buffer
Sourcepub fn remove_range(&mut self, char_from: usize, char_to: usize)
pub fn remove_range(&mut self, char_from: usize, char_to: usize)
Remove the requested range (from..to) from the visible region of the buffer.
§Panics
This method will panic if char_from < char_to
Sourcepub fn char_to_byte(&self, char_idx: usize) -> usize
pub fn char_to_byte(&self, char_idx: usize) -> usize
Convert a character offset within the logical buffer to a byte offset within the logical buffer. This is used to account for multi-byte characters within the buffer and is treated as a String-like index but it does not account for the position of the gap.
pub fn byte_to_raw_byte(&self, byte: usize) -> usize
Trait Implementations§
Source§impl Edit for GapBuffer
impl Edit for GapBuffer
Source§impl<'a> TextProvider<&'a [u8]> for &'a GapBuffer
impl<'a> TextProvider<&'a [u8]> for &'a GapBuffer
impl Eq for GapBuffer
impl StructuralPartialEq for GapBuffer
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.