pub struct CommandHistory { /* private fields */ }Expand description
Manages command history for undo/redo operations.
The history maintains two stacks:
- Undo stack: Commands that can be undone
- Redo stack: Commands that can be redone (cleared when new commands are added)
Thread-safe using Arc<Mutex<>> for interior mutability. Cloning shares the same history: every clone reads and writes the one set of stacks.
No method on this type panics on a poisoned mutex: a poisoned lock is
recovered rather than propagated, so a panic inside one caller’s Command
cannot leave the history permanently unusable for everyone else.
Each CodeEditor owns one of these and records every edit into it, so
most applications observe it through CodeEditor::can_undo,
CodeEditor::is_modified, and the Message::Undo / Message::Redo
messages rather than constructing one directly.
§Example
use iced_code_editor::{CodeEditor, Message};
let mut editor = CodeEditor::new("hello", "rs");
assert!(!editor.can_undo());
let _ = editor.update(&Message::Paste(" world".to_string()));
assert!(editor.can_undo());
assert!(editor.is_modified());
let _ = editor.update(&Message::Undo);
assert_eq!(editor.content(), "hello");
assert!(editor.can_redo());Implementations§
Source§impl CommandHistory
impl CommandHistory
Sourcepub fn push(&self, command: Box<dyn Command>)
pub fn push(&self, command: Box<dyn Command>)
Adds a command to the history.
This clears the redo stack and adds the command to the undo stack. If currently grouping commands, adds to the current group instead.
The Command trait is internal to this crate, so an application does
not call this directly — CodeEditor pushes a command for each edit
it performs. The example below shows the observable effect.
§Arguments
command- The command to add
§Example
use iced_code_editor::{CodeEditor, Message};
let mut editor = CodeEditor::new("hello", "rs");
// Each edit the editor performs pushes a command.
let _ = editor.update(&Message::Paste("!".to_string()));
assert!(editor.can_undo());
// Pushing after an undo discards the redo stack: the alternative
// future is gone once a new edit diverges from it.
let _ = editor.update(&Message::Undo);
assert!(editor.can_redo());
let _ = editor.update(&Message::Paste("?".to_string()));
assert!(!editor.can_redo());Sourcepub fn undo(&self, buffer: &mut TextBuffer, cursor: &mut (usize, usize)) -> bool
pub fn undo(&self, buffer: &mut TextBuffer, cursor: &mut (usize, usize)) -> bool
Undoes the last command.
Any open group is closed first, so an in-progress run of typing undoes as one unit rather than being left half-open.
TextBuffer is internal to this crate, so an application drives this
through Message::Undo rather than calling it directly.
§Arguments
buffer- The text buffer to modifycursor- The cursor position to update
§Returns
true if a command was undone, false if nothing to undo
§Example
use iced_code_editor::{CodeEditor, Message};
let mut editor = CodeEditor::new("hello", "rs");
let _ = editor.update(&Message::Paste(" world".to_string()));
assert_eq!(editor.content(), " worldhello");
let _ = editor.update(&Message::Undo);
assert_eq!(editor.content(), "hello");
// Undoing with an empty stack is a harmless no-op.
let _ = editor.update(&Message::Undo);
assert_eq!(editor.content(), "hello");Sourcepub fn redo(&self, buffer: &mut TextBuffer, cursor: &mut (usize, usize)) -> bool
pub fn redo(&self, buffer: &mut TextBuffer, cursor: &mut (usize, usize)) -> bool
Redoes the last undone command.
TextBuffer is internal to this crate, so an application drives this
through Message::Redo rather than calling it directly.
§Arguments
buffer- The text buffer to modifycursor- The cursor position to update
§Returns
true if a command was redone, false if nothing to redo
§Example
use iced_code_editor::{CodeEditor, Message};
let mut editor = CodeEditor::new("hello", "rs");
let _ = editor.update(&Message::Paste(" world".to_string()));
let _ = editor.update(&Message::Undo);
assert_eq!(editor.content(), "hello");
let _ = editor.update(&Message::Redo);
assert_eq!(editor.content(), " worldhello");Sourcepub fn can_undo(&self) -> bool
pub fn can_undo(&self) -> bool
Returns whether there are commands that can be undone.
An open group counts as undoable even before it is closed, so an “Undo” menu item stays enabled while the user is mid-word.
§Returns
true if undo would do something, false otherwise
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
assert!(!history.can_undo());Sourcepub fn can_redo(&self) -> bool
pub fn can_redo(&self) -> bool
Returns whether there are commands that can be redone.
§Returns
true if redo would do something, false otherwise
§Example
use iced_code_editor::{CodeEditor, Message};
let mut editor = CodeEditor::new("hello", "rs");
assert!(!editor.can_redo());
// Redo only becomes available after an undo.
let _ = editor.update(&Message::Paste("!".to_string()));
assert!(!editor.can_redo());
let _ = editor.update(&Message::Undo);
assert!(editor.can_redo());Sourcepub fn mark_saved(&self)
pub fn mark_saved(&self)
Marks the current position as the save point.
This is used to track whether the document has been modified since the last save. Call this after successfully saving the file.
§Example
use iced_code_editor::{CodeEditor, Message};
let mut editor = CodeEditor::new("hello", "rs");
let _ = editor.update(&Message::Paste("!".to_string()));
assert!(editor.is_modified());
// After the host has written the file to disk.
editor.mark_saved();
assert!(!editor.is_modified());
// Undoing back past the save point marks the document dirty again.
let _ = editor.update(&Message::Undo);
assert!(editor.is_modified());Sourcepub fn is_modified(&self) -> bool
pub fn is_modified(&self) -> bool
Returns whether the document has been modified since the last save.
Compares the current undo depth against the depth recorded by
Self::mark_saved, so undoing back to the saved state reports “not
modified” again rather than staying permanently dirty.
§Returns
true if there are unsaved changes, false otherwise
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
// A fresh history has nothing to save.
assert!(!history.is_modified());Sourcepub fn clear(&self)
pub fn clear(&self)
Clears all history.
This removes all undo/redo commands and resets the save point. Useful when starting a new document or resetting the editor state.
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
// ... perform some operations ...
// Clear everything when opening a new document
history.clear();
assert_eq!(history.undo_count(), 0);
assert_eq!(history.redo_count(), 0);
assert!(!history.is_modified());Sourcepub fn begin_group(&self)
pub fn begin_group(&self)
Begins grouping subsequent commands into a composite.
All commands added via push() will be grouped together until
end_group() is called. This is useful for grouping consecutive
typing operations, so one undo removes a whole word rather than one
character at a time.
Calling this while a group is already open does nothing, so grouping does not nest.
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
history.begin_group();
// An open group counts as undoable, so an "Undo" menu item stays
// enabled while the user is still typing.
assert!(history.can_undo());
// Nothing was actually pushed, so closing it adds no entry.
history.end_group();
assert_eq!(history.undo_count(), 0);Sourcepub fn end_group(&self)
pub fn end_group(&self)
Ends the current command grouping.
The grouped commands are added to the history as a single composite command, so one undo reverses all of them. If no commands were grouped, nothing is added — an empty group leaves no trace.
Safe to call when no group is open.
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
history.begin_group();
history.end_group();
// An empty group adds nothing to the undo stack.
assert_eq!(history.undo_count(), 0);
assert!(!history.can_undo());
// Closing when nothing is open is harmless.
history.end_group();Sourcepub fn set_max_size(&self, max_size: usize)
pub fn set_max_size(&self, max_size: usize)
Sets the maximum history size.
If the current history exceeds the new size, older commands are removed. This is useful for adjusting memory usage based on system resources.
§Arguments
max_size- New maximum size (number of commands to keep)
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
// Increase limit for memory-rich environments
history.set_max_size(500);
assert_eq!(history.max_size(), 500);
// Decrease limit for constrained environments
history.set_max_size(50);
assert_eq!(history.max_size(), 50);Sourcepub fn undo_count(&self) -> usize
pub fn undo_count(&self) -> usize
Returns the current number of undo operations available.
This can be useful for displaying history statistics or managing UI state (e.g., enabling/disabling undo buttons).
§Returns
The number of commands that can be undone.
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
assert_eq!(history.undo_count(), 0);
// After adding commands...
// assert!(history.undo_count() > 0);Sourcepub fn redo_count(&self) -> usize
pub fn redo_count(&self) -> usize
Returns the current number of redo operations available.
This can be useful for displaying history statistics or managing UI state (e.g., enabling/disabling redo buttons).
§Returns
The number of commands that can be redone.
§Example
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
assert_eq!(history.redo_count(), 0);
// After undoing some commands...
// assert!(history.redo_count() > 0);Trait Implementations§
Source§impl Clone for CommandHistory
impl Clone for CommandHistory
Source§impl Debug for CommandHistory
impl Debug for CommandHistory
Auto Trait Implementations§
impl Forget for CommandHistory
impl Freeze for CommandHistory
impl RefUnwindSafe for CommandHistory
impl Send for CommandHistory
impl Sync for CommandHistory
impl Unpin for CommandHistory
impl UnsafeUnpin for CommandHistory
impl UnwindSafe for CommandHistory
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<State, Message> IntoBoot<State, Message> for State
impl<State, Message> IntoBoot<State, Message> for State
Source§fn into_boot(self) -> (State, Task<Message>)
fn into_boot(self) -> (State, Task<Message>)
Application.