| 1 |
use std::fmt; |
| 2 |
use std::ptr::NonNull; |
| 3 |
|
| 4 |
use crate::node::{Node, NodeRaw}; |
| 5 |
use crate::{Point, TreeCursor}; |
| 6 |
|
| 7 |
|
| 8 |
pub(super) enum SyntaxTreeData {} |
| 9 |
|
| 10 |
pub struct Tree { |
| 11 |
ptr: NonNull<SyntaxTreeData>, |
| 12 |
} |
| 13 |
|
| 14 |
impl Tree { |
| 15 |
pub(super) unsafe fn from_raw(raw: NonNull<SyntaxTreeData>) -> Tree { |
| 16 |
Tree { ptr: raw } |
| 17 |
} |
| 18 |
|
| 19 |
pub(super) fn as_raw(&self) -> NonNull<SyntaxTreeData> { |
| 20 |
self.ptr |
| 21 |
} |
| 22 |
|
| 23 |
pub fn root_node(&self) -> Node<'_> { |
| 24 |
unsafe { Node::from_raw(ts_tree_root_node(self.ptr)).unwrap() } |
| 25 |
} |
| 26 |
|
| 27 |
pub fn edit(&mut self, edit: &InputEdit) { |
| 28 |
unsafe { ts_tree_edit(self.ptr, edit) } |
| 29 |
} |
| 30 |
|
| 31 |
pub fn walk(&self) -> TreeCursor<'_> { |
| 32 |
self.root_node().walk() |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl fmt::Debug for Tree { |
| 37 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 38 |
write!(f, "{{Tree {:?}}}", self.root_node()) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl Drop for Tree { |
| 43 |
fn drop(&mut self) { |
| 44 |
unsafe { ts_tree_delete(self.ptr) } |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl Clone for Tree { |
| 49 |
fn clone(&self) -> Self { |
| 50 |
unsafe { |
| 51 |
Tree { |
| 52 |
ptr: ts_tree_copy(self.ptr), |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
unsafe impl Send for Tree {} |
| 59 |
unsafe impl Sync for Tree {} |
| 60 |
|
| 61 |
#[repr(C)] |
| 62 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 63 |
pub struct InputEdit { |
| 64 |
pub start_byte: u32, |
| 65 |
pub old_end_byte: u32, |
| 66 |
pub new_end_byte: u32, |
| 67 |
pub start_point: Point, |
| 68 |
pub old_end_point: Point, |
| 69 |
pub new_end_point: Point, |
| 70 |
} |
| 71 |
|
| 72 |
impl InputEdit { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
pub fn offset(&self) -> i32 { |
| 79 |
self.new_end_byte as i32 - self.old_end_byte as i32 |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
extern "C" { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
fn ts_tree_copy(self_: NonNull<SyntaxTreeData>) -> NonNull<SyntaxTreeData>; |
| 88 |
|
| 89 |
fn ts_tree_delete(self_: NonNull<SyntaxTreeData>); |
| 90 |
|
| 91 |
fn ts_tree_root_node<'tree>(self_: NonNull<SyntaxTreeData>) -> NodeRaw; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fn ts_tree_edit(self_: NonNull<SyntaxTreeData>, edit: &InputEdit); |
| 98 |
} |