history: Ensure unique History in redos and undos when push. (#761)

This commit is contained in:
Jason Lee 2025-04-01 19:35:16 +08:00 committed by GitHub
parent ebe93210af
commit 99e7f8ee70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,6 +13,11 @@ pub trait HistoryItem: Clone + PartialEq {
/// This is now used in Input for undo/redo operations. You can also use this in
/// your own models to keep track of changes, for example to track the tab
/// history for prev/next features.
///
/// ## Use cases
///
/// - Undo/redo operations in Input
/// - Tracking tab history for prev/next features
#[derive(Debug)]
pub struct History<I: HistoryItem> {
undos: Vec<I>,
@ -86,6 +91,7 @@ where
if self.unique {
self.undos.retain(|c| *c != item);
self.redos.retain(|c| *c != item);
}
let mut item = item;
@ -251,6 +257,13 @@ mod tests {
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].tab_index, 1);
assert_eq!(history.redos().len(), 1);
// Push duplicate, should be ignored
history.push(2.into());
assert_eq!(history.undos().len(), 2);
assert_eq!(history.redos().len(), 1);
// Redo the last undone change
let changes = history.redo().unwrap();
assert_eq!(changes.len(), 1);
@ -260,7 +273,7 @@ mod tests {
history.push(3.into());
// Check the version and undo stack
assert_eq!(history.version(), 6);
assert_eq!(history.version(), 7);
assert_eq!(history.undos().len(), 4);
// Undo all changes