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 Jul 19, 2023
1 parent d1b7cfe commit 9bc49a0
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use helix_core::{
syntax::LanguageServerFeature,
text_annotations::TextAnnotations,
textobject,
tree_sitter::Node,
tree_sitter::{Node, Tree},
unicode::width::UnicodeWidthChar,
visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes,
RopeReader, RopeSlice, Selection, SmallVec, Tendril, Transaction,
Expand Down Expand Up @@ -414,6 +414,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 @@ -4693,38 +4694,50 @@ fn select_prev_sibling(cx: &mut Context) {
select_sibling_impl(cx, &|node| Node::prev_sibling(&node))
}

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
}
});
};

motion(cx.editor);
cx.editor.last_motion = Some(Motion(Box::new(motion)));
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

0 comments on commit 9bc49a0

Please sign in to comment.