Skip to main content

PieceTree

Struct PieceTree 

Source
pub struct PieceTree {
    pub root: NodeRef,
    pub transaction_depth: usize,
    pub undo_stack: Vec<HistoryEntry>,
    pub redo_stack: Vec<HistoryEntry>,
    pub pieces: Pieces,
    pub buffers: Buffers,
    pub scratch_index_map: Vec<u32>,
    /* private fields */
}

Fields§

§root: NodeRef§transaction_depth: usize

Tracks nested undo groups. 0 if we are outside any group.

§undo_stack: Vec<HistoryEntry>§redo_stack: Vec<HistoryEntry>§pieces: Pieces§buffers: Buffers§scratch_index_map: Vec<u32>

Implementations§

Source§

impl PieceTree

Source

pub fn split(&mut self, root: NodeRef, offset: u32) -> (NodeRef, NodeRef)

Slices a tree exactly at offset, returning (LeftTree, RightTree)

Source

pub fn concat(&mut self, left: NodeRef, right: NodeRef) -> NodeRef

Glues two arbitrary trees together by extracting the max element of the left tree

Source§

impl PieceTree

Source

pub fn new() -> Self

Source

pub fn from_str(text: &str) -> Self

Source§

impl PieceTree

Source

pub fn take_snapshot(&self, current_cursor: u32) -> HistoryEntry

Captures the current state of the document.

Source

pub fn snap_to(&mut self, snapshot: HistoryEntry, current_cursor: u32) -> u32

Restores the tree to a previously saved snapshot. Returns the cursor offset from the snapshot.

Source§

impl PieceTree

Source

pub fn begin_undo_group(&mut self, cursor_offset: u32)

Starts a new undo group, saves the current state if this is the outermost group.

Source

pub fn end_undo_group(&mut self)

Ends the current undo group

Source

pub fn commit_head(&mut self, cursor_offset: u32)

Source

pub fn try_undo(&mut self, current_cursor: u32) -> Option<u32>

Source

pub fn try_redo(&mut self, current_cursor: u32) -> Option<u32>

Source

pub fn total_length(&self) -> u32

Source

pub fn apply_edits(&mut self, primary_cursor_offset: u32, edits: &mut [Edit])

Source

pub fn insert_char(&mut self, offset: u32, ch: char)

Inserts a single character at the specified logical byte offset.

Source

pub fn insert_char_no_commit(&mut self, offset: u32, ch: char)

Inserts a single character at the specified logical byte offset.

Source

pub fn insert(&mut self, offset: u32, text: &str)

Source

pub fn remove_at(&mut self, offset: u32, length: u32)

Source

pub fn insert_no_commit(&mut self, offset: u32, text: &str)

Source

pub fn remove_no_commit(&mut self, offset: u32, length: u32)

Source

pub fn get_piece(&self, index: NodeRef) -> Piece

Source

pub fn find_position( &self, offset: u32, prefer_left: bool, ) -> Option<(NodeRef, u32)>

Source

pub fn write_to<W: Write>(&self, writer: W) -> Result<()>

Source§

impl PieceTree

Source

pub fn compact(&mut self)

Source

pub fn squash(&mut self)

Source§

impl PieceTree

Source

pub fn node_arena_bytes(&self) -> u32

Total bytes allocated for the node arena (includes NIL sentinel and all historical nodes retained for undo/redo).

Source

pub fn mod_buffer_bytes(&self) -> u32

Bytes consumed by the modifications buffer (append-only, never shrinks).

Source

pub fn original_buffers_bytes(&self) -> u32

Bytes consumed by all original (read) buffers.

Source

pub fn history_bytes(&self) -> u32

Bytes consumed by undo + redo history entries.

Source

pub fn node_count(&self) -> u32

Number of live nodes in the arena (includes NIL and all historical nodes).

Source

pub fn memory_usage(&self) -> MemoryUsage

Aggregate memory usage breakdown.

Source§

impl PieceTree

Source

pub fn slice<R: RangeBounds<u32>>(&self, range: R) -> TreeSlice<'_>

Source

pub fn slice_chars_range<R: RangeBounds<u32>>(&self, range: R) -> TreeSlice<'_>

Slice by char range.

Source§

impl PieceTree

Source

pub fn read_largest_contigous_chunk_at_byte(&self, offset: u32) -> &[u8]

Fast-path chunk reader specifically designed for the Tree-sitter C API. Given an absolute byte offset, it returns the largest contiguous byte slice available starting exactly at that offset.

Source

pub fn line_to_byte(&self, target_line: u32) -> Option<u32>

Source

pub fn char_to_byte(&self, char_index: u32) -> Option<u32>

Source

pub fn byte_to_char(&self, byte_offset: u32) -> Option<u32>

Source

pub fn byte_to_line_col(&self, offset: u32) -> Option<(u32, u32)>

Source

pub fn pieces(&self) -> PieceTreeIter<'_>

Source

pub fn get_line_range(&self, line: u32) -> Option<(u32, u32)>

Source

pub fn get_line_content_allocating(&self, line: u32) -> Option<String>

Source

pub fn chunks_at_byte(&self, start: u32, end: u32) -> ChunkIter<'_>

Returns an iterator of &str chunks over the given byte range. Zero allocation.

Source

pub fn byte(&self, offset: u32) -> Option<u8>

Byte at a given byte offset. O(log n).

Source

pub fn char(&self, char_index: u32) -> Option<char>

Char at a given char index. O(log n) to find the piece, then a short scan within the piece.

Source

pub fn slice_chars(&self, char_start: u32, char_end: u32) -> SliceChars<'_>

Returns a non-allocating iterator of chars over the given char range. Backed by TreeWalker::seek so it reuses existing infrastructure.

Source

pub fn slice_bytes(&self, byte_start: u32, byte_end: u32) -> SliceChars<'_>

Returns a non-allocating iterator of chars over the given byte range.

Source

pub fn line(&self, line: u32) -> Option<ChunkIter<'_>>

Non-allocating line view: returns a ChunkIter over the byte range of line. Line numbers are 0-based. The trailing \n is included if present.

Source

pub fn len_lines(&self) -> u32

Number of lines (= newline count + 1)

Source

pub fn len_chars(&self) -> u32

Source

pub fn len_bytes(&self) -> u32

Source

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

Source

pub fn chars_rev(&self) -> ReverseTreeWalker<'_>

Source

pub fn lines(&self) -> LinesIter<'_>

Source

pub fn remove<R>(&mut self, range: R)
where R: RangeBounds<u32>,

Source

pub fn char_to_line(&self, char_index: u32) -> Option<u32>

Source

pub fn char_to_line_col(&self, char_index: u32) -> Option<(u32, u32)>

Source

pub fn line_to_char(&self, line: u32) -> Option<u32>

Source§

impl PieceTree

Source

pub fn debug(&self, f: &mut impl Write) -> Result

Trait Implementations§

Source§

impl Clone for PieceTree

Source§

fn clone(&self) -> PieceTree

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for PieceTree

Source§

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

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

impl Default for PieceTree

Source§

fn default() -> PieceTree

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

impl Display for PieceTree

Source§

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

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

impl<T> From<T> for PieceTree
where T: AsRef<str>,

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl FromStr for PieceTree

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. 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> CloneToUninit for T
where T: Clone,

Source§

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<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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

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

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.