pub struct TextBuffer { /* private fields */ }Expand description
A text buffer for a text editor.
Implementations§
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn new_rc(small: bool) -> Result<RcTextBuffer>
pub fn new_rc(small: bool) -> Result<RcTextBuffer>
Creates a new text buffer inside an Rc.
See TextBuffer::new().
Sourcepub fn new(small: bool) -> Result<Self>
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.
Sourcepub fn text_length(&self) -> usize
pub fn text_length(&self) -> usize
Length of the document in bytes.
Sourcepub fn logical_line_count(&self) -> CoordType
pub fn logical_line_count(&self) -> CoordType
Number of logical lines in the document, that is, lines separated by newlines.
Sourcepub fn visual_line_count(&self) -> CoordType
pub fn visual_line_count(&self) -> CoordType
Number of visual lines in the document, that is, the number of lines after layout.
Sourcepub fn generation(&self) -> u32
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.
Sourcepub fn mark_as_dirty(&mut self)
pub fn mark_as_dirty(&mut self)
Force the buffer to be dirty.
Sourcepub fn encoding(&self) -> &'static str
pub fn encoding(&self) -> &'static str
The encoding used during reading/writing. “UTF-8” is the default.
Sourcepub fn set_encoding(&mut self, encoding: &'static str)
pub fn set_encoding(&mut self, encoding: &'static str)
Set the encoding used during reading/writing.
Sourcepub fn set_crlf(&mut self, crlf: bool)
pub fn set_crlf(&mut self, crlf: bool)
Changes the newline type without normalizing the document.
Sourcepub fn normalize_newlines(&mut self, crlf: bool)
pub fn normalize_newlines(&mut self, crlf: bool)
Changes the newline type used in the document.
NOTE: Cannot be undone.
Sourcepub fn set_insert_final_newline(&mut self, enabled: bool)
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.
Sourcepub fn is_overtype(&self) -> bool
pub fn is_overtype(&self) -> bool
Whether to insert or overtype text when writing.
Sourcepub fn set_overtype(&mut self, overtype: bool)
pub fn set_overtype(&mut self, overtype: bool)
Set the overtype mode.
Sourcepub fn cursor_logical_pos(&self) -> Point
pub fn cursor_logical_pos(&self) -> Point
Gets the logical cursor position, that is, the position in lines and graphemes per line.
Sourcepub fn cursor_visual_pos(&self) -> Point
pub fn cursor_visual_pos(&self) -> Point
Gets the visual cursor position, that is, the position in laid out rows and columns.
Sourcepub fn margin_width(&self) -> CoordType
pub fn margin_width(&self) -> CoordType
Gets the width of the left margin.
Sourcepub fn set_margin_enabled(&mut self, enabled: bool) -> bool
pub fn set_margin_enabled(&mut self, enabled: bool) -> bool
Is the left margin enabled?
Sourcepub fn text_width(&self) -> CoordType
pub fn text_width(&self) -> CoordType
Gets the width of the text contents for layout.
Sourcepub fn make_cursor_visible(&mut self)
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.
Sourcepub fn take_cursor_visibility_request(&mut self) -> bool
pub fn take_cursor_visibility_request(&mut self) -> bool
For the TUI code to retrieve a prior TextBuffer::make_cursor_visible() request.
Sourcepub fn is_word_wrap_enabled(&self) -> bool
pub fn is_word_wrap_enabled(&self) -> bool
Is word-wrap enabled?
Technically, this is a misnomer, because it’s line-wrapping.
Sourcepub fn set_word_wrap(&mut self, enabled: bool)
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.
Sourcepub fn set_width(&mut self, width: CoordType) -> bool
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?
Sourcepub fn tab_size(&self) -> CoordType
pub fn tab_size(&self) -> CoordType
Set the tab width. Could be anything, but is expected to be 1-8.
Sourcepub fn set_tab_size(&mut self, width: CoordType) -> bool
pub fn set_tab_size(&mut self, width: CoordType) -> bool
Set the tab size. Clamped to 1-8.
Sourcepub fn indent_with_tabs(&self) -> bool
pub fn indent_with_tabs(&self) -> bool
Returns whether tabs are used for indentation.
Sourcepub fn set_indent_with_tabs(&mut self, indent_with_tabs: bool)
pub fn set_indent_with_tabs(&mut self, indent_with_tabs: bool)
Sets whether tabs or spaces are used for indentation.
Sourcepub fn set_line_highlight_enabled(&mut self, enabled: bool)
pub fn set_line_highlight_enabled(&mut self, enabled: bool)
Sets whether the line the cursor is on should be highlighted.
pub fn reflow(&mut self)
Sourcepub fn copy_from_str(&mut self, text: &dyn ReadableDocument)
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.
Sourcepub fn save_as_string(&mut self, dst: &mut dyn WriteableDocument)
pub fn save_as_string(&mut self, dst: &mut dyn WriteableDocument)
Copies the contents of the buffer into a string.
Sourcepub fn read_file(
&mut self,
file: &mut File,
encoding: Option<&'static str>,
) -> Result<()>
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.
Sourcepub fn write_file(&mut self, file: &mut File) -> Result<()>
pub fn write_file(&mut self, file: &mut File) -> Result<()>
Writes the text buffer contents to a file, handling BOM and encoding.
Sourcepub fn has_selection(&self) -> bool
pub fn has_selection(&self) -> bool
Returns the current selection.
Sourcepub fn selection_update_offset(&mut self, offset: usize)
pub fn selection_update_offset(&mut self, offset: usize)
Moves the cursor by offset and updates the selection to contain it.
Sourcepub fn selection_update_visual(&mut self, visual_pos: Point)
pub fn selection_update_visual(&mut self, visual_pos: Point)
Moves the cursor to visual_pos and updates the selection to contain it.
Sourcepub fn selection_update_logical(&mut self, logical_pos: Point)
pub fn selection_update_logical(&mut self, logical_pos: Point)
Moves the cursor to logical_pos and updates the selection to contain it.
Sourcepub fn selection_update_delta(
&mut self,
granularity: CursorMovement,
delta: CoordType,
)
pub fn selection_update_delta( &mut self, granularity: CursorMovement, delta: CoordType, )
Moves the cursor by delta and updates the selection to contain it.
Sourcepub fn select_word(&mut self)
pub fn select_word(&mut self)
Select the current word.
Sourcepub fn select_line(&mut self)
pub fn select_line(&mut self)
Select the current line.
Sourcepub fn select_all(&mut self)
pub fn select_all(&mut self)
Select the entire document.
Sourcepub fn start_selection(&mut self)
pub fn start_selection(&mut self)
Starts a new selection, if there’s none already.
Sourcepub fn clear_selection(&mut self) -> bool
pub fn clear_selection(&mut self) -> bool
Destroy the current selection.
Sourcepub fn find_and_select(
&mut self,
pattern: &str,
options: SearchOptions,
) -> Result<()>
pub fn find_and_select( &mut self, pattern: &str, options: SearchOptions, ) -> Result<()>
Find the next occurrence of the given pattern and select it.
Sourcepub fn find_and_replace(
&mut self,
pattern: &str,
options: SearchOptions,
replacement: &str,
) -> Result<()>
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.
Sourcepub fn find_and_replace_all(
&mut self,
pattern: &str,
options: SearchOptions,
replacement: &str,
) -> Result<()>
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.
Sourcepub fn cursor_move_to_offset(&mut self, offset: usize)
pub fn cursor_move_to_offset(&mut self, offset: usize)
Moves the cursor to the given offset.
Sourcepub fn cursor_move_to_logical(&mut self, pos: Point)
pub fn cursor_move_to_logical(&mut self, pos: Point)
Moves the cursor to the given logical position.
Sourcepub fn cursor_move_to_visual(&mut self, pos: Point)
pub fn cursor_move_to_visual(&mut self, pos: Point)
Moves the cursor to the given visual position.
Sourcepub fn cursor_move_delta(
&mut self,
granularity: CursorMovement,
delta: CoordType,
)
pub fn cursor_move_delta( &mut self, granularity: CursorMovement, delta: CoordType, )
Moves the cursor by the given delta.
Sourcepub unsafe fn set_cursor(&mut self, cursor: Cursor)
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.
Sourcepub fn render(
&mut self,
origin: Point,
destination: Rect,
focused: bool,
fb: &mut Framebuffer,
) -> Option<RenderResult>
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.
pub fn cut(&mut self, clipboard: &mut Clipboard)
pub fn copy(&mut self, clipboard: &mut Clipboard)
pub fn paste(&mut self, clipboard: &Clipboard)
Sourcepub fn write_canon(&mut self, text: &[u8])
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.
Sourcepub fn write_raw(&mut self, text: &[u8])
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.
Sourcepub fn delete(&mut self, granularity: CursorMovement, delta: CoordType)
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.
Sourcepub fn indent_end_logical_pos(&self) -> Point
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.
Sourcepub fn unindent(&mut self)
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.
Sourcepub fn extract_user_selection(&mut self, delete: bool) -> Option<Vec<u8>>
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.
Sourcepub fn selection_range(&self) -> Option<(Cursor, Cursor)>
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.
Sourcepub fn read_forward(&self, off: usize) -> &[u8] ⓘ
pub fn read_forward(&self, off: usize) -> &[u8] ⓘ
For interfacing with ICU.