|
| 1 | +use super::{common, Chunk, Tag}; |
| 2 | +use anyhow::Result; |
| 3 | +use std::sync::LazyLock; |
| 4 | +use tree_sitter_css::HIGHLIGHTS_QUERY; |
| 5 | +use tree_sitter_css::LANGUAGE; |
| 6 | +use tree_sitter_highlight::HighlightConfiguration; |
| 7 | + |
| 8 | +const NAMES: &[&str] = &[ |
| 9 | + "tag", // body |
| 10 | + "property", // font-size |
| 11 | + "variable", // --foo-bar |
| 12 | + "function", // calc() |
| 13 | + "number", // 42 |
| 14 | + "string", // "foo" |
| 15 | + "comment", // /* comment */ |
| 16 | +]; |
| 17 | +const TAGS: &[Tag] = &[ |
| 18 | + Tag::Name, |
| 19 | + Tag::Boolean, // we only have one "Name", so this is a workaround. |
| 20 | + Tag::Text, |
| 21 | + Tag::Text, |
| 22 | + Tag::Number, |
| 23 | + Tag::String, |
| 24 | + Tag::Comment, |
| 25 | +]; |
| 26 | + |
| 27 | +static CONFIG: LazyLock<HighlightConfiguration> = LazyLock::new(|| { |
| 28 | + let mut config = HighlightConfiguration::new(LANGUAGE.into(), "", HIGHLIGHTS_QUERY, "", "") |
| 29 | + .expect("failed to build syntax highlighter"); |
| 30 | + config.configure(NAMES); |
| 31 | + config |
| 32 | +}); |
| 33 | + |
| 34 | +pub fn highlight(input: &[u8]) -> Result<Vec<Chunk>> { |
| 35 | + common::highlight(&CONFIG, TAGS, input) |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod tests { |
| 40 | + use super::*; |
| 41 | + |
| 42 | + #[ignore] |
| 43 | + #[test] |
| 44 | + fn debug() { |
| 45 | + common::debug( |
| 46 | + LANGUAGE.into(), |
| 47 | + HIGHLIGHTS_QUERY, |
| 48 | + b"p > span { color: red; font-size: 42px; content: \"foo\"; margin: var(--foo) } /* foo */", |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_tags_ok() { |
| 54 | + common::test_tags_ok(LANGUAGE.into(), HIGHLIGHTS_QUERY, NAMES, TAGS); |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_highlight() { |
| 59 | + let input = b"\ |
| 60 | + p > span { \n\ |
| 61 | + color: red;\n\ |
| 62 | + font-size: 42px;\n\ |
| 63 | + content: \"foo\";\n\ |
| 64 | + margin: var(--foo);\n\ |
| 65 | + }\n\ |
| 66 | + /* foo */\n\ |
| 67 | + "; |
| 68 | + let chunks = highlight(input).unwrap(); |
| 69 | + assert_eq!( |
| 70 | + chunks, |
| 71 | + vec![ |
| 72 | + (Tag::Name, "p".to_string()), |
| 73 | + (Tag::Text, " > ".to_string()), |
| 74 | + (Tag::Name, "span".to_string()), |
| 75 | + (Tag::Text, " { \n".to_string()), |
| 76 | + (Tag::Boolean, "color".to_string()), |
| 77 | + (Tag::Text, ": red;\n".to_string()), |
| 78 | + (Tag::Boolean, "font-size".to_string()), |
| 79 | + (Tag::Text, ": ".to_string()), |
| 80 | + (Tag::Number, "42px".to_string()), |
| 81 | + (Tag::Text, ";\n".to_string()), |
| 82 | + (Tag::Boolean, "content".to_string()), |
| 83 | + (Tag::Text, ": ".to_string()), |
| 84 | + (Tag::String, "\"foo\"".to_string()), |
| 85 | + (Tag::Text, ";\n".to_string()), |
| 86 | + (Tag::Boolean, "margin".to_string()), |
| 87 | + (Tag::Text, ": var(--foo);\n}\n".to_string()), |
| 88 | + (Tag::Comment, "/* foo */\n".to_string()), |
| 89 | + ] |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments