Skip to main content

vtcode_core/ui/
syntax_highlight.rs

1#[cfg(feature = "tui")]
2pub use vtcode_tui::ui::syntax_highlight::{
3    available_themes, default_theme_name, find_syntax_by_extension, find_syntax_by_name,
4    find_syntax_by_token, find_syntax_plain_text, highlight_code_to_ansi,
5    highlight_code_to_anstyle_line_segments, highlight_code_to_line_segments,
6    highlight_code_to_segments, highlight_line_for_diff, highlight_line_to_anstyle_segments,
7    load_theme, should_highlight, syntax_set,
8};
9
10use crate::ui::theme::get_syntax_theme_for_ui_theme;
11
12// ── Headless stubs ──────────────────────────────────────────────────────────
13//
14// When the TUI crate is not compiled in, syntax highlighting is unavailable.
15// Every function returns a no-op / passthrough result so callers compile
16// without cfg-gating each call site.
17
18#[cfg(not(feature = "tui"))]
19mod headless_highlight {
20    use anstyle::Style;
21
22    pub fn available_themes() -> Vec<&'static str> {
23        vec!["plain"]
24    }
25    pub fn default_theme_name() -> &'static str {
26        "plain"
27    }
28    pub fn find_syntax_by_extension(_ext: &str) -> Option<&'static str> {
29        None
30    }
31    pub fn find_syntax_by_name(_name: &str) -> Option<&'static str> {
32        None
33    }
34    pub fn find_syntax_by_token(_token: &str) -> Option<&'static str> {
35        None
36    }
37    pub fn find_syntax_plain_text() -> &'static str {
38        "text"
39    }
40    pub fn highlight_code_to_ansi(code: &str, _language: Option<&str>, _theme: &str) -> String {
41        code.to_string()
42    }
43    pub fn highlight_code_to_anstyle_line_segments(
44        code: &str,
45        _language: Option<&str>,
46        _theme: &str,
47    ) -> Vec<Vec<(Style, String)>> {
48        code.lines()
49            .map(|line| vec![(Style::default(), line.to_string())])
50            .collect()
51    }
52    pub fn highlight_code_to_line_segments(
53        code: &str,
54        language: Option<&str>,
55        theme: &str,
56    ) -> Vec<Vec<(Style, String)>> {
57        highlight_code_to_anstyle_line_segments(code, language, theme)
58    }
59    pub fn highlight_code_to_segments(
60        code: &str,
61        _language: Option<&str>,
62        _theme: &str,
63    ) -> Vec<(Style, String)> {
64        vec![(Style::default(), code.to_string())]
65    }
66    pub fn highlight_line_for_diff(
67        line: &str,
68        _language: Option<&str>,
69    ) -> Option<Vec<(Style, String)>> {
70        Some(vec![(Style::default(), line.to_string())])
71    }
72    pub fn highlight_line_to_anstyle_segments(
73        line: &str,
74        _language: Option<&str>,
75        _theme: &str,
76    ) -> Vec<(Style, String)> {
77        vec![(Style::default(), line.to_string())]
78    }
79    pub fn load_theme(_theme: &str) -> Option<&'static str> {
80        None
81    }
82    pub fn should_highlight(_language: Option<&str>, _theme: &str) -> bool {
83        false
84    }
85    pub fn syntax_set() -> Option<()> {
86        None
87    }
88}
89
90#[cfg(not(feature = "tui"))]
91pub use headless_highlight::*;
92
93/// Get the recommended syntax theme for the current core UI theme.
94#[inline]
95pub fn get_active_syntax_theme() -> &'static str {
96    get_syntax_theme_for_ui_theme(&crate::ui::theme::active_theme_id())
97}
98
99/// Get the recommended syntax theme for a specific UI theme.
100#[inline]
101pub fn get_syntax_theme(theme: &str) -> &'static str {
102    get_syntax_theme_for_ui_theme(theme)
103}