From ad07fa8a9eda6f0dc375383060666062efe244a4 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Tue, 12 Mar 2024 00:50:34 -0400 Subject: [PATCH] feat(bindings/rust): expose `Parser::included_ranges` --- cli/src/tests/parser_test.rs | 10 ++++++++++ lib/binding_rust/lib.rs | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cli/src/tests/parser_test.rs b/cli/src/tests/parser_test.rs index b57981b6..725a165f 100644 --- a/cli/src/tests/parser_test.rs +++ b/cli/src/tests/parser_test.rs @@ -817,9 +817,19 @@ fn test_parsing_with_one_included_range() { let script_content_node = html_tree.root_node().child(1).unwrap().child(1).unwrap(); assert_eq!(script_content_node.kind(), "raw_text"); + assert_eq!( + parser.included_ranges(), + &[Range { + start_byte: 0, + end_byte: u32::MAX as usize, + start_point: Point::new(0, 0), + end_point: Point::new(u32::MAX as usize, u32::MAX as usize), + }] + ); parser .set_included_ranges(&[script_content_node.range()]) .unwrap(); + assert_eq!(parser.included_ranges(), &[script_content_node.range()]); parser.set_language(&get_language("javascript")).unwrap(); let js_tree = parser.parse(source_code, None).unwrap(); diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 0ec9a8a0..3cc2cc01 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -431,6 +431,7 @@ impl Default for Parser { impl Parser { /// Create a new parser. + #[doc(alias = "ts_parser_new")] #[must_use] pub fn new() -> Self { unsafe { @@ -778,6 +779,24 @@ impl Parser { } } + /// Get the ranges of text that the parser will include when parsing. + #[doc(alias = "ts_parser_included_ranges")] + #[must_use] + pub fn included_ranges(&self) -> Vec { + let mut count = 0u32; + unsafe { + let ptr = + ffi::ts_parser_included_ranges(self.0.as_ptr(), std::ptr::addr_of_mut!(count)); + let ranges = slice::from_raw_parts(ptr, count as usize); + let result = ranges + .iter() + .copied() + .map(std::convert::Into::into) + .collect(); + result + } + } + /// Get the parser's current cancellation flag pointer. /// /// # Safety @@ -893,6 +912,7 @@ impl Tree { } /// Get the included ranges that were used to parse the syntax tree. + #[doc(alias = "ts_tree_included_ranges")] #[must_use] pub fn included_ranges(&self) -> Vec { let mut count = 0u32;