summaryrefslogtreecommitdiff
path: root/helix-term
diff options
context:
space:
mode:
authorMichael Davis2022-08-31 08:48:33 +0000
committerGitHub2022-08-31 08:48:33 +0000
commit83f177d2701b0ec58abcb47c2a1ee133e72342e4 (patch)
treece1f1eb7caef4e5b63b9247e8b9310b4b7efccc7 /helix-term
parent7547a961bb29b7619cf683a241781041aa208c37 (diff)
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
Diffstat (limited to 'helix-term')
-rw-r--r--helix-term/src/commands.rs41
1 files changed, 24 insertions, 17 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 5bce6ca6..41960d35 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -4251,26 +4251,33 @@ fn scroll_down(cx: &mut Context) {
scroll(cx, cx.count(), Direction::Forward);
}
-fn goto_ts_object_impl(cx: &mut Context, object: &str, direction: Direction) {
+fn goto_ts_object_impl(cx: &mut Context, object: &'static str, direction: Direction) {
let count = cx.count();
- let (view, doc) = current!(cx.editor);
- let text = doc.text().slice(..);
- let range = doc.selection(view.id).primary();
+ let motion = move |editor: &mut Editor| {
+ let (view, doc) = current!(editor);
+ if let Some((lang_config, syntax)) = doc.language_config().zip(doc.syntax()) {
+ let text = doc.text().slice(..);
+ let root = syntax.tree().root_node();
- let new_range = match doc.language_config().zip(doc.syntax()) {
- Some((lang_config, syntax)) => movement::goto_treesitter_object(
- text,
- range,
- object,
- direction,
- syntax.tree().root_node(),
- lang_config,
- count,
- ),
- None => range,
- };
+ let selection = doc.selection(view.id).clone().transform(|range| {
+ movement::goto_treesitter_object(
+ text,
+ range,
+ object,
+ direction,
+ root,
+ lang_config,
+ count,
+ )
+ });
- doc.set_selection(view.id, Selection::single(new_range.anchor, new_range.head));
+ doc.set_selection(view.id, selection);
+ } else {
+ editor.set_status("Syntax-tree is not available in current buffer");
+ }
+ };
+ motion(cx.editor);
+ cx.editor.last_motion = Some(Motion(Box::new(motion)));
}
fn goto_next_function(cx: &mut Context) {