From a0359f7f22d26548d5680fb1dc177f6d4b07ad11 Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Tue, 6 Jun 2023 01:48:36 +0200 Subject: make TS matching fallback to plaintext --- helix-core/src/match_brackets.rs | 81 ++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 37 deletions(-) (limited to 'helix-core/src/match_brackets.rs') diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs index 434df904..7fda6d7e 100644 --- a/helix-core/src/match_brackets.rs +++ b/helix-core/src/match_brackets.rs @@ -1,8 +1,10 @@ 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; @@ -27,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; } @@ -45,13 +47,18 @@ 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().descendant_for_byte_range(pos, pos)?; @@ -92,7 +99,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> } } if node.is_named() { - return None; + break; } } @@ -104,8 +111,16 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> return doc.try_byte_to_char(close.start_byte()).ok(); } } - node = node.parent()?; + 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. @@ -120,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) { @@ -179,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(); @@ -196,22 +208,8 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> { Some((start_byte, end_byte)) } -/// Checks if `node` or its siblings (at most MATCH_LIMIT nodes) is the specified openiing char -/// -/// # Returns -/// -/// The position of the found node or `None` otherwise -fn find_open_pair(doc: &Rope, node: Option, open: char) -> Option { - iter::successors(node, |node| node.prev_sibling()) - .take(MATCH_LIMIT) - .find_map(|node| { - let (pos, c) = as_char(doc, &node)?; - (c == open).then_some(pos) - }) -} - /// Tests if this node is a pair close char and returns the expected open char -fn as_close_pair(doc: &Rope, node: &Node) -> Option { +fn as_close_pair(doc: RopeSlice, node: &Node) -> Option { let close = as_char(doc, node)?.1; PAIRS .iter() @@ -223,8 +221,17 @@ fn as_close_pair(doc: &Rope, node: &Node) -> Option { /// # Returns /// /// The position of the found node or `None` otherwise -fn find_close_pair(doc: &Rope, node: Option, close: char) -> Option { - iter::successors(node, |node| node.next_sibling()) +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)?; @@ -233,15 +240,15 @@ fn find_close_pair(doc: &Rope, node: Option, close: char) -> Option } /// Tests if this node is a pair close char and returns the expected open char -fn as_open_pair(doc: &Rope, node: &Node) -> Option { - let close = as_char(doc, node)?.1; +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)) } -/// Tests if this node is a pair opening and returns the expected close char -fn as_char(doc: &Rope, node: &Node) -> Option<(usize, char)> { +/// 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; @@ -257,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"); }; -- cgit v1.2.3-70-g09d2