From 72a843c09d9c83eb6c5210242ad41e415006c4ef Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sun, 19 Feb 2023 22:22:43 +0100 Subject: [PATCH] _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. --- helix-term/src/commands.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 232ecadcd7a6..fe0184e0df50 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1051,8 +1051,7 @@ where .transform(|range| move_fn(text, range, count, behavior)); doc.set_selection(view.id, selection); }; - motion(cx.editor); - cx.editor.last_motion = Some(Motion(Box::new(motion))); + _apply_motion(cx, motion) } fn goto_prev_paragraph(cx: &mut CommandContext) { @@ -1224,6 +1223,7 @@ where _ => return, }; + // NOTE: not using _apply_motion as the repitition isn't identical find_char_impl(cx.editor, &search_fn, inclusive, extend, ch, count); cx.editor.last_motion = Some(Motion(Box::new(move |editor: &mut Editor| { find_char_impl(editor, &search_fn, inclusive, true, ch, 1); @@ -3084,8 +3084,7 @@ fn goto_next_change_impl(cx: &mut CommandContext, direction: Direction) { doc.set_selection(view.id, selection) }; - motion(cx.editor); - cx.editor.last_motion = Some(Motion(Box::new(motion))); + _apply_motion(cx, motion); } /// Returns the [Range] for a [Hunk] in the given text. @@ -4316,8 +4315,7 @@ fn expand_selection(cx: &mut CommandContext) { } } }; - motion(cx.editor); - cx.editor.last_motion = Some(Motion(Box::new(motion))); + _apply_motion(cx, motion); } fn shrink_selection(cx: &mut CommandContext) { @@ -4342,8 +4340,7 @@ fn shrink_selection(cx: &mut CommandContext) { doc.set_selection(view.id, selection); } }; - motion(cx.editor); - cx.editor.last_motion = Some(Motion(Box::new(motion))); + _apply_motion(cx, motion); } fn select_sibling_impl(cx: &mut CommandContext, sibling_fn: &'static F) @@ -4361,8 +4358,7 @@ where doc.set_selection(view.id, selection); } }; - motion(cx.editor); - cx.editor.last_motion = Some(Motion(Box::new(motion))); + _apply_motion(cx, motion); } fn select_next_sibling(cx: &mut CommandContext) { @@ -4642,8 +4638,7 @@ fn goto_ts_object_impl(cx: &mut CommandContext, object: &'static str, direction: editor.set_status("Syntax-tree is not available in current buffer"); } }; - motion(cx.editor); - cx.editor.last_motion = Some(Motion(Box::new(motion))); + _apply_motion(cx, motion); } fn goto_next_function(cx: &mut CommandContext) { @@ -4694,6 +4689,12 @@ fn select_textobject_inner(cx: &mut CommandContext) { select_textobject(cx, textobject::TextObject::Inside); } +//TODO: move +fn _apply_motion(cx: &mut CommandContext, motion: F) { + motion(cx.editor); + cx.editor.last_motion = Some(Motion(Box::new(motion))); +} + fn select_textobject(cx: &mut CommandContext, objtype: textobject::TextObject) { let count = cx.count(); @@ -4764,8 +4765,7 @@ fn select_textobject(cx: &mut CommandContext, objtype: textobject::TextObject) { }); doc.set_selection(view.id, selection); }; - textobject(cx.editor); - cx.editor.last_motion = Some(Motion(Box::new(textobject))); + _apply_motion(cx, textobject); } });