pub struct Document {
Show 13 fields pub file_name: String, pub file: Rope, pub loaded_to: usize, pub lines: Vec<String>, pub dbl_map: CharMap, pub tab_map: CharMap, pub size: Size, pub cursor: Loc, pub offset: Loc, pub char_ptr: usize, pub event_mgmt: EventMgmt, pub modified: bool, pub tab_width: usize,
}
Expand description

A document struct manages a file. It has tools to read, write and traverse a document. By default, it uses file buffering so it can open almost immediately. To start executing events, remember to use the Document::exe function and check out the documentation for Event to learn how to form editing events.

Fields§

§file_name: String

The file name of the document opened

§file: Rope

The rope of the document to facilitate reading and writing to disk

§loaded_to: usize

Contains the number of lines buffered into the document

§lines: Vec<String>

Cache of all the loaded lines in this document

§dbl_map: CharMap

Stores the locations of double width characters

§tab_map: CharMap

Stores the locations of tab characters

§size: Size

Contains the size of this document for purposes of offset

§cursor: Loc

Contains where the cursor is within the terminal

§offset: Loc

Contains the offset (scrolling for longer documents)

§char_ptr: usize

Keeps track of where the character pointer is

§event_mgmt: EventMgmt

Manages events, for the purpose of undo and redo

§modified: bool

true if the file has been modified since saving, false otherwise

§tab_width: usize

The number of spaces a tab should be rendered as

Implementations§

source§

impl Document

source

pub fn open<S: Into<String>>(size: Size, file_name: S) -> Result<Self>

Open a document from a file name.

§Errors

Returns an error when file doesn’t exist, or has incorrect permissions. Also returns an error if the rope fails to initialise due to character set issues or disk errors.

source

pub fn set_tab_width(&mut self, tab_width: usize)

Sets the tab display width measured in spaces, default being 4

source

pub fn save(&mut self) -> Result<()>

Save back to the file the document was opened from.

§Errors

Returns an error if the file fails to write, due to permissions or character set issues.

source

pub fn save_as(&self, file_name: &str) -> Result<()>

Save to a specified file.

§Errors

Returns an error if the file fails to write, due to permissions or character set issues.

source

pub fn exe(&mut self, ev: Event) -> Result<()>

Execute an event, registering it in the undo / redo. You should always edit a document through this method to ensure undo and redo work.

§Errors

Will return an error if the event was unable to be completed.

source

pub fn undo(&mut self) -> Result<()>

Undo the last patch in the document.

§Errors

Will return an error if any of the events failed to be reversed.

source

pub fn redo(&mut self) -> Result<()>

Redo the last patch in the document.

§Errors

Will return an error if any of the events failed to be re-executed.

source

pub fn forth(&mut self, ev: Event) -> Result<()>

Handle an editing event, use the method exe for executing events.

§Errors

Returns an error if there is a problem with the specified operation.

source

pub fn insert(&mut self, loc: &Loc, st: &str) -> Result<()>

Inserts a string into this document.

§Errors

Returns an error if location is out of range.

source

pub fn delete<R>(&mut self, x: R, y: usize) -> Result<()>
where R: RangeBounds<usize>,

Deletes a range from this document.

§Errors

Returns an error if location is out of range.

source

pub fn insert_line(&mut self, loc: usize, contents: String) -> Result<()>

Inserts a line into the document.

§Errors

Returns an error if location is out of range.

source

pub fn delete_line(&mut self, loc: usize) -> Result<()>

Deletes a line from the document.

§Errors

Returns an error if location is out of range.

source

pub fn split_down(&mut self, loc: &Loc) -> Result<()>

Split a line in half, putting the right hand side below on a new line. For when the return key is pressed.

§Errors

Returns an error if location is out of range.

source

pub fn splice_up(&mut self, y: usize) -> Result<()>

Remove the line below the specified location and append that to it. For when backspace is pressed on the start of a line.

§Errors

Returns an error if location is out of range.

source

pub fn move_up(&mut self) -> Status

Move the cursor up

source

pub fn move_down(&mut self) -> Status

Move the cursor down

source

pub fn move_left(&mut self) -> Status

Move the cursor left

source

pub fn move_right(&mut self) -> Status

Move the cursor right

source

pub fn move_home(&mut self)

Move to the start of the line

source

pub fn move_end(&mut self)

Move to the end of the line

source

pub fn move_top(&mut self)

Move to the top of the document

source

pub fn move_bottom(&mut self)

Move to the bottom of the document

source

pub fn move_page_up(&mut self)

Move up by 1 page

source

pub fn move_page_down(&mut self)

Move down by 1 page

source

pub fn move_prev_word(&mut self) -> Status

Moves to the previous word in the document

source

pub fn move_next_word(&mut self) -> Status

Moves to the next word in the document

source

pub fn next_match(&mut self, regex: &str, inc: usize) -> Option<Match>

Function to search the document to find the next occurance of a regex

source

pub fn prev_match(&mut self, regex: &str) -> Option<Match>

Function to search the document to find the previous occurance of a regex

source

pub fn replace(&mut self, loc: Loc, target: &str, into: &str) -> Result<()>

Replace a specific part of the document with another string.

§Errors

Will error if the replacement failed to be executed.

source

pub fn replace_all(&mut self, target: &str, into: &str)

Replace all instances of a regex with another string

source

pub fn goto(&mut self, loc: &Loc)

Function to go to a specific position

source

pub fn goto_x(&mut self, x: usize)

Function to go to a specific x position

source

pub fn goto_y(&mut self, y: usize)

Function to go to a specific y position

source

pub fn out_of_range(&self, x: usize, y: usize) -> Result<()>

Determines if specified coordinates are out of range of the document.

§Errors

Returns an error when the given coordinates are out of range.

source

pub fn valid_range(&self, start: usize, end: usize, y: usize) -> Result<()>

Determines if a range is in range of the document.

§Errors

Returns an error when the given range is out of range.

source

pub fn load_to(&mut self, to: usize)

Load lines in this document up to a specified index. This must be called before starting to edit the document as this is the function that actually load and processes the text.

source

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

Get the line at a specified index

source

pub fn line_trim( &self, line: usize, start: usize, length: usize ) -> Option<String>

Get the line at a specified index and trim it

source

pub fn len_lines(&self) -> usize

Returns the number of lines in the document

source

pub fn line_number(&self, request: usize) -> String

Evaluate the line number text for a specific line

source

pub fn is_dbl_width(&self, y: usize, x: usize) -> bool

Determine if a character at a certain location is a double width character. x is the display index.

source

pub fn is_tab(&self, y: usize, x: usize) -> bool

Determine if a character at a certain location is a tab character. x is the display index.

source

pub fn width_of(&self, y: usize, x: usize) -> usize

Determine the width of a character at a certain location

source

pub const fn loc(&self) -> Loc

Get the current position within the document, including offset

source

pub const fn char_loc(&self) -> Loc

Get the current position within the document, with x being the character index

Trait Implementations§

source§

impl Clone for Document

source§

fn clone(&self) -> Document

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 Document

source§

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

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

impl PartialEq for Document

source§

fn eq(&self, other: &Document) -> 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 Eq for Document

source§

impl StructuralPartialEq for Document

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> ToOwned for T
where 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 T
where 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 T
where 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.