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 make_contiguous(&mut self) -> &[u8] ⓘ
pub fn make_contiguous(&mut self) -> &[u8] ⓘ
Rearrange the internal storage of this buffer so that the active data is in a single contiguous slice which is then returned.
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Whether or not the full data within the buffer is contiguous (all on one side of the gap).
Sourcepub fn as_str(&mut self) -> &str
pub fn as_str(&mut self) -> &str
The contents of the buffer as a single &str.
This method requires a mutable reference as we need to move the gap in order to ensure that all of the active data within the buffer is contiguous.
Sourcepub unsafe fn substr_from(&self, byte_offset: usize) -> &str
pub unsafe fn substr_from(&self, byte_offset: usize) -> &str
A contiguous substring of the buffer from the give byte offset.
§Safety
You must call GapBuffer::make_contiguous before calling this method.
Sourcepub unsafe fn assume_contiguous_bytes(&self) -> &[u8] ⓘ
pub unsafe fn assume_contiguous_bytes(&self) -> &[u8] ⓘ
Assume that the gap is at 0 and return the full contents of the inner buffer as a slice of bytes.
§Safety
You must call GapBuffer::make_contiguous before calling this method.
Sourcepub unsafe fn assume_contiguous_str(&self) -> &str
pub unsafe fn assume_contiguous_str(&self) -> &str
Assume that the gap is at 0 and return the full contents of the inner buffer as a &str
§Safety
You must call GapBuffer::make_contiguous before calling this method.
Sourcepub fn as_strs(&self) -> (&str, &str)
pub fn as_strs(&self) -> (&str, &str)
The contents of the buffer either side of the current gap as &str slices.
If make_contiguous was previously called, the first &str
will be empty and the full content of the buffer will be in the second &str.
Sourcepub fn as_byte_slices(&self) -> (&[u8], &[u8])
pub fn as_byte_slices(&self) -> (&[u8], &[u8])
The contents of the buffer either side of the current gap as byte slices.
If make_contiguous was previously called, the first slice will be empty and the full content of the buffer will be in the second slice.
Sourcepub fn has_trailing_newline(&self) -> bool
pub fn has_trailing_newline(&self) -> bool
Whether or not the contents of the buffer end with a final newline character.
POSIX semantics define a line as “A sequence of zero or more non-newline characters plus a terminating newline character.”
See: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
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
Sourcepub fn line_end_byte(&self, line_idx: usize) -> usize
pub fn line_end_byte(&self, line_idx: usize) -> usize
The byte offset of the end of the given line.
For lines with a trailing newline this is the position of the newline character, for the final line (if there is no trailing newline) this is the end of the buffer.
pub fn lines_before_byte_offset(&self, byte_offset: usize) -> 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 char_at(&self, byte_idx: usize) -> char
pub fn char_at(&self, byte_idx: usize) -> char
The character at the specified byte index.
§Panics
This method will panic if the given byte index is out of bounds
Sourcepub fn get_char_at(&self, byte_idx: usize) -> Option<char>
pub fn get_char_at(&self, byte_idx: usize) -> Option<char>
The character at the specified byte index.
Sourcepub fn line_is_blank(&self, line_idx: usize) -> bool
pub fn line_is_blank(&self, line_idx: usize) -> bool
Returns true if the requested line is empty or only contains a single trailing newline.
See line for panic details.
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
pub fn as_slice(&self) -> Slice<'_>
pub fn chars_in_raw_range(&self, raw_from: usize, raw_to: usize) -> usize
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 line_to_byte(&self, line_idx: usize) -> usize
pub fn line_to_byte(&self, line_idx: usize) -> usize
Convert a line index to the byte index of its first character
§Panics
This method will panic if the given byte index is out of bounds
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 character index.
This is O(1) if idx is at the current gap start and the gap is large enough to accommodate 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 character index.
This is O(1) if idx is at the current gap start and the gap is large enough to accommodate 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 or if the requested range is out of bounds.
Sourcepub fn byte_to_char(&self, byte_idx: usize) -> usize
pub fn byte_to_char(&self, byte_idx: usize) -> usize
Convert a logical byte offset into a character offset within the buffer.
This is primarily used to create substrings via the GapBuffer::substr_from method when running regular expressions over a gap buffer.
This is a simplified version of the equivalent logic in offset_char_to_raw_byte without the cache and fast search on single line buffers. This is not typically in the hot path for general editor functionality so we don’t mind being a little slower in order to keep the logic easier to reason about
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.
Sourcepub fn char_range_to_byte_range(
&self,
ch_from: usize,
ch_to: usize,
) -> (usize, usize)
pub fn char_range_to_byte_range( &self, ch_from: usize, ch_to: usize, ) -> (usize, usize)
Convert a character range to a byte range
pub fn byte_to_raw_byte(&self, byte: usize) -> usize
Trait Implementations§
Source§impl Edit for GapBuffer
impl Edit for GapBuffer
Source§fn begin_edit_transaction(&mut self)
fn begin_edit_transaction(&mut self)
Source§fn end_edit_transaction(&mut self)
fn end_edit_transaction(&mut self)
Source§impl Haystack<Regex> for GapBuffer
impl Haystack<Regex> for GapBuffer
Source§fn is_match_between(&self, re: &Regex, from: usize, to: usize) -> bool
fn is_match_between(&self, re: &Regex, from: usize, to: usize) -> bool
Source§fn captures_between(
&self,
re: &Regex,
from: usize,
to: usize,
) -> Option<RawCaptures>
fn captures_between( &self, re: &Regex, from: usize, to: usize, ) -> Option<RawCaptures>
Source§impl Haystack for GapBuffer
impl Haystack for GapBuffer
fn try_make_contiguous(&mut self)
fn is_contiguous(&self) -> bool
fn len(&self) -> usize
fn substr_from<'a>(&'a self, offset: usize) -> Option<Cow<'a, str>>
fn substr<'a>(&'a self, from: usize, to: usize) -> Cow<'a, str>
fn byte_to_char(&self, byte_idx: usize) -> Option<usize>
fn char_to_byte(&self, char_idx: usize) -> Option<usize>
fn iter_from(&self, from: usize) -> Option<impl Iterator<Item = (usize, char)>>
fn iter_between( &self, from: usize, to: usize, ) -> impl Iterator<Item = (usize, char)>
fn rev_iter_between( &self, from: usize, to: usize, ) -> impl Iterator<Item = (usize, char)>
Source§impl Sliceable for GapBuffer
impl Sliceable 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§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)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.