diff options
author | Skyler Hawthorne | 2022-08-17 01:28:41 +0000 |
---|---|---|
committer | Michael Davis | 2023-08-01 14:41:42 +0000 |
commit | 93acb538121cab36712f40f26fa287df93817de5 (patch) | |
tree | b0ad0a3037d62750b4da6498a96b82758c25a18c /helix-term | |
parent | 1d702ea191e9bf84566ea0008755d1ab53d19265 (diff) |
add node boundary movement
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/commands.rs | 44 | ||||
-rw-r--r-- | helix-term/src/keymap/default.rs | 5 | ||||
-rw-r--r-- | helix-term/tests/test/commands.rs | 1 | ||||
-rw-r--r-- | helix-term/tests/test/commands/movement.rs | 199 |
4 files changed, 249 insertions, 0 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index bf60ad71..fc01eec7 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -247,6 +247,8 @@ impl MappableCommand { move_prev_long_word_start, "Move to start of previous long word", move_next_long_word_end, "Move to end of next long word", move_prev_long_word_end, "Move to end of previous long word", + move_parent_node_end, "Move to end of the parent node", + move_parent_node_start, "Move to beginning of the parent node", extend_next_word_start, "Extend to start of next word", extend_prev_word_start, "Extend to start of previous word", extend_next_word_end, "Extend to end of next word", @@ -255,6 +257,8 @@ impl MappableCommand { extend_prev_long_word_start, "Extend to start of previous long word", extend_next_long_word_end, "Extend to end of next long word", extend_prev_long_word_end, "Extend to end of prev long word", + extend_parent_node_end, "Extend to end of the parent node", + extend_parent_node_start, "Extend to beginning of the parent node", find_till_char, "Move till next occurrence of char", find_next_char, "Move to next occurrence of char", extend_till_char, "Extend till next occurrence of char", @@ -4605,6 +4609,46 @@ fn select_prev_sibling(cx: &mut Context) { select_sibling_impl(cx, &|node| Node::prev_sibling(&node)) } +fn move_node_bound_impl(cx: &mut Context, dir: Direction, movement: Movement) { + let motion = move |editor: &mut Editor| { + let (view, doc) = current!(editor); + + if let Some(syntax) = doc.syntax() { + let text = doc.text().slice(..); + let current_selection = doc.selection(view.id); + + let selection = movement::move_parent_node_end( + syntax, + text, + current_selection.clone(), + dir, + movement, + ); + + doc.set_selection(view.id, selection); + } + }; + + motion(cx.editor); + cx.editor.last_motion = Some(Motion(Box::new(motion))); +} + +pub fn move_parent_node_end(cx: &mut Context) { + move_node_bound_impl(cx, Direction::Forward, Movement::Move) +} + +pub fn move_parent_node_start(cx: &mut Context) { + move_node_bound_impl(cx, Direction::Backward, Movement::Move) +} + +pub fn extend_parent_node_end(cx: &mut Context) { + move_node_bound_impl(cx, Direction::Forward, Movement::Extend) +} + +pub fn extend_parent_node_start(cx: &mut Context) { + move_node_bound_impl(cx, Direction::Backward, Movement::Extend) +} + fn match_brackets(cx: &mut Context) { let (view, doc) = current!(cx.editor); let is_select = cx.editor.mode == Mode::Select; diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index 37983352..419e376f 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -88,6 +88,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> { "A-i" | "A-down" => shrink_selection, "A-p" | "A-left" => select_prev_sibling, "A-n" | "A-right" => select_next_sibling, + "A-e" => move_parent_node_end, + "A-b" => move_parent_node_start, "%" => select_all, "x" => extend_line_below, @@ -336,6 +338,9 @@ pub fn default() -> HashMap<Mode, KeyTrie> { "B" => extend_prev_long_word_start, "E" => extend_next_long_word_end, + "A-e" => extend_parent_node_end, + "A-b" => extend_parent_node_start, + "n" => extend_search_next, "N" => extend_search_prev, diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index b13c37bc..b3e13551 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -2,6 +2,7 @@ use helix_term::application::Application; use super::*; +mod movement; mod write; #[tokio::test(flavor = "multi_thread")] diff --git a/helix-term/tests/test/commands/movement.rs b/helix-term/tests/test/commands/movement.rs new file mode 100644 index 00000000..03dc7ba9 --- /dev/null +++ b/helix-term/tests/test/commands/movement.rs @@ -0,0 +1,199 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +async fn test_move_parent_node_end() -> anyhow::Result<()> { + let tests = vec![ + // single cursor stays single cursor, first goes to end of current + // node, then parent + ( + helpers::platform_line(indoc! {r##" + fn foo() { + let result = if true { + "yes" + } else { + "no#["|]# + } + } + "##}), + "<A-e>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else { + \"no\"#[\n|]# + } + } + "}), + ), + ( + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else { + \"no\"#[\n|]# + } + } + "}), + "<A-e>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else { + \"no\" + }#[\n|]# + } + "}), + ), + // select mode extends + ( + helpers::platform_line(indoc! {r##" + fn foo() { + let result = if true { + "yes" + } else { + #["no"|]# + } + } + "##}), + "v<A-e><A-e>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else { + #[\"no\" + }\n|]# + } + "}), + ), + ]; + + for test in tests { + test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?; + } + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn test_move_parent_node_start() -> anyhow::Result<()> { + let tests = vec![ + // single cursor stays single cursor, first goes to end of current + // node, then parent + ( + helpers::platform_line(indoc! {r##" + fn foo() { + let result = if true { + "yes" + } else { + "no#["|]# + } + } + "##}), + "<A-b>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else { + #[\"|]#no\" + } + } + "}), + ), + ( + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else { + \"no\"#[\n|]# + } + } + "}), + "<A-b>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else #[{|]# + \"no\" + } + } + "}), + ), + ( + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else #[{|]# + \"no\" + } + } + "}), + "<A-b>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } #[e|]#lse { + \"no\" + } + } + "}), + ), + // select mode extends + ( + helpers::platform_line(indoc! {r##" + fn foo() { + let result = if true { + "yes" + } else { + #["no"|]# + } + } + "##}), + "v<A-b><A-b>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } else #[|{ + ]#\"no\" + } + } + "}), + ), + ( + helpers::platform_line(indoc! {r##" + fn foo() { + let result = if true { + "yes" + } else { + #["no"|]# + } + } + "##}), + "v<A-b><A-b><A-b>", + helpers::platform_line(indoc! {"\ + fn foo() { + let result = if true { + \"yes\" + } #[|else { + ]#\"no\" + } + } + "}), + ), + ]; + + for test in tests { + test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?; + } + + Ok(()) +} |