From 0f76923a4df2cac295580c2febb72dbf7c562a12 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 23 Jul 2023 08:08:43 +0800 Subject: [PATCH] feat: restore original cursor on undoing --- src/core/input/input.rs | 20 +++++++++++++------- src/core/input/snaps.rs | 12 +++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/core/input/input.rs b/src/core/input/input.rs index 9275d841..aaba385a 100644 --- a/src/core/input/input.rs +++ b/src/core/input/input.rs @@ -97,19 +97,25 @@ impl Input { } #[inline] - pub fn visual(&mut self) -> bool { - self.snap_mut().visual(); - self.escape() - } + pub fn visual(&mut self) -> bool { self.snap_mut().visual() } #[inline] pub fn undo(&mut self) -> bool { - self.snaps.undo(); - self.escape() + if !self.snaps.undo() { + return false; + } + self.escape(); + true } #[inline] - pub fn redo(&mut self) -> bool { self.snaps.redo() } + pub fn redo(&mut self) -> bool { + if !self.snaps.redo() { + return false; + } + self.escape(); + true + } pub fn move_(&mut self, step: isize) -> bool { let snap = self.snap(); diff --git a/src/core/input/snaps.rs b/src/core/input/snaps.rs index 45fe09e5..2076a724 100644 --- a/src/core/input/snaps.rs +++ b/src/core/input/snaps.rs @@ -1,3 +1,5 @@ +use std::mem; + use super::InputSnap; #[derive(Default, PartialEq, Eq)] @@ -37,7 +39,8 @@ impl InputSnaps { } pub(super) fn tag(&mut self) -> bool { - if self.current.value == self.versions[self.idx].value { + self.catch(); + if self.versions[self.idx].value == self.current.value { return false; } @@ -46,6 +49,13 @@ impl InputSnaps { self.idx += 1; true } + + #[inline] + pub(super) fn catch(&mut self) { + let value = mem::replace(&mut self.versions[self.idx].value, String::new()); + self.versions[self.idx] = self.current.clone(); + self.versions[self.idx].value = value; + } } impl InputSnaps {