Skip to content

Commit 5c60e04

Browse files
committed
fix(rust): parse_with_options
1 parent e2c3d4c commit 5c60e04

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

bindings/rust/parser.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl MarkdownParser {
275275
callback: &mut F,
276276
old_tree: Option<&MarkdownTree>,
277277
block_options: Option<ParseOptions<'_>>,
278-
inline_options: Option<ParseOptions<'_>>,
278+
mut inline_options: Option<ParseOptions<'_>>,
279279
) -> Option<MarkdownTree> {
280280
let MarkdownParser {
281281
parser,
@@ -345,7 +345,7 @@ impl MarkdownParser {
345345
let inline_tree = parser.parse_with_options(
346346
callback,
347347
old_tree.and_then(|old_tree| old_tree.inline_trees.get(i)),
348-
inline_options,
348+
inline_options.as_mut().map(parse_options_borrow_mut),
349349
)?;
350350
inline_trees.push(inline_tree);
351351
inline_indices.insert(node.id(), i);
@@ -374,7 +374,26 @@ impl MarkdownParser {
374374
/// * The timeout set with [tree_sitter::Parser::set_timeout_micros] expired
375375
/// * The cancellation flag set with [tree_sitter::Parser::set_cancellation_flag] was flipped
376376
pub fn parse(&mut self, text: &[u8], old_tree: Option<&MarkdownTree>) -> Option<MarkdownTree> {
377-
self.parse_with_options(&mut |byte, _| &text[byte..], old_tree, None)
377+
self.parse_with_options(&mut |byte, _| &text[byte..], old_tree, None, None)
378+
}
379+
}
380+
381+
/// Creates a new [ParseOptions] that mutably borrows all values while not consuming the original.
382+
fn parse_options_borrow_mut<'a>(options: &'a mut ParseOptions<'_>) -> ParseOptions<'a> {
383+
// This is needed because we need to pass in `ParseOptions` multiple times for the inline
384+
// language. Since the struct contains `&mut`, it cannot implement `Clone` or `Copy`. We get
385+
// around this by manually specifying that we mean to temporarily borrow all fields.
386+
// This is obviously somewhat hacky. If `tree-sitter` adds fields to `ParseOptions` that are not
387+
// `&mut`, this method might become implossible. Given this situation, the "proper" way to solve
388+
// this problem seems to be changing the param type to `&mut ParseOptions` for
389+
// `Parser::parse_with_options()` in upstream `tree-sitter` crate.
390+
let ParseOptions { progress_callback } = options;
391+
ParseOptions {
392+
// Cannot do `progress_callback.as_mut().map(..)` due to lifetime complications.
393+
progress_callback: match progress_callback {
394+
Some(cb) => Some(*cb),
395+
None => None,
396+
},
378397
}
379398
}
380399

0 commit comments

Comments
 (0)