pub struct Document {}
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
impl Document
sourcepub fn open<S: Into<String>>(size: Size, file_name: S) -> Result<Self>
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.
sourcepub fn set_tab_width(&mut self, tab_width: usize)
pub fn set_tab_width(&mut self, tab_width: usize)
Sets the tab display width measured in spaces, default being 4
sourcepub fn save(&mut self) -> Result<()>
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.
sourcepub fn save_as(&self, file_name: &str) -> Result<()>
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.
sourcepub fn exe(&mut self, ev: Event) -> Result<()>
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.
sourcepub fn undo(&mut self) -> Result<()>
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.
sourcepub fn redo(&mut self) -> Result<()>
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.
sourcepub fn forth(&mut self, ev: Event) -> Result<()>
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.
sourcepub fn delete_line(&mut self, loc: usize) -> Result<()>
pub fn delete_line(&mut self, loc: usize) -> Result<()>
sourcepub fn split_down(&mut self, loc: &Loc) -> Result<()>
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.
sourcepub fn splice_up(&mut self, y: usize) -> Result<()>
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.
sourcepub fn move_right(&mut self) -> Status
pub fn move_right(&mut self) -> Status
Move the cursor right
sourcepub fn move_bottom(&mut self)
pub fn move_bottom(&mut self)
Move to the bottom of the document
sourcepub fn move_page_up(&mut self)
pub fn move_page_up(&mut self)
Move up by 1 page
sourcepub fn move_page_down(&mut self)
pub fn move_page_down(&mut self)
Move down by 1 page
sourcepub fn move_prev_word(&mut self) -> Status
pub fn move_prev_word(&mut self) -> Status
Moves to the previous word in the document
sourcepub fn move_next_word(&mut self) -> Status
pub fn move_next_word(&mut self) -> Status
Moves to the next word in the document
sourcepub fn next_match(&mut self, regex: &str, inc: usize) -> Option<Match>
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
sourcepub fn prev_match(&mut self, regex: &str) -> Option<Match>
pub fn prev_match(&mut self, regex: &str) -> Option<Match>
Function to search the document to find the previous occurance of a regex
sourcepub fn replace(&mut self, loc: Loc, target: &str, into: &str) -> Result<()>
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.
sourcepub fn replace_all(&mut self, target: &str, into: &str)
pub fn replace_all(&mut self, target: &str, into: &str)
Replace all instances of a regex with another string
sourcepub fn out_of_range(&self, x: usize, y: usize) -> Result<()>
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.
sourcepub fn valid_range(&self, start: usize, end: usize, y: usize) -> Result<()>
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.
sourcepub fn load_to(&mut self, to: usize)
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.
sourcepub fn line_trim(
&self,
line: usize,
start: usize,
length: usize
) -> Option<String>
pub fn line_trim( &self, line: usize, start: usize, length: usize ) -> Option<String>
Get the line at a specified index and trim it
sourcepub fn line_number(&self, request: usize) -> String
pub fn line_number(&self, request: usize) -> String
Evaluate the line number text for a specific line
sourcepub fn is_dbl_width(&self, y: usize, x: usize) -> bool
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.
sourcepub fn is_tab(&self, y: usize, x: usize) -> bool
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.