Skip to content

Commit 024e2eb

Browse files
committed
syntax: Use matches rather than captures in textobject queries
Previously, `TextObject::capture_nodes` and `capture_nodes_any` collected the captures within a match for every capture which occurred, so for a query like [ (line_comment) (block_comment) ] @comment.inside and text like // comment 1 // comment 2 `capture_nodes` would produce `CaptureNode::Single` for the first comment and also `CaptureNode::Grouped` for both comments. Instead we want to have tree-sitter complete the full match and then consider nodes from the requested capture. Then `capture_nodes` produces a single `CaptureNode::Grouped` for both comments. This fixes the behavior of `mac` in languages like Rust.
1 parent 3338fc6 commit 024e2eb

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

helix-core/src/syntax.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ impl TextObjectQuery {
10541054
let mut cursor = InactiveQueryCursor::new(0..u32::MAX, TREE_SITTER_MATCH_LIMIT)
10551055
.execute_query(&self.query, node, RopeInput::new(slice));
10561056
let capture_node = iter::from_fn(move || {
1057-
let (mat, _) = cursor.next_matched_node()?;
1057+
let mat = cursor.next_match()?;
10581058
Some(mat.nodes_for_capture(capture).cloned().collect())
10591059
})
10601060
.filter_map(move |nodes: Vec<_>| {
@@ -1241,8 +1241,13 @@ mod test {
12411241
};
12421242

12431243
test("quantified_nodes", 1..37);
1244-
// NOTE: Enable after implementing proper node group capturing
1245-
// test("quantified_nodes_grouped", 1..37);
1244+
test("quantified_nodes_grouped", 1..37);
1245+
// TODO: the query for this works instead as
1246+
// ```
1247+
// ((line_comment) @capture (line_comment) @capture)
1248+
// ```
1249+
// The query used in this test case only captures the first line_comment node.
1250+
// Determine if this behavior is intentional in tree-sitter.
12461251
// test("multiple_nodes_grouped", 1..37);
12471252
}
12481253

0 commit comments

Comments
 (0)