type_sitter_lib/node/
tree.rs1use crate::{raw, InputEdit, LanguageRef, Node, NodeResult, Range, TreeCursor};
2#[cfg(feature = "yak-sitter")]
3use std::iter::Once;
4use std::marker::PhantomData;
5#[cfg(unix)]
6use std::os::unix::io::AsRawFd;
7#[cfg(windows)]
8use std::os::windows::io::AsRawHandle;
9#[cfg(feature = "yak-sitter")]
10use std::path::Path;
11#[cfg(feature = "yak-sitter")]
12use tree_sitter::TextProvider;
13
14#[derive(Clone, Debug)]
15#[repr(transparent)]
16pub struct Tree<Root: Node<'static>>(pub raw::Tree, PhantomData<Root>);
17
18impl<Root: Node<'static>> Tree<Root> {
19 pub fn wrap(tree: raw::Tree) -> Self {
24 Self(tree, PhantomData)
25 }
26
27 pub fn wrap_ref(tree: &raw::Tree) -> &Self {
29 unsafe { &*(tree as *const raw::Tree as *const Self) }
31 }
32
33 pub fn wrap_mut(tree: &mut raw::Tree) -> &mut Self {
35 unsafe { &mut *(tree as *mut raw::Tree as *mut Self) }
37 }
38
39 #[inline]
41 #[cfg(feature = "yak-sitter")]
42 pub fn text(&self) -> &str {
43 self.0.text()
44 }
45
46 #[inline]
50 #[cfg(feature = "yak-sitter")]
51 pub fn path(&self) -> Option<&Path> {
52 self.0.path()
53 }
54
55 pub fn root_node(&self) -> NodeResult<'_, Root::WithLifetime<'_>> {
60 Root::WithLifetime::try_from_raw(self.0.root_node())
61 }
62
63 #[inline]
65 pub fn walk(&self) -> TreeCursor<'_> {
66 TreeCursor(self.0.walk())
67 }
68
69 #[inline]
71 pub fn included_ranges(&self) -> Vec<Range> {
72 self.0.included_ranges()
73 }
74
75 #[inline]
77 pub fn changed_ranges(&self, other: &Tree<Root>) -> impl ExactSizeIterator<Item = Range> {
78 self.0.changed_ranges(&other.0)
79 }
80
81 #[inline]
83 pub fn language(&self) -> LanguageRef<'_> {
84 self.0.language()
85 }
86
87 #[inline]
90 pub fn print_dot_graph(
91 &self,
92 #[cfg(unix)] file: &impl AsRawFd,
93 #[cfg(windows)] file: &impl AsRawHandle,
94 ) {
95 self.0.print_dot_graph(file)
96 }
97
98 #[inline]
100 pub fn edit(&mut self, edit: &InputEdit) {
101 self.0.edit(edit)
102 }
103}
104
105#[cfg(feature = "yak-sitter")]
106impl<'tree, Root: Node<'static>> TextProvider<&'tree str> for &'tree Tree<Root> {
107 type I = Once<&'tree str>;
108
109 fn text(&mut self, node: tree_sitter::Node) -> Self::I {
110 TextProvider::text(&mut &self.0, node)
111 }
112}