TextBuffer

Struct TextBuffer 

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

A text buffer for a text editor.

Implementations§

Source§

impl TextBuffer

Source

pub fn new_rc(small: bool) -> Result<RcTextBuffer>

Creates a new text buffer inside an Rc. See TextBuffer::new().

Source

pub fn new(small: bool) -> Result<Self>

Creates a new text buffer. With small you can control if the buffer is optimized for <1MiB contents.

Source

pub fn text_length(&self) -> usize

Length of the document in bytes.

Source

pub fn logical_line_count(&self) -> CoordType

Number of logical lines in the document, that is, lines separated by newlines.

Source

pub fn visual_line_count(&self) -> CoordType

Number of visual lines in the document, that is, the number of lines after layout.

Source

pub fn is_dirty(&self) -> bool

Does the buffer need to be saved?

Source

pub fn generation(&self) -> u32

The buffer generation changes on every edit. With this you can check if it has changed since the last time you called this function.

Source

pub fn mark_as_dirty(&mut self)

Force the buffer to be dirty.

Source

pub fn encoding(&self) -> &'static str

The encoding used during reading/writing. “UTF-8” is the default.

Source

pub fn set_encoding(&mut self, encoding: &'static str)

Set the encoding used during reading/writing.

Source

pub fn is_crlf(&self) -> bool

The newline type used in the document. LF or CRLF.

Source

pub fn set_crlf(&mut self, crlf: bool)

Changes the newline type without normalizing the document.

Source

pub fn normalize_newlines(&mut self, crlf: bool)

Changes the newline type used in the document.

NOTE: Cannot be undone.

Source

pub fn set_insert_final_newline(&mut self, enabled: bool)

If enabled, automatically insert a final newline when typing at the end of the file.

Source

pub fn is_overtype(&self) -> bool

Whether to insert or overtype text when writing.

Source

pub fn set_overtype(&mut self, overtype: bool)

Set the overtype mode.

Source

pub fn cursor_logical_pos(&self) -> Point

Gets the logical cursor position, that is, the position in lines and graphemes per line.

Source

pub fn cursor_visual_pos(&self) -> Point

Gets the visual cursor position, that is, the position in laid out rows and columns.

Source

pub fn margin_width(&self) -> CoordType

Gets the width of the left margin.

Source

pub fn set_margin_enabled(&mut self, enabled: bool) -> bool

Is the left margin enabled?

Source

pub fn text_width(&self) -> CoordType

Gets the width of the text contents for layout.

Source

pub fn make_cursor_visible(&mut self)

Ask the TUI system to scroll the buffer and make the cursor visible.

TODO: This function shows that TextBuffer is poorly abstracted away from the TUI system. The only reason this exists is so that if someone outside the TUI code enables word-wrap, the TUI code recognizes this and scrolls the cursor into view. But outside of this scrolling, views, etc., are all UI concerns = this should not be here.

Source

pub fn take_cursor_visibility_request(&mut self) -> bool

For the TUI code to retrieve a prior TextBuffer::make_cursor_visible() request.

Source

pub fn is_word_wrap_enabled(&self) -> bool

Is word-wrap enabled?

Technically, this is a misnomer, because it’s line-wrapping.

Source

pub fn set_word_wrap(&mut self, enabled: bool)

Enable or disable word-wrap.

NOTE: It’s expected that the tui code calls set_width() sometime after this. This will then trigger the actual recalculation of the cursor position.

Source

pub fn set_width(&mut self, width: CoordType) -> bool

Set the width available for layout.

Ideally this would be a pure UI concern, but the text buffer needs this so that it can abstract away visual cursor movement such as “go a line up”. What would that even mean if it didn’t know how wide a line is?

Source

pub fn tab_size(&self) -> CoordType

Set the tab width. Could be anything, but is expected to be 1-8.

Source

pub fn set_tab_size(&mut self, width: CoordType) -> bool

Set the tab size. Clamped to 1-8.

Source

pub fn indent_with_tabs(&self) -> bool

Returns whether tabs are used for indentation.

Source

pub fn set_indent_with_tabs(&mut self, indent_with_tabs: bool)

Sets whether tabs or spaces are used for indentation.

Source

pub fn set_line_highlight_enabled(&mut self, enabled: bool)

Sets whether the line the cursor is on should be highlighted.

Source

pub fn set_ruler(&mut self, column: CoordType)

Sets a ruler column, e.g. 80.

Source

pub fn reflow(&mut self)

Source

pub fn copy_from_str(&mut self, text: &dyn ReadableDocument)

Replaces the entire buffer contents with the given text. Assumes that the line count doesn’t change.

Source

pub fn save_as_string(&mut self, dst: &mut dyn WriteableDocument)

Copies the contents of the buffer into a string.

Source

pub fn read_file( &mut self, file: &mut File, encoding: Option<&'static str>, ) -> Result<()>

Reads a file from disk into the text buffer, detecting encoding and BOM.

Source

pub fn write_file(&mut self, file: &mut File) -> Result<()>

Writes the text buffer contents to a file, handling BOM and encoding.

Source

pub fn has_selection(&self) -> bool

Returns the current selection.

Source

pub fn selection_update_offset(&mut self, offset: usize)

Moves the cursor by offset and updates the selection to contain it.

Source

pub fn selection_update_visual(&mut self, visual_pos: Point)

Moves the cursor to visual_pos and updates the selection to contain it.

Source

pub fn selection_update_logical(&mut self, logical_pos: Point)

Moves the cursor to logical_pos and updates the selection to contain it.

Source

pub fn selection_update_delta( &mut self, granularity: CursorMovement, delta: CoordType, )

Moves the cursor by delta and updates the selection to contain it.

Source

pub fn select_word(&mut self)

Select the current word.

Source

pub fn select_line(&mut self)

Select the current line.

Source

pub fn select_all(&mut self)

Select the entire document.

Source

pub fn start_selection(&mut self)

Starts a new selection, if there’s none already.

Source

pub fn clear_selection(&mut self) -> bool

Destroy the current selection.

Source

pub fn find_and_select( &mut self, pattern: &str, options: SearchOptions, ) -> Result<()>

Find the next occurrence of the given pattern and select it.

Source

pub fn find_and_replace( &mut self, pattern: &str, options: SearchOptions, replacement: &str, ) -> Result<()>

Find the next occurrence of the given pattern and replace it with replacement.

Source

pub fn find_and_replace_all( &mut self, pattern: &str, options: SearchOptions, replacement: &str, ) -> Result<()>

Find all occurrences of the given pattern and replace them with replacement.

Source

pub fn cursor_move_to_offset(&mut self, offset: usize)

Moves the cursor to the given offset.

Source

pub fn cursor_move_to_logical(&mut self, pos: Point)

Moves the cursor to the given logical position.

Source

pub fn cursor_move_to_visual(&mut self, pos: Point)

Moves the cursor to the given visual position.

Source

pub fn cursor_move_delta( &mut self, granularity: CursorMovement, delta: CoordType, )

Moves the cursor by the given delta.

Source

pub unsafe fn set_cursor(&mut self, cursor: Cursor)

Sets the cursor to the given position, and clears the selection.

§Safety

This function performs no checks that the cursor is valid. “Valid” in this case means that the TextBuffer has not been modified since you received the cursor from this class.

Source

pub fn render( &mut self, origin: Point, destination: Rect, focused: bool, fb: &mut Framebuffer, ) -> Option<RenderResult>

Extracts a rectangular region of the text buffer and writes it to the framebuffer. The destination rect is framebuffer coordinates. The extracted region within this text buffer has the given origin and the same size as the destination rect.

Source

pub fn cut(&mut self, clipboard: &mut Clipboard)

Source

pub fn copy(&mut self, clipboard: &mut Clipboard)

Source

pub fn paste(&mut self, clipboard: &Clipboard)

Source

pub fn write_canon(&mut self, text: &[u8])

Inserts the user input text at the current cursor position. Replaces tabs with whitespace if needed, etc.

Source

pub fn write_raw(&mut self, text: &[u8])

Inserts text as-is at the current cursor position. The only transformation applied is that newlines are normalized.

Source

pub fn delete(&mut self, granularity: CursorMovement, delta: CoordType)

Deletes 1 grapheme cluster from the buffer. cursor_movements is expected to be -1 for backspace and 1 for delete. If there’s a current selection, it will be deleted and cursor_movements ignored. The selection is cleared after the call. Deletes characters from the buffer based on a delta from the cursor.

Source

pub fn indent_end_logical_pos(&self) -> Point

Returns the logical position of the first character on this line. Return .x == 0 if there are no non-whitespace characters.

Source

pub fn unindent(&mut self)

Unindents the current selection or line.

TODO: This function is ripe for some optimizations:

  • Instead of replacing the entire selection, it should unindent each line directly (as if multiple cursors had been used).
  • The cursor movement at the end is rather costly, but at least without word wrap it should be possible to calculate it directly from the removed amount.
Source

pub fn extract_user_selection(&mut self, delete: bool) -> Option<Vec<u8>>

Extracts the contents of the current selection the user made. This differs from TextBuffer::extract_selection() in that it does nothing if the selection was made by searching.

Source

pub fn selection_range(&self) -> Option<(Cursor, Cursor)>

Returns the current selection anchors, or None if there is no selection. The returned logical positions are sorted.

Source

pub fn undo(&mut self)

Undo the last edit operation.

Source

pub fn redo(&mut self)

Redo the last undo operation.

Source

pub fn read_forward(&self, off: usize) -> &[u8]

For interfacing with ICU.

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