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 Jan 27, 2024
1 parent b6c6e09 commit 1302f26
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 @@ -421,6 +421,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 @@ -4620,37 +4621,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

0 comments on commit 1302f26

Please sign in to comment.