2021-11-20 15:17:25 +01:00
|
|
|
use tree_sitter::Node;
|
|
|
|
|
2021-07-01 21:24:22 +02:00
|
|
|
use crate::{Rope, Syntax};
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-11-08 01:57:26 +01:00
|
|
|
const PAIRS: &[(char, char)] = &[
|
|
|
|
('(', ')'),
|
|
|
|
('{', '}'),
|
|
|
|
('[', ']'),
|
|
|
|
('<', '>'),
|
|
|
|
('\'', '\''),
|
2021-11-20 15:17:25 +01:00
|
|
|
('\"', '\"'),
|
2021-11-08 01:57:26 +01:00
|
|
|
];
|
2021-11-20 15:17:25 +01:00
|
|
|
|
2021-12-24 02:57:31 +01:00
|
|
|
// limit matching pairs to only ( ) { } [ ] < > ' ' " "
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-11-20 15:17:25 +01:00
|
|
|
// Returns the position of the matching bracket under cursor.
|
|
|
|
//
|
|
|
|
// If the cursor is one the opening bracket, the position of
|
|
|
|
// the closing bracket is returned. If the cursor in the closing
|
|
|
|
// bracket, the position of the opening bracket is returned.
|
|
|
|
//
|
|
|
|
// If the cursor is not on a bracket, `None` is returned.
|
|
|
|
#[must_use]
|
|
|
|
pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
|
|
|
|
if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
find_pair(syntax, doc, pos, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the position of the bracket that is closing the current scope.
|
|
|
|
//
|
|
|
|
// If the cursor is on an opening or closing bracket, the function
|
|
|
|
// behaves equivalent to [`find_matching_bracket`].
|
|
|
|
//
|
|
|
|
// If the cursor position is within a scope, the function searches
|
|
|
|
// for the surrounding scope that is surrounded by brackets and
|
|
|
|
// returns the position of the closing bracket for that scope.
|
|
|
|
//
|
|
|
|
// If no surrounding scope is found, the function returns `None`.
|
2021-03-24 06:52:13 +01:00
|
|
|
#[must_use]
|
2021-11-20 15:17:25 +01:00
|
|
|
pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
|
|
|
|
find_pair(syntax, doc, pos, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option<usize> {
|
2021-03-26 03:02:32 +01:00
|
|
|
let tree = syntax.tree();
|
2021-11-20 15:17:25 +01:00
|
|
|
let pos = doc.char_to_byte(pos);
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-11-20 15:17:25 +01:00
|
|
|
let mut node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-11-20 15:17:25 +01:00
|
|
|
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));
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-11-20 15:17:25 +01:00
|
|
|
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.
|
|
|
|
return Some(end_char);
|
|
|
|
}
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-11-20 15:17:25 +01:00
|
|
|
if traverse_parents {
|
|
|
|
node = node.parent()?;
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2021-06-03 10:35:17 +02:00
|
|
|
}
|
2021-11-20 15:17:25 +01:00
|
|
|
}
|
2021-06-03 10:35:17 +02:00
|
|
|
|
2021-11-20 15:17:25 +01:00
|
|
|
fn is_valid_bracket(c: char) -> bool {
|
|
|
|
PAIRS.iter().any(|(l, r)| *l == c || *r == c)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid_pair(doc: &Rope, 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)> {
|
2021-06-05 06:00:43 +02:00
|
|
|
let len = doc.len_bytes();
|
2021-11-20 15:17:25 +01:00
|
|
|
|
2021-07-23 03:45:43 +02:00
|
|
|
let start_byte = node.start_byte();
|
2021-11-20 15:17:25 +01:00
|
|
|
let end_byte = node.end_byte().saturating_sub(1);
|
|
|
|
|
2021-07-23 03:45:43 +02:00
|
|
|
if start_byte >= len || end_byte >= len {
|
2021-06-05 06:00:43 +02:00
|
|
|
return None;
|
|
|
|
}
|
2021-07-23 03:45:43 +02:00
|
|
|
|
2021-11-20 15:17:25 +01:00
|
|
|
Some((start_byte, end_byte))
|
2021-03-22 09:58:49 +01:00
|
|
|
}
|