|
| 1 | +use colored::Colorize; |
| 2 | +use syntect::easy::HighlightLines; |
| 3 | +use syntect::highlighting::{Style, ThemeSet}; |
| 4 | +use syntect::parsing::SyntaxSet; |
| 5 | +use syntect::util::as_24_bit_terminal_escaped; |
| 6 | + |
| 7 | +pub struct SyntaxHighlighter { |
| 8 | + syntax_set: SyntaxSet, |
| 9 | + theme_set: ThemeSet, |
| 10 | +} |
| 11 | + |
| 12 | +impl SyntaxHighlighter { |
| 13 | + pub fn new() -> Self { |
| 14 | + Self { |
| 15 | + syntax_set: SyntaxSet::load_defaults_newlines(), |
| 16 | + theme_set: ThemeSet::load_defaults(), |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + pub fn highlight_text(&self, text: &str) -> String { |
| 21 | + let mut result = String::new(); |
| 22 | + let mut in_code_block = false; |
| 23 | + let mut code_block = String::new(); |
| 24 | + let mut language = String::new(); |
| 25 | + |
| 26 | + for line in text.lines() { |
| 27 | + if line.starts_with("```") { |
| 28 | + if in_code_block { |
| 29 | + result.push_str(&self.highlight_code(&code_block, &language)); |
| 30 | + result.push('\n'); |
| 31 | + code_block.clear(); |
| 32 | + language.clear(); |
| 33 | + in_code_block = false; |
| 34 | + } else { |
| 35 | + language = line.trim_start_matches('`').trim().to_string(); |
| 36 | + in_code_block = true; |
| 37 | + } |
| 38 | + } else if in_code_block { |
| 39 | + code_block.push_str(line); |
| 40 | + code_block.push('\n'); |
| 41 | + } else { |
| 42 | + result.push_str(line); |
| 43 | + result.push('\n'); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if in_code_block { |
| 48 | + result.push_str(&self.highlight_code(&code_block, &language)); |
| 49 | + } |
| 50 | + |
| 51 | + result.trim_end().to_string() |
| 52 | + } |
| 53 | + |
| 54 | + fn highlight_code(&self, code: &str, language: &str) -> String { |
| 55 | + let syntax = if language.is_empty() { |
| 56 | + self.syntax_set.find_syntax_plain_text() |
| 57 | + } else { |
| 58 | + self.syntax_set |
| 59 | + .find_syntax_by_token(language) |
| 60 | + .or_else(|| self.syntax_set.find_syntax_by_extension(language)) |
| 61 | + .unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()) |
| 62 | + }; |
| 63 | + |
| 64 | + let theme = &self.theme_set.themes["base16-ocean.dark"]; |
| 65 | + let mut highlighter = HighlightLines::new(syntax, theme); |
| 66 | + |
| 67 | + let mut result = String::new(); |
| 68 | + result.push_str(&format!("{}\n", "┌─────".dimmed())); |
| 69 | + |
| 70 | + for line in code.lines() { |
| 71 | + let ranges: Vec<(Style, &str)> = highlighter |
| 72 | + .highlight_line(line, &self.syntax_set) |
| 73 | + .unwrap_or_default(); |
| 74 | + let escaped = as_24_bit_terminal_escaped(&ranges[..], false); |
| 75 | + result.push_str(&format!("{} {}\n", "│".dimmed(), escaped)); |
| 76 | + } |
| 77 | + |
| 78 | + result.push_str(&format!("{}", "└─────".dimmed())); |
| 79 | + result |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Default for SyntaxHighlighter { |
| 84 | + fn default() -> Self { |
| 85 | + Self::new() |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use super::*; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_highlighter_creation() { |
| 95 | + let highlighter = SyntaxHighlighter::new(); |
| 96 | + assert!(!highlighter.syntax_set.syntaxes().is_empty()); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_plain_text() { |
| 101 | + let highlighter = SyntaxHighlighter::new(); |
| 102 | + let text = "Hello, world!"; |
| 103 | + let result = highlighter.highlight_text(text); |
| 104 | + assert!(result.contains("Hello, world!")); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_code_block_detection() { |
| 109 | + let highlighter = SyntaxHighlighter::new(); |
| 110 | + let text = "Here is some code:\n```rust\nfn main() {}\n```"; |
| 111 | + let result = highlighter.highlight_text(text); |
| 112 | + assert!(result.contains("Here is some code:")); |
| 113 | + assert!(result.contains("main")); |
| 114 | + } |
| 115 | +} |
0 commit comments