pub struct TextBuffer { /* private fields */ }Expand description
A Ropey-backed text buffer.
Invariants and conventions:
- The backing store is a
ropey::Rope. - Public APIs should generally speak in char indices (Unicode scalar value offsets) and logical positions (line/col in chars) because Ropey’s safe indexing APIs are char-based.
- Byte indexing can be supported where needed, but should not be the primary index type for the editor core.
Higher-level editor state (modes, undo, viewports, etc.) should be built on top of this type rather than embedded inside it.
Implementations§
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self>
Load a file as UTF-8 and create a buffer.
This uses ropey’s streaming reader path and requires valid UTF-8.
Sourcepub fn rope(&self) -> &Rope
pub fn rope(&self) -> &Rope
Access the underlying rope.
Prefer higher-level APIs in other modules for most editor operations.
Sourcepub fn rope_mut(&mut self) -> &mut Rope
pub fn rope_mut(&mut self) -> &mut Rope
Mutable access to the underlying rope (use this sparingly!)
Prefer dedicated editing APIs so invariants and bookkeeping remain easy to maintain.
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn insert(&mut self, pos: Pos, text: &str) -> Pos
pub fn insert(&mut self, pos: Pos, text: &str) -> Pos
Insert text at the given logical position.
Returns the new cursor position (at the end of inserted text).
This is a primitive operation for higher-level editing commands.
Sourcepub fn delete_range(&mut self, a: Pos, b: Pos) -> Pos
pub fn delete_range(&mut self, a: Pos, b: Pos) -> Pos
Delete a range between two positions (order-independent).
Returns the resulting cursor position (at the start of deletion).
Sourcepub fn delete_selection(&mut self, sel: Selection) -> (Pos, bool)
pub fn delete_selection(&mut self, sel: Selection) -> (Pos, bool)
Delete the selection (if any). Returns (new_cursor, did_delete).
Sourcepub fn backspace(&mut self, sel: Selection) -> Selection
pub fn backspace(&mut self, sel: Selection) -> Selection
Backspace behavior:
- if the selection is non-empty, delete it
- otherwise delete the char before the cursor (if any)
Returns an empty selection at the updated cursor.
Sourcepub fn delete(&mut self, sel: Selection) -> Selection
pub fn delete(&mut self, sel: Selection) -> Selection
Delete (forward) behavior:
- if the selection is non-empty, delete it
- otherwise delete the char at the cursor (if any)
Returns an empty selection at the updated cursor.
Sourcepub fn insert_newline(&mut self, sel: Selection) -> Selection
pub fn insert_newline(&mut self, sel: Selection) -> Selection
Insert a newline at the cursor (or replace the selection).
Returns an empty selection at the updated cursor.
Sourcepub fn replace_line_indent(
&mut self,
line: usize,
indent: &str,
) -> Option<(usize, usize)>
pub fn replace_line_indent( &mut self, line: usize, indent: &str, ) -> Option<(usize, usize)>
Replace the leading whitespace on line.
Returns (removed_chars, added_chars) when the line changed.
Sourcepub fn apply_edit(&mut self, edit: Edit) -> Pos
pub fn apply_edit(&mut self, edit: Edit) -> Pos
Apply an Edit expressed in char indices.
Returns the resulting cursor position (end of inserted text, or start of deletion).
Sourcepub fn apply_edits(&mut self, edits: &[Edit]) -> EditBatchSummary
pub fn apply_edits(&mut self, edits: &[Edit]) -> EditBatchSummary
Apply multiple edits sequentially and return a transaction-style summary.
Edits are applied in input order against the current buffer state.
Sourcepub fn replace_selection(&mut self, sel: Selection, text: &str) -> Selection
pub fn replace_selection(&mut self, sel: Selection, text: &str) -> Selection
Replace the current selection with text (if selection is empty, behaves like insert).
This is a convenience method that a bunch of editor actions can use.
Returns an empty selection at the updated cursor.
Sourcepub fn paste_before(&mut self, cursor: Pos, text: &str, linewise: bool) -> Pos
pub fn paste_before(&mut self, cursor: Pos, text: &str, linewise: bool) -> Pos
Paste text before the given cursor.
When linewise is true, insertion happens at the start of the current line
and the returned cursor stays at that insertion point.
When linewise is false, insertion happens at the cursor and the returned
cursor is at the end of inserted text.
Sourcepub fn paste_after(&mut self, cursor: Pos, text: &str, linewise: bool) -> Pos
pub fn paste_after(&mut self, cursor: Pos, text: &str, linewise: bool) -> Pos
Paste text after the given cursor.
When linewise is true, insertion happens at the beginning of the next
logical line (clamped at the buffer boundary), and the returned cursor
stays at the insertion point.
When linewise is false, insertion happens after the cursor char on the
current line (or at line end when already at EOL), and the returned cursor
is at the end of inserted text.
Sourcepub fn move_line_range_up_once(
&mut self,
start_line: usize,
end_line_inclusive: usize,
) -> Option<(usize, usize)>
pub fn move_line_range_up_once( &mut self, start_line: usize, end_line_inclusive: usize, ) -> Option<(usize, usize)>
Move a contiguous line range up by one line.
Returns the moved range after the operation, or None when movement is
not possible (for example when the range already starts at line 0).
Sourcepub fn move_line_range_up(
&mut self,
start_line: usize,
end_line_inclusive: usize,
count: usize,
) -> Option<(usize, usize)>
pub fn move_line_range_up( &mut self, start_line: usize, end_line_inclusive: usize, count: usize, ) -> Option<(usize, usize)>
Move a contiguous line range up by up to count lines.
Returns the moved range after all possible steps, or None when the range
cannot be moved up at all.
Sourcepub fn move_line_range_down_once(
&mut self,
start_line: usize,
end_line_inclusive: usize,
) -> Option<(usize, usize)>
pub fn move_line_range_down_once( &mut self, start_line: usize, end_line_inclusive: usize, ) -> Option<(usize, usize)>
Move a contiguous line range down by one line.
Returns the moved range after the operation, or None when movement is
not possible (for example when the range already ends at the final line).
Sourcepub fn move_line_range_down(
&mut self,
start_line: usize,
end_line_inclusive: usize,
count: usize,
) -> Option<(usize, usize)>
pub fn move_line_range_down( &mut self, start_line: usize, end_line_inclusive: usize, count: usize, ) -> Option<(usize, usize)>
Move a contiguous line range down by up to count lines.
Returns the moved range after all possible steps, or None when the range
cannot be moved down at all.
Sourcepub fn indent_line_span(
&mut self,
start_line: usize,
end_line_inclusive: usize,
count: usize,
) -> Vec<(usize, usize)>
pub fn indent_line_span( &mut self, start_line: usize, end_line_inclusive: usize, count: usize, ) -> Vec<(usize, usize)>
Indent each line in a contiguous span by count tab characters.
Returns (line, chars_added) for every touched line.
Sourcepub fn outdent_line_span(
&mut self,
start_line: usize,
end_line_inclusive: usize,
count: usize,
) -> Vec<(usize, usize)>
pub fn outdent_line_span( &mut self, start_line: usize, end_line_inclusive: usize, count: usize, ) -> Vec<(usize, usize)>
Outdent each line in a contiguous span by up to count levels.
One outdent level removes either one leading tab or up to four leading spaces.
Returns (line, chars_removed) for every touched line.
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn len_lines(&self) -> usize
pub fn len_lines(&self) -> usize
Number of lines in the buffer.
Ropey counts lines by '\n' boundaries and always reports at least 1 line,
even for empty text.
Sourcepub fn clamp_line(&self, line: usize) -> usize
pub fn clamp_line(&self, line: usize) -> usize
Clamp a line index to the valid range [0, len_lines - 1].
If the buffer is empty, Ropey still reports len_lines() == 1, so this
always returns a valid line index.
Sourcepub fn line_to_char(&self, line: usize) -> usize
pub fn line_to_char(&self, line: usize) -> usize
Returns the absolute char index at the start of line.
line is clamped into a valid range.
Sourcepub fn char_to_line(&self, char_idx: usize) -> usize
pub fn char_to_line(&self, char_idx: usize) -> usize
Returns the line index containing char_idx.
char_idx is clamped to [0, len_chars].
Sourcepub fn line_len_chars(&self, line: usize) -> usize
pub fn line_len_chars(&self, line: usize) -> usize
Returns the length of line in chars, excluding a trailing '\n' if present.
This corresponds to the number of valid “columns” for a (line, col) cursor
model where the newline is not considered part of the line.
Sourcepub fn line_first_non_whitespace_col(&self, line: usize) -> usize
pub fn line_first_non_whitespace_col(&self, line: usize) -> usize
Returns the first non-whitespace column on line.
If the line is all whitespace or empty, this returns line_len_chars(line).
Sourcepub fn line_string(&self, line: usize) -> String
pub fn line_string(&self, line: usize) -> String
Returns the line content as a String, excluding a trailing '\n' if present.
Sourcepub fn line_slice(&self, line: usize) -> RopeSlice<'_>
pub fn line_slice(&self, line: usize) -> RopeSlice<'_>
Returns a non-allocating line slice excluding a trailing '\n' if present.
Sourcepub fn line_char_range(&self, line: usize) -> Range<usize>
pub fn line_char_range(&self, line: usize) -> Range<usize>
Returns the char range [start, end) for the line content, excluding a trailing '\n'.
This will be useful for operations like “delete to end of line” or yanking the line content without the newline.
Sourcepub fn line_full_end_char(&self, line: usize) -> usize
pub fn line_full_end_char(&self, line: usize) -> usize
Returns the absolute char index of the end of line, including a trailing
'\n' when one exists.
This is useful for line-block transforms that need stable slice boundaries across both newline-terminated and non-terminated final lines.
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn clamp_pos(&self, pos: Pos) -> Pos
pub fn clamp_pos(&self, pos: Pos) -> Pos
Clamp a position to a valid location in the buffer.
- Line is clamped to
[0, len_lines - 1] - Column is clamped to
[0, line_len_chars(line)]
Sourcepub fn pos_to_char(&self, pos: Pos) -> usize
pub fn pos_to_char(&self, pos: Pos) -> usize
Convert Pos (line+col) to absolute char index in the rope.
The position is clamped first.
Sourcepub fn char_to_pos(&self, char_idx: usize) -> Pos
pub fn char_to_pos(&self, char_idx: usize) -> Pos
Convert absolute char index to Pos (line+col).
char_idx is clamped to [0, len_chars].
Sourcepub fn move_left(&self, pos: Pos) -> Pos
pub fn move_left(&self, pos: Pos) -> Pos
Move position left by one char, staying within buffer.
Sourcepub fn move_right(&self, pos: Pos) -> Pos
pub fn move_right(&self, pos: Pos) -> Pos
Move position right by one char, staying within buffer.
Sourcepub fn move_up(&self, pos: Pos) -> Pos
pub fn move_up(&self, pos: Pos) -> Pos
Move up one line, preserving column as much as possible.
This implementation does not track a preferred column.
Sourcepub fn move_down(&self, pos: Pos) -> Pos
pub fn move_down(&self, pos: Pos) -> Pos
Move down one line, preserving column as much as possible.
This implementation does not track a preferred column.
Sourcepub fn char_at(&self, pos: Pos) -> Option<char>
pub fn char_at(&self, pos: Pos) -> Option<char>
Get the char at a position, if it’s within the line’s content (not including newline).
Sourcepub fn char_before(&self, pos: Pos) -> Option<char>
pub fn char_before(&self, pos: Pos) -> Option<char>
Get the char before a position, if one exists.
Source§impl TextBuffer
impl TextBuffer
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn line_span_char_range(
&self,
start_line: usize,
end_line_inclusive: usize,
) -> Range<usize>
pub fn line_span_char_range( &self, start_line: usize, end_line_inclusive: usize, ) -> Range<usize>
Return a char range spanning full lines from start_line..=end_line_inclusive.
The end of the range includes a trailing newline when one exists.
Sourcepub fn line_span_pos_range(
&self,
start_line: usize,
end_line_inclusive: usize,
) -> (Pos, Pos)
pub fn line_span_pos_range( &self, start_line: usize, end_line_inclusive: usize, ) -> (Pos, Pos)
Return a position range spanning full lines from start_line..=end_line_inclusive.
Sourcepub fn line_span_text(
&self,
start_line: usize,
end_line_inclusive: usize,
) -> String
pub fn line_span_text( &self, start_line: usize, end_line_inclusive: usize, ) -> String
Return text spanning full lines from start_line..=end_line_inclusive.
This preserves the underlying buffer content exactly.
Sourcepub fn line_span_text_linewise_register(
&self,
start_line: usize,
end_line_inclusive: usize,
) -> String
pub fn line_span_text_linewise_register( &self, start_line: usize, end_line_inclusive: usize, ) -> String
Return line-span text suitable for a line-wise register.
Ensures the returned text ends with '\n'.
Sourcepub fn visual_charwise_pos_range(&self, selection: Selection) -> (Pos, Pos)
pub fn visual_charwise_pos_range(&self, selection: Selection) -> (Pos, Pos)
Return the charwise visual selection as an order-normalized deletion range.
Visual charwise mode is inclusive of the cursor endpoint.
Sourcepub fn visual_linewise_pos_range(&self, selection: Selection) -> (Pos, Pos)
pub fn visual_linewise_pos_range(&self, selection: Selection) -> (Pos, Pos)
Return the linewise visual selection as a full-line deletion range.
Sourcepub fn visual_charwise_text(&self, selection: Selection) -> String
pub fn visual_charwise_text(&self, selection: Selection) -> String
Return visual charwise selection text (inclusive endpoint semantics).
Sourcepub fn visual_linewise_text(&self, selection: Selection) -> String
pub fn visual_linewise_text(&self, selection: Selection) -> String
Return visual linewise selection text for line-wise register semantics.
Sourcepub fn visual_blockwise_pos_ranges(
&self,
selection: Selection,
) -> Vec<(Pos, Pos)>
pub fn visual_blockwise_pos_ranges( &self, selection: Selection, ) -> Vec<(Pos, Pos)>
Return visual block selection text/deletion slices without line collapsing.
Sourcepub fn visual_blockwise_delete_ranges(
&self,
selection: Selection,
) -> Vec<(Pos, Pos)>
pub fn visual_blockwise_delete_ranges( &self, selection: Selection, ) -> Vec<(Pos, Pos)>
Return deletion ranges for visual block mode.
When the block fully covers a line’s content, delete the whole logical line so trailing text is pulled upward instead of leaving empty rows.
Sourcepub fn visual_blockwise_text(&self, selection: Selection) -> String
pub fn visual_blockwise_text(&self, selection: Selection) -> String
Return visual selection text for the current visual mode.
Sourcepub fn visual_selection_pos_ranges(
&self,
selection: Selection,
mode: VisualModeKind,
) -> Vec<(Pos, Pos)>
pub fn visual_selection_pos_ranges( &self, selection: Selection, mode: VisualModeKind, ) -> Vec<(Pos, Pos)>
Return visual selection deletion bounds for the current visual mode.
Sourcepub fn visual_selection_text(
&self,
selection: Selection,
mode: VisualModeKind,
) -> String
pub fn visual_selection_text( &self, selection: Selection, mode: VisualModeKind, ) -> String
Return visual selection text for the current visual mode.
Sourcepub fn visual_selection_edit_plan(
&self,
selection: Selection,
mode: VisualModeKind,
) -> VisualSelectionEditPlan
pub fn visual_selection_edit_plan( &self, selection: Selection, mode: VisualModeKind, ) -> VisualSelectionEditPlan
Build a mode-aware visual selection edit plan.
Sourcepub fn visual_selection_char_range_on_line(
&self,
selection: Selection,
mode: VisualModeKind,
line_idx: usize,
) -> Option<Range<usize>>
pub fn visual_selection_char_range_on_line( &self, selection: Selection, mode: VisualModeKind, line_idx: usize, ) -> Option<Range<usize>>
Return selection char bounds for a specific line_idx.
The returned range is half-open (start..end) in char-column units for
that line and follows visual-mode inclusive endpoint behaviour.
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn slice_chars_ref(&self, start: usize, end: usize) -> RopeSlice<'_>
pub fn slice_chars_ref(&self, start: usize, end: usize) -> RopeSlice<'_>
Get a non-allocating slice for a character range [start, end).
- Indices are clamped to the buffer bounds.
- If
start > end, the values are swapped.
Sourcepub fn slice_selection_ref(&self, sel: Selection) -> RopeSlice<'_>
pub fn slice_selection_ref(&self, sel: Selection) -> RopeSlice<'_>
Get a non-allocating slice for a selection (ordered).
Sourcepub fn slice_pos_range_ref(&self, a: Pos, b: Pos) -> RopeSlice<'_>
pub fn slice_pos_range_ref(&self, a: Pos, b: Pos) -> RopeSlice<'_>
Convenience: non-allocating slice by two logical positions (order-independent).
Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Get the full buffer as a String.
For large buffers, this allocates. Kept as an inherent method so call sites
can use b.to_string() without depending on Display/ToString.
Sourcepub fn slice_chars(&self, start: usize, end: usize) -> String
pub fn slice_chars(&self, start: usize, end: usize) -> String
Get a String for a character range [start, end).
- Indices are clamped to the buffer bounds.
- If
start > end, the values are swapped.
This is a convenience API; it allocates.
Sourcepub fn slice_selection(&self, sel: Selection) -> String
pub fn slice_selection(&self, sel: Selection) -> String
Get the selected text for a selection (ordered).
This is a convenience API; it allocates.
Sourcepub fn slice_pos_range(&self, a: Pos, b: Pos) -> String
pub fn slice_pos_range(&self, a: Pos, b: Pos) -> String
Convenience: slice by two logical positions (order-independent).
This is useful when there are two cursors/marks and the substring
between them without explicitly building a Selection.
Source§impl TextBuffer
impl TextBuffer
pub fn text_object_selection( &self, cursor: Pos, spec: TextObjectSpec, ) -> Option<(Selection, VisualModeKind)>
pub fn text_object_edit_plan( &self, cursor: Pos, spec: TextObjectSpec, ) -> Option<TextObjectEditPlan>
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn word_start_before(&self, pos: Pos) -> Pos
pub fn word_start_before(&self, pos: Pos) -> Pos
Find the start of the previous/current word-like run (b-style).
Sourcepub fn word_end_after(&self, pos: Pos) -> Pos
pub fn word_end_after(&self, pos: Pos) -> Pos
Find the end of the current/next word-like run (e-style).
Sourcepub fn word_start_after(&self, pos: Pos) -> Pos
pub fn word_start_after(&self, pos: Pos) -> Pos
Find the start of the next word-like run (w-style).
Trait Implementations§
Source§impl Clone for TextBuffer
impl Clone for TextBuffer
Source§fn clone(&self) -> TextBuffer
fn clone(&self) -> TextBuffer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more