2021-07-01 21:24:22 +02:00
|
|
|
use crate::{Rope, Syntax};
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-06-03 10:35:17 +02:00
|
|
|
const PAIRS: &[(char, char)] = &[('(', ')'), ('{', '}'), ('[', ']'), ('<', '>')];
|
2021-03-22 09:58:49 +01:00
|
|
|
// limit matching pairs to only ( ) { } [ ] < >
|
|
|
|
|
2021-03-24 06:52:13 +01:00
|
|
|
#[must_use]
|
2021-03-22 09:58:49 +01:00
|
|
|
pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
|
2021-03-26 03:02:32 +01:00
|
|
|
let tree = syntax.tree();
|
2021-03-22 09:58:49 +01:00
|
|
|
|
|
|
|
let byte_pos = doc.char_to_byte(pos);
|
|
|
|
|
|
|
|
// most naive implementation: find the innermost syntax node, if we're at the edge of a node,
|
|
|
|
// return the other edge.
|
|
|
|
|
2021-07-01 21:55:18 +02:00
|
|
|
let node = match tree
|
2021-03-22 09:58:49 +01:00
|
|
|
.root_node()
|
|
|
|
.named_descendant_for_byte_range(byte_pos, byte_pos)
|
|
|
|
{
|
|
|
|
Some(node) => node,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
2021-06-03 10:35:17 +02:00
|
|
|
if node.is_error() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:58:49 +01:00
|
|
|
let start_byte = node.start_byte();
|
2021-06-05 06:00:43 +02:00
|
|
|
let len = doc.len_bytes();
|
|
|
|
if start_byte >= len {
|
|
|
|
return None;
|
|
|
|
}
|
2021-03-22 09:58:49 +01:00
|
|
|
let end_byte = node.end_byte() - 1; // it's end exclusive
|
2021-06-04 02:40:42 +02:00
|
|
|
let start_char = doc.byte_to_char(start_byte);
|
|
|
|
let end_char = doc.byte_to_char(end_byte);
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-06-04 02:40:42 +02:00
|
|
|
if PAIRS.contains(&(doc.char(start_char), doc.char(end_char))) {
|
2021-06-03 10:35:17 +02:00
|
|
|
if start_byte == byte_pos {
|
2021-06-04 02:40:42 +02:00
|
|
|
return Some(end_char);
|
2021-06-03 10:35:17 +02:00
|
|
|
}
|
2021-03-22 09:58:49 +01:00
|
|
|
|
2021-06-03 10:35:17 +02:00
|
|
|
if end_byte == byte_pos {
|
2021-06-04 02:40:42 +02:00
|
|
|
return Some(start_char);
|
2021-06-03 10:35:17 +02:00
|
|
|
}
|
2021-03-22 09:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|