Struct add_ed::History

source ·
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: bool

If 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,

source

pub fn new() -> Self

Create new History instance

  • Only an empty present state exists.
  • Considered saved at initial empty state.
source

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.

source

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.

source

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.

source

pub fn current(&self) -> &T

Get an immutable view into the currently viewed point in history

source

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.
source

pub fn undo(&mut self, steps: isize) -> Result<()>

Move the given number of steps back into history

source

pub fn undo_range(&self) -> Result<Range<isize>>

Returns how far you can undo/redo

source

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.

source

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.

Trait Implementations§

source§

impl<T> Clone for History<T>where T: Default + Debug + Snapshot + PartialEq + Clone,

source§

fn clone(&self) -> History<T>

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<T> Debug for History<T>where T: Default + Debug + Snapshot + PartialEq + Debug,

source§

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

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

impl<T> Default for History<T>where T: Default + Debug + Snapshot + PartialEq,

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for History<T>where T: RefUnwindSafe,

§

impl<T> Send for History<T>where T: Send,

§

impl<T> Sync for History<T>where T: Sync,

§

impl<T> Unpin for History<T>where T: Unpin,

§

impl<T> UnwindSafe for History<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AsAny for Twhere T: Any,

source§

fn as_any(&self) -> &(dyn Any + 'static)

source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

source§

fn type_name(&self) -> &'static str

Gets the type name of self
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast for Twhere T: AsAny + ?Sized,

source§

fn is<T>(&self) -> boolwhere T: AsAny,

Returns true if the boxed type is the same as T. Read more
source§

fn downcast_ref<T>(&self) -> Option<&T>where T: AsAny,

Forward to the method defined on the type Any.
source§

fn downcast_mut<T>(&mut self) -> Option<&mut T>where T: AsAny,

Forward to the method defined on the type Any.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.