on master help sign in
Refactor goto_ts_object_impl as a motion (#3264)

This refactor changes the overall structure of the goto_ts_object_impl command without removing any functionality from its behavior. The refactored motion: * acts on all selections instead of reducing to one selection * may be repeated with the `repeat_last_motion` (A-.) command * informs the user when the syntax-tree is not accessible in the current buffer

commit83f177d2701b0ec58abcb47c2a1ee133e72342e4 authorMichael Davis <mcarsondavis@gmail.com> dateWed 31 Aug 2022, 03:48 (4 years ago) parent7547a96
1 file changed+24−17
helix-term/src/commands.rs+24−17
helix-term/src/commands.rs+24−17
@@ -4251,26 +4251,33 @@ fn scroll_down(cx: &mut Context) {
42514251 scroll(cx, cx.count(), Direction::Forward);
42524252}
42534253
4254−fn goto_ts_object_impl(cx: &mut Context, object: &str, direction: Direction) {
4254+fn goto_ts_object_impl(cx: &mut Context, object: &'static str, direction: Direction) {
42554255 let count = cx.count();
4256− let (view, doc) = current!(cx.editor);
4257− let text = doc.text().slice(..);
4258− let range = doc.selection(view.id).primary();
4256+ let motion = move |editor: &mut Editor| {
4257+ let (view, doc) = current!(editor);
4258+ if let Some((lang_config, syntax)) = doc.language_config().zip(doc.syntax()) {
4259+ let text = doc.text().slice(..);
4260+ let root = syntax.tree().root_node();
42594261
4260− let new_range = match doc.language_config().zip(doc.syntax()) {
4261− Some((lang_config, syntax)) => movement::goto_treesitter_object(
4262− text,
4263− range,
4264− object,
4265− direction,
4266− syntax.tree().root_node(),
4267− lang_config,
4268− count,
4269− ),
4270− None => range,
4271− };
4262+ let selection = doc.selection(view.id).clone().transform(|range| {
4263+ movement::goto_treesitter_object(
4264+ text,
4265+ range,
4266+ object,
4267+ direction,
4268+ root,
4269+ lang_config,
4270+ count,
4271+ )
4272+ });
42724273
4273− doc.set_selection(view.id, Selection::single(new_range.anchor, new_range.head));
4274+ doc.set_selection(view.id, selection);
4275+ } else {
4276+ editor.set_status("Syntax-tree is not available in current buffer");
4277+ }
4278+ };
4279+ motion(cx.editor);
4280+ cx.editor.last_motion = Some(Motion(Box::new(motion)));
42744281}
42754282
42764283fn goto_next_function(cx: &mut Context) {