Skip to content

Commit c68fc54

Browse files
committed
feat: locateFile for nodejs
1 parent 570be1b commit c68fc54

File tree

1 file changed

+15
-12
lines changed
  • crates/web-tree-sitter-sg/src

1 file changed

+15
-12
lines changed

crates/web-tree-sitter-sg/src/lib.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use core::cell::RefCell;
66
use js_sys::{Array, Error, Function, JsString, Object, Promise, Reflect, Uint8Array};
7-
use wasm_bindgen::{prelude::*, JsCast};
7+
use wasm_bindgen::{JsCast, prelude::*};
88
use wasm_bindgen_futures::JsFuture;
99

1010
trait JsValueExt {
@@ -41,15 +41,21 @@ thread_local! {
4141
pub struct TreeSitter;
4242

4343
impl TreeSitter {
44-
pub async fn init() -> Result<(), JsError> {
44+
pub async fn init(locate_file: Option<Function>) -> Result<(), JsError> {
4545
#![allow(non_snake_case)]
4646

4747
// Exit early if `web-tree-sitter` is already initialized
4848
if TREE_SITTER_INITIALIZED.with(|cell| *cell.borrow()) {
4949
return Ok(());
5050
}
5151

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+
}
5359

5460
// Set `web-tree-sitter` to initialized
5561
TREE_SITTER_INITIALIZED.with(|cell| cell.replace(true));
@@ -154,7 +160,7 @@ extern {
154160
fn delete(this: &LoggerParams, val: &JsString);
155161
}
156162

157-
#[wasm_bindgen(module="web-tree-sitter")]
163+
#[wasm_bindgen(module = "web-tree-sitter")]
158164
extern {
159165
#[derive(Clone, Debug, PartialEq)]
160166
pub type Language;
@@ -292,8 +298,7 @@ impl Default for Point {
292298
}
293299
}
294300

295-
impl Eq for Point {
296-
}
301+
impl Eq for Point {}
297302

298303
impl std::hash::Hash for Point {
299304
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
@@ -538,8 +543,7 @@ impl Default for Range {
538543
}
539544
}
540545

541-
impl Eq for Range {
542-
}
546+
impl Eq for Range {}
543547

544548
impl std::hash::Hash for Range {
545549
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
@@ -743,8 +747,7 @@ impl PartialEq<SyntaxNode> for SyntaxNode {
743747
}
744748
}
745749

746-
impl Eq for SyntaxNode {
747-
}
750+
impl Eq for SyntaxNode {}
748751

749752
impl std::hash::Hash for SyntaxNode {
750753
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
@@ -850,14 +853,14 @@ extern {
850853
pub fn reset(this: &TreeCursor, node: &SyntaxNode);
851854
}
852855

853-
#[wasm_bindgen(module="web-tree-sitter")]
856+
#[wasm_bindgen(module = "web-tree-sitter")]
854857
extern {
855858
#[derive(Clone, Debug)]
856859
pub type Parser;
857860

858861
// Static Methods
859862
#[wasm_bindgen(static_method_of = Parser)]
860-
pub fn init() -> Promise;
863+
pub fn init(options: Option<&Object>) -> Promise;
861864

862865
// Constructor
863866

0 commit comments

Comments
 (0)