|
4 | 4 |
|
5 | 5 | use core::cell::RefCell; |
6 | 6 | use js_sys::{Array, Error, Function, JsString, Object, Promise, Reflect, Uint8Array}; |
7 | | -use wasm_bindgen::{prelude::*, JsCast}; |
| 7 | +use wasm_bindgen::{JsCast, prelude::*}; |
8 | 8 | use wasm_bindgen_futures::JsFuture; |
9 | 9 |
|
10 | 10 | trait JsValueExt { |
@@ -41,27 +41,27 @@ thread_local! { |
41 | 41 | pub struct TreeSitter; |
42 | 42 |
|
43 | 43 | impl TreeSitter { |
44 | | - pub async fn init() -> Result<(), JsError> { |
| 44 | + pub async fn init(locate_file: Option<Function>) -> Result<(), JsError> { |
45 | 45 | #![allow(non_snake_case)] |
46 | 46 |
|
47 | 47 | // Exit early if `web-tree-sitter` is already initialized |
48 | 48 | if TREE_SITTER_INITIALIZED.with(|cell| *cell.borrow()) { |
49 | 49 | return Ok(()); |
50 | 50 | } |
51 | 51 |
|
52 | | - JsFuture::from(Parser::init()).await.lift_error()?; |
| 52 | + if let Some(locate_file) = locate_file { |
| 53 | + let options = Object::new(); |
| 54 | + Reflect::set(&options, &"locateFile".into(), &locate_file.into()).unwrap(); |
| 55 | + JsFuture::from(Parser::init(Some(&options))).await.lift_error()?; |
| 56 | + } else { |
| 57 | + JsFuture::from(Parser::init(None)).await.lift_error()?; |
| 58 | + } |
53 | 59 |
|
54 | 60 | // Set `web-tree-sitter` to initialized |
55 | 61 | TREE_SITTER_INITIALIZED.with(|cell| cell.replace(true)); |
56 | 62 |
|
57 | 63 | Ok(()) |
58 | 64 | } |
59 | | - |
60 | | - fn init_guard() { |
61 | | - if !TREE_SITTER_INITIALIZED.with(|cell| *cell.borrow()) { |
62 | | - wasm_bindgen::throw_str("TreeSitter::init must be called to initialize the library"); |
63 | | - } |
64 | | - } |
65 | 65 | } |
66 | 66 |
|
67 | 67 | #[wasm_bindgen] |
@@ -154,7 +154,7 @@ extern { |
154 | 154 | fn delete(this: &LoggerParams, val: &JsString); |
155 | 155 | } |
156 | 156 |
|
157 | | -#[wasm_bindgen(module="web-tree-sitter")] |
| 157 | +#[wasm_bindgen(module = "web-tree-sitter")] |
158 | 158 | extern { |
159 | 159 | #[derive(Clone, Debug, PartialEq)] |
160 | 160 | pub type Language; |
@@ -204,15 +204,13 @@ extern { |
204 | 204 |
|
205 | 205 | impl Language { |
206 | 206 | pub async fn load_bytes(bytes: &Uint8Array) -> Result<Language, LanguageError> { |
207 | | - TreeSitter::init_guard(); |
208 | 207 | JsFuture::from(Language::__load_bytes(bytes)) |
209 | 208 | .await |
210 | 209 | .map(JsCast::unchecked_into) |
211 | 210 | .map_err(JsCast::unchecked_into) |
212 | 211 | } |
213 | 212 |
|
214 | 213 | pub async fn load_path(path: &str) -> Result<Language, LanguageError> { |
215 | | - TreeSitter::init_guard(); |
216 | 214 | JsFuture::from(Language::__load_path(path)) |
217 | 215 | .await |
218 | 216 | .map(JsCast::unchecked_into) |
@@ -292,8 +290,7 @@ impl Default for Point { |
292 | 290 | } |
293 | 291 | } |
294 | 292 |
|
295 | | -impl Eq for Point { |
296 | | -} |
| 293 | +impl Eq for Point {} |
297 | 294 |
|
298 | 295 | impl std::hash::Hash for Point { |
299 | 296 | fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
@@ -538,8 +535,7 @@ impl Default for Range { |
538 | 535 | } |
539 | 536 | } |
540 | 537 |
|
541 | | -impl Eq for Range { |
542 | | -} |
| 538 | +impl Eq for Range {} |
543 | 539 |
|
544 | 540 | impl std::hash::Hash for Range { |
545 | 541 | fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
@@ -743,8 +739,7 @@ impl PartialEq<SyntaxNode> for SyntaxNode { |
743 | 739 | } |
744 | 740 | } |
745 | 741 |
|
746 | | -impl Eq for SyntaxNode { |
747 | | -} |
| 742 | +impl Eq for SyntaxNode {} |
748 | 743 |
|
749 | 744 | impl std::hash::Hash for SyntaxNode { |
750 | 745 | fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
@@ -850,14 +845,14 @@ extern { |
850 | 845 | pub fn reset(this: &TreeCursor, node: &SyntaxNode); |
851 | 846 | } |
852 | 847 |
|
853 | | -#[wasm_bindgen(module="web-tree-sitter")] |
| 848 | +#[wasm_bindgen(module = "web-tree-sitter")] |
854 | 849 | extern { |
855 | 850 | #[derive(Clone, Debug)] |
856 | 851 | pub type Parser; |
857 | 852 |
|
858 | 853 | // Static Methods |
859 | 854 | #[wasm_bindgen(static_method_of = Parser)] |
860 | | - pub fn init() -> Promise; |
| 855 | + pub fn init(options: Option<&Object>) -> Promise; |
861 | 856 |
|
862 | 857 | // Constructor |
863 | 858 |
|
@@ -905,7 +900,6 @@ extern { |
905 | 900 |
|
906 | 901 | impl Parser { |
907 | 902 | pub fn new() -> Result<Parser, ParserError> { |
908 | | - TreeSitter::init_guard(); |
909 | 903 | let result = Parser::__new()?; |
910 | 904 | Ok(result) |
911 | 905 | } |
|
0 commit comments