QueryMatch#
Contains all captures for a query match. More...
import Script
Properties#
Name | |
---|---|
array<QueryCapture> | captures |
bool | isEmpty |
Methods#
Name | |
---|---|
RangeMark | get(string name) |
vector<RangeMark> | getAll(string name) |
vector<RangeMark> | getAllInRange(string name, RangeMark range) |
RangeMark | getAllJoined(string name) |
RangeMark | getInRange(string name, RangeMark range) |
array<QueryMatch> | queryIn(capture, query) |
Detailed Description#
The QueryMatch object allows you to get access to all the captures made by a Tree-sitter query.
Some high-level functions on CodeDocument and its subclasses also return QueryMatch instances. Usually these functions list which captures their matches will include.
Note
If you expect a query will only return a single QueryMatch, you can uses Javascripts destructuring assignment to easily get the right match:
// Note the [] surrounding `match`
let [match] = document.query("...");
if (match) { // In case the query fails, match will be undefined.
// ...
}
Property Documentation#
array<QueryCapture> captures#
List of all the captures in the current document.
This allows you to get access to both the range and the name of the capture.
Note
Usually you won't need to access the captures directly. Instead prefer to use the getter functions.
bool isEmpty#
Return true if the QueryMatch
is empty.
Method Documentation#
RangeMark get(string name)#
Returns the range covered by the first capture with the given name
.
This allows you to easily interact with a capture, if you know it will only cover a single node.
let [function] = document.query("...");
// Print the captured text
Message.log(match.get("parameter-list").text);
// Replace the captured text with something else
match.get("parameter-list").replace("(int myParameter)");
See the RangeMark documentation for more information.
vector<RangeMark> getAll(string name)#
Returns all ranges that are covered by the captures of the given name
vector<RangeMark> getAllInRange(string name, RangeMark range)#
Returns all ranges that are covered by the captures of the given name
in the given range
.
RangeMark getAllJoined(string name)#
Returns a smallest range that contains all captures for the given name
.
RangeMark getInRange(string name, RangeMark range)#
Returns the range covered by the first capture with the given name
in the given range
.
array<QueryMatch> queryIn(capture, query)#
Executes the treesitter query
on all nodes that were captured under the capture
name.
This is useful if you want to query for nodes that might be nested arbitrarily deeply within a larger construct.
E.g. To search for all "return" statements within a function, no matter how deep they are nested:
let [function] = document.query(`
(function_definition
declarator: (
; Some query to find a specific function
)
body: (compound_statement) @body)
`);
let return_statements = function.queryIn("body", "(return_statement) @return");