-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rs
More file actions
41 lines (36 loc) · 1.16 KB
/
parser.rs
File metadata and controls
41 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use ropey::Rope;
use tree_sitter::{Language, Parser, Point, Tree};
use std::sync::{Arc, Mutex, OnceLock};
fn parser() -> &'static Arc<Mutex<Parser>> {
static HASHMAP: OnceLock<Arc<Mutex<Parser>>> = OnceLock::new();
HASHMAP.get_or_init(|| {
let mut parser = Parser::new();
parser
.set_language(&tree_sitter_earthfile::language())
.expect("Unable to load the earthfile language");
Arc::new(Mutex::new(parser))
})
}
pub fn language() -> Language {
tree_sitter_earthfile::language()
}
pub fn parse(text: impl AsRef<[u8]>, old_tree: Option<&Tree>) -> Tree {
parser().lock().unwrap().parse(text, old_tree).unwrap()
}
pub fn parse_rope(rope: &Rope, old_tree: Option<&Tree>) -> Tree {
parser()
.lock()
.unwrap()
.parse_with(
&mut |byte: usize, _pos: Point| -> &[u8] {
if let Some((text, byte_idx, _, _)) = rope.get_chunk_at_byte(byte) {
let start = byte - byte_idx;
&text.as_bytes()[start..]
} else {
&[]
}
},
old_tree,
)
.unwrap()
}