tree_sitter_utils/input.rs
1//! The [`Input`] type — a cheap, copyable snapshot passed to every handler.
2
3/// A snapshot of the current tree-sitter node together with caller-supplied
4/// context.
5///
6/// `Input` is designed to be passed by value; it is [`Copy`] whenever `Ctx`
7/// is [`Copy`].
8///
9/// # Example
10///
11/// ```rust
12/// // Input<()> uses the unit type as a zero-sized context.
13/// // In real usage Ctx is typically a shared reference to your extractor.
14/// use tree_sitter_utils::Input;
15/// fn accepts_input(_: Input<()>) {}
16/// ```
17#[derive(Clone, Copy)]
18pub struct Input<'tree, Ctx> {
19 /// The current syntax-tree node being examined.
20 pub node: tree_sitter::Node<'tree>,
21 /// Caller-supplied context (e.g. a reference to an extractor struct).
22 pub ctx: Ctx,
23 /// An optional trigger character hint (e.g. `.` or `(`), if the call
24 /// site has one.
25 pub trigger_char: Option<char>,
26}
27
28impl<'tree, Ctx> Input<'tree, Ctx> {
29 /// Construct a new [`Input`].
30 ///
31 /// # Example
32 ///
33 /// ```rust
34 /// # /* doc-test is compile-only; we cannot build a real Node here */
35 /// use tree_sitter_utils::Input;
36 /// // Input::new(node, ctx, None)
37 /// ```
38 #[inline]
39 pub fn new(node: tree_sitter::Node<'tree>, ctx: Ctx, trigger_char: Option<char>) -> Self {
40 Self { node, ctx, trigger_char }
41 }
42
43 /// Return a copy of this input with `node` replaced.
44 ///
45 /// Useful inside combinators that need to walk the tree.
46 ///
47 /// # Example
48 ///
49 /// ```rust
50 /// # /* compile-only */
51 /// use tree_sitter_utils::Input;
52 /// // let parent_input = input.with_node(parent_node);
53 /// ```
54 #[inline]
55 pub fn with_node(self, node: tree_sitter::Node<'tree>) -> Self {
56 Self { node, ..self }
57 }
58}