Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/movement.rs')
| -rw-r--r-- | helix-core/src/movement.rs | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs index 09a99db2..e446d8cc 100644 --- a/helix-core/src/movement.rs +++ b/helix-core/src/movement.rs @@ -1,6 +1,7 @@ -use std::{borrow::Cow, cmp::Reverse, iter}; +use std::{cmp::Reverse, iter}; use ropey::iter::Chars; +use tree_sitter::{Node, QueryCursor}; use crate::{ char_idx_at_visual_offset, @@ -12,10 +13,9 @@ use crate::{ }, line_ending::rope_is_line_ending, position::char_idx_at_visual_block_offset, - syntax, + syntax::LanguageConfiguration, text_annotations::TextAnnotations, textobject::TextObject, - tree_sitter::Node, visual_offset_from_block, Range, RopeSlice, Selection, Syntax, }; @@ -560,23 +560,21 @@ fn reached_target(target: WordMotionTarget, prev_ch: char, next_ch: char) -> boo /// Finds the range of the next or previous textobject in the syntax sub-tree of `node`. /// Returns the range in the forwards direction. -#[allow(clippy::too_many_arguments)] pub fn goto_treesitter_object( slice: RopeSlice, range: Range, object_name: &str, dir: Direction, - slice_tree: &Node, - syntax: &Syntax, - loader: &syntax::Loader, + slice_tree: Node, + lang_config: &LanguageConfiguration, count: usize, ) -> Range { - let textobject_query = loader.textobject_query(syntax.root_language()); let get_range = move |range: Range| -> Option<Range> { let byte_pos = slice.char_to_byte(range.cursor(slice)); let cap_name = |t: TextObject| format!("{}.{}", object_name, t); - let nodes = textobject_query?.capture_nodes_any( + let mut cursor = QueryCursor::new(); + let nodes = lang_config.textobject_query()?.capture_nodes_any( &[ &cap_name(TextObject::Movement), &cap_name(TextObject::Around), @@ -584,6 +582,7 @@ pub fn goto_treesitter_object( ], slice_tree, slice, + &mut cursor, )?; let node = match dir { @@ -618,15 +617,14 @@ pub fn goto_treesitter_object( last_range } -fn find_parent_start<'tree>(node: &Node<'tree>) -> Option<Node<'tree>> { +fn find_parent_start(mut node: Node) -> Option<Node> { let start = node.start_byte(); - let mut node = Cow::Borrowed(node); while node.start_byte() >= start || !node.is_named() { - node = Cow::Owned(node.parent()?); + node = node.parent()?; } - Some(node.into_owned()) + Some(node) } pub fn move_parent_node_end( @@ -637,8 +635,8 @@ pub fn move_parent_node_end( movement: Movement, ) -> Selection { selection.transform(|range| { - let start_from = text.char_to_byte(range.from()) as u32; - let start_to = text.char_to_byte(range.to()) as u32; + let start_from = text.char_to_byte(range.from()); + let start_to = text.char_to_byte(range.to()); let mut node = match syntax.named_descendant_for_byte_range(start_from, start_to) { Some(node) => node, @@ -656,18 +654,18 @@ pub fn move_parent_node_end( // moving forward, we always want to move one past the end of the // current node, so use the end byte of the current node, which is an exclusive // end of the range - Direction::Forward => text.byte_to_char(node.end_byte() as usize), + Direction::Forward => text.byte_to_char(node.end_byte()), // moving backward, we want the cursor to land on the start char of // the current node, or if it is already at the start of a node, to traverse up to // the parent Direction::Backward => { - let end_head = text.byte_to_char(node.start_byte() as usize); + let end_head = text.byte_to_char(node.start_byte()); // if we're already on the beginning, look up to the parent if end_head == range.cursor(text) { - node = find_parent_start(&node).unwrap_or(node); - text.byte_to_char(node.start_byte() as usize) + node = find_parent_start(node).unwrap_or(node); + text.byte_to_char(node.start_byte()) } else { end_head } |