Skip to content

Commit

Permalink
_apply_motion generalization where possible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gibbz00 committed Feb 20, 2023
1 parent 42c1d81 commit 72a843c
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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<F>(cx: &mut CommandContext, sibling_fn: &'static F)
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -4694,6 +4689,12 @@ fn select_textobject_inner(cx: &mut CommandContext) {
select_textobject(cx, textobject::TextObject::Inside);
}

//TODO: move
fn _apply_motion<F: Fn(&mut Editor) + 'static>(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();

Expand Down Expand Up @@ -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);
}
});

Expand Down

0 comments on commit 72a843c

Please sign in to comment.