Skip to content

Commit 83f177d

Browse files
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
1 parent 7547a96 commit 83f177d

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
@@ -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

42764283
fn goto_next_function(cx: &mut Context) {

0 commit comments

Comments
 (0)