Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer { /* private fields */ }
Expand description

A text buffer with rope-based storage.

The buffer stores text as a rope — a balanced B-tree of text chunks. This provides O(log n) insert/delete, O(1) clone via structural sharing (Arc<Node>), and O(log n) position conversion.

§Invariants

  • An empty buffer has zero lines (not one empty line)
  • Positions are clamped to valid ranges on access

§Cursor Isolation (#471)

Buffer does NOT have a cursor field. Cursor is per-client state in Window. All edit operations take explicit positions instead of using an internal cursor.

§Example

use reovim_kernel::api::v1::*;

let mut buf = Buffer::from_string("Hello\nWorld");
assert_eq!(buf.line_count(), 2);
assert_eq!(buf.line(0), Some("Hello"));

buf.insert_at(Position::new(0, 5), "!");
assert_eq!(buf.line(0), Some("Hello!"));

Implementations§

Source§

impl Buffer

Source

pub fn new() -> Self

Create a new empty buffer.

Source

pub fn with_id(id: BufferId) -> Self

Create a buffer with a specific ID.

This is primarily useful for testing.

Source

pub fn from_string(content: &str) -> Self

Create a buffer from a string.

The string is split by newlines into lines. An empty string results in a buffer with zero lines.

Source

pub const fn id(&self) -> BufferId

Get the buffer ID.

Source

pub const fn is_modified(&self) -> bool

Check if the buffer has unsaved modifications.

Source

pub const fn set_modified(&mut self, modified: bool)

Mark the buffer as modified or unmodified.

Source

pub fn file_path(&self) -> Option<&str>

Get the file path associated with this buffer.

Source

pub fn set_file_path(&mut self, path: Option<String>)

Set the file path for this buffer.

Source

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

Compute hash of a line for cache validation.

Uses DefaultHasher for speed over cryptographic strength. Returns None if line index is out of bounds.

Source

pub fn line_hashes(&self) -> Vec<u64>

Get all line hashes (for saturator requests).

Source

pub fn line_count(&self) -> usize

Get the number of lines in the buffer.

Source

pub fn is_empty(&self) -> bool

Check if the buffer is empty (has no lines).

Source

pub fn line(&self, index: usize) -> Option<&str>

Get a specific line by index.

Returns None if the index is out of bounds.

Source

pub fn line_len(&self, index: usize) -> Option<usize>

Get the length of a specific line in characters.

Returns None if the index is out of bounds.

Source

pub fn content(&self) -> String

Get the full content as a string (lines joined with newlines).

Source

pub fn set_content(&mut self, content: &str)

Set the full content from a string.

This replaces all existing content.

Source

pub fn insert_at(&mut self, pos: Position, text: &str)

Insert text at a specific position.

This is a pure text operation - cursor management is the caller’s responsibility.

Source

pub fn delete_at(&mut self, pos: Position, count: usize) -> String

Delete text at a specific position.

Returns the deleted text. This is a pure text operation - cursor management is the caller’s responsibility.

Source

pub fn delete_range(&mut self, start: Position, end: Position) -> String

Delete a range of text.

Returns the deleted text.

Source

pub fn position_to_byte(&self, pos: Position) -> usize

Convert a position to a byte offset in the full content.

This is useful for tree-sitter and other byte-based APIs.

Source

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

Convert a byte offset to a position.

Trait Implementations§

Source§

impl Clone for Buffer

Source§

fn clone(&self) -> Buffer

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Buffer

Source§

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

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

impl Default for Buffer

Source§

fn default() -> Self

Returns the “default value” for a 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, 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.