pub struct History<T>{
pub dont_snapshot: bool,
/* private fields */
}
Expand description
A history abstraction over generic objects used by add-ed.
Handles snapshotting and moving over the history of snapshots. Currently uses a revert style of undo inspired by this reasoning.
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: bool
If true all calls to History::snapshot
are ignored (including the
automatic call upon running .current_mut()
).
Intended for macro execution, when it would be confusing to create multiple snapshots for what the user sees as a single action.
(If a point in history is viewed a snapshot reverting to that point in history will be created before mutable access no matter if this variable is set to true.)
Implementations§
source§impl<T> History<T>
impl<T> History<T>
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. If it is uncertain or difficult to track it will return false.
sourcepub fn set_saved(&mut self)
pub fn set_saved(&mut self)
Mark the currently viewed buffer state as saved
If dont_snapshot
is set this instead behaves as
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, modification_cause: String) -> &mut T
pub fn current_mut(&mut self, modification_cause: String) -> &mut T
Get a mutable state to make new changes
- Takes a string describing what is causing this new snapshot. (Should generally be the full command, if not be as clear as possible.)
- If currently viewing history, will create a revert snapshot at end of history.
- Unless self.dont_snapshot, will create a new snapshot tagged with the given cause for modification.
- Returns mutable access to the snapshot at the end of history.
sourcepub fn snapshot(&mut self, modification_cause: String)
pub fn snapshot(&mut self, modification_cause: String)
Manually add a snapshot
Takes a String as an argument that should describe what causes the change seen in the created snapshot relative to the preceding 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.
sourcepub fn snapshots(&self) -> &Vec<(String, T)>
pub fn snapshots(&self) -> &Vec<(String, T)>
Accessor to view the full list of snapshots
- Entries are in order of creation, the first operation is first in the list.
- The string beside the snapshot describes what caused the state in the snapshot (relative to the preceeding snapshot).
sourcepub fn saved_i(&self) -> Option<usize>
pub fn saved_i(&self) -> Option<usize>
Getter for what index was last saved
Returns None if no index is believed to be saved.
Intended to be used to enrich when listing snapshots by marking the one considered saved.
sourcepub fn set_viewed_i(&mut self, new_i: usize) -> Result<&str>
pub fn set_viewed_i(&mut self, new_i: usize) -> Result<&str>
Setter for currently viewed snapshot index
Returns the modification cause for the now viewed index.
Will return error if given index doesn’t hold a snapshot (aka. is too big).