pub struct EditorBuffer { /* private fields */ }
Expand description

Stores the data for a single editor buffer. Please do not construct this struct directly and use new_empty instead.

  1. This struct is stored in the r3bl_redux::Store’s state field.
  2. And it is paired w/ EditorEngine at runtime; which is responsible for rendering it to TUI, and handling user input.

Modifying the buffer

InputEvent is converted into an EditorEvent (by EditorEngineApi::apply_event), which is then used to modify the EditorBuffer via:

  1. EditorEvent::apply_editor_event
  2. EditorEvent::apply_editor_events

In order for the commands to be executed, the functions in EditorEngineInternalApi are used.

These functions take any one of the following args:

  1. EditorArgsMut
  2. EditorArgs
  3. EditorBuffer and EditorEngine

Accessing and mutating the fields (w/ validation)

All the fields in this struct are private. In order to access them you have to use the accessor associated functions. To mutate them, you have to use the get_mut method, which returns a tuple w/ mutable references to the fields. This rather strange design allows for all mutations to be tracked easily and allows for validation operations to be applied post mutation (by validate_editor_buffer_change::apply_change).

Different kinds of caret positions

There are two variants for the caret position value:

  1. CaretKind::Raw - this is the position of the caret (unadjusted for scroll_offset) and this represents the position of the caret in the viewport.
  2. CaretKind::ScrollAdjusted - this is the position of the caret (adjusted for scroll_offset) and represents the position of the caret in the buffer (not the viewport).

Fields

Please don’t mutate these fields directly, they are not marked pub to guard from unintentional mutation. To mutate or access access it, use get_mut.

lines

A list of lines representing the document being edited.

caret_display_position

This is the “display” (or display_col_index) and not “logical” (or logical_index) position (both are defined in tui_core::graphemes). Please take a look at tui_core::graphemes::UnicodeString, specifically the methods in tui_core::graphemes::access for more details on how the conversion between “display” and “logical” indices is done.

  1. It represents the current caret position (relative to the style_adjusted_origin_pos of the enclosing FlexBox).
  2. It works w/ crate::RenderOp::MoveCursorPositionRelTo as well.

💡 For the diagrams below, the caret is where and intersects.

Start of line:

R ┌──────────┐
0 ▸abcab     │
  └▴─────────┘
  C0123456789

Middle of line:

R ┌──────────┐
0 ▸abcab     │
  └───▴──────┘
  C0123456789

End of line:

R ┌──────────┐
0 ▸abcab     │
  └─────▴────┘
  C0123456789

scroll_offset

The col and row offset for scrolling if active. This is not marked pub in order to guard mutation. In order to access it, use get_mut.

Vertical scrolling and viewport

                   +0--------------------+
                   0                     |
                   |        above        | <- caret_row_adj
                   |                     |
                   +--- scroll_offset ---+
             ->    |         ↑           |      ↑
             |     |                     |      |
  caret.row  |     |      within vp      |  vp height
             |     |                     |      |
             ->    |         ↓           |      ↓
                   +--- scroll_offset ---+
                   |    + vp height      |
                   |                     |
                   |        below        | <- caret_row_adj
                   |                     |
                   +---------------------+

Horizontal scrolling and viewport

          <-   vp width   ->
+0--------+----------------+---------->
0         |                |
| left of |<-  within vp ->| right of
|         |                |
+---------+----------------+---------->
      scroll_offset    scroll_offset
                       + vp width

file_extension

This is used for syntax highlighting. It is a 2 character string, eg: rs or md that is used to lookup the syntax highlighting rules for the language in [find_syntax_by_extensionsyntect::parsing::SyntaxSet::find_syntax_by_extension.

selection_map

The SelectionMap is used to keep track of the selections in the buffer. Each entry in the map represents a row of text in the buffer.

Implementations§

source§

impl EditorBuffer

source

pub fn new_empty(file_extension: Option<&str>) -> EditorBuffer

Marker method to make it easy to search for where an empty instance is created.

source§

impl EditorBuffer

source

pub fn has_file_extension(&self) -> bool

source

pub fn get_maybe_file_extension(&self) -> Option<&str>

source

pub fn is_empty(&self) -> bool

source

pub fn len(&self) -> ChUnit

source

pub fn get_line_display_width(&self, row_index: ChUnit) -> ChUnit

source

pub fn get_lines(&self) -> &Vec<UnicodeString>

source

pub fn get_as_string(&self) -> String

source

pub fn set_lines(&mut self, lines: Vec<String>)

source

pub fn get_caret(&self, kind: CaretKind) -> Position

Returns the current caret position in two variants:

  1. CaretKind::Raw -> The raw caret position not adjusted for scrolling.
  2. CaretKind::ScrollAdjusted -> The caret position adjusted for scrolling using scroll_offset.
source

pub fn calc_scroll_adj_caret_row( caret: &Position, scroll_offset: &Position ) -> usize

Scroll adjusted caret row = caret.row + scroll_offset.row.

source

pub fn calc_scroll_adj_caret_col( caret: &Position, scroll_offset: &Position ) -> usize

Scroll adjusted caret col = caret.col + scroll_offset.col.

source

pub fn get_scroll_offset(&self) -> Position

source

pub fn get_mut( &mut self ) -> (&mut Vec<UnicodeString>, &mut Position, &mut Position, &mut SelectionMap)

Returns:

  1. /* lines */ &mut Vec<UnicodeString>,
  2. /* caret */ &mut Position,
  3. /* scroll_offset */ &mut ScrollOffset,

Even though this struct is mutable by editor_ops.rs, this method is provided to mark when mutable access is made to this struct. This makes it easy to determine what code mutates this struct, since it is necessary to validate things after mutation quite a bit in editor_ops.rs.

source

pub fn has_selection(&self) -> bool

source

pub fn clear_selection(&mut self)

source

pub fn get_selection_map(&self) -> &SelectionMap

Trait Implementations§

source§

impl Clone for EditorBuffer

source§

fn clone(&self) -> EditorBuffer

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for EditorBuffer

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for EditorBuffer

source§

fn deserialize<__D>( __deserializer: __D ) -> Result<EditorBuffer, <__D as Deserializer<'de>>::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl GetSize for EditorBuffer

source§

fn get_heap_size(&self) -> usize

Determines how many bytes this object occupies inside the heap. Read more
source§

fn get_stack_size() -> usize

Determines how may bytes this object occupies inside the stack. Read more
source§

fn get_size(&self) -> usize

Determines the total size of the object. Read more
source§

impl PartialEq for EditorBuffer

source§

fn eq(&self, other: &EditorBuffer) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for EditorBuffer

source§

fn serialize<__S>( &self, __serializer: __S ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl StructuralPartialEq for EditorBuffer

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,