MCPcopy Create free account
hub / github.com/GCWing/BitFun / highlight_code

Function highlight_code

src/apps/cli/src/ui/syntax_highlight.rs:48–82  ·  view source on GitHub ↗
(
    content: &str,
    file_ext: &str,
    hl_theme: HighlightTheme,
)

Source from the content-addressed store, hash-verified

46/// Falls back to plain text if the language is not recognized.
47#[allow(dead_code)]
48pub 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.

Callers

nothing calls this directly

Calls 4

syntect_to_ratatui_styleFunction · 0.85
syntect_theme_nameMethod · 0.80
collectMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected