-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathcss.rs
More file actions
92 lines (85 loc) · 2.61 KB
/
Copy pathcss.rs
File metadata and controls
92 lines (85 loc) · 2.61 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
85
86
87
88
89
90
91
92
use super::{Chunk, Tag, common};
use anyhow::Result;
use std::sync::LazyLock;
use tree_sitter_css::HIGHLIGHTS_QUERY;
use tree_sitter_css::LANGUAGE;
use tree_sitter_highlight::HighlightConfiguration;
const NAMES: &[&str] = &[
"tag", // body
"property", // font-size
"variable", // --foo-bar
"function", // calc()
"number", // 42
"string", // "foo"
"comment", // /* comment */
];
const TAGS: &[Tag] = &[
Tag::Name,
Tag::Boolean, // we only have one "Name", so this is a workaround.
Tag::Text,
Tag::Text,
Tag::Number,
Tag::String,
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"p > span { color: red; font-size: 42px; content: \"foo\"; margin: var(--foo) } /* foo */",
);
}
#[test]
fn test_tags_ok() {
common::test_tags_ok(LANGUAGE.into(), HIGHLIGHTS_QUERY, NAMES, TAGS);
}
#[test]
fn test_highlight() {
let input = b"\
p > span { \n\
color: red;\n\
font-size: 42px;\n\
content: \"foo\";\n\
margin: var(--foo);\n\
}\n\
/* foo */\n\
";
let chunks = highlight(input).unwrap();
assert_eq!(
chunks,
vec![
(Tag::Name, "p".to_string()),
(Tag::Text, " > ".to_string()),
(Tag::Name, "span".to_string()),
(Tag::Text, " { \n".to_string()),
(Tag::Boolean, "color".to_string()),
(Tag::Text, ": red;\n".to_string()),
(Tag::Boolean, "font-size".to_string()),
(Tag::Text, ": ".to_string()),
(Tag::Number, "42px".to_string()),
(Tag::Text, ";\n".to_string()),
(Tag::Boolean, "content".to_string()),
(Tag::Text, ": ".to_string()),
(Tag::String, "\"foo\"".to_string()),
(Tag::Text, ";\n".to_string()),
(Tag::Boolean, "margin".to_string()),
(Tag::Text, ": var(--foo);\n}\n".to_string()),
(Tag::Comment, "/* foo */\n".to_string()),
]
);
}
}