pub struct Buffer { /* private fields */ }Expand description
A text buffer with line-based storage.
The buffer stores text as a vector of lines, where each line is a String without the trailing newline character. This provides efficient line-based access while maintaining a simple implementation.
§Invariants
- An empty buffer has zero lines (not one empty line)
- Lines do not contain newline characters
- 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
impl Buffer
Sourcepub const fn with_id(id: BufferId) -> Self
pub const fn with_id(id: BufferId) -> Self
Create a buffer with a specific ID.
This is primarily useful for testing.
Sourcepub fn from_string(content: &str) -> Self
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.
Sourcepub const fn is_modified(&self) -> bool
pub const fn is_modified(&self) -> bool
Check if the buffer has unsaved modifications.
Sourcepub const fn set_modified(&mut self, modified: bool)
pub const fn set_modified(&mut self, modified: bool)
Mark the buffer as modified or unmodified.
Sourcepub fn set_file_path(&mut self, path: Option<String>)
pub fn set_file_path(&mut self, path: Option<String>)
Set the file path for this buffer.
Sourcepub fn line_hash(&self, line_idx: usize) -> Option<u64>
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.
Sourcepub fn line_hashes(&self) -> Vec<u64>
pub fn line_hashes(&self) -> Vec<u64>
Get all line hashes (for saturator requests).
Sourcepub const fn line_count(&self) -> usize
pub const fn line_count(&self) -> usize
Get the number of lines in the buffer.
Sourcepub fn line(&self, index: usize) -> Option<&str>
pub fn line(&self, index: usize) -> Option<&str>
Get a specific line by index.
Returns None if the index is out of bounds.
Sourcepub fn line_len(&self, index: usize) -> Option<usize>
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.
Sourcepub fn set_content(&mut self, content: &str)
pub fn set_content(&mut self, content: &str)
Set the full content from a string.
This replaces all existing content and resets the cursor.
Sourcepub fn insert_at(&mut self, pos: Position, text: &str)
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.
Sourcepub fn delete_at(&mut self, pos: Position, count: usize) -> String
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.
Sourcepub fn delete_range(&mut self, start: Position, end: Position) -> String
pub fn delete_range(&mut self, start: Position, end: Position) -> String
Delete a range of text.
Returns the deleted text.
Sourcepub fn position_to_byte(&self, pos: Position) -> usize
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.
Sourcepub fn byte_to_position(&self, byte_offset: usize) -> Position
pub fn byte_to_position(&self, byte_offset: usize) -> Position
Convert a byte offset to a position.