pub struct PieceTree { /* private fields */ }Expand description
The main piece table structure with integrated line tracking
Implementations§
Source§impl PieceTree
impl PieceTree
Sourcepub fn new(
location: BufferLocation,
offset: usize,
bytes: usize,
line_feed_cnt: Option<usize>,
) -> Self
pub fn new( location: BufferLocation, offset: usize, bytes: usize, line_feed_cnt: Option<usize>, ) -> Self
Create a new piece table with a single initial piece
Sourcepub fn from_leaves(leaves: &[LeafData]) -> Self
pub fn from_leaves(leaves: &[LeafData]) -> Self
Build a PieceTree from an explicit list of leaves.
Sourcepub fn find_by_offset(&self, offset: usize) -> Option<PieceInfo>
pub fn find_by_offset(&self, offset: usize) -> Option<PieceInfo>
Find the piece at the given byte offset
Sourcepub fn cursor_at_offset(&self, offset: usize) -> Cursor
pub fn cursor_at_offset(&self, offset: usize) -> Cursor
Create a cursor at the given byte offset Note: line/col calculation should be done by LineIndex
Sourcepub fn insert(
&mut self,
offset: usize,
location: BufferLocation,
buffer_offset: usize,
bytes: usize,
line_feed_cnt: Option<usize>,
buffers: &[StringBuffer],
) -> Cursor
pub fn insert( &mut self, offset: usize, location: BufferLocation, buffer_offset: usize, bytes: usize, line_feed_cnt: Option<usize>, buffers: &[StringBuffer], ) -> Cursor
Insert text at the given offset Returns new cursor after the inserted text line_feed_cnt: number of line feeds in the inserted text (None if unknown/not computed) buffers: reference to the string buffers for computing line feeds during splits
Sourcepub fn root(&self) -> Arc<PieceTreeNode>
pub fn root(&self) -> Arc<PieceTreeNode>
Get a clone of the root node (shared via Arc)
Sourcepub fn insert_at_position(
&mut self,
line: usize,
column: usize,
location: BufferLocation,
buffer_offset: usize,
bytes: usize,
line_feed_cnt: usize,
buffers: &[StringBuffer],
) -> Cursor
pub fn insert_at_position( &mut self, line: usize, column: usize, location: BufferLocation, buffer_offset: usize, bytes: usize, line_feed_cnt: usize, buffers: &[StringBuffer], ) -> Cursor
Insert text at the given position (line, column) Returns new cursor after the inserted text This converts position to offset, then uses path-copying insert
Sourcepub fn split_at_offset(&mut self, offset: usize, buffers: &[StringBuffer])
pub fn split_at_offset(&mut self, offset: usize, buffers: &[StringBuffer])
Split a piece at the given offset without inserting anything This is useful for isolating a chunk of a large piece for partial loading
If the offset is in the middle of a piece, that piece will be split into two pieces. If the offset is at a piece boundary, nothing changes. Does nothing if offset is 0 or >= total_bytes.
Sourcepub fn replace_buffer_reference(
&mut self,
old_buffer_id: usize,
old_buffer_offset: usize,
old_buffer_bytes: usize,
new_buffer_location: BufferLocation,
)
pub fn replace_buffer_reference( &mut self, old_buffer_id: usize, old_buffer_offset: usize, old_buffer_bytes: usize, new_buffer_location: BufferLocation, )
Replace buffer references in pieces This is used when creating chunk buffers from large unloaded buffers
Finds all pieces that reference the old buffer at the specified offset/bytes and updates them to reference the new buffer at offset 0.
Sourcepub fn delete(
&mut self,
offset: usize,
delete_bytes: usize,
buffers: &[StringBuffer],
)
pub fn delete( &mut self, offset: usize, delete_bytes: usize, buffers: &[StringBuffer], )
Delete text starting at offset for the given number of bytes
Sourcepub fn delete_position_range(
&mut self,
start_line: usize,
start_column: usize,
end_line: usize,
end_column: usize,
buffers: &[StringBuffer],
)
pub fn delete_position_range( &mut self, start_line: usize, start_column: usize, end_line: usize, end_column: usize, buffers: &[StringBuffer], )
Delete text in a range specified by positions (start_line, start_col) to (end_line, end_col) Converts positions to offsets, then uses path-copying delete
Sourcepub fn total_bytes(&self) -> usize
pub fn total_bytes(&self) -> usize
Get the total number of bytes in the document
Sourcepub fn line_count(&self) -> Option<usize>
pub fn line_count(&self) -> Option<usize>
Get the total number of lines in the document Line count = line feeds + 1 Returns None if any piece has unknown line count
Sourcepub fn piece_info_for_line(
&self,
target_line: usize,
) -> Option<(usize, usize, usize, usize, usize)>
pub fn piece_info_for_line( &self, target_line: usize, ) -> Option<(usize, usize, usize, usize, usize)>
Find the document byte offset and metadata for the piece containing a target line.
Uses only the tree’s lf_left / line_feed_cnt metadata (no buffer data needed).
Returns (doc_byte_offset, buffer_id, piece_offset, piece_bytes, lines_before_piece)
where lines_before_piece is the number of complete lines before this piece.
Sourcepub fn update_leaf_line_feeds(&mut self, updates: &[(usize, usize)])
pub fn update_leaf_line_feeds(&mut self, updates: &[(usize, usize)])
Rebuild the tree with updated line feed counts from external data.
Takes a slice of (leaf_index, line_feed_count) pairs. Each entry updates
the corresponding leaf (by order in the tree) with the given line feed count.
After updating, rebuilds the tree so that lf_left values on internal nodes
are correct.
This keeps the piece tree free of I/O — the caller is responsible for reading chunk data and counting line feeds externally.
Sourcepub fn update_leaf_line_feeds_path_copy(&mut self, updates: &[(usize, usize)])
pub fn update_leaf_line_feeds_path_copy(&mut self, updates: &[(usize, usize)])
Update leaf line feed counts using path-copying.
Like update_leaf_line_feeds but only clones nodes along each updated
leaf’s root-to-leaf path, preserving Arc::ptr_eq for unmodified subtrees.
Sourcepub fn get_leaves(&self) -> Vec<LeafData>
pub fn get_leaves(&self) -> Vec<LeafData>
Get all leaves in order (for debugging)
Sourcepub fn split_leaves_to_chunk_size(&mut self, max_bytes: usize)
pub fn split_leaves_to_chunk_size(&mut self, max_bytes: usize)
Split any leaves larger than max_bytes into smaller pieces.
This is a bulk operation that collects all leaves, subdivides oversized
ones, and rebuilds the tree once. Line-feed counts on the new sub-leaves
are set to None (the caller is expected to fill them in via scanning).
Sourcepub fn offset_to_position(
&self,
offset: usize,
buffers: &[StringBuffer],
) -> Option<(usize, usize)>
pub fn offset_to_position( &self, offset: usize, buffers: &[StringBuffer], ) -> Option<(usize, usize)>
Convert byte offset to line/column position using tree’s line metadata
Sourcepub fn position_to_offset(
&self,
line: usize,
column: usize,
buffers: &[StringBuffer],
) -> usize
pub fn position_to_offset( &self, line: usize, column: usize, buffers: &[StringBuffer], ) -> usize
Convert line/column position to byte offset using tree’s line metadata
Sourcepub fn line_range(
&self,
line: usize,
buffers: &[StringBuffer],
) -> Option<(usize, Option<usize>)>
pub fn line_range( &self, line: usize, buffers: &[StringBuffer], ) -> Option<(usize, Option<usize>)>
Get the byte range for a specific line
Sourcepub fn iter_pieces_in_range(&self, start: usize, end: usize) -> PieceRangeIter ⓘ
pub fn iter_pieces_in_range(&self, start: usize, end: usize) -> PieceRangeIter ⓘ
Iterate through pieces overlapping a byte range Does ONE O(log n) tree traversal, then iterates sequentially
Sourcepub fn apply_bulk_edits<F>(
&mut self,
edits: &[(usize, usize, &str)],
buffers: &[StringBuffer],
add_text_fn: F,
) -> isize
pub fn apply_bulk_edits<F>( &mut self, edits: &[(usize, usize, &str)], buffers: &[StringBuffer], add_text_fn: F, ) -> isize
Apply multiple edits in a single tree traversal + rebuild
§Arguments
edits- Vec of (position, delete_len, insert_text), MUST be sorted descending by positionbuffers- Reference to string buffers (for line feed computation)add_text_fn- Function to add text to buffer, returns (BufferLocation, offset, bytes)
§Complexity
O(pieces + edits) instead of O(pieces × edits)
§Returns
The net change in total bytes
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PieceTree
impl RefUnwindSafe for PieceTree
impl Send for PieceTree
impl Sync for PieceTree
impl Unpin for PieceTree
impl UnsafeUnpin for PieceTree
impl UnwindSafe for PieceTree
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<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more