Skip to content

Commit eae079a

Browse files
committed
Refactor goto_ts_object_impl as a motion
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
1 parent a8b123f commit eae079a

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

helix-term/src/commands.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4220,26 +4220,33 @@ fn scroll_down(cx: &mut Context) {
42204220
scroll(cx, cx.count(), Direction::Forward);
42214221
}
42224222

4223-
fn goto_ts_object_impl(cx: &mut Context, object: &str, direction: Direction) {
4223+
fn goto_ts_object_impl(cx: &mut Context, object: &'static str, direction: Direction) {
42244224
let count = cx.count();
4225-
let (view, doc) = current!(cx.editor);
4226-
let text = doc.text().slice(..);
4227-
let range = doc.selection(view.id).primary();
4225+
let motion = move |editor: &mut Editor| {
4226+
let (view, doc) = current!(editor);
4227+
if let Some((lang_config, syntax)) = doc.language_config().zip(doc.syntax()) {
4228+
let text = doc.text().slice(..);
4229+
let root = syntax.tree().root_node();
42284230

4229-
let new_range = match doc.language_config().zip(doc.syntax()) {
4230-
Some((lang_config, syntax)) => movement::goto_treesitter_object(
4231-
text,
4232-
range,
4233-
object,
4234-
direction,
4235-
syntax.tree().root_node(),
4236-
lang_config,
4237-
count,
4238-
),
4239-
None => range,
4240-
};
4231+
let selection = doc.selection(view.id).clone().transform(|range| {
4232+
movement::goto_treesitter_object(
4233+
text,
4234+
range,
4235+
object,
4236+
direction,
4237+
root,
4238+
lang_config,
4239+
count,
4240+
)
4241+
});
42414242

4242-
doc.set_selection(view.id, Selection::single(new_range.anchor, new_range.head));
4243+
doc.set_selection(view.id, selection);
4244+
} else {
4245+
editor.set_status("Syntax-tree is not available in current buffer");
4246+
}
4247+
};
4248+
motion(cx.editor);
4249+
cx.editor.last_motion = Some(Motion(Box::new(motion)));
42434250
}
42444251

42454252
fn goto_next_function(cx: &mut Context) {

0 commit comments

Comments
 (0)