@@ -1,8 +1,10 @@
1use std::iter;
1use std::iter;
3use tree_sitter::Node;
4use tree_sitter::Node;
5−use crate::{Rope, Syntax};
6+use crate::movement::Direction::{self, Backward, Forward};
7const MAX_PLAINTEXT_SCAN: usize = 10000;
9const MAX_PLAINTEXT_SCAN: usize = 10000;
8const MATCH_LIMIT: usize = 16;
10const MATCH_LIMIT: usize = 16;
@@ -27,7 +29,7 @@ const PAIRS: &[(char, char)] = &[
28/// If no matching bracket is found, `None` is returned.
30/// If no matching bracket is found, `None` is returned.
29#[must_use]
31#[must_use]
30−pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
32+pub fn find_matching_bracket(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
31 if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
33 if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
32 return None;
34 return None;
@@ -45,13 +47,18 @@ pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<
46// If no surrounding scope is found, the function returns `None`.
48// If no surrounding scope is found, the function returns `None`.
47#[must_use]
49#[must_use]
48−pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
50+pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
49 find_pair(syntax, doc, pos, true)
51 find_pair(syntax, doc, pos, true)
52−fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option<usize> {
58+ traverse_parents: bool,
53 let tree = syntax.tree();
60 let tree = syntax.tree();
54− let pos = doc.char_to_byte(pos);
61+ let pos = doc.char_to_byte(pos_);
56 let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?;
63 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) ->
94 if node.is_named() {
101 if node.is_named() {
95− return None;
102+ break;
@@ -104,8 +111,16 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
104 return doc.try_byte_to_char(close.start_byte()).ok();
111 return doc.try_byte_to_char(close.start_byte()).ok();
107− node = node.parent()?;
114+ let Some(parent) = node.parent() else { break; };
117+ let node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;
118+ if node.child_count() != 0 {
121+ let node_start = doc.byte_to_char(node.start_byte());
122+ find_matching_bracket_plaintext(doc.byte_slice(node.byte_range()), pos_ - node_start)
123+ .map(|pos| pos + node_start)
111/// Returns the position of the matching bracket under cursor.
126/// 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) ->
121/// If no matching bracket is found, `None` is returned.
136/// If no matching bracket is found, `None` is returned.
122#[must_use]
137#[must_use]
123−pub fn find_matching_bracket_current_line_plaintext(
138+pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option<usize> {
127 // Don't do anything when the cursor is not on top of a bracket.
139 // Don't do anything when the cursor is not on top of a bracket.
128 let bracket = doc.char(cursor_pos);
140 let bracket = doc.char(cursor_pos);
129 if !is_valid_bracket(bracket) {
141 if !is_valid_bracket(bracket) {
@@ -179,11 +191,11 @@ fn is_forward_bracket(c: char) -> bool {
179 PAIRS.iter().any(|(l, _)| *l == c)
191 PAIRS.iter().any(|(l, _)| *l == c)
182−fn is_valid_pair(doc: &Rope, start_char: usize, end_char: usize) -> bool {
194+fn is_valid_pair(doc: RopeSlice, start_char: usize, end_char: usize) -> bool {
183 PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
195 PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
186−fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
198+fn surrounding_bytes(doc: RopeSlice, node: &Node) -> Option<(usize, usize)> {
187 let len = doc.len_bytes();
199 let len = doc.len_bytes();
189 let start_byte = node.start_byte();
201 let start_byte = node.start_byte();
@@ -196,22 +208,8 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
196 Some((start_byte, end_byte))
208 Some((start_byte, end_byte))
199−/// Checks if `node` or its siblings (at most MATCH_LIMIT nodes) is the specified openiing char
203−/// The position of the found node or `None` otherwise
204−fn find_open_pair(doc: &Rope, node: Option<Node>, open: char) -> Option<usize> {
205− iter::successors(node, |node| node.prev_sibling())
208− let (pos, c) = as_char(doc, &node)?;
209− (c == open).then_some(pos)
213/// Tests if this node is a pair close char and returns the expected open char
211/// Tests if this node is a pair close char and returns the expected open char
214−fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
212+fn as_close_pair(doc: RopeSlice, node: &Node) -> Option<char> {
215 let close = as_char(doc, node)?.1;
213 let close = as_char(doc, node)?.1;
@@ -223,8 +221,17 @@ fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
223/// # Returns
221/// # Returns
225/// The position of the found node or `None` otherwise
223/// The position of the found node or `None` otherwise
226−fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize> {
224+fn find_pair_end(
228+ direction: Direction,
230+ let advance = match direction {
231+ Forward => Node::next_sibling,
232+ Backward => Node::prev_sibling,
227− iter::successors(node, |node| node.next_sibling())
234+ iter::successors(node, advance)
228 .take(MATCH_LIMIT)
235 .take(MATCH_LIMIT)
229 .find_map(|node| {
236 .find_map(|node| {
230 let (pos, c) = as_char(doc, &node)?;
237 let (pos, c) = as_char(doc, &node)?;
@@ -233,15 +240,15 @@ fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize>
235/// Tests if this node is a pair close char and returns the expected open char
242/// Tests if this node is a pair close char and returns the expected open char
236−fn as_open_pair(doc: &Rope, node: &Node) -> Option<char> {
243+fn as_open_pair(doc: RopeSlice, node: &Node) -> Option<char> {
237− let close = as_char(doc, node)?.1;
244+ let open = as_char(doc, node)?.1;
240 .find_map(|&(open_, close)| (open_ == open).then_some(close))
247 .find_map(|&(open_, close)| (open_ == open).then_some(close))
243−/// Tests if this node is a pair opening and returns the expected close char
250+/// If node is a single char return it (and its char position)
244−fn as_char(doc: &Rope, node: &Node) -> Option<(usize, char)> {
251+fn as_char(doc: RopeSlice, node: &Node) -> Option<(usize, char)> {
245 // TODO: multi char/non ASCII pairs
252 // TODO: multi char/non ASCII pairs
246 if node.byte_range().len() != 1 {
253 if node.byte_range().len() != 1 {
247 return None;
254 return None;
@@ -257,11 +264,11 @@ mod tests {
258 fn test_find_matching_bracket_current_line_plaintext() {
265 fn test_find_matching_bracket_current_line_plaintext() {
259 let assert = |input: &str, pos, expected| {
266 let assert = |input: &str, pos, expected| {
260− let input = &Rope::from(input);
267+ let input = RopeSlice::from(input);
261− let actual = find_matching_bracket_current_line_plaintext(input, pos);
268+ let actual = find_matching_bracket_plaintext(input, pos);
262 assert_eq!(expected, actual.unwrap());
269 assert_eq!(expected, actual.unwrap());
264− let actual = find_matching_bracket_current_line_plaintext(input, expected);
271+ let actual = find_matching_bracket_plaintext(input, expected);
265 assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
272 assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");