From 1848d0bc3632ef128d70736695a3fb969f6e0c69 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 4 Nov 2022 15:24:07 -0700 Subject: [PATCH] Add tree included ranges getter Co-authored-by: Nathan Sobo --- lib/binding_rust/bindings.rs | 3 +++ lib/binding_rust/lib.rs | 9 +++++++++ lib/include/tree_sitter/api.h | 2 ++ lib/src/tree.c | 7 +++++++ 4 files changed, 21 insertions(+) diff --git a/lib/binding_rust/bindings.rs b/lib/binding_rust/bindings.rs index 0266521d..b4ec9bed 100644 --- a/lib/binding_rust/bindings.rs +++ b/lib/binding_rust/bindings.rs @@ -345,6 +345,9 @@ extern "C" { #[doc = " Get the language that was used to parse the syntax tree."] pub fn ts_tree_language(arg1: *const TSTree) -> *const TSLanguage; } +extern "C" { + pub fn ts_tree_included_ranges(arg1: *const TSTree, length: *mut u32) -> *mut TSRange; +} extern "C" { #[doc = " Edit the syntax tree to keep it in sync with source code that has been"] #[doc = " edited."] diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 934915fe..9a6d3ea2 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -763,6 +763,15 @@ impl Tree { util::CBufferIter::new(ptr, count as usize).map(|r| r.into()) } } + + pub fn included_ranges(&self) -> Vec { + let mut count = 0u32; + unsafe { + let ptr = ffi::ts_tree_included_ranges(self.0.as_ptr(), &mut count as *mut u32); + let ranges = Vec::from_raw_parts(ptr, count as usize, count as usize); + ranges.into_iter().map(|range| range.into()).collect() + } + } } impl fmt::Debug for Tree { diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index 727dded3..5e9ce753 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -381,6 +381,8 @@ TSNode ts_tree_root_node_with_offset( */ const TSLanguage *ts_tree_language(const TSTree *); +TSRange *ts_tree_included_ranges(const TSTree *, uint32_t *length); + /** * Edit the syntax tree to keep it in sync with source code that has been * edited. diff --git a/lib/src/tree.c b/lib/src/tree.c index 103ba84f..6f747dd1 100644 --- a/lib/src/tree.c +++ b/lib/src/tree.c @@ -85,6 +85,13 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit) { ts_subtree_pool_delete(&pool); } +TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length) { + *length = self->included_range_count; + TSRange *ranges = ts_calloc(self->included_range_count, sizeof(TSRange)); + memcpy(ranges, self->included_ranges, self->included_range_count * sizeof(TSRange)); + return ranges; +} + TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count) { TreeCursor cursor1 = {NULL, array_new()}; TreeCursor cursor2 = {NULL, array_new()};