From 5e0b3cc28b8ae6da1f41b47080a695c99bbc844a Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 25 Jan 2024 14:05:42 -0500 Subject: [PATCH] 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. --- helix-core/src/match_brackets.rs | 8 +++----- helix-core/src/syntax.rs | 9 ++++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs index 150679b5c..b8bcc28ca 100644 --- a/helix-core/src/match_brackets.rs +++ b/helix-core/src/match_brackets.rs @@ -57,10 +57,10 @@ fn find_pair( pos_: usize, traverse_parents: bool, ) -> Option { - let tree = syntax.tree(); 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 { if node.is_named() { @@ -118,9 +118,7 @@ fn find_pair( }; node = parent; } - let node = tree - .root_node() - .named_descendant_for_byte_range(pos, pos + 1)?; + let node = root.named_descendant_for_byte_range(pos, pos + 1)?; if node.child_count() != 0 { return None; } diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index a5a85c575..a403e7ccf 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -1338,7 +1338,7 @@ impl Syntax { result } - pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option> { + pub fn tree_for_byte_range(&self, start: usize, end: usize) -> &Tree { let mut container_id = self.root; for (layer_id, layer) in self.layers.iter() { @@ -1349,8 +1349,11 @@ impl Syntax { } } - self.layers[container_id] - .tree() + self.layers[container_id].tree() + } + + pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option> { + self.tree_for_byte_range(start, end) .root_node() .descendant_for_byte_range(start, end) }