Expand description
Syntax Highlighting Engine
Global syntax highlighting using syntect with TextMate themes.
Follows the architecture from OpenAI Codex PRs #11447 and #12581.
§Architecture
- SyntaxSet: Process-global singleton (~250 grammars, loaded once)
- ThemeSet: Process-global singleton loaded once
- Highlighting: Guardrails skip large inputs (>512KB or >10K lines)
§Usage
use crate::ui::syntax_highlight::{
highlight_code_to_segments, get_active_syntax_theme
};
use crate::ui::theme::active_theme_id;
// Auto-resolve syntax theme from current UI theme
let syntax_theme = get_active_syntax_theme();
// Highlight code with proper theme
let segments = highlight_code_to_segments(code, Some("rust"), syntax_theme);§Performance
- Single SyntaxSet load (~1MB, ~50ms)
- Single ThemeSet load shared by all highlighters
- Input guardrails prevent highlighting huge files
- Parser state preserved across multiline constructs
Structs§
- Diff
Scope Background Rgbs - Raw RGB diff backgrounds extracted from syntax theme scopes.
Functions§
- available_
themes - Get all available theme names
- default_
theme_ name - Get the default syntax theme name
- diff_
scope_ background_ rgbs - Resolve diff-scope background colors from the currently active syntax theme.
- find_
syntax_ by_ extension - Find syntax by file extension
- find_
syntax_ by_ name - Find syntax by exact name
- find_
syntax_ by_ token - Find syntax by language token (e.g., “rust”, “python”)
- find_
syntax_ plain_ text - Get plain text syntax fallback
- get_
active_ syntax_ theme - Get the recommended syntax theme for the current UI theme
- get_
syntax_ theme - Get the recommended syntax theme for a specific UI theme
- highlight_
code_ to_ ansi - Convert code to ANSI escape sequences
- highlight_
code_ to_ anstyle_ line_ segments - Highlight code and convert to
anstylesegments with optional bg stripping. - highlight_
code_ to_ line_ segments - Highlight code and return styled segments per line.
- highlight_
code_ to_ segments - Highlight code and return styled segments
- highlight_
line_ for_ diff - Highlight a single line (for diff rendering)
- highlight_
line_ to_ anstyle_ segments - Highlight one line and convert to
anstylesegments with optional bg stripping. - load_
theme - Load a theme from the process-global theme set.
- should_
highlight - Check if input should be highlighted (guardrails)
- syntax_
set - Get the global SyntaxSet reference