Skip to content

Commit 5e0b3cc

Browse files
the-mikedavisarchseer
authored andcommitted
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.
1 parent 9978d42 commit 5e0b3cc

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

helix-core/src/match_brackets.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ fn find_pair(
5757
pos_: usize,
5858
traverse_parents: bool,
5959
) -> Option<usize> {
60-
let tree = syntax.tree();
6160
let pos = doc.char_to_byte(pos_);
6261

63-
let mut node = tree.root_node().descendant_for_byte_range(pos, pos + 1)?;
62+
let root = syntax.tree_for_byte_range(pos, pos + 1).root_node();
63+
let mut node = root.descendant_for_byte_range(pos, pos + 1)?;
6464

6565
loop {
6666
if node.is_named() {
@@ -118,9 +118,7 @@ fn find_pair(
118118
};
119119
node = parent;
120120
}
121-
let node = tree
122-
.root_node()
123-
.named_descendant_for_byte_range(pos, pos + 1)?;
121+
let node = root.named_descendant_for_byte_range(pos, pos + 1)?;
124122
if node.child_count() != 0 {
125123
return None;
126124
}

helix-core/src/syntax.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,7 @@ impl Syntax {
13381338
result
13391339
}
13401340

1341-
pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Node<'_>> {
1341+
pub fn tree_for_byte_range(&self, start: usize, end: usize) -> &Tree {
13421342
let mut container_id = self.root;
13431343

13441344
for (layer_id, layer) in self.layers.iter() {
@@ -1349,8 +1349,11 @@ impl Syntax {
13491349
}
13501350
}
13511351

1352-
self.layers[container_id]
1353-
.tree()
1352+
self.layers[container_id].tree()
1353+
}
1354+
1355+
pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Node<'_>> {
1356+
self.tree_for_byte_range(start, end)
13541357
.root_node()
13551358
.descendant_for_byte_range(start, end)
13561359
}

0 commit comments

Comments
 (0)