pub struct History<T>where
T: Default + Debug + Snapshot + PartialEq,{
pub dont_snapshot: bool,
/* private fields */
}Expand description
A history abstraction over generic objects used by add-ed.
Handles snapshotting and undo/redo to move over the history of snapshots. Currently uses a revert style of undo, more details/links on github.
Automatically manages snapshot creation upon mutable access to the current
point in history. Further allows pausing snapshot creation via
[History.dont_snapshot] as well as manual snapshot creation via
[History.snapshot] (for use during script/macro execution, to make each
snapshot correspond to a user action).
Fields§
§dont_snapshot: boolIf true all calls to History::snapshot are ignored
Intended for macro execution, when it would be confusing to create multiple snapshots for what the user sees as a single action.
Implementations§
source§impl<T> History<T>where
T: Default + Debug + Snapshot + PartialEq,
impl<T> History<T>where T: Default + Debug + Snapshot + PartialEq,
sourcepub fn new() -> Self
pub fn new() -> Self
Create new History instance
- Only an empty present state exists.
- Considered saved at initial empty state.
sourcepub fn saved(&self) -> bool
pub fn saved(&self) -> bool
Get if the buffer is saved
It aims to be true when the viewed buffer matches the data last saved.
sourcepub fn set_saved(&mut self)
pub fn set_saved(&mut self)
Mark the currently viewed buffer state as saved
If [Self.dont_snapshot] is set this instead behaves as
[Self.set_unsaved], as we cannot be sure a snapshot will exist
corresponding to the state in which the buffer was saved.
sourcepub fn set_unsaved(&mut self)
pub fn set_unsaved(&mut self)
Declare that no known buffer state is saved
Mainly useful for testing, but may be relevant when knowing that file was changed on disk.
sourcepub fn current_mut(&mut self) -> Result<&mut T>
pub fn current_mut(&mut self) -> Result<&mut T>
Get a mutable state to make new changes
- If currently viewing history, will create a revert snapshot at end of history.
- Unless self.dont_snapshot, will create a new snapshot.
- Returns mutable access to the snapshot at the end of history.
sourcepub fn undo(&mut self, steps: isize) -> Result<()>
pub fn undo(&mut self, steps: isize) -> Result<()>
Move the given number of steps back into history
sourcepub fn undo_range(&self) -> Result<Range<isize>>
pub fn undo_range(&self) -> Result<Range<isize>>
Returns how far you can undo/redo
sourcepub fn snapshot(&mut self) -> Result<()>
pub fn snapshot(&mut self) -> Result<()>
Manually add a snapshot
The only case this should be needed is before setting dont_snapshot for
a script execution. If dont_snapshot isn’t set snapshots are created
automatically whenever [Self.current_mut] is executed.
sourcepub fn dedup_present(&mut self)
pub fn dedup_present(&mut self)
Checks if the last two snapshots in history are identical. If yes deletes one of them.
Intended for use by macros and scripts, as they have to add a snapshot even for non-mutating scripts since they don’t know if a script will modify the buffer. By running this after macro execution the snapshot will be deleted if extraneous and left if relevant.