GapBuffer

Struct GapBuffer 

Source
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

Source

pub fn new() -> Self

Construct a new empty GapBuffer

Source

pub fn len(&self) -> usize

The current length of “active” data in the buffer (i.e. not including the gap)

Source

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.

Source

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.

Source

pub fn is_contiguous(&self) -> bool

Whether or not the full data within the buffer is contiguous (all on one side of the gap).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

pub fn chars(&self) -> Chars<'_>

Iterate over the characters of the buffer

Source

pub fn iter_lines(&self) -> impl Iterator<Item = Slice<'_>>

Iterate over the lines of the buffer

Source

pub fn len_lines(&self) -> usize

The number of lines within the buffer

Source

pub fn len_chars(&self) -> usize

The number of characters in the buffer

Source

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.

Source

pub fn lines_before_byte_offset(&self, byte_offset: usize) -> usize

Source

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.

Source

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

Source

pub fn get_char(&self, char_idx: usize) -> Option<char>

The character at the specified character index.

Source

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

Source

pub fn get_char_at(&self, byte_idx: usize) -> Option<char>

The character at the specified byte index.

Source

pub fn line(&self, line_idx: usize) -> Slice<'_>

The requested line as a Slice.

§Panics

This method will panic if the given line index is out of bounds

Source

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.

Source

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

Source

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.

Source

pub fn slice_from_byte_offsets( &self, byte_from: usize, byte_to: usize, ) -> Slice<'_>

An exclusive range of characters from the buffer

Source

pub fn slice(&self, char_from: usize, char_to: usize) -> Slice<'_>

An exclusive range of characters from the buffer

Source

pub fn as_slice(&self) -> Slice<'_>

Source

pub fn chars_in_raw_range(&self, raw_from: usize, raw_to: usize) -> usize

Source

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

Source

pub fn try_char_to_line(&self, char_idx: usize) -> Option<usize>

Convert a character index to the index of the line containing it

Source

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

Source

pub fn try_line_to_char(&self, line_idx: usize) -> Option<usize>

Convert a line index to the character index of its first character

Source

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

Source

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.

Source

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.

Source

pub fn remove_char(&mut self, char_idx: usize)

Remove the requested character index from the visible region of the buffer

Source

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.

Source

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

Source

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.

Source

pub fn char_range_to_byte_range( &self, ch_from: usize, ch_to: usize, ) -> (usize, usize)

Convert a character range to a byte range

Source

pub fn byte_to_raw_byte(&self, byte: usize) -> usize

Trait Implementations§

Source§

impl Clone for GapBuffer

Source§

fn clone(&self) -> GapBuffer

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GapBuffer

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for GapBuffer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for GapBuffer

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Edit for GapBuffer

Source§

fn insert(&mut self, idx: usize, s: &str)

Insert a string at the specified index
Source§

fn remove(&mut self, from: usize, to: usize)

Remove all characters from (from..to)
Source§

fn begin_edit_transaction(&mut self)

Mark the start of an edit transaction
Source§

fn end_edit_transaction(&mut self)

Mark the end of an edit transaction
Source§

impl From<&str> for GapBuffer

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for GapBuffer

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl Haystack<Regex> for GapBuffer

Source§

fn is_match_between(&self, re: &Regex, from: usize, to: usize) -> bool

Returns true if there is a match for the regex between the given byte offsets in the haystack. Read more
Source§

fn captures_between( &self, re: &Regex, from: usize, to: usize, ) -> Option<RawCaptures>

Searches for the first match of this regex between the given byte offsets in the given haystack, returning the overall match along with the matches of each capture group in the regex. If no match is found, then None is returned. Read more
Source§

impl Haystack for GapBuffer

Source§

fn try_make_contiguous(&mut self)

Source§

fn is_contiguous(&self) -> bool

Source§

fn len(&self) -> usize

Source§

fn substr_from<'a>(&'a self, offset: usize) -> Option<Cow<'a, str>>

Source§

fn substr<'a>(&'a self, from: usize, to: usize) -> Cow<'a, str>

Source§

fn byte_to_char(&self, byte_idx: usize) -> Option<usize>

Source§

fn char_to_byte(&self, char_idx: usize) -> Option<usize>

Source§

fn iter_from(&self, from: usize) -> Option<impl Iterator<Item = (usize, char)>>

Source§

fn iter_between( &self, from: usize, to: usize, ) -> impl Iterator<Item = (usize, char)>

Source§

fn rev_iter_between( &self, from: usize, to: usize, ) -> impl Iterator<Item = (usize, char)>

Source§

impl<'a> PartialEq<&'a str> for GapBuffer

Source§

fn eq(&self, other: &&'a str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for GapBuffer

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for GapBuffer

Source§

fn eq(&self, other: &GapBuffer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sliceable for GapBuffer

Source§

type Slice<'h> = Slice<'h> where Self: 'h

The output of the slice method.
Source§

fn char_at(&self, byte_offset: usize) -> Option<char>

Returns the character at the given byte offset if one exists, otherwise None.
Source§

fn slice(&self, range: Range<usize>) -> Self::Slice<'_>

The contiguous sub-section of self that is denoted by the given byte Range.
Source§

fn max_len(&self) -> usize

The maximum length in bytes. Read more
Source§

impl<'a> TextProvider<&'a [u8]> for &'a GapBuffer

Source§

type I = SliceIter<'a>

Source§

fn text(&mut self, node: Node<'_>) -> Self::I

Source§

impl Writable for GapBuffer

Source§

fn write_to<W>(&self, w: &mut W) -> Result<usize>
where W: Write,

Writes self to the given io::Write, returning the number of bytes written.
Source§

impl Eq for GapBuffer

Source§

impl StructuralPartialEq for GapBuffer

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more