feat(bindings/rust): expose Parser::included_ranges

This commit is contained in:
Amaan Qureshi 2024-03-12 00:50:34 -04:00
parent d4862ff9f7
commit ad07fa8a9e
2 changed files with 30 additions and 0 deletions

View file

@ -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();

View file

@ -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<Range> {
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<Range> {
let mut count = 0u32;