-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathjavascript.rs
More file actions
84 lines (77 loc) · 2.25 KB
/
javascript.rs
File metadata and controls
84 lines (77 loc) · 2.25 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use super::{Chunk, Tag, common};
use anyhow::Result;
use std::sync::LazyLock;
use tree_sitter_highlight::HighlightConfiguration;
use tree_sitter_javascript::HIGHLIGHT_QUERY as HIGHLIGHTS_QUERY;
use tree_sitter_javascript::LANGUAGE;
const NAMES: &[&str] = &[
"keyword", // let
"function", // *function* () {
"variable", // let *foo* = ...
"property", // foo.*bar* = ...
"constant", // *true*
"string", // "string"
"number", // 42
"comment", // /* comments */
];
const TAGS: &[Tag] = &[
Tag::Name,
Tag::Text,
Tag::Text,
Tag::Text,
Tag::Boolean,
Tag::String,
Tag::Number,
Tag::Comment,
];
static CONFIG: LazyLock<HighlightConfiguration> = LazyLock::new(|| {
let mut config = HighlightConfiguration::new(LANGUAGE.into(), "", HIGHLIGHTS_QUERY, "", "")
.expect("failed to build syntax highlighter");
config.configure(NAMES);
config
});
pub fn highlight(input: &[u8]) -> Result<Vec<Chunk>> {
common::highlight(&CONFIG, TAGS, input)
}
#[cfg(test)]
mod tests {
use super::*;
#[ignore]
#[test]
fn debug() {
common::debug(
LANGUAGE.into(),
HIGHLIGHTS_QUERY,
b"function foo() { let bar = true && 42 && 'qux'; foo.bar = 42; } // comment",
);
}
#[test]
fn test_tags_ok() {
common::test_tags_ok(LANGUAGE.into(), HIGHLIGHTS_QUERY, NAMES, TAGS);
}
#[test]
fn test_highlight() {
let input = b"\
function foo() {\n\
let bar = true && 42 && 'qux';\n\
} // comment\n\
";
let chunks = highlight(input).unwrap();
assert_eq!(
chunks,
vec![
(Tag::Name, "function ".to_string()),
(Tag::Text, "foo() {\n".to_string()),
(Tag::Name, "let ".to_string()),
(Tag::Text, "bar = ".to_string()),
(Tag::Boolean, "true".to_string()),
(Tag::Text, " && ".to_string()),
(Tag::Number, "42".to_string()),
(Tag::Text, " && ".to_string()),
(Tag::String, "'qux'".to_string()),
(Tag::Text, ";\n} ".to_string()),
(Tag::Comment, "// comment\n".to_string()),
]
);
}
}