diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs index fd43a110e796..7fda6d7e5e1a 100644 --- a/helix-core/src/match_brackets.rs +++ b/helix-core/src/match_brackets.rs @@ -1,8 +1,13 @@ +use std::iter; + +use ropey::RopeSlice; use tree_sitter::Node; -use crate::{Rope, Syntax}; +use crate::movement::Direction::{self, Backward, Forward}; +use crate::Syntax; const MAX_PLAINTEXT_SCAN: usize = 10000; +const MATCH_LIMIT: usize = 16; // Limit matching pairs to only ( ) { } [ ] < > ' ' " " const PAIRS: &[(char, char)] = &[ @@ -24,7 +29,7 @@ const PAIRS: &[(char, char)] = &[ /// /// If no matching bracket is found, `None` is returned. #[must_use] -pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option { +pub fn find_matching_bracket(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option { if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) { return None; } @@ -42,35 +47,80 @@ pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option< // // If no surrounding scope is found, the function returns `None`. #[must_use] -pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option { +pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option { find_pair(syntax, doc, pos, true) } -fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option { +fn find_pair( + syntax: &Syntax, + doc: RopeSlice, + pos_: usize, + traverse_parents: bool, +) -> Option { let tree = syntax.tree(); - let pos = doc.char_to_byte(pos); + let pos = doc.char_to_byte(pos_); - let mut node = tree.root_node().named_descendant_for_byte_range(pos, pos)?; + let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?; loop { - let (start_byte, end_byte) = surrounding_bytes(doc, &node)?; - let (start_char, end_char) = (doc.byte_to_char(start_byte), doc.byte_to_char(end_byte)); + if node.is_named() { + let (start_byte, end_byte) = surrounding_bytes(doc, &node)?; + let (start_char, end_char) = (doc.byte_to_char(start_byte), doc.byte_to_char(end_byte)); + + if is_valid_pair(doc, start_char, end_char) { + if end_byte == pos { + return Some(start_char); + } + + // We return the end char if the cursor is either on the start char + // or at some arbitrary position between start and end char. + if traverse_parents || start_byte == pos { + return Some(end_char); + } + } + } + // this node itselt wasn't a pair but maybe its siblings are + + // check if we are *on* the pair (special cased so we don't look + // at the current node twice and to jump to the start on that case) + if let Some(open) = as_close_pair(doc, &node) { + if let Some(pair_start) = find_pair_end(doc, node.prev_sibling(), open, Backward) { + return Some(pair_start); + } + } - if is_valid_pair(doc, start_char, end_char) { - if end_byte == pos { - return Some(start_char); + if !traverse_parents { + // check if we are *on* the opening pair (special cased here as + // an opptimization since we only care about bracket on the cursor + // here) + if let Some(close) = as_open_pair(doc, &node) { + if let Some(pair_end) = find_pair_end(doc, node.next_sibling(), close, Forward) { + return Some(pair_end); + } + } + if node.is_named() { + break; } - // We return the end char if the cursor is either on the start char - // or at some arbitrary position between start and end char. - return Some(end_char); } - if traverse_parents { - node = node.parent()?; - } else { - return None; + for close in + iter::successors(node.next_sibling(), |node| node.next_sibling()).take(MATCH_LIMIT) + { + let Some(open) = as_close_pair(doc, &close) else { continue; }; + if find_pair_end(doc, Some(node), open, Backward).is_some() { + return doc.try_byte_to_char(close.start_byte()).ok(); + } } + let Some(parent) = node.parent() else { break; }; + node = parent; } + let node = tree.root_node().named_descendant_for_byte_range(pos, pos)?; + if node.child_count() != 0 { + return None; + } + let node_start = doc.byte_to_char(node.start_byte()); + find_matching_bracket_plaintext(doc.byte_slice(node.byte_range()), pos_ - node_start) + .map(|pos| pos + node_start) } /// Returns the position of the matching bracket under cursor. @@ -85,10 +135,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> /// /// If no matching bracket is found, `None` is returned. #[must_use] -pub fn find_matching_bracket_current_line_plaintext( - doc: &Rope, - cursor_pos: usize, -) -> Option { +pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option { // Don't do anything when the cursor is not on top of a bracket. let bracket = doc.char(cursor_pos); if !is_valid_bracket(bracket) { @@ -144,11 +191,11 @@ fn is_forward_bracket(c: char) -> bool { PAIRS.iter().any(|(l, _)| *l == c) } -fn is_valid_pair(doc: &Rope, start_char: usize, end_char: usize) -> bool { +fn is_valid_pair(doc: RopeSlice, start_char: usize, end_char: usize) -> bool { PAIRS.contains(&(doc.char(start_char), doc.char(end_char))) } -fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> { +fn surrounding_bytes(doc: RopeSlice, node: &Node) -> Option<(usize, usize)> { let len = doc.len_bytes(); let start_byte = node.start_byte(); @@ -161,6 +208,55 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> { Some((start_byte, end_byte)) } +/// Tests if this node is a pair close char and returns the expected open char +fn as_close_pair(doc: RopeSlice, node: &Node) -> Option { + let close = as_char(doc, node)?.1; + PAIRS + .iter() + .find_map(|&(open, close_)| (close_ == close).then_some(open)) +} + +/// Checks if `node` or its siblings (at most MATCH_LIMIT nodes) is the specified closing char +/// +/// # Returns +/// +/// The position of the found node or `None` otherwise +fn find_pair_end( + doc: RopeSlice, + node: Option, + end_char: char, + direction: Direction, +) -> Option { + let advance = match direction { + Forward => Node::next_sibling, + Backward => Node::prev_sibling, + }; + iter::successors(node, advance) + .take(MATCH_LIMIT) + .find_map(|node| { + let (pos, c) = as_char(doc, &node)?; + (end_char == c).then_some(pos) + }) +} + +/// Tests if this node is a pair close char and returns the expected open char +fn as_open_pair(doc: RopeSlice, node: &Node) -> Option { + let open = as_char(doc, node)?.1; + PAIRS + .iter() + .find_map(|&(open_, close)| (open_ == open).then_some(close)) +} + +/// If node is a single char return it (and its char position) +fn as_char(doc: RopeSlice, node: &Node) -> Option<(usize, char)> { + // TODO: multi char/non ASCII pairs + if node.byte_range().len() != 1 { + return None; + } + let pos = doc.try_byte_to_char(node.start_byte()).ok()?; + Some((pos, doc.char(pos))) +} + #[cfg(test)] mod tests { use super::*; @@ -168,11 +264,11 @@ mod tests { #[test] fn test_find_matching_bracket_current_line_plaintext() { let assert = |input: &str, pos, expected| { - let input = &Rope::from(input); - let actual = find_matching_bracket_current_line_plaintext(input, pos); + let input = RopeSlice::from(input); + let actual = find_matching_bracket_plaintext(input, pos); assert_eq!(expected, actual.unwrap()); - let actual = find_matching_bracket_current_line_plaintext(input, expected); + let actual = find_matching_bracket_plaintext(input, expected); assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour"); }; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 1e89fe1cb69b..608b0b934599 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4535,8 +4535,8 @@ fn match_brackets(cx: &mut Context) { let selection = doc.selection(view.id).clone().transform(|range| { let pos = range.cursor(text_slice); if let Some(matched_pos) = doc.syntax().map_or_else( - || match_brackets::find_matching_bracket_current_line_plaintext(text, pos), - |syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text, pos), + || match_brackets::find_matching_bracket_plaintext(text.slice(..), pos), + |syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text.slice(..), pos), ) { range.put_cursor(text_slice, matched_pos, is_select) } else { diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 43b5d1af6ec3..ee47b1b675eb 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -501,7 +501,9 @@ impl EditorView { use helix_core::match_brackets; let pos = doc.selection(view.id).primary().cursor(text); - if let Some(pos) = match_brackets::find_matching_bracket(syntax, doc.text(), pos) { + if let Some(pos) = + match_brackets::find_matching_bracket(syntax, doc.text().slice(..), pos) + { // ensure col is on screen if let Some(highlight) = theme.find_scope_index_exact("ui.cursor.match") { return vec![(highlight, pos..pos + 1)];