pub trait DocumentModel {
// Required methods
fn capabilities(&self) -> DocumentCapabilities;
fn get_viewport_content(
&mut self,
start_pos: DocumentPosition,
max_lines: usize,
) -> Result<ViewportContent>;
fn position_to_offset(&self, pos: DocumentPosition) -> Result<usize>;
fn offset_to_position(&self, offset: usize) -> DocumentPosition;
fn get_range(
&mut self,
start: DocumentPosition,
end: DocumentPosition,
) -> Result<String>;
fn get_line_content(&mut self, line_number: usize) -> Option<String>;
fn get_chunk_at_offset(
&mut self,
offset: usize,
size: usize,
) -> Result<(usize, String)>;
fn insert(&mut self, pos: DocumentPosition, text: &str) -> Result<usize>;
fn delete(
&mut self,
start: DocumentPosition,
end: DocumentPosition,
) -> Result<()>;
fn replace(
&mut self,
start: DocumentPosition,
end: DocumentPosition,
text: &str,
) -> Result<()>;
fn find_matches(
&mut self,
pattern: &str,
search_range: Option<(DocumentPosition, DocumentPosition)>,
) -> Result<Vec<usize>>;
// Provided method
fn has_line_index(&self) -> bool { ... }
}Expand description
High-level document interface supporting both line and byte operations
This trait provides a clean abstraction for all editor operations, whether rendering, editing, or searching. It works transparently with both small files (with line indexing) and huge files (with lazy loading).
Required Methods§
Sourcefn capabilities(&self) -> DocumentCapabilities
fn capabilities(&self) -> DocumentCapabilities
Get document capabilities
Sourcefn get_viewport_content(
&mut self,
start_pos: DocumentPosition,
max_lines: usize,
) -> Result<ViewportContent>
fn get_viewport_content( &mut self, start_pos: DocumentPosition, max_lines: usize, ) -> Result<ViewportContent>
Get content at a viewport (the core rendering primitive)
Returns lines starting from position, up to max_lines. This works for both line-based and byte-based positions.
For large files, this automatically loads chunks on-demand (never scans entire file).
Sourcefn position_to_offset(&self, pos: DocumentPosition) -> Result<usize>
fn position_to_offset(&self, pos: DocumentPosition) -> Result<usize>
Convert position to byte offset (always works)
Sourcefn offset_to_position(&self, offset: usize) -> DocumentPosition
fn offset_to_position(&self, offset: usize) -> DocumentPosition
Convert byte offset to a position
For huge files without line index, returns ByteOffset. For small files, returns LineColumn.
Sourcefn get_range(
&mut self,
start: DocumentPosition,
end: DocumentPosition,
) -> Result<String>
fn get_range( &mut self, start: DocumentPosition, end: DocumentPosition, ) -> Result<String>
Get a range of text by positions May trigger lazy loading for large files
Sourcefn get_line_content(&mut self, line_number: usize) -> Option<String>
fn get_line_content(&mut self, line_number: usize) -> Option<String>
Get a single line if line indexing is available
Returns None if line indexing is not available. For large files, this may trigger lazy loading of chunks.
Sourcefn get_chunk_at_offset(
&mut self,
offset: usize,
size: usize,
) -> Result<(usize, String)>
fn get_chunk_at_offset( &mut self, offset: usize, size: usize, ) -> Result<(usize, String)>
Get text around a byte offset (for operations that don’t need exact lines)
Returns (offset, content) where offset is the start of returned content. May trigger lazy loading for large files
Sourcefn insert(&mut self, pos: DocumentPosition, text: &str) -> Result<usize>
fn insert(&mut self, pos: DocumentPosition, text: &str) -> Result<usize>
Insert text at a position
Returns the number of bytes inserted.
Sourcefn delete(
&mut self,
start: DocumentPosition,
end: DocumentPosition,
) -> Result<()>
fn delete( &mut self, start: DocumentPosition, end: DocumentPosition, ) -> Result<()>
Delete a range
Sourcefn replace(
&mut self,
start: DocumentPosition,
end: DocumentPosition,
text: &str,
) -> Result<()>
fn replace( &mut self, start: DocumentPosition, end: DocumentPosition, text: &str, ) -> Result<()>
Replace a range
Sourcefn find_matches(
&mut self,
pattern: &str,
search_range: Option<(DocumentPosition, DocumentPosition)>,
) -> Result<Vec<usize>>
fn find_matches( &mut self, pattern: &str, search_range: Option<(DocumentPosition, DocumentPosition)>, ) -> Result<Vec<usize>>
Find all matches of a pattern in a range
Returns byte offsets (always precise). May trigger lazy loading for large files
Provided Methods§
Sourcefn has_line_index(&self) -> bool
fn has_line_index(&self) -> bool
Check if line indexing is available
Implementors§
impl DocumentModel for EditorState
Implement DocumentModel trait for EditorState
This provides a clean abstraction layer between rendering/editing operations and the underlying text buffer implementation.