11use std:: collections:: HashMap ;
22use std:: num:: NonZeroU16 ;
33
4- use tree_sitter:: { InputEdit , Language , Node , Parser , Point , Range , Tree , TreeCursor } ;
4+ use tree_sitter:: {
5+ InputEdit , Language , Node , ParseOptions , Parser , Point , Range , Tree , TreeCursor ,
6+ } ;
57
68use crate :: { INLINE_LANGUAGE , LANGUAGE } ;
79
@@ -16,7 +18,7 @@ pub struct MarkdownParser {
1618
1719/// A stateful object for walking a [`MarkdownTree`] efficiently.
1820///
19- /// This exposes the same methdos as [`TreeCursor`], but abstracts away the
21+ /// This exposes the same methods as [`TreeCursor`], but abstracts away the
2022/// double block / inline structure of [`MarkdownTree`].
2123pub struct MarkdownCursor < ' a > {
2224 markdown_tree : & ' a MarkdownTree ,
@@ -33,7 +35,7 @@ impl<'a> MarkdownCursor<'a> {
3335 }
3436 }
3537
36- /// Returns `true` if the current node is from the (inline language)[INLINE_LANGUAGE]
38+ /// Returns `true` if the current node is from the [INLINE_LANGUAGE] (inline language)
3739 ///
3840 /// This information is needed to handle "tree-sitter internal" data like
3941 /// [`field_id`](Self::field_id) correctly.
@@ -230,6 +232,28 @@ impl MarkdownTree {
230232 }
231233}
232234
235+ /// The options used while parsing a [`MarkdownTree`].
236+ ///
237+ /// This abstracts away the double block / inline structure of [`MarkdownParser`].
238+ #[ derive( Default ) ]
239+ pub struct MarkdownParseOptions < ' a > {
240+ block_options : Option < ParseOptions < ' a > > ,
241+ inline_options : Option < ParseOptions < ' a > > ,
242+ }
243+
244+ impl < ' a > MarkdownParseOptions < ' a > {
245+ /// Creates a new [MarkdownParseOptions] instance.
246+ pub fn new (
247+ block_options : Option < ParseOptions < ' a > > ,
248+ inline_options : Option < ParseOptions < ' a > > ,
249+ ) -> Self {
250+ MarkdownParseOptions {
251+ block_options,
252+ inline_options,
253+ }
254+ }
255+ }
256+
233257impl Default for MarkdownParser {
234258 fn default ( ) -> Self {
235259 let block_language = LANGUAGE . into ( ) ;
@@ -244,6 +268,16 @@ impl Default for MarkdownParser {
244268}
245269
246270impl MarkdownParser {
271+ /// Parse a slice of UTF8 text.
272+ #[ deprecated( since = "0.5.0" , note = "Prefer `parse_with_options` instead" ) ]
273+ pub fn parse_with < T : AsRef < [ u8 ] > , F : FnMut ( usize , Point ) -> T > (
274+ & mut self ,
275+ callback : & mut F ,
276+ old_tree : Option < & MarkdownTree > ,
277+ ) -> Option < MarkdownTree > {
278+ self . parse_with_options ( callback, old_tree, MarkdownParseOptions :: default ( ) )
279+ }
280+
247281 /// Parse a slice of UTF8 text.
248282 ///
249283 /// # Arguments:
@@ -252,14 +286,17 @@ impl MarkdownParser {
252286 /// If the text of the document has changed since `old_tree` was
253287 /// created, then you must edit `old_tree` to match the new text using
254288 /// [MarkdownTree::edit].
289+ /// * `options` The [options][MarkdownParseOptions] used for parsing.
290+ /// Use `MarkdownParseOptions::default()` if you don't need to pass any options.
255291 ///
256292 /// Returns a [MarkdownTree] if parsing succeeded, or `None` if:
257293 /// * The timeout set with [tree_sitter::Parser::set_timeout_micros] expired
258294 /// * The cancellation flag set with [tree_sitter::Parser::set_cancellation_flag] was flipped
259- pub fn parse_with < T : AsRef < [ u8 ] > , F : FnMut ( usize , Point ) -> T > (
295+ pub fn parse_with_options < T : AsRef < [ u8 ] > , F : FnMut ( usize , Point ) -> T > (
260296 & mut self ,
261297 callback : & mut F ,
262298 old_tree : Option < & MarkdownTree > ,
299+ mut options : MarkdownParseOptions < ' _ > ,
263300 ) -> Option < MarkdownTree > {
264301 let MarkdownParser {
265302 parser,
@@ -275,7 +312,7 @@ impl MarkdownParser {
275312 let block_tree = parser. parse_with_options (
276313 callback,
277314 old_tree. map ( |tree| & tree. block_tree ) ,
278- None , // No progress callback
315+ options . block_options . as_mut ( ) . map ( |b_opt| b_opt . reborrow ( ) ) ,
279316 ) ?;
280317 let ( mut inline_trees, mut inline_indices) = if let Some ( old_tree) = old_tree {
281318 let len = old_tree. inline_trees . len ( ) ;
@@ -329,7 +366,10 @@ impl MarkdownParser {
329366 let inline_tree = parser. parse_with_options (
330367 callback,
331368 old_tree. and_then ( |old_tree| old_tree. inline_trees . get ( i) ) ,
332- None , // No progress callback
369+ options
370+ . inline_options
371+ . as_mut ( )
372+ . map ( |b_opt| b_opt. reborrow ( ) ) ,
333373 ) ?;
334374 inline_trees. push ( inline_tree) ;
335375 inline_indices. insert ( node. id ( ) , i) ;
@@ -358,7 +398,11 @@ impl MarkdownParser {
358398 /// * The timeout set with [tree_sitter::Parser::set_timeout_micros] expired
359399 /// * The cancellation flag set with [tree_sitter::Parser::set_cancellation_flag] was flipped
360400 pub fn parse ( & mut self , text : & [ u8 ] , old_tree : Option < & MarkdownTree > ) -> Option < MarkdownTree > {
361- self . parse_with ( & mut |byte, _| & text[ byte..] , old_tree)
401+ self . parse_with_options (
402+ & mut |byte, _| & text[ byte..] ,
403+ old_tree,
404+ MarkdownParseOptions :: default ( ) ,
405+ )
362406 }
363407}
364408
0 commit comments