pub struct NodeMatch<'t, D>(/* private fields */)
where
D: Doc;Expand description
Result of a successful pattern match containing the matched node and captured variables.
NodeMatch combines an AST node with the meta-variables captured during
pattern matching. It acts like a regular Node (through [Deref]) while
also providing access to captured variables through [get_env].
§Lifetime
The lifetime 't ties the match to its source document, ensuring memory safety.
§Usage Patterns
// Use as a regular node
let text = node_match.text();
let position = node_match.start_pos();
// Access captured meta-variables
let env = node_match.get_env();
let captured_name = env.get_match("VAR_NAME").unwrap();
// Generate replacement code
let edit = node_match.replace_by("new code with $VAR_NAME");§Type Parameters
't- Lifetime tied to the source documentD: Doc- Document type containing the source and language info
Implementations§
Source§impl<'tree, D> NodeMatch<'tree, D>where
D: Doc,
impl<'tree, D> NodeMatch<'tree, D>where
D: Doc,
pub const fn new( node: Node<'tree, D>, env: MetaVarEnv<'tree, D>, ) -> NodeMatch<'tree, D>
pub const fn get_node(&self) -> &Node<'tree, D>
Sourcepub const fn get_env(&self) -> &MetaVarEnv<'tree, D>
pub const fn get_env(&self) -> &MetaVarEnv<'tree, D>
Returns the populated MetaVarEnv for this match.
pub const fn get_env_mut(&mut self) -> &mut MetaVarEnv<'tree, D>
Methods from Deref<Target = Node<'tree, D>>§
pub fn get_doc(&self) -> &'r D
pub fn node_id(&self) -> usize
pub fn is_leaf(&self) -> bool
Sourcepub fn is_named_leaf(&self) -> bool
pub fn is_named_leaf(&self) -> bool
if has no named children.
N.B. it is different from is_named && is_leaf
pub fn is_error(&self) -> bool
pub fn kind(&self) -> Cow<'_, str>
pub fn kind_id(&self) -> u16
pub fn is_named(&self) -> bool
pub fn is_missing(&self) -> bool
Sourcepub fn start_pos(&self) -> Position
pub fn start_pos(&self) -> Position
Nodes’ start position in terms of zero-based rows and columns.
pub fn text(&self) -> Cow<'r, str>
pub fn lang(&self) -> &'r <D as Doc>::Lang
Sourcepub fn get_inner_node(&self) -> <D as Doc>::Node<'r>
pub fn get_inner_node(&self) -> <D as Doc>::Node<'r>
the underlying tree-sitter Node
pub fn root(&self) -> &'r Root<D>
pub fn matches<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn inside<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn has<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn precedes<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn follows<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn parent(&self) -> Option<Node<'r, D>>
pub fn children(&self) -> impl ExactSizeIterator
pub fn child(&self, nth: usize) -> Option<Node<'r, D>>
pub fn field(&self, name: &str) -> Option<Node<'r, D>>
pub fn child_by_field_id(&self, field_id: u16) -> Option<Node<'r, D>>
pub fn field_children(&self, name: &str) -> impl Iterator<Item = Node<'r, D>>
Sourcepub fn ancestors(&self) -> impl Iterator<Item = Node<'r, D>>
pub fn ancestors(&self) -> impl Iterator<Item = Node<'r, D>>
Returns all ancestors nodes of self.
Using cursor is overkill here because adjust cursor is too expensive.
pub fn next(&self) -> Option<Node<'r, D>>
Sourcepub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>>
pub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>>
Returns all sibling nodes next to self.
pub fn prev(&self) -> Option<Node<'r, D>>
pub fn prev_all(&self) -> impl Iterator<Item = Node<'r, D>>
pub fn dfs<'s>(&'s self) -> impl Iterator<Item = Node<'r, D>> + 's
pub fn find<M>(&self, pat: M) -> Option<NodeMatch<'r, D>>where
M: Matcher,
pub fn find_all<'s, M>(
&'s self,
pat: M,
) -> impl Iterator<Item = NodeMatch<'r, D>> + 'swhere
M: Matcher + 's,
pub fn replace<M, R>( &self, matcher: M, replacer: R, ) -> Option<Edit<<D as Doc>::Source>>
pub fn after(&self) -> Edit<<D as Doc>::Source>
pub fn before(&self) -> Edit<<D as Doc>::Source>
pub fn append(&self) -> Edit<<D as Doc>::Source>
pub fn prepend(&self) -> Edit<<D as Doc>::Source>
Trait Implementations§
Source§impl<'tree, D> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
impl<'tree, D> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
Source§impl<'tree, D> Deref for NodeMatch<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
impl<'tree, D> Deref for NodeMatch<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
Auto Trait Implementations§
impl<'t, D> Freeze for NodeMatch<'t, D>
impl<'t, D> RefUnwindSafe for NodeMatch<'t, D>where
<D as Doc>::Node<'t>: RefUnwindSafe,
D: RefUnwindSafe,
<<D as Doc>::Source as Content>::Underlying: RefUnwindSafe,
impl<'t, D> Send for NodeMatch<'t, D>
impl<'t, D> Sync for NodeMatch<'t, D>
impl<'t, D> Unpin for NodeMatch<'t, D>
impl<'t, D> UnsafeUnpin for NodeMatch<'t, D>
impl<'t, D> UnwindSafe for NodeMatch<'t, D>where
<D as Doc>::Node<'t>: UnwindSafe,
D: RefUnwindSafe,
<<D as Doc>::Source as Content>::Underlying: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more