feat: add API for editing points and ranges
This commit is contained in:
parent
1a0868c487
commit
a69367f739
9 changed files with 209 additions and 39 deletions
|
|
@ -495,6 +495,14 @@ extern "C" {
|
|||
#[doc = " Check if two nodes are identical."]
|
||||
pub fn ts_node_eq(self_: TSNode, other: TSNode) -> bool;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Edit a point to keep it in-sync with source code that has been edited.\n\n This function updates a single point's byte offset and row/column position\n based on an edit operation. This is useful for editing points without\n requiring a tree or node instance."]
|
||||
pub fn ts_point_edit(point: *mut TSPoint, point_byte: *mut u32, edit: *const TSInputEdit);
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Edit a range to keep it in-sync with source code that has been edited.\n\n This function updates a range's start and end positions based on an edit\n operation. This is useful for editing ranges without requiring a tree\n or node instance."]
|
||||
pub fn ts_range_edit(range: *mut TSRange, edit: *const TSInputEdit);
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Create a new tree cursor starting from the given node.\n\n A tree cursor allows you to walk a syntax tree more efficiently than is\n possible using the [`TSNode`] functions. It is a mutable object that is always\n on a certain syntax node, and can be moved imperatively to different nodes.\n\n Note that the given node is considered the root of the cursor,\n and the cursor cannot walk outside this node."]
|
||||
pub fn ts_tree_cursor_new(node: TSNode) -> TSTreeCursor;
|
||||
|
|
|
|||
|
|
@ -120,6 +120,48 @@ pub struct InputEdit {
|
|||
pub new_end_position: Point,
|
||||
}
|
||||
|
||||
impl InputEdit {
|
||||
/// Edit a point to keep it in-sync with source code that has been edited.
|
||||
///
|
||||
/// This function updates a single point's byte offset and row/column position
|
||||
/// based on this edit operation. This is useful for editing points without
|
||||
/// requiring a tree or node instance.
|
||||
#[doc(alias = "ts_point_edit")]
|
||||
pub fn edit_point(&self, point: &mut Point, byte: &mut usize) {
|
||||
let edit = self.into();
|
||||
let mut ts_point = (*point).into();
|
||||
let mut ts_byte = *byte as u32;
|
||||
|
||||
unsafe {
|
||||
ffi::ts_point_edit(
|
||||
core::ptr::addr_of_mut!(ts_point),
|
||||
core::ptr::addr_of_mut!(ts_byte),
|
||||
&edit,
|
||||
);
|
||||
}
|
||||
|
||||
*point = ts_point.into();
|
||||
*byte = ts_byte as usize;
|
||||
}
|
||||
|
||||
/// Edit a range to keep it in-sync with source code that has been edited.
|
||||
///
|
||||
/// This function updates a range's start and end positions based on this edit
|
||||
/// operation. This is useful for editing ranges without requiring a tree
|
||||
/// or node instance.
|
||||
#[doc(alias = "ts_range_edit")]
|
||||
pub fn edit_range(&self, range: &mut Range) {
|
||||
let edit = self.into();
|
||||
let mut ts_range = (*range).into();
|
||||
|
||||
unsafe {
|
||||
ffi::ts_range_edit(core::ptr::addr_of_mut!(ts_range), &edit);
|
||||
}
|
||||
|
||||
*range = ts_range.into();
|
||||
}
|
||||
}
|
||||
|
||||
/// A single node within a syntax [`Tree`].
|
||||
#[doc(alias = "TSNode")]
|
||||
#[derive(Clone, Copy)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue