Skip to content

Commit

Permalink
feat(command): select_all_children_in_selection
Browse files Browse the repository at this point in the history
  • Loading branch information
dead10ck committed Apr 1, 2024
1 parent 2ba8f46 commit 24aeefe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
53 changes: 34 additions & 19 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use helix_core::{
syntax::{BlockCommentToken, LanguageServerFeature},
text_annotations::{Overlay, TextAnnotations},
textobject,
tree_sitter::Tree,
unicode::width::UnicodeWidthChar,
visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes,
RopeReader, RopeSlice, Selection, SmallVec, Tendril, Transaction,
Expand Down Expand Up @@ -439,6 +440,7 @@ impl MappableCommand {
select_prev_sibling, "Select previous sibling the in syntax tree",
select_all_siblings, "Select all siblings of the current node",
select_all_children, "Select all children of the current node",
select_all_children_in_selection, "Select all children of the current node that are contained in the current selection",
jump_forward, "Jump forward on jumplist",
jump_backward, "Jump backward on jumplist",
save_selection, "Save current selection to jumplist",
Expand Down Expand Up @@ -4943,37 +4945,50 @@ pub fn extend_parent_node_start(cx: &mut Context) {
move_node_bound_impl(cx, Direction::Backward, Movement::Extend)
}

fn select_all_impl<F>(editor: &mut Editor, select_fn: F)
where
F: Fn(&Tree, RopeSlice, Selection) -> Selection,
{
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 = select_fn(syntax.tree(), text, current_selection.clone());
doc.set_selection(view.id, selection);
}
}

fn select_all_siblings(cx: &mut Context) {
let motion = |editor: &mut Editor| {
let (view, doc) = current!(editor);
select_all_impl(editor, object::select_all_siblings);
};

if let Some(syntax) = doc.syntax() {
let text = doc.text().slice(..);
let current_selection = doc.selection(view.id);
let selection =
object::select_all_siblings(syntax.tree(), text, current_selection.clone());
doc.set_selection(view.id, selection);
}
cx.editor.apply_motion(motion);
}

fn select_all_children_in_selection(cx: &mut Context) {
let motion = |editor: &mut Editor| {
select_all_impl(editor, |tree, text, selection| {
let all_children = object::select_all_children(tree, text, selection.clone());

if selection.contains(&all_children) {
all_children
} else {
selection
}
});
};

cx.editor.apply_motion(motion);
}

fn select_all_children(cx: &mut Context) {
let motion = |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 =
object::select_all_children(syntax.tree(), text, current_selection.clone());
doc.set_selection(view.id, selection);
}
select_all_impl(editor, object::select_all_children);
};

motion(cx.editor);
cx.editor.last_motion = Some(Motion(Box::new(motion)));
cx.editor.apply_motion(motion);
}

fn match_brackets(cx: &mut Context) {
Expand Down
40 changes: 20 additions & 20 deletions helix-term/tests/test/commands/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,82 +607,82 @@ async fn select_all_children() -> anyhow::Result<()> {
let tests = vec![
// basic tests
(
helpers::platform_line(indoc! {r##"
indoc! {r##"
let foo = bar#[(a, b, c)|]#;
"##}),
"##},
"<A-I>",
helpers::platform_line(indoc! {r##"
indoc! {r##"
let foo = bar(#[a|]#, #(b|)#, #(c|)#);
"##}),
"##},
),
(
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = #[[
1,
2,
3,
4,
5,
]|]#;
"##}),
"##},
"<A-I>",
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##}),
"##},
),
// direction is preserved
(
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = #[|[
1,
2,
3,
4,
5,
]]#;
"##}),
"##},
"<A-I>",
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = [
#[|1]#,
#(|2)#,
#(|3)#,
#(|4)#,
#(|5)#,
];
"##}),
"##},
),
// can't pick any more children - selection stays the same
(
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##}),
"##},
"<A-I>",
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##}),
"##},
),
// each cursor does the sibling select independently
(
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = #[|[
1,
2,
Expand All @@ -698,9 +698,9 @@ async fn select_all_children() -> anyhow::Result<()> {
"four",
"five",
]|)#;
"##}),
"##},
"<A-I>",
helpers::platform_line(indoc! {r##"
indoc! {r##"
let a = [
#[|1]#,
#(|2)#,
Expand All @@ -716,7 +716,7 @@ async fn select_all_children() -> anyhow::Result<()> {
#("four"|)#,
#("five"|)#,
];
"##}),
"##},
),
];

Expand Down

0 comments on commit 24aeefe

Please sign in to comment.