Skip to main content

NodeMatch

Struct NodeMatch 

Source
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 document
  • D: Doc - Document type containing the source and language info

Implementations§

Source§

impl<'tree, D> NodeMatch<'tree, D>
where D: Doc,

Source

pub const fn new( node: Node<'tree, D>, env: MetaVarEnv<'tree, D>, ) -> NodeMatch<'tree, D>

Source

pub const fn get_node(&self) -> &Node<'tree, D>

Source

pub const fn get_env(&self) -> &MetaVarEnv<'tree, D>

Returns the populated MetaVarEnv for this match.

Source

pub const fn get_env_mut(&mut self) -> &mut MetaVarEnv<'tree, D>

Source§

impl<D> NodeMatch<'_, D>
where D: Doc,

Source

pub fn replace_by<R>(&self, replacer: R) -> Edit<<D as Doc>::Source>
where R: Replacer<D>,

Methods from Deref<Target = Node<'tree, D>>§

Source

pub fn get_doc(&self) -> &'r D

Source

pub fn node_id(&self) -> usize

Source

pub fn is_leaf(&self) -> bool

Source

pub fn is_named_leaf(&self) -> bool

if has no named children. N.B. it is different from is_named && is_leaf

Source

pub fn is_error(&self) -> bool

Source

pub fn kind(&self) -> Cow<'_, str>

Source

pub fn kind_id(&self) -> u16

Source

pub fn is_named(&self) -> bool

Source

pub fn is_missing(&self) -> bool

Source

pub fn range(&self) -> Range<usize>

byte offsets of start and end.

Source

pub fn start_pos(&self) -> Position

Nodes’ start position in terms of zero-based rows and columns.

Source

pub fn end_pos(&self) -> Position

Nodes’ end position in terms of rows and columns.

Source

pub fn text(&self) -> Cow<'r, str>

Source

pub fn lang(&self) -> &'r <D as Doc>::Lang

Source

pub fn get_inner_node(&self) -> <D as Doc>::Node<'r>

the underlying tree-sitter Node

Source

pub fn root(&self) -> &'r Root<D>

Source

pub fn matches<M>(&self, m: M) -> bool
where M: Matcher,

Source

pub fn inside<M>(&self, m: M) -> bool
where M: Matcher,

Source

pub fn has<M>(&self, m: M) -> bool
where M: Matcher,

Source

pub fn precedes<M>(&self, m: M) -> bool
where M: Matcher,

Source

pub fn follows<M>(&self, m: M) -> bool
where M: Matcher,

Source

pub fn parent(&self) -> Option<Node<'r, D>>

Source

pub fn children(&self) -> impl ExactSizeIterator

Source

pub fn child(&self, nth: usize) -> Option<Node<'r, D>>

Source

pub fn field(&self, name: &str) -> Option<Node<'r, D>>

Source

pub fn child_by_field_id(&self, field_id: u16) -> Option<Node<'r, D>>

Source

pub fn field_children(&self, name: &str) -> impl Iterator<Item = Node<'r, D>>

Source

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.

Source

pub fn next(&self) -> Option<Node<'r, D>>

Source

pub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>>

Returns all sibling nodes next to self.

Source

pub fn prev(&self) -> Option<Node<'r, D>>

Source

pub fn prev_all(&self) -> impl Iterator<Item = Node<'r, D>>

Source

pub fn dfs<'s>(&'s self) -> impl Iterator<Item = Node<'r, D>> + 's

Source

pub fn find<M>(&self, pat: M) -> Option<NodeMatch<'r, D>>
where M: Matcher,

Source

pub fn find_all<'s, M>( &'s self, pat: M, ) -> impl Iterator<Item = NodeMatch<'r, D>> + 's
where M: Matcher + 's,

Source

pub fn replace<M, R>( &self, matcher: M, replacer: R, ) -> Option<Edit<<D as Doc>::Source>>
where M: Matcher, R: Replacer<D>,

Source

pub fn after(&self) -> Edit<<D as Doc>::Source>

Source

pub fn before(&self) -> Edit<<D as Doc>::Source>

Source

pub fn append(&self) -> Edit<<D as Doc>::Source>

Source

pub fn prepend(&self) -> Edit<<D as Doc>::Source>

Source

pub fn empty(&self) -> Option<Edit<<D as Doc>::Source>>

Empty children. Remove all child node

Source

pub fn remove(&self) -> Edit<<D as Doc>::Source>

Remove the node itself

Trait Implementations§

Source§

impl<'tree, D> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>
where D: Doc,

NodeMatch is an immutable view to Node

Source§

fn borrow(&self) -> &Node<'tree, D>

Immutably borrows from an owned value. Read more
Source§

impl<'t, D> Clone for NodeMatch<'t, D>
where D: Clone + Doc,

Source§

fn clone(&self) -> NodeMatch<'t, D>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'t, D> Debug for NodeMatch<'t, D>
where D: Debug + Doc,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'tree, D> Deref for NodeMatch<'tree, D>
where D: Doc,

NodeMatch is an immutable view to Node

Source§

type Target = Node<'tree, D>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<NodeMatch<'tree, D> as Deref>::Target

Dereferences the value.
Source§

impl<'tree, D> From<Node<'tree, D>> for NodeMatch<'tree, D>
where D: Doc,

Source§

fn from(node: Node<'tree, D>) -> NodeMatch<'tree, D>

Converts to this type from the input type.
Source§

impl<'tree, D> From<NodeMatch<'tree, D>> for Node<'tree, D>
where D: Doc,

NodeMatch is an immutable view to Node

Source§

fn from(node_match: NodeMatch<'tree, D>) -> Node<'tree, D>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'t, D> Freeze for NodeMatch<'t, D>
where <D as Doc>::Node<'t>: Freeze,

§

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>
where <D as Doc>::Node<'t>: Unpin, <<D as Doc>::Source as Content>::Underlying: Unpin,

§

impl<'t, D> UnsafeUnpin for NodeMatch<'t, D>
where <D as Doc>::Node<'t>: UnsafeUnpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more