Skip to main content

vtcode_core/ui/
syntax_highlight.rs

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