| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use std::sync::OnceLock; |
| 5 | | use syntect::html::{ClassStyle, ClassedHTMLGenerator}; |
| 6 | | use syntect::parsing::SyntaxSet; |
| 7 | | use syntect::util::LinesWithEndings; |
| 8 | |
|
| 9 | 75× | fn syntaxes() -> &'static SyntaxSet { |
| 10 | | static SET: OnceLock<SyntaxSet> = OnceLock::new(); |
| 11 | 75× | SET.get_or_init(SyntaxSet::load_defaults_newlines) |
| 12 | 75× | } |
| 13 | |
|
| 14 | |
|
| 15 | 60× | pub fn language(path: &str) -> Option<&'static str> { |
| 16 | 60× | let ext = path.rsplit('.').next().unwrap_or(""); |
| 17 | 60× | let name = path.rsplit('/').next().unwrap_or(path); |
| 18 | 60× | let set = syntaxes(); |
| 19 | 60× | set.find_syntax_by_extension(ext) |
| 20 | 60× | .or_else(|| set.find_syntax_by_extension(name)) |
| 21 | 60× | .or_else(|| match name { |
| 22 | 4× | "Makefile" | "GNUmakefile" => set.find_syntax_by_name("Makefile"), |
| 23 | 4× | "Dockerfile" => set.find_syntax_by_name("Dockerfile"), |
| 24 | 4× | _ => None, |
| 25 | 4× | }) |
| 26 | 60× | .map(|s| s.name.as_str()) |
| 27 | 60× | } |
| 28 | |
|
| 29 | |
|
| 30 | 44× | fn cache() -> &'static super::cache::Lru<Vec<String>> { |
| 31 | | static C: OnceLock<super::cache::Lru<Vec<String>>> = OnceLock::new(); |
| 32 | 44× | C.get_or_init(|| super::cache::Lru::new(64 << 20)) |
| 33 | 44× | } |
| 34 | |
|
| 35 | |
|
| 36 | 2× | pub fn cache_stats() -> (u64, u64, usize) { |
| 37 | 2× | cache().stats() |
| 38 | 2× | } |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 42× | pub fn lines(path: &str, text: &str) -> Vec<String> { |
| 44 | 42× | let name = path.rsplit('/').next().unwrap_or(path); |
| 45 | 42× | let key = super::cache::key(&[b"hl1", name.as_bytes(), text.as_bytes()]); |
| 46 | 42× | cache().get_or(key, || lines_uncached(path, text)).as_ref().clone() |
| 47 | 42× | } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | | pub const MAX_HIGHLIGHTED_LINE: usize = 4_000; |
| 54 | |
|
| 55 | 16× | fn lines_uncached(path: &str, text: &str) -> Vec<String> { |
| 56 | 48× | if text.lines().any(|l| l.len() > MAX_HIGHLIGHTED_LINE) { |
| 57 | 1× | return text.lines().map(super::escape).collect(); |
| 58 | 15× | } |
| 59 | 15× | let set = syntaxes(); |
| 60 | 15× | let ext = path.rsplit('.').next().unwrap_or(""); |
| 61 | 15× | let name = path.rsplit('/').next().unwrap_or(path); |
| 62 | 15× | let syntax = set.find_syntax_by_extension(ext).or_else(|| set.find_syntax_by_extension(name)).or_else(|| match name { |
| 63 | 1× | "Makefile" | "GNUmakefile" => set.find_syntax_by_name("Makefile"), |
| 64 | 1× | _ => None, |
| 65 | 1× | }); |
| 66 | 15× | let Some(syntax) = syntax else { |
| 67 | 1× | return text.lines().map(super::escape).collect(); |
| 68 | | }; |
| 69 | 14× | let mut generator = ClassedHTMLGenerator::new_with_class_style(syntax, set, ClassStyle::SpacedPrefixed { prefix: "sy-" }); |
| 70 | 14× | let mut out = Vec::new(); |
| 71 | 45× | for line in LinesWithEndings::from(text) { |
| 72 | 45× | if generator.parse_html_for_line_which_includes_newline(line).is_err() { |
| 73 | 0× | return text.lines().map(super::escape).collect(); |
| 74 | 45× | } |
| 75 | | } |
| 76 | 14× | let html = generator.finalize(); |
| 77 | | |
| 78 | | |
| 79 | 14× | let mut open: Vec<String> = Vec::new(); |
| 80 | 14× | let mut raw_lines: Vec<&str> = html.split('\n').collect(); |
| 81 | | |
| 82 | | |
| 83 | 14× | if raw_lines.last().is_some_and(|l| l.replace("</span>", "").trim().is_empty()) { |
| 84 | 12× | raw_lines.pop(); |
| 85 | 12× | } |
| 86 | 45× | for raw in raw_lines { |
| 87 | 45× | let mut line = String::new(); |
| 88 | 75× | for tag in &open { |
| 89 | 75× | line.push_str(tag); |
| 90 | 75× | } |
| 91 | 45× | line.push_str(raw); |
| 92 | | |
| 93 | 45× | let mut i = 0; |
| 94 | 540× | while let Some(p) = raw[i..].find('<') { |
| 95 | 495× | let start = i + p; |
| 96 | 495× | let Some(end_rel) = raw[start..].find('>') else { break }; |
| 97 | 495× | let tag = &raw[start..start + end_rel + 1]; |
| 98 | 495× | if tag.starts_with("</") { |
| 99 | 241× | open.pop(); |
| 100 | 254× | } else if tag.starts_with("<span") { |
| 101 | 254× | open.push(tag.to_string()); |
| 102 | 254× | } |
| 103 | 495× | i = start + end_rel + 1; |
| 104 | | } |
| 105 | 88× | for _ in &open { |
| 106 | 88× | line.push_str("</span>"); |
| 107 | 88× | } |
| 108 | 45× | out.push(line); |
| 109 | | } |
| 110 | 14× | out |
| 111 | 16× | } |
| 112 | |
|
| 113 | | #[cfg(test)] |
| 114 | | mod tests { |
| 115 | | use super::*; |
| 116 | |
|
| 117 | | #[test] |
| 118 | 1× | fn highlights_rust_and_falls_back() { |
| 119 | 1× | let l = lines("main.rs", "fn main() {\n let x = 1;\n}\n"); |
| 120 | 1× | assert_eq!(l.len(), 3); |
| 121 | 1× | assert!(l[0].contains("<span class=\"sy-source sy-rust\">"), "{}", l[0]); |
| 122 | 1× | assert!(l[0].contains("sy-storage sy-type sy-function"), "{}", l[0]); |
| 123 | | |
| 124 | 1× | let c = lines("main.rs", "// note\n"); |
| 125 | 1× | assert!(c[0].contains("sy-comment") && !c[0].contains("\"comment") && !c[0].contains(" comment"), "{}", c[0]); |
| 126 | 1× | assert!(l[1].ends_with("</span>"), "spans are balanced per line: {}", l[1]); |
| 127 | 1× | assert_eq!(lines("x.unknownext", "a<b\nc"), vec!["a<b", "c"]); |
| 128 | 1× | assert_eq!(language("a/b/lib.rs"), Some("Rust")); |
| 129 | 1× | assert_eq!(language("Makefile"), Some("Makefile")); |
| 130 | 1× | assert_eq!(language("blob.bin"), None); |
| 131 | 1× | } |
| 132 | |
|
| 133 | | |
| 134 | | #[test] |
| 135 | 1× | fn a_text_with_a_very_long_line_is_shown_plain() { |
| 136 | 1× | let long = format!("var a=\"<\";{}\nb();\n", "x+".repeat(MAX_HIGHLIGHTED_LINE)); |
| 137 | 1× | let started = std::time::Instant::now(); |
| 138 | 1× | let l = lines("app.min.js", &long); |
| 139 | | |
| 140 | | |
| 141 | 1× | let bound = std::time::Duration::from_millis(if cfg!(debug_assertions) { 3000 } else { 500 }); |
| 142 | 1× | assert!(started.elapsed() < bound, "{:?}", started.elapsed()); |
| 143 | 1× | assert_eq!(l.len(), 2); |
| 144 | 1× | assert!(l[0].starts_with("var a="<";") && !l[0].contains("<span"), "{}", &l[0][..40]); |
| 145 | 1× | assert!(lines("app.js", "var a = 1;\n")[0].contains("<span"), "a normal file is highlighted"); |
| 146 | 1× | } |
| 147 | | } |