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 ( ) ;
@@ -252,14 +276,17 @@ impl MarkdownParser {
252276 /// If the text of the document has changed since `old_tree` was
253277 /// created, then you must edit `old_tree` to match the new text using
254278 /// [MarkdownTree::edit].
279+ /// * `options` The [options][MarkdownParseOptions] used for parsing.
280+ /// Use `MarkdownParseOptions::default()` if you don't need to pass any options.
255281 ///
256282 /// Returns a [MarkdownTree] if parsing succeeded, or `None` if:
257283 /// * The timeout set with [tree_sitter::Parser::set_timeout_micros] expired
258284 /// * 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 > (
285+ pub fn parse_with_options < T : AsRef < [ u8 ] > , F : FnMut ( usize , Point ) -> T > (
260286 & mut self ,
261287 callback : & mut F ,
262288 old_tree : Option < & MarkdownTree > ,
289+ mut options : MarkdownParseOptions < ' _ > ,
263290 ) -> Option < MarkdownTree > {
264291 let MarkdownParser {
265292 parser,
@@ -275,7 +302,7 @@ impl MarkdownParser {
275302 let block_tree = parser. parse_with_options (
276303 callback,
277304 old_tree. map ( |tree| & tree. block_tree ) ,
278- None , // No progress callback
305+ options . block_options . as_mut ( ) . map ( |b_opt| b_opt . reborrow ( ) ) ,
279306 ) ?;
280307 let ( mut inline_trees, mut inline_indices) = if let Some ( old_tree) = old_tree {
281308 let len = old_tree. inline_trees . len ( ) ;
@@ -329,7 +356,10 @@ impl MarkdownParser {
329356 let inline_tree = parser. parse_with_options (
330357 callback,
331358 old_tree. and_then ( |old_tree| old_tree. inline_trees . get ( i) ) ,
332- None , // No progress callback
359+ options
360+ . inline_options
361+ . as_mut ( )
362+ . map ( |b_opt| b_opt. reborrow ( ) ) ,
333363 ) ?;
334364 inline_trees. push ( inline_tree) ;
335365 inline_indices. insert ( node. id ( ) , i) ;
@@ -358,7 +388,11 @@ impl MarkdownParser {
358388 /// * The timeout set with [tree_sitter::Parser::set_timeout_micros] expired
359389 /// * The cancellation flag set with [tree_sitter::Parser::set_cancellation_flag] was flipped
360390 pub fn parse ( & mut self , text : & [ u8 ] , old_tree : Option < & MarkdownTree > ) -> Option < MarkdownTree > {
361- self . parse_with ( & mut |byte, _| & text[ byte..] , old_tree)
391+ self . parse_with_options (
392+ & mut |byte, _| & text[ byte..] ,
393+ old_tree,
394+ MarkdownParseOptions :: default ( ) ,
395+ )
362396 }
363397}
364398
0 commit comments