Merge pull request #1759 from rhysd/doc-alias

Add C API names as document aliases in Rust docs
This commit is contained in:
Max Brunsfeld 2022-05-31 14:45:18 -07:00 committed by GitHub
commit efe009f47d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,21 +25,25 @@ use std::{
/// assigned an ABI version number that corresponds to the current CLI version.
/// The Tree-sitter library is generally backwards-compatible with languages
/// generated using older CLI versions, but is not forwards-compatible.
#[doc(alias = "TREE_SITTER_LANGUAGE_VERSION")]
pub const LANGUAGE_VERSION: usize = ffi::TREE_SITTER_LANGUAGE_VERSION;
/// The earliest ABI version that is supported by the current version of the
/// library.
#[doc(alias = "TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION")]
pub const MIN_COMPATIBLE_LANGUAGE_VERSION: usize = ffi::TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION;
pub const PARSER_HEADER: &'static str = include_str!("../include/tree_sitter/parser.h");
/// An opaque object that defines how to parse a particular language. The code for each
/// `Language` is generated by the Tree-sitter CLI.
#[doc(alias = "TSLanguage")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Language(*const ffi::TSLanguage);
/// A tree that represents the syntactic structure of a source code file.
#[doc(alias = "TSTree")]
pub struct Tree(NonNull<ffi::TSTree>);
/// A position in a multi-line text document, in terms of rows and columns.
@ -73,11 +77,13 @@ pub struct InputEdit {
}
/// A single node within a syntax `Tree`.
#[doc(alias = "TSNode")]
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Node<'a>(ffi::TSNode, PhantomData<&'a ()>);
/// A stateful object that this is used to produce a `Tree` based on some source code.
#[doc(alias = "TSParser")]
pub struct Parser(NonNull<ffi::TSParser>);
/// A type of log message.
@ -91,9 +97,11 @@ pub enum LogType {
type Logger<'a> = Box<dyn FnMut(LogType, &str) + 'a>;
/// A stateful object for walking a syntax `Tree` efficiently.
#[doc(alias = "TSTreeCursor")]
pub struct TreeCursor<'a>(ffi::TSTreeCursor, PhantomData<&'a ()>);
/// A set of patterns that match nodes in a syntax tree.
#[doc(alias = "TSQuery")]
#[derive(Debug)]
pub struct Query {
ptr: NonNull<ffi::TSQuery>,
@ -129,6 +137,7 @@ impl From<ffi::TSQuantifier> for CaptureQuantifier {
}
/// A stateful object for executing a `Query` on a syntax `Tree`.
#[doc(alias = "TSQueryCursor")]
pub struct QueryCursor {
ptr: NonNull<ffi::TSQueryCursor>,
}
@ -243,16 +252,19 @@ pub struct LossyUtf8<'a> {
impl Language {
/// Get the ABI version number that indicates which version of the Tree-sitter CLI
/// that was used to generate this `Language`.
#[doc(alias = "ts_language_version")]
pub fn version(&self) -> usize {
unsafe { ffi::ts_language_version(self.0) as usize }
}
/// Get the number of distinct node types in this language.
#[doc(alias = "ts_language_symbol_count")]
pub fn node_kind_count(&self) -> usize {
unsafe { ffi::ts_language_symbol_count(self.0) as usize }
}
/// Get the name of the node kind for the given numerical id.
#[doc(alias = "ts_language_symbol_name")]
pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str> {
let ptr = unsafe { ffi::ts_language_symbol_name(self.0, id) };
if ptr.is_null() {
@ -263,6 +275,7 @@ impl Language {
}
/// Get the numeric id for the given node kind.
#[doc(alias = "ts_language_symbol_for_name")]
pub fn id_for_node_kind(&self, kind: &str, named: bool) -> u16 {
unsafe {
ffi::ts_language_symbol_for_name(
@ -280,6 +293,7 @@ impl Language {
unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolType_TSSymbolTypeRegular }
}
#[doc(alias = "ts_language_symbol_type")]
pub fn node_kind_is_visible(&self, id: u16) -> bool {
unsafe {
ffi::ts_language_symbol_type(self.0, id) <= ffi::TSSymbolType_TSSymbolTypeAnonymous
@ -287,11 +301,13 @@ impl Language {
}
/// Get the number of distinct field names in this language.
#[doc(alias = "ts_language_field_count")]
pub fn field_count(&self) -> usize {
unsafe { ffi::ts_language_field_count(self.0) as usize }
}
/// Get the field names for the given numerical id.
#[doc(alias = "ts_language_field_name_for_id")]
pub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str> {
let ptr = unsafe { ffi::ts_language_field_name_for_id(self.0, field_id) };
if ptr.is_null() {
@ -302,6 +318,7 @@ impl Language {
}
/// Get the numerical id for the given field name.
#[doc(alias = "ts_language_field_id_for_name")]
pub fn field_id_for_name(&self, field_name: impl AsRef<[u8]>) -> Option<u16> {
let field_name = field_name.as_ref();
let id = unsafe {
@ -336,6 +353,7 @@ impl Parser {
/// Tree-sitter CLI. Check the language's version using [Language::version]
/// and compare it to this library's [LANGUAGE_VERSION](LANGUAGE_VERSION) and
/// [MIN_COMPATIBLE_LANGUAGE_VERSION](MIN_COMPATIBLE_LANGUAGE_VERSION) constants.
#[doc(alias = "ts_parser_set_language")]
pub fn set_language(&mut self, language: Language) -> Result<(), LanguageError> {
let version = language.version();
if version < MIN_COMPATIBLE_LANGUAGE_VERSION || version > LANGUAGE_VERSION {
@ -349,6 +367,7 @@ impl Parser {
}
/// Get the parser's current language.
#[doc(alias = "ts_parser_language")]
pub fn language(&self) -> Option<Language> {
let ptr = unsafe { ffi::ts_parser_language(self.0.as_ptr()) };
if ptr.is_null() {
@ -359,12 +378,14 @@ impl Parser {
}
/// Get the parser's current logger.
#[doc(alias = "ts_parser_logger")]
pub fn logger(&self) -> Option<&Logger> {
let logger = unsafe { ffi::ts_parser_logger(self.0.as_ptr()) };
unsafe { (logger.payload as *mut Logger).as_ref() }
}
/// Set the logging callback that a parser should use during parsing.
#[doc(alias = "ts_parser_set_logger")]
pub fn set_logger(&mut self, logger: Option<Logger>) {
let prev_logger = unsafe { ffi::ts_parser_logger(self.0.as_ptr()) };
if !prev_logger.payload.is_null() {
@ -412,12 +433,14 @@ impl Parser {
/// to pipe these graphs directly to a `dot(1)` process in order to generate
/// SVG output.
#[cfg(unix)]
#[doc(alias = "ts_parser_print_dot_graphs")]
pub fn print_dot_graphs(&mut self, file: &impl AsRawFd) {
let fd = file.as_raw_fd();
unsafe { ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), ffi::dup(fd)) }
}
/// Stop the parser from printing debugging graphs while parsing.
#[doc(alias = "ts_parser_print_dot_graphs")]
pub fn stop_printing_dot_graphs(&mut self) {
unsafe { ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), -1) }
}
@ -435,6 +458,7 @@ impl Parser {
/// * The parser has not yet had a language assigned with [Parser::set_language]
/// * The timeout set with [Parser::set_timeout_micros] expired
/// * The cancellation flag set with [Parser::set_cancellation_flag] was flipped
#[doc(alias = "ts_parser_parse")]
pub fn parse(&mut self, text: impl AsRef<[u8]>, old_tree: Option<&Tree>) -> Option<Tree> {
let bytes = text.as_ref();
let len = bytes.len();
@ -577,6 +601,7 @@ impl Parser {
/// by default, it will resume where it left off on the next call to `parse` or
/// other parsing functions. If you don't want to resume, and instead intend to
/// use this parser to parse some other document, you must call `reset` first.
#[doc(alias = "ts_parser_reset")]
pub fn reset(&mut self) {
unsafe { ffi::ts_parser_reset(self.0.as_ptr()) }
}
@ -584,6 +609,7 @@ impl Parser {
/// Get the duration in microseconds that parsing is allowed to take.
///
/// This is set via [set_timeout_micros](Parser::set_timeout_micros).
#[doc(alias = "ts_parser_timeout_micros")]
pub fn timeout_micros(&self) -> u64 {
unsafe { ffi::ts_parser_timeout_micros(self.0.as_ptr()) }
}
@ -593,6 +619,7 @@ impl Parser {
///
/// If parsing takes longer than this, it will halt early, returning `None`.
/// See `parse` for more information.
#[doc(alias = "ts_parser_set_timeout_micros")]
pub fn set_timeout_micros(&mut self, timeout_micros: u64) {
unsafe { ffi::ts_parser_set_timeout_micros(self.0.as_ptr(), timeout_micros) }
}
@ -613,6 +640,7 @@ impl Parser {
/// ```
/// If this requirement is not satisfied, method will return IncludedRangesError
/// error with an offset in the passed ranges slice pointing to a first incorrect range.
#[doc(alias = "ts_parser_set_included_ranges")]
pub fn set_included_ranges<'a>(
&mut self,
ranges: &'a [Range],
@ -642,6 +670,7 @@ impl Parser {
}
/// Get the parser's current cancellation flag pointer.
#[doc(alias = "ts_parser_cancellation_flag")]
pub unsafe fn cancellation_flag(&self) -> Option<&AtomicUsize> {
(ffi::ts_parser_cancellation_flag(self.0.as_ptr()) as *const AtomicUsize).as_ref()
}
@ -651,6 +680,7 @@ impl Parser {
/// If a pointer is assigned, then the parser will periodically read from
/// this pointer during parsing. If it reads a non-zero value, it will halt early,
/// returning `None`. See [parse](Parser::parse) for more information.
#[doc(alias = "ts_parser_set_cancellation_flag")]
pub unsafe fn set_cancellation_flag(&mut self, flag: Option<&AtomicUsize>) {
if let Some(flag) = flag {
ffi::ts_parser_set_cancellation_flag(
@ -673,11 +703,13 @@ impl Drop for Parser {
impl Tree {
/// Get the root node of the syntax tree.
#[doc(alias = "ts_tree_root_node")]
pub fn root_node(&self) -> Node {
Node::new(unsafe { ffi::ts_tree_root_node(self.0.as_ptr()) }).unwrap()
}
/// Get the language that was used to parse the syntax tree.
#[doc(alias = "ts_tree_language")]
pub fn language(&self) -> Language {
Language(unsafe { ffi::ts_tree_language(self.0.as_ptr()) })
}
@ -687,6 +719,7 @@ impl Tree {
///
/// You must describe the edit both in terms of byte offsets and in terms of
/// row/column coordinates.
#[doc(alias = "ts_tree_edit")]
pub fn edit(&mut self, edit: &InputEdit) {
let edit = edit.into();
unsafe { ffi::ts_tree_edit(self.0.as_ptr(), &edit) };
@ -704,6 +737,7 @@ impl Tree {
/// ranges match up to the new tree. Generally, you'll want to call this method right
/// after calling one of the [Parser::parse] functions. Call it on the old tree that
/// was passed to parse, and pass the new tree that was returned from `parse`.
#[doc(alias = "ts_tree_get_changed_ranges")]
pub fn changed_ranges(&self, other: &Tree) -> impl ExactSizeIterator<Item = Range> {
let mut count = 0u32;
unsafe {
@ -755,11 +789,13 @@ impl<'tree> Node<'tree> {
}
/// Get this node's type as a numerical id.
#[doc(alias = "ts_node_symbol")]
pub fn kind_id(&self) -> u16 {
unsafe { ffi::ts_node_symbol(self.0) }
}
/// Get this node's type as a string.
#[doc(alias = "ts_node_type")]
pub fn kind(&self) -> &'static str {
unsafe { CStr::from_ptr(ffi::ts_node_type(self.0)) }
.to_str()
@ -767,6 +803,7 @@ impl<'tree> Node<'tree> {
}
/// Get the [Language] that was used to parse this node's syntax tree.
#[doc(alias = "ts_tree_language")]
pub fn language(&self) -> Language {
Language(unsafe { ffi::ts_tree_language(self.0.tree) })
}
@ -775,6 +812,7 @@ impl<'tree> Node<'tree> {
///
/// Named nodes correspond to named rules in the grammar, whereas *anonymous* nodes
/// correspond to string literals in the grammar.
#[doc(alias = "ts_node_is_named")]
pub fn is_named(&self) -> bool {
unsafe { ffi::ts_node_is_named(self.0) }
}
@ -783,17 +821,20 @@ impl<'tree> Node<'tree> {
///
/// Extra nodes represent things like comments, which are not required the grammar,
/// but can appear anywhere.
#[doc(alias = "ts_node_is_extra")]
pub fn is_extra(&self) -> bool {
unsafe { ffi::ts_node_is_extra(self.0) }
}
/// Check if this node has been edited.
#[doc(alias = "ts_node_has_changes")]
pub fn has_changes(&self) -> bool {
unsafe { ffi::ts_node_has_changes(self.0) }
}
/// Check if this node represents a syntax error or contains any syntax errors anywhere
/// within it.
#[doc(alias = "ts_node_has_error")]
pub fn has_error(&self) -> bool {
unsafe { ffi::ts_node_has_error(self.0) }
}
@ -810,16 +851,19 @@ impl<'tree> Node<'tree> {
///
/// Missing nodes are inserted by the parser in order to recover from certain kinds of
/// syntax errors.
#[doc(alias = "ts_node_is_missing")]
pub fn is_missing(&self) -> bool {
unsafe { ffi::ts_node_is_missing(self.0) }
}
/// Get the byte offsets where this node starts.
#[doc(alias = "ts_node_start_byte")]
pub fn start_byte(&self) -> usize {
unsafe { ffi::ts_node_start_byte(self.0) as usize }
}
/// Get the byte offsets where this node end.
#[doc(alias = "ts_node_end_byte")]
pub fn end_byte(&self) -> usize {
unsafe { ffi::ts_node_end_byte(self.0) as usize }
}
@ -841,12 +885,14 @@ impl<'tree> Node<'tree> {
}
/// Get this node's start position in terms of rows and columns.
#[doc(alias = "ts_node_start_point")]
pub fn start_position(&self) -> Point {
let result = unsafe { ffi::ts_node_start_point(self.0) };
result.into()
}
/// Get this node's end position in terms of rows and columns.
#[doc(alias = "ts_node_end_point")]
pub fn end_position(&self) -> Point {
let result = unsafe { ffi::ts_node_end_point(self.0) };
result.into()
@ -858,11 +904,13 @@ impl<'tree> Node<'tree> {
/// This method is fairly fast, but its cost is technically log(i), so you
/// if you might be iterating over a long list of children, you should use
/// [Node::children] instead.
#[doc(alias = "ts_node_child")]
pub fn child(&self, i: usize) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_child(self.0, i as u32) })
}
/// Get this node's number of children.
#[doc(alias = "ts_node_child_count")]
pub fn child_count(&self) -> usize {
unsafe { ffi::ts_node_child_count(self.0) as usize }
}
@ -873,6 +921,7 @@ impl<'tree> Node<'tree> {
/// This method is fairly fast, but its cost is technically log(i), so you
/// if you might be iterating over a long list of children, you should use
/// [Node::named_children] instead.
#[doc(alias = "ts_node_named_child")]
pub fn named_child<'a>(&'a self, i: usize) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_named_child(self.0, i as u32) })
}
@ -880,6 +929,7 @@ impl<'tree> Node<'tree> {
/// Get this node's number of *named* children.
///
/// See also [Node::is_named].
#[doc(alias = "ts_node_named_child_count")]
pub fn named_child_count(&self) -> usize {
unsafe { ffi::ts_node_named_child_count(self.0) as usize }
}
@ -888,6 +938,7 @@ impl<'tree> Node<'tree> {
///
/// If multiple children may have the same field name, access them using
/// [children_by_field_name](Node::children_by_field_name)
#[doc(alias = "ts_node_child_by_field_name")]
pub fn child_by_field_name(&self, field_name: impl AsRef<[u8]>) -> Option<Self> {
let field_name = field_name.as_ref();
Self::new(unsafe {
@ -903,11 +954,13 @@ impl<'tree> Node<'tree> {
///
/// See also [child_by_field_name](Node::child_by_field_name). You can convert a field name to
/// an id using [Language::field_id_for_name].
#[doc(alias = "ts_node_child_by_field_id")]
pub fn child_by_field_id(&self, field_id: u16) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_child_by_field_id(self.0, field_id) })
}
/// Get the field name of this node's child at the given index.
#[doc(alias = "ts_node_field_name_for_child")]
pub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str> {
unsafe {
let ptr = ffi::ts_node_field_name_for_child(self.0, child_index);
@ -1003,31 +1056,37 @@ impl<'tree> Node<'tree> {
}
/// Get this node's immediate parent.
#[doc(alias = "ts_node_parent")]
pub fn parent(&self) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_parent(self.0) })
}
/// Get this node's next sibling.
#[doc(alias = "ts_node_next_sibling")]
pub fn next_sibling(&self) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_next_sibling(self.0) })
}
/// Get this node's previous sibling.
#[doc(alias = "ts_node_prev_sibling")]
pub fn prev_sibling(&self) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_prev_sibling(self.0) })
}
/// Get this node's next named sibling.
#[doc(alias = "ts_node_next_named_sibling")]
pub fn next_named_sibling(&self) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_next_named_sibling(self.0) })
}
/// Get this node's previous named sibling.
#[doc(alias = "ts_node_prev_named_sibling")]
pub fn prev_named_sibling(&self) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_prev_named_sibling(self.0) })
}
/// Get the smallest node within this node that spans the given range.
#[doc(alias = "ts_node_descendant_for_byte_range")]
pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Self> {
Self::new(unsafe {
ffi::ts_node_descendant_for_byte_range(self.0, start as u32, end as u32)
@ -1035,6 +1094,7 @@ impl<'tree> Node<'tree> {
}
/// Get the smallest named node within this node that spans the given range.
#[doc(alias = "ts_node_named_descendant_for_byte_range")]
pub fn named_descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Self> {
Self::new(unsafe {
ffi::ts_node_named_descendant_for_byte_range(self.0, start as u32, end as u32)
@ -1042,6 +1102,7 @@ impl<'tree> Node<'tree> {
}
/// Get the smallest node within this node that spans the given range.
#[doc(alias = "ts_node_descendant_for_point_range")]
pub fn descendant_for_point_range(&self, start: Point, end: Point) -> Option<Self> {
Self::new(unsafe {
ffi::ts_node_descendant_for_point_range(self.0, start.into(), end.into())
@ -1049,12 +1110,14 @@ impl<'tree> Node<'tree> {
}
/// Get the smallest named node within this node that spans the given range.
#[doc(alias = "ts_node_named_descendant_for_point_range")]
pub fn named_descendant_for_point_range(&self, start: Point, end: Point) -> Option<Self> {
Self::new(unsafe {
ffi::ts_node_named_descendant_for_point_range(self.0, start.into(), end.into())
})
}
#[doc(alias = "ts_node_string")]
pub fn to_sexp(&self) -> String {
let c_string = unsafe { ffi::ts_node_string(self.0) };
let result = unsafe { CStr::from_ptr(c_string) }
@ -1074,6 +1137,7 @@ impl<'tree> Node<'tree> {
}
/// Create a new [TreeCursor] starting from this node.
#[doc(alias = "ts_tree_cursor_new")]
pub fn walk(&self) -> TreeCursor<'tree> {
TreeCursor(unsafe { ffi::ts_tree_cursor_new(self.0) }, PhantomData)
}
@ -1085,6 +1149,7 @@ impl<'tree> Node<'tree> {
/// afterward will already reflect the edit. You only need to use [Node::edit]
/// when you have a specific [Node] instance that you want to keep and continue
/// to use after an edit.
#[doc(alias = "ts_node_edit")]
pub fn edit(&mut self, edit: &InputEdit) {
let edit = edit.into();
unsafe { ffi::ts_node_edit(&mut self.0 as *mut ffi::TSNode, &edit) }
@ -1123,6 +1188,7 @@ impl<'a> fmt::Debug for Node<'a> {
impl<'a> TreeCursor<'a> {
/// Get the tree cursor's current [Node].
#[doc(alias = "ts_tree_cursor_current_node")]
pub fn node(&self) -> Node<'a> {
Node(
unsafe { ffi::ts_tree_cursor_current_node(&self.0) },
@ -1133,6 +1199,7 @@ impl<'a> TreeCursor<'a> {
/// Get the numerical field id of this tree cursor's current node.
///
/// See also [field_name](TreeCursor::field_name).
#[doc(alias = "ts_tree_cursor_current_field_id")]
pub fn field_id(&self) -> Option<u16> {
unsafe {
let id = ffi::ts_tree_cursor_current_field_id(&self.0);
@ -1145,6 +1212,7 @@ impl<'a> TreeCursor<'a> {
}
/// Get the field name of this tree cursor's current node.
#[doc(alias = "ts_tree_cursor_current_field_name")]
pub fn field_name(&self) -> Option<&'static str> {
unsafe {
let ptr = ffi::ts_tree_cursor_current_field_name(&self.0);
@ -1160,6 +1228,7 @@ impl<'a> TreeCursor<'a> {
///
/// This returns `true` if the cursor successfully moved, and returns `false`
/// if there were no children.
#[doc(alias = "ts_tree_cursor_goto_first_child")]
pub fn goto_first_child(&mut self) -> bool {
return unsafe { ffi::ts_tree_cursor_goto_first_child(&mut self.0) };
}
@ -1168,6 +1237,7 @@ impl<'a> TreeCursor<'a> {
///
/// This returns `true` if the cursor successfully moved, and returns `false`
/// if there was no parent node (the cursor was already on the root node).
#[doc(alias = "ts_tree_cursor_goto_parent")]
pub fn goto_parent(&mut self) -> bool {
return unsafe { ffi::ts_tree_cursor_goto_parent(&mut self.0) };
}
@ -1176,6 +1246,7 @@ impl<'a> TreeCursor<'a> {
///
/// This returns `true` if the cursor successfully moved, and returns `false`
/// if there was no next sibling node.
#[doc(alias = "ts_tree_cursor_goto_next_sibling")]
pub fn goto_next_sibling(&mut self) -> bool {
return unsafe { ffi::ts_tree_cursor_goto_next_sibling(&mut self.0) };
}
@ -1185,6 +1256,7 @@ impl<'a> TreeCursor<'a> {
///
/// This returns the index of the child node if one was found, and returns `None`
/// if no such child was found.
#[doc(alias = "ts_tree_cursor_goto_first_child_for_byte")]
pub fn goto_first_child_for_byte(&mut self, index: usize) -> Option<usize> {
let result =
unsafe { ffi::ts_tree_cursor_goto_first_child_for_byte(&mut self.0, index as u32) };
@ -1200,6 +1272,7 @@ impl<'a> TreeCursor<'a> {
///
/// This returns the index of the child node if one was found, and returns `None`
/// if no such child was found.
#[doc(alias = "ts_tree_cursor_goto_first_child_for_point")]
pub fn goto_first_child_for_point(&mut self, point: Point) -> Option<usize> {
let result =
unsafe { ffi::ts_tree_cursor_goto_first_child_for_point(&mut self.0, point.into()) };
@ -1211,6 +1284,7 @@ impl<'a> TreeCursor<'a> {
}
/// Re-initialize this tree cursor to start at a different node.
#[doc(alias = "ts_tree_cursor_reset")]
pub fn reset(&mut self, node: Node<'a>) {
unsafe { ffi::ts_tree_cursor_reset(&mut self.0, node.0) };
}
@ -1538,6 +1612,7 @@ impl Query {
}
/// Get the byte offset where the given pattern starts in the query's source.
#[doc(alias = "ts_query_start_byte_for_pattern")]
pub fn start_byte_for_pattern(&self, pattern_index: usize) -> usize {
if pattern_index >= self.text_predicates.len() {
panic!(
@ -1552,6 +1627,7 @@ impl Query {
}
/// Get the number of patterns in the query.
#[doc(alias = "ts_query_pattern_count")]
pub fn pattern_count(&self) -> usize {
unsafe { ffi::ts_query_pattern_count(self.ptr.as_ptr()) as usize }
}
@ -1603,6 +1679,7 @@ impl Query {
///
/// This prevents the capture from being returned in matches, and also avoids any
/// resource usage associated with recording the capture.
#[doc(alias = "ts_query_disable_capture")]
pub fn disable_capture(&mut self, name: &str) {
unsafe {
ffi::ts_query_disable_capture(
@ -1617,6 +1694,7 @@ impl Query {
///
/// This prevents the pattern from matching, and also avoids any resource usage
/// associated with the pattern.
#[doc(alias = "ts_query_disable_pattern")]
pub fn disable_pattern(&mut self, index: usize) {
unsafe { ffi::ts_query_disable_pattern(self.ptr.as_ptr(), index as u32) }
}
@ -1625,6 +1703,7 @@ impl Query {
///
/// A query step is 'definite' if its parent pattern will be guaranteed to match
/// successfully once it reaches the step.
#[doc(alias = "ts_query_is_pattern_guaranteed_at_step")]
pub fn is_pattern_guaranteed_at_step(&self, byte_offset: usize) -> bool {
unsafe {
ffi::ts_query_is_pattern_guaranteed_at_step(self.ptr.as_ptr(), byte_offset as u32)
@ -1698,6 +1777,7 @@ impl QueryCursor {
/// Create a new cursor for executing a given query.
///
/// The cursor stores the state that is needed to iteratively search for matches.
#[doc(alias = "ts_query_cursor_new")]
pub fn new() -> Self {
QueryCursor {
ptr: unsafe { NonNull::new_unchecked(ffi::ts_query_cursor_new()) },
@ -1705,12 +1785,14 @@ impl QueryCursor {
}
/// Return the maximum number of in-progress matches for this cursor.
#[doc(alias = "ts_query_cursor_match_limit")]
pub fn match_limit(&self) -> u32 {
unsafe { ffi::ts_query_cursor_match_limit(self.ptr.as_ptr()) }
}
/// Set the maximum number of in-progress matches for this cursor. The limit must be > 0 and
/// <= 65536.
#[doc(alias = "ts_query_cursor_set_match_limit")]
pub fn set_match_limit(&mut self, limit: u32) {
unsafe {
ffi::ts_query_cursor_set_match_limit(self.ptr.as_ptr(), limit);
@ -1719,6 +1801,7 @@ impl QueryCursor {
/// Check if, on its last execution, this cursor exceeded its maximum number of
/// in-progress matches.
#[doc(alias = "ts_query_cursor_did_exceed_match_limit")]
pub fn did_exceed_match_limit(&self) -> bool {
unsafe { ffi::ts_query_cursor_did_exceed_match_limit(self.ptr.as_ptr()) }
}
@ -1728,6 +1811,7 @@ impl QueryCursor {
/// Each match contains the index of the pattern that matched, and a list of captures.
/// Because multiple patterns can match the same set of nodes, one match may contain
/// captures that appear *before* some of the captures from a previous match.
#[doc(alias = "ts_query_cursor_exec")]
pub fn matches<'a, 'tree: 'a, T: TextProvider<'a> + 'a>(
&'a mut self,
query: &'a Query,
@ -1750,6 +1834,7 @@ impl QueryCursor {
///
/// This is useful if you don't care about which pattern matched, and just want a single,
/// ordered sequence of captures.
#[doc(alias = "ts_query_cursor_exec")]
pub fn captures<'a, 'tree: 'a, T: TextProvider<'a> + 'a>(
&'a mut self,
query: &'a Query,
@ -1769,6 +1854,7 @@ impl QueryCursor {
}
/// Set the range in which the query will be executed, in terms of byte offsets.
#[doc(alias = "ts_query_cursor_set_byte_range")]
pub fn set_byte_range(&mut self, range: ops::Range<usize>) -> &mut Self {
unsafe {
ffi::ts_query_cursor_set_byte_range(
@ -1781,6 +1867,7 @@ impl QueryCursor {
}
/// Set the range in which the query will be executed, in terms of rows and columns.
#[doc(alias = "ts_query_cursor_set_point_range")]
pub fn set_point_range(&mut self, range: ops::Range<Point>) -> &mut Self {
unsafe {
ffi::ts_query_cursor_set_point_range(
@ -1798,6 +1885,7 @@ impl<'a, 'tree> QueryMatch<'a, 'tree> {
self.id
}
#[doc(alias = "ts_query_cursor_remove_match")]
pub fn remove(self) {
unsafe { ffi::ts_query_cursor_remove_match(self.cursor, self.id) }
}
@ -1965,12 +2053,14 @@ impl<'a, 'tree, T: TextProvider<'a>> Iterator for QueryCaptures<'a, 'tree, T> {
}
impl<'a, 'tree, T: TextProvider<'a>> QueryMatches<'a, 'tree, T> {
#[doc(alias = "ts_query_cursor_set_byte_range")]
pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
unsafe {
ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
}
}
#[doc(alias = "ts_query_cursor_set_point_range")]
pub fn set_point_range(&mut self, range: ops::Range<Point>) {
unsafe {
ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
@ -1979,12 +2069,14 @@ impl<'a, 'tree, T: TextProvider<'a>> QueryMatches<'a, 'tree, T> {
}
impl<'a, 'tree, T: TextProvider<'a>> QueryCaptures<'a, 'tree, T> {
#[doc(alias = "ts_query_cursor_set_byte_range")]
pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
unsafe {
ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
}
}
#[doc(alias = "ts_query_cursor_set_point_range")]
pub fn set_point_range(&mut self, range: ops::Range<Point>) {
unsafe {
ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
@ -2209,6 +2301,7 @@ extern "C" {
static mut FREE_FN: unsafe extern "C" fn(ptr: *mut c_void) = free;
#[doc(alias = "ts_set_allocator")]
pub unsafe fn set_allocator(
new_malloc: Option<unsafe extern "C" fn(usize) -> *mut c_void>,
new_calloc: Option<unsafe extern "C" fn(usize, usize) -> *mut c_void>,