type_sitter_lib/query/mod.rs
1use crate::raw;
2pub use captures::*;
3pub use cursor::*;
4pub use match_captures::*;
5pub use matches::*;
6
7/// [`QueryCaptures`]
8mod captures;
9/// [`QueryCursorExt`] to run typed queries
10mod cursor;
11/// [`QueryMatchCaptures`]
12mod match_captures;
13/// [`QueryMatches`]
14mod matches;
15
16/// A query which can generate type-safe matches and captures, which contain [typed nodes](Node)
17pub trait Query {
18 /// A match of this typed query (runtime pattern index)
19 type Match<'query, 'tree: 'query>: QueryMatch<'query, 'tree>;
20 /// An capture of this typed query (runtime capture index)
21 type Capture<'query, 'tree: 'query>: QueryCapture<'query, 'tree>;
22
23 /// The string used to generate this query
24 fn as_str(&self) -> &'static str;
25
26 /// The underlying tree-sitter `Query`
27 fn raw(&self) -> &'static raw::Query;
28
29 /// Wrap a tree-sitter `QueryMatch` which you know came from this query.
30 ///
31 /// # Safety
32 /// The match must have come from this query.
33 unsafe fn wrap_match<'query, 'tree>(
34 &self,
35 r#match: raw::QueryMatch<'query, 'tree>,
36 ) -> Self::Match<'query, 'tree>;
37
38 /// Wrap a reference to a tree-sitter `QueryMatch` which you know came from this query.
39 ///
40 /// # Safety
41 /// The match must have come from this query.
42 unsafe fn wrap_match_ref<'m, 'query, 'tree>(
43 &self,
44 r#match: &'m raw::QueryMatch<'query, 'tree>,
45 ) -> &'m Self::Match<'query, 'tree>;
46
47 //noinspection RsDuplicatedTraitMethodBinding -- IntelliJ inspection bug.
48 /// Wrap a tree-sitter `QueryCapture` which you know came from this query.
49 ///
50 /// # Safety
51 /// The capture must have come from this query.
52 unsafe fn wrap_capture<'query, 'tree: 'query>(
53 &self,
54 #[cfg(feature = "yak-sitter")] capture: raw::QueryCapture<'query, 'tree>,
55 #[cfg(not(feature = "yak-sitter"))] capture: raw::QueryCapture<'tree>,
56 ) -> Self::Capture<'query, 'tree>;
57}