From c1488267e59a341ece2a8bb274e66127dca72088 Mon Sep 17 00:00:00 2001 From: Gabriel Hansson Date: Sun, 9 Jul 2023 22:50:24 +0200 Subject: (Updated) Apply motion API refinements (#6078) * _apply_motion generalization where possible API encourages users to not forget setting `editor.last_motion` when applying a motion. But also not setting `last_motion` without applying a motion first. * (rename) will_find_char -> find_char method name makes it sound like it would be returning a boolean. * use _apply_motion in find_char Feature that falls out from this is that repetitions of t,T,f,F are saved with the context extention/move and count. (Not defaulting to extend by 1 count). * Finalize apply_motion API last_motion is now a private field and can only be set by calling Editor.apply_motion(). Removing need (and possibility) of writing: `motion(editor); editor.last_motion = motion` Now it's just: `editor.apply_motion(motion)` * editor.last_message: rm Box wrap around Arc * Use pre-existing `Direction` rather than custom `SearchDirection`. * `LastMotion` type alias for `Option>` * Take motion rather than cloning it. Co-authored-by: Michael Davis * last_motion as Option. * Use `Box` over `Arc` for `last_motion`. --------- Co-authored-by: Michael Davis --- helix-view/src/editor.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 630ea5cf..affe87dd 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -834,18 +834,6 @@ impl Default for SearchConfig { } } -pub struct Motion(pub Box); -impl Motion { - pub fn run(&self, e: &mut Editor) { - (self.0)(e) - } -} -impl std::fmt::Debug for Motion { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("motion") - } -} - #[derive(Debug, Clone, Default)] pub struct Breakpoint { pub id: Option, @@ -910,8 +898,7 @@ pub struct Editor { pub auto_pairs: Option, pub idle_timer: Pin>, - pub last_motion: Option, - + last_motion: Option, pub last_completion: Option, pub exit_code: i32, @@ -945,6 +932,8 @@ pub struct Editor { pub completion_request_handle: Option>, } +pub type Motion = Box; + pub type RedrawHandle = (Arc, Arc>); #[derive(Debug)] @@ -1051,6 +1040,19 @@ impl Editor { } } + pub fn apply_motion(&mut self, motion: F) { + motion(self); + self.last_motion = Some(Box::new(motion)); + } + + pub fn repeat_last_motion(&mut self, count: usize) { + if let Some(motion) = self.last_motion.take() { + for _ in 0..count { + motion(self); + } + self.last_motion = Some(motion); + } + } /// Current editing mode for the [`Editor`]. pub fn mode(&self) -> Mode { self.mode -- cgit v1.2.3-70-g09d2