Skip to main content

Buffer

Struct Buffer 

Source
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

Source

pub fn new(id: BufferId) -> Self

An empty, untitled buffer.

Source

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.

Source

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).

Source

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.

Source

pub fn to_disk_string(&self) -> String

The document as it would be written out, with the on-disk line ending restored.

Source

pub fn id(&self) -> BufferId

Source

pub fn path(&self) -> Option<&Path>

Source

pub fn text(&self) -> &Rope

Source

pub fn version(&self) -> Version

Source

pub fn line_ending(&self) -> LineEnding

Source

pub fn selection(&self) -> &Selection

Source

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.

Source

pub fn is_dirty(&self) -> bool

Whether there are changes not yet written to disk.

Source

pub fn display_name(&self) -> String

The name to show on a tab.

Source

pub fn can_undo(&self) -> bool

Source

pub fn can_redo(&self) -> bool

Source

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.

Source

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.

Source

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.

Source

pub fn edit( &mut self, from: usize, to: usize, insert: &str, source: EditSource, ) -> EditResult<()>

Convenience for the common shape: replace from..to with insert.

Source

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.

Source

pub fn move_left(&mut self)

Source

pub fn move_right(&mut self)

Source

pub fn move_line_start(&mut self)

Source

pub fn move_line_end(&mut self)

Source

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.

Source

pub fn decorations(&self) -> &DecorationSet

The cursor as a (line, column) pair, for the status bar and the renderer.

Source

pub fn decorations_mut(&mut self) -> &mut DecorationSet

Source

pub fn line_range(&self, line: usize) -> (usize, usize)

The char range of line, for clipping decorations to it.

Source

pub fn scroll_top(&self) -> usize

Source

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).

Source

pub fn cursor_position(&self) -> (usize, usize)

Source

pub fn insert(&mut self, text: &str, source: EditSource) -> EditResult<()>

Insert text at the cursor, replacing the selection if there is one.

Source

pub fn delete_backward(&mut self) -> EditResult<()>

Delete backwards: the selection if there is one, otherwise the preceding char.

Source

pub fn delete_forward(&mut self) -> EditResult<()>

Delete forwards: the selection if there is one, otherwise the following char.

Source

pub fn undo(&mut self) -> bool

Reverse the most recent undo group. Returns whether anything happened.

Source

pub fn redo(&mut self) -> bool

Trait Implementations§

Source§

impl Debug for Buffer

Source§

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

Formats the value using the given formatter. 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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.