pub struct Buffer { /* private fields */ }Expand description
A text buffer: the document, where the cursor is, and how it got here.
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn from_text(id: BufferId, path: Option<PathBuf>, text: &str) -> Self
pub fn from_text(id: BufferId, path: Option<PathBuf>, text: &str) -> Self
A buffer over text already in hand. Used by tests and by the agent’s view of a
file it supplied; Buffer::load is the path for files on disk.
Sourcepub fn load(
id: BufferId,
fs: &dyn FileSystemService,
path: &Path,
) -> EditResult<Self>
pub fn load( id: BufferId, fs: &dyn FileSystemService, path: &Path, ) -> EditResult<Self>
Read a file through the service boundary — never std::fs (CONTRIBUTING.md invariants).
Sourcepub fn save(&mut self, fs: &dyn FileSystemService) -> EditResult<()>
pub fn save(&mut self, fs: &dyn FileSystemService) -> EditResult<()>
Write the buffer back, restoring the line ending it arrived with.
Saving ends the current undo group: a save is a boundary the user thinks in, so typing afterwards should not merge into what was already written out.
Sourcepub fn to_disk_string(&self) -> String
pub fn to_disk_string(&self) -> String
The document as it would be written out, with the on-disk line ending restored.
pub fn id(&self) -> BufferId
pub fn path(&self) -> Option<&Path>
pub fn text(&self) -> &Rope
pub fn version(&self) -> Version
pub fn line_ending(&self) -> LineEnding
pub fn selection(&self) -> &Selection
Sourcepub fn set_selection(&mut self, selection: Selection)
pub fn set_selection(&mut self, selection: Selection)
Move the cursor. Ends the current undo group, because a deliberate move is where a user expects one undo step to stop and the next to begin.
Sourcepub fn display_name(&self) -> String
pub fn display_name(&self) -> String
The name to show on a tab.
pub fn can_undo(&self) -> bool
pub fn can_redo(&self) -> bool
Sourcepub fn take_pending_changes(&mut self) -> Vec<ChangeSet>
pub fn take_pending_changes(&mut self) -> Vec<ChangeSet>
Drain the changes this buffer has not yet reported for document sync.
Captured at mutation time because it cannot be reconstructed afterwards:
History::Entry records neither a version nor a source.
Sourcepub fn transaction(
&mut self,
changes: ChangeSet,
source: EditSource,
) -> EditTransaction
pub fn transaction( &mut self, changes: ChangeSet, source: EditSource, ) -> EditTransaction
Build a transaction against the current state, for source.
Goes through History::group_for, so a run of typing coalesces and anything
else gets its own undo step without the caller having to know the policy.
Sourcepub fn apply(&mut self, transaction: &EditTransaction) -> EditResult<()>
pub fn apply(&mut self, transaction: &EditTransaction) -> EditResult<()>
Apply a transaction — the single path by which this document ever changes.
Rejects anything authored against a different revision or a different-sized document. That check is the whole reason agent edits are safe: a proposal written while the human was typing cannot land blind, it lands rebased or not at all.
Sourcepub fn edit(
&mut self,
from: usize,
to: usize,
insert: &str,
source: EditSource,
) -> EditResult<()>
pub fn edit( &mut self, from: usize, to: usize, insert: &str, source: EditSource, ) -> EditResult<()>
Convenience for the common shape: replace from..to with insert.
Sourcepub fn mark_saved(&mut self, version: Version)
pub fn mark_saved(&mut self, version: Version)
Record that version reached disk.
The async save path: the write happens on the worker thread, so by the time it succeeds the user may have typed again. Comparing versions rather than clearing a flag means a buffer edited mid-write stays correctly dirty.
pub fn move_left(&mut self)
pub fn move_right(&mut self)
pub fn move_line_start(&mut self)
pub fn move_line_end(&mut self)
Sourcepub fn move_line(&mut self, down: bool)
pub fn move_line(&mut self, down: bool)
Move a line up or down, keeping the sticky column so a short line in between does not permanently drag the cursor left.
Sourcepub fn decorations(&self) -> &DecorationSet
pub fn decorations(&self) -> &DecorationSet
The cursor as a (line, column) pair, for the status bar and the renderer.
pub fn decorations_mut(&mut self) -> &mut DecorationSet
Sourcepub fn line_range(&self, line: usize) -> (usize, usize)
pub fn line_range(&self, line: usize) -> (usize, usize)
The char range of line, for clipping decorations to it.
pub fn scroll_top(&self) -> usize
Sourcepub fn scroll_to_cursor(&mut self, height: usize)
pub fn scroll_to_cursor(&mut self, height: usize)
Scroll the least amount that brings the cursor back into a height-line viewport.
Called by the commands that move the cursor, never by the renderer — render
stays a pure function of the model (ARCHITECTURE.md §7.1).
pub fn cursor_position(&self) -> (usize, usize)
Sourcepub fn insert(&mut self, text: &str, source: EditSource) -> EditResult<()>
pub fn insert(&mut self, text: &str, source: EditSource) -> EditResult<()>
Insert text at the cursor, replacing the selection if there is one.
Sourcepub fn delete_backward(&mut self) -> EditResult<()>
pub fn delete_backward(&mut self) -> EditResult<()>
Delete backwards: the selection if there is one, otherwise the preceding char.
Sourcepub fn delete_forward(&mut self) -> EditResult<()>
pub fn delete_forward(&mut self) -> EditResult<()>
Delete forwards: the selection if there is one, otherwise the following char.
Sourcepub fn undo(&mut self) -> bool
pub fn undo(&mut self) -> bool
Reverse the most recent undo group. Returns whether anything happened.