pub struct CompiledQuery { /* private fields */ }Expand description
A query compiled against one language’s grammar.
Implementations§
Source§impl CompiledQuery
impl CompiledQuery
Sourcepub fn compile(
language: &dyn Language,
source: &str,
) -> Result<Self, CompileError>
pub fn compile( language: &dyn Language, source: &str, ) -> Result<Self, CompileError>
Compile query source against a language.
§Errors
Returns CompileError for malformed syntax, unknown node kinds or fields, and for
a query that binds no captures.
Sourcepub const fn language(&self) -> LanguageId
pub const fn language(&self) -> LanguageId
The language this query was compiled against.
Sourcepub fn capture_names(&self) -> &[String]
pub fn capture_names(&self) -> &[String]
Capture names, in capture-index order.
Sourcepub fn pattern_count(&self) -> usize
pub fn pattern_count(&self) -> usize
How many alternative patterns the query contains.
Sourcepub fn for_each_match<'tree>(
&self,
tree: &'tree Tree,
source: &[u8],
visit: impl FnMut(QueryMatch<'_, 'tree>),
)
pub fn for_each_match<'tree>( &self, tree: &'tree Tree, source: &[u8], visit: impl FnMut(QueryMatch<'_, 'tree>), )
Run the query over a tree, invoking visit once per match.
A callback rather than an iterator, and rather than returning a Vec: matches
borrow the tree, and collecting them would allocate for every match on the hot path
this crate exists to keep cheap.
Matches arrive in the order tree-sitter walks the tree, which is deterministic for a given tree and query. Nothing downstream may depend on that order beyond determinism itself — violations are sorted before reporting regardless.
Sourcepub fn for_each_match_in<'tree>(
&self,
node: Node<'tree>,
source: &[u8],
visit: impl FnMut(QueryMatch<'_, 'tree>),
)
pub fn for_each_match_in<'tree>( &self, node: Node<'tree>, source: &[u8], visit: impl FnMut(QueryMatch<'_, 'tree>), )
The same, scoped to one node’s subtree.
What ctx.querySubtree is built on: a rule that has already matched a function and
wants to look inside it should not have to filter the whole file’s matches by
position, which is both slower and easy to get subtly wrong at boundaries.