(
content: &str,
file_ext: &str,
hl_theme: HighlightTheme,
)
| 46 | /// Falls back to plain text if the language is not recognized. |
| 47 | #[allow(dead_code)] |
| 48 | pub fn highlight_code<'a>( |
| 49 | content: &str, |
| 50 | file_ext: &str, |
| 51 | hl_theme: HighlightTheme, |
| 52 | ) -> Vec<Line<'a>> { |
| 53 | let syntax = SYNTAX_SET |
| 54 | .find_syntax_by_extension(file_ext) |
| 55 | .unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text()); |
| 56 | |
| 57 | let theme = &THEME_SET.themes[hl_theme.syntect_theme_name()]; |
| 58 | let mut highlighter = HighlightLines::new(syntax, theme); |
| 59 | let mut lines = Vec::new(); |
| 60 | |
| 61 | for line_str in LinesWithEndings::from(content) { |
| 62 | match highlighter.highlight_line(line_str, &SYNTAX_SET) { |
| 63 | Ok(ranges) => { |
| 64 | let spans: Vec<Span<'a>> = ranges |
| 65 | .into_iter() |
| 66 | .map(|(style, text)| { |
| 67 | Span::styled(text.to_string(), syntect_to_ratatui_style(&style)) |
| 68 | }) |
| 69 | .collect(); |
| 70 | lines.push(Line::from(spans)); |
| 71 | } |
| 72 | Err(_) => { |
| 73 | // Fallback: plain text |
| 74 | lines.push(Line::from(Span::raw( |
| 75 | line_str.trim_end_matches('\n').to_string(), |
| 76 | ))); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | lines |
| 82 | } |
| 83 | |
| 84 | /// Highlight a single bash command line (e.g. "ls -la --color"). |
| 85 | /// Returns a Line with syntax-highlighted spans. |
nothing calls this directly
no test coverage detected