pub struct KindMatcher { /* private fields */ }Expand description
Matcher that finds AST nodes based on their syntactic type (kind).
KindMatcher is the simplest type of matcher - it matches nodes whose
type matches a specific string. Every AST node has a “kind” that describes
what syntax element it represents.
§Examples
ⓘ
// Match all function declarations
let matcher = KindMatcher::new("function_declaration", &language);
let functions: Vec<_> = root.find_all(&matcher).collect();
// Match all identifiers
let id_matcher = KindMatcher::new("identifier", &language);
let identifiers: Vec<_> = root.find_all(&id_matcher).collect();
// Find syntax errors in code
let error_matcher = KindMatcher::error_matcher();
let errors: Vec<_> = root.find_all(&error_matcher).collect();§Common Node Kinds
The exact node kinds depend on the language, but common examples include:
"function_declaration"- Function definitions"identifier"- Variable/function names"string_literal"- String values"number"- Numeric literals"ERROR"- Syntax errors
Implementations§
Source§impl KindMatcher
impl KindMatcher
pub fn new<L: Language>(node_kind: &str, lang: &L) -> Self
pub fn try_new<L: Language>( node_kind: &str, lang: &L, ) -> Result<Self, KindMatcherError>
pub const fn from_id(kind: u16) -> Self
Sourcepub const fn is_invalid(&self) -> bool
pub const fn is_invalid(&self) -> bool
Whether the kind matcher contains undefined tree-sitter kind.
Sourcepub const fn error_matcher() -> Self
pub const fn error_matcher() -> Self
Construct a matcher that only matches ERROR
Trait Implementations§
Source§impl Clone for KindMatcher
impl Clone for KindMatcher
Source§fn clone(&self) -> KindMatcher
fn clone(&self) -> KindMatcher
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for KindMatcher
impl Debug for KindMatcher
Source§impl Matcher for KindMatcher
impl Matcher for KindMatcher
Source§fn match_node_with_env<'tree, D: Doc>(
&self,
node: Node<'tree, D>,
_env: &mut Cow<'_, MetaVarEnv<'tree, D>>,
) -> Option<Node<'tree, D>>
fn match_node_with_env<'tree, D: Doc>( &self, node: Node<'tree, D>, _env: &mut Cow<'_, MetaVarEnv<'tree, D>>, ) -> Option<Node<'tree, D>>
Attempt to match a node, updating the meta-variable environment. Read more
Auto Trait Implementations§
impl Freeze for KindMatcher
impl RefUnwindSafe for KindMatcher
impl Send for KindMatcher
impl Sync for KindMatcher
impl Unpin for KindMatcher
impl UnsafeUnpin for KindMatcher
impl UnwindSafe for KindMatcher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more