Skip to content

Commit

Permalink
Use injection syntax trees for bracket matching
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
the-mikedavis authored and archseer committed Jan 28, 2024
1 parent 9978d42 commit 5e0b3cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 3 additions & 5 deletions helix-core/src/match_brackets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ fn find_pair(
pos_: usize,
traverse_parents: bool,
) -> Option<usize> {
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() {
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 6 additions & 3 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ impl Syntax {
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;

for (layer_id, layer) in self.layers.iter() {
Expand All @@ -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<Node<'_>> {
self.tree_for_byte_range(start, end)
.root_node()
.descendant_for_byte_range(start, end)
}
Expand Down

0 comments on commit 5e0b3cc

Please sign in to comment.