Skip to main content

Buffer

Struct Buffer 

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

In-memory text buffer + cursor.

This is the core type the rest of hjkl-buffer builds on. The runtime viewport state the host publishes per render frame (top_row, top_col, width, height, wrap, text_width) lived on this struct prior to 0.0.34 (Patch C-δ.1); it now lives on the engine Host adapter. Methods that need viewport input (e.g. Buffer::ensure_cursor_visible, Buffer::cursor_screen_row) take a &Viewport / &mut Viewport parameter so the rope-walking math stays here while the runtime state moves out.

The lines invariant — at least one entry, never empty — is preserved by every mutation.

0.0.37: the per-row syntax span cache + the / search FSM state (pattern, per-row match cache, wrapscan) moved off Buffer per step 3 of DESIGN_33_METHOD_CLASSIFICATION.md. Spans now flow through the engine’s Editor::buffer_spans (populated from Host::syntax_highlights / install_syntax_spans) and pass into crate::BufferView as a slice parameter. Search state lives on Editor::search_state; the renderer takes the active pattern as a parameter.

Implementations§

Source§

impl Buffer

Source

pub fn new() -> Self

Construct an empty buffer with one empty row + cursor at (0, 0). Caller publishes a viewport size on first draw.

Source

pub fn from_str(text: &str) -> Self

Build a buffer from a flat string. Splits on \n; a trailing \n produces a trailing empty line (matches every text editor’s behaviour and keeps from_text(buf.as_string()) an identity round-trip in the common case).

Source

pub fn lines(&self) -> &[String]

Source

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

Source

pub fn cursor(&self) -> Position

Source

pub fn dirty_gen(&self) -> u64

Source

pub fn set_cursor(&mut self, pos: Position)

Set cursor without scrolling. Caller is responsible for calling Buffer::ensure_cursor_visible when they want viewport follow. Clamps row and col to valid positions so motion helpers don’t have to repeat the bound check.

Source

pub fn ensure_cursor_visible(&mut self, viewport: &mut Viewport)

Bring the cursor into the visible viewport, scrolling by the minimum amount needed. When viewport.wrap != Wrap::None and viewport.text_width > 0, scrolling is screen-line aware: top_row is advanced one visible doc row at a time until the cursor’s screen row falls inside the viewport’s height.

0.0.34 (Patch C-δ.1): the viewport is no longer a buffer field; callers thread a &mut Viewport (typically owned by the engine Host).

Source

pub fn cursor_screen_row(&self, viewport: &Viewport) -> Option<usize>

Cursor’s screen row offset (0-based) from viewport.top_row under the current wrap mode + text_width. None when wrap is off, the cursor row is hidden by a fold, or the cursor sits above top_row. Used by host-side scrolloff math.

Source

pub fn screen_rows_between( &self, viewport: &Viewport, start: usize, end: usize, ) -> usize

Number of screen rows the doc range start..=end occupies under the current wrap mode. Skips fold-hidden rows. Empty / past-end ranges return 0. Wrap::None returns the visible doc-row count (one screen row per doc row).

Source

pub fn max_top_for_height(&self, viewport: &Viewport, height: usize) -> usize

Earliest top_row such that screen_rows_between(top, last) is at least height. Lets host-side scrolloff math clamp top_row so the buffer never leaves blank rows below the content. When the buffer’s total screen rows are smaller than height this returns 0.

Source

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

Clamp pos to the buffer’s content. Out-of-range row gets pulled to the last row; out-of-range col gets pulled to the row’s char count (one past last char — insertion point).

Source

pub fn replace_all(&mut self, text: &str)

Replace the buffer’s full text in place. Cursor is clamped to the new content. Used during the migration off tui-textarea so the buffer can mirror the textarea’s content after every edit without rebuilding the whole struct.

Source

pub fn as_string(&self) -> String

Concatenate the rows into a single String joined by \n. Inverse of Buffer::from_str for content built without a trailing newline.

Source

pub fn row_count(&self) -> usize

Number of rows in the buffer. Always >= 1.

Source§

impl Buffer

Source

pub fn apply_edit(&mut self, edit: Edit) -> Edit

Apply edit and return the inverse. Pushing the inverse back through Buffer::apply_edit restores the previous state.

Source§

impl Buffer

Source

pub fn folds(&self) -> &[Fold]

Source

pub fn add_fold(&mut self, start_row: usize, end_row: usize, closed: bool)

Register a new fold. If an existing fold has the same start_row, it’s replaced; otherwise the new one is inserted in start-row order. Empty / inverted ranges are rejected.

Source

pub fn remove_fold_at(&mut self, row: usize) -> bool

Drop the fold whose range covers row. Returns true when a fold was actually removed.

Source

pub fn open_fold_at(&mut self, row: usize) -> bool

Open the fold at row (no-op if already open or no fold).

Source

pub fn close_fold_at(&mut self, row: usize) -> bool

Close the fold at row (no-op if already closed or no fold).

Source

pub fn toggle_fold_at(&mut self, row: usize) -> bool

Flip the closed/open state of the fold containing row.

Source

pub fn open_all_folds(&mut self)

zR — open every fold.

Source

pub fn clear_all_folds(&mut self)

zE — eliminate every fold.

Source

pub fn close_all_folds(&mut self)

zM — close every fold.

Source

pub fn fold_at_row(&self, row: usize) -> Option<&Fold>

First fold whose range contains row (most folds are non-overlapping in vim’s model, so the first match is the only match). Useful for the host’s za/zo/zc handlers.

Source

pub fn is_row_hidden(&self, row: usize) -> bool

True iff row is hidden by a closed fold (any fold).

Source

pub fn next_visible_row(&self, row: usize) -> Option<usize>

First visible row strictly after row, skipping any rows hidden by closed folds. Returns None past the end of the buffer. Drives fold-aware j: closed folds count as a single visual line.

Source

pub fn prev_visible_row(&self, row: usize) -> Option<usize>

First visible row strictly before row, skipping hidden rows. Returns None past the top of the buffer.

Source

pub fn invalidate_folds_in_range(&mut self, start_row: usize, end_row: usize)

Drop every fold that touches [start_row, end_row]. Edit paths call this to invalidate folds whose contents the user just mutated — vim’s “edits inside a fold open it” behaviour.

Trait Implementations§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.