Use injection syntax trees for bracket matching

Previously we used the root syntax tree for bracket matching. We can use
the new functionality in `Syntax` for finding the correct syntax tree
for a given byte range though so we use the correct syntax tree within
injections. This improves bracket matching behavior within HTML
injections like script or style tags for example.
pull/9386/head
Michael Davis 10 months ago committed by Blaž Hrastnik
parent 9978d421fe
commit 5e0b3cc28b

@ -57,10 +57,10 @@ fn find_pair(
pos_: usize, pos_: usize,
traverse_parents: bool, traverse_parents: bool,
) -> Option<usize> { ) -> Option<usize> {
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 + 1)?; let root = syntax.tree_for_byte_range(pos, pos + 1).root_node();
let mut node = root.descendant_for_byte_range(pos, pos + 1)?;
loop { loop {
if node.is_named() { if node.is_named() {
@ -118,9 +118,7 @@ fn find_pair(
}; };
node = parent; node = parent;
} }
let node = tree let node = root.named_descendant_for_byte_range(pos, pos + 1)?;
.root_node()
.named_descendant_for_byte_range(pos, pos + 1)?;
if node.child_count() != 0 { if node.child_count() != 0 {
return None; return None;
} }

@ -1338,7 +1338,7 @@ impl Syntax {
result result
} }
pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Node<'_>> { pub fn tree_for_byte_range(&self, start: usize, end: usize) -> &Tree {
let mut container_id = self.root; let mut container_id = self.root;
for (layer_id, layer) in self.layers.iter() { for (layer_id, layer) in self.layers.iter() {
@ -1349,8 +1349,11 @@ impl Syntax {
} }
} }
self.layers[container_id] self.layers[container_id].tree()
.tree() }
pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Node<'_>> {
self.tree_for_byte_range(start, end)
.root_node() .root_node()
.descendant_for_byte_range(start, end) .descendant_for_byte_range(start, end)
} }

Loading…
Cancel
Save