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: usizeTracks 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
impl PieceTree
pub fn new() -> Self
pub fn from_arc_str(text: impl Into<Arc<str>>) -> Self
Sourcepub fn split_off(&mut self, byte_offset: u32) -> Self
pub fn split_off(&mut self, byte_offset: u32) -> Self
Splits the tree at byte_offset, returning the right half as a new
PieceTree.
After this call, self contains [0, byte_offset) and
the returned tree contains [byte_offset, end).
byte_offset must be on a UTF-8 char boundary. Panics if byte_offset > self.len_bytes().
Runs in O(log n).
pub fn from_reader<R: Read>(reader: R) -> Result<Self>
Source§impl PieceTree
impl PieceTree
Sourcepub fn take_snapshot(&self, current_cursor: u32) -> HistoryEntry
pub fn take_snapshot(&self, current_cursor: u32) -> HistoryEntry
Captures the current state of the document.
Sourcepub fn snap_to(&mut self, snapshot: HistoryEntry, current_cursor: u32) -> u32
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
impl PieceTree
Sourcepub fn begin_undo_group(&mut self, cursor_offset: u32)
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.
Sourcepub fn end_undo_group(&mut self)
pub fn end_undo_group(&mut self)
Ends the current undo group
pub fn commit_head(&mut self, cursor_offset: u32)
pub fn try_undo(&mut self, current_cursor: u32) -> Option<u32>
pub fn try_redo(&mut self, current_cursor: u32) -> Option<u32>
pub fn total_length(&self) -> u32
pub fn apply_edits(&mut self, primary_cursor_offset: u32, edits: &mut [Edit])
Sourcepub fn insert_char(&mut self, offset: u32, ch: char)
pub fn insert_char(&mut self, offset: u32, ch: char)
Inserts a single character at the specified logical byte offset.
Sourcepub fn insert_char_no_commit(&mut self, offset: u32, ch: char)
pub fn insert_char_no_commit(&mut self, offset: u32, ch: char)
Inserts a single character at the specified logical byte offset.
pub fn insert(&mut self, offset: u32, text: &str)
pub fn remove_at(&mut self, offset: u32, length: u32)
pub fn insert_no_commit(&mut self, offset: u32, text: &str)
pub fn remove_no_commit(&mut self, offset: u32, length: u32)
pub fn get_piece(&self, index: NodeRef) -> Piece
pub fn find_position( &self, offset: u32, prefer_left: bool, ) -> Option<(NodeRef, u32)>
pub fn write_to<W: Write>(&self, writer: W) -> Result<()>
Source§impl PieceTree
impl PieceTree
Sourcepub fn node_arena_bytes(&self) -> u32
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).
Sourcepub fn mod_buffer_bytes(&self) -> u32
pub fn mod_buffer_bytes(&self) -> u32
Bytes consumed by the modifications buffer (append-only, never shrinks).
Sourcepub fn original_buffers_bytes(&self) -> u32
pub fn original_buffers_bytes(&self) -> u32
Bytes consumed by all original (read) buffers.
Sourcepub fn history_bytes(&self) -> u32
pub fn history_bytes(&self) -> u32
Bytes consumed by undo + redo history entries.
Sourcepub fn node_count(&self) -> u32
pub fn node_count(&self) -> u32
Number of live nodes in the arena (includes NIL and all historical nodes).
Sourcepub fn memory_usage(&self) -> MemoryUsage
pub fn memory_usage(&self) -> MemoryUsage
Aggregate memory usage breakdown.
Source§impl PieceTree
Tree Iterators
impl PieceTree
Tree Iterators
pub fn slice_whole(&self) -> TreeSlice<'_>
pub fn chunks(&self) -> ChunkIter<'_> ⓘ
pub fn bytes(&self) -> impl Iterator<Item = u8> + '_
pub fn chars(&self) -> impl Iterator<Item = char> + '_
pub fn lines(&self) -> SliceLines<'_> ⓘ
pub fn chars_rev(&self) -> ReverseTreeWalker<'_> ⓘ
pub fn chars_at_rev(&self, char_idx: u32) -> ReverseTreeWalker<'_> ⓘ
pub fn chunks_at_byte(&self, byte_idx: u32) -> ChunkIter<'_> ⓘ
pub fn bytes_at(&self, byte_idx: u32) -> impl Iterator<Item = u8> + '_
pub fn chars_at(&self, char_idx: u32) -> impl Iterator<Item = char> + '_
pub fn lines_at(&self, line_idx: u32) -> SliceLines<'_> ⓘ
Source§impl PieceTree
impl PieceTree
pub fn slice<R: RangeBounds<u32>>(&self, range: R) -> TreeSlice<'_>
Sourcepub fn slice_chars_range<R: RangeBounds<u32>>(&self, range: R) -> TreeSlice<'_>
pub fn slice_chars_range<R: RangeBounds<u32>>(&self, range: R) -> TreeSlice<'_>
Slice by char range.
Source§impl PieceTree
impl PieceTree
Sourcepub fn read_largest_contigous_chunk_at_byte(&self, offset: u32) -> (&[u8], u32)
pub fn read_largest_contigous_chunk_at_byte(&self, offset: u32) -> (&[u8], u32)
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.
pub fn chunk_at_byte(&self, offset: u32) -> (&[u8], u32)
pub fn chunk_at_char(&self, char_index: u32) -> (&[u8], u32)
pub fn chunk_at_line_break(&self, break_index: u32) -> (&[u8], u32)
pub fn line_break_to_byte(&self, break_index: u32) -> u32
pub fn try_line_break_to_byte(&self, break_index: u32) -> Option<u32>
pub fn line_to_byte(&self, target_line: u32) -> u32
pub fn try_line_to_byte(&self, target_line: u32) -> Option<u32>
pub fn char_to_byte(&self, char_index: u32) -> u32
pub fn try_char_to_byte(&self, char_index: u32) -> Option<u32>
pub fn byte_to_char(&self, byte_offset: u32) -> u32
pub fn try_byte_to_char(&self, byte_offset: u32) -> Option<u32>
pub fn byte_to_line(&self, offset: u32) -> u32
pub fn try_byte_to_line(&self, offset: u32) -> Option<u32>
pub fn byte_to_line_col(&self, offset: u32) -> (u32, u32)
pub fn try_byte_to_line_col(&self, offset: u32) -> Option<(u32, u32)>
pub fn pieces(&self) -> PieceTreeIter<'_> ⓘ
pub fn get_line_range(&self, line: u32) -> (u32, u32)
pub fn try_get_line_range(&self, line: u32) -> Option<(u32, u32)>
pub fn get_line_content_allocating(&self, line: u32) -> Option<String>
Sourcepub fn char(&self, char_index: u32) -> char
pub fn char(&self, char_index: u32) -> char
Char at a given char index. O(log n) to find the piece, then a short scan within the piece.
Sourcepub fn try_char(&self, char_index: u32) -> Option<char>
pub fn try_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.
Sourcepub fn slice_chars(&self, char_start: u32, char_end: u32) -> SliceChars<'_> ⓘ
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.
Sourcepub fn slice_bytes(&self, byte_start: u32, byte_end: u32) -> SliceChars<'_> ⓘ
pub fn slice_bytes(&self, byte_start: u32, byte_end: u32) -> SliceChars<'_> ⓘ
Returns a non-allocating iterator of chars over the given byte range.
Sourcepub fn line(&self, line: u32) -> TreeSlice<'_>
pub fn line(&self, line: u32) -> TreeSlice<'_>
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.