@@ -0,0 +1,34 @@
1+use crate::{Range, Rope, Selection, Syntax};
2+
3+// const PAIRS: &[(char, char)] = &[('(', ')'), ('{', '}'), ('[', ']')];
4+// limit matching pairs to only ( ) { } [ ] < >
5+
6+pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
7+ let tree = syntax.root_layer.tree.as_ref().unwrap();
8+
9+ let byte_pos = doc.char_to_byte(pos);
10+
11+ // most naive implementation: find the innermost syntax node, if we're at the edge of a node,
12+ // return the other edge.
13+
14+ let mut node = match tree
15+ .root_node()
16+ .named_descendant_for_byte_range(byte_pos, byte_pos)
17+ {
18+ Some(node) => node,
19+ None => return None,
20+ };
21+
22+ let start_byte = node.start_byte();
23+ let end_byte = node.end_byte() - 1; // it's end exclusive
24+
25+ if start_byte == byte_pos {
26+ return Some(doc.byte_to_char(end_byte));
27+ }
28+
29+ if end_byte == byte_pos {
30+ return Some(doc.byte_to_char(start_byte));
31+ }
32+
33+ None
34+}