use std::fmt;
use std::ptr::NonNull;
use crate::node::{Node, NodeRaw};
use crate::{Point, TreeCursor};
pub(super) enum SyntaxTreeData {}
pub struct Tree {
ptr: NonNull<SyntaxTreeData>,
}
impl Tree {
pub(super) unsafe fn from_raw(raw: NonNull<SyntaxTreeData>) -> Tree {
Tree { ptr: raw }
}
pub(super) fn as_raw(&self) -> NonNull<SyntaxTreeData> {
self.ptr
}
pub fn root_node(&self) -> Node<'_> {
unsafe { Node::from_raw(ts_tree_root_node(self.ptr)).unwrap() }
}
pub fn edit(&mut self, edit: &InputEdit) {
unsafe { ts_tree_edit(self.ptr, edit) }
}
pub fn walk(&self) -> TreeCursor<'_> {
self.root_node().walk()
}
}
impl fmt::Debug for Tree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{{Tree {:?}}}", self.root_node())
}
}
impl Drop for Tree {
fn drop(&mut self) {
unsafe { ts_tree_delete(self.ptr) }
}
}
impl Clone for Tree {
fn clone(&self) -> Self {
unsafe {
Tree {
ptr: ts_tree_copy(self.ptr),
}
}
}
}
unsafe impl Send for Tree {}
unsafe impl Sync for Tree {}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct InputEdit {
pub start_byte: u32,
pub old_end_byte: u32,
pub new_end_byte: u32,
pub start_point: Point,
pub old_end_point: Point,
pub new_end_point: Point,
}
impl InputEdit {
pub fn offset(&self) -> i32 {
self.new_end_byte as i32 - self.old_end_byte as i32
}
}
extern "C" {
fn ts_tree_copy(self_: NonNull<SyntaxTreeData>) -> NonNull<SyntaxTreeData>;
fn ts_tree_delete(self_: NonNull<SyntaxTreeData>);
fn ts_tree_root_node<'tree>(self_: NonNull<SyntaxTreeData>) -> NodeRaw;
fn ts_tree_edit(self_: NonNull<SyntaxTreeData>, edit: &InputEdit);
}