Skip to main content

Module syntax_highlight

Module syntax_highlight 

Source
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§

DiffScopeBackgroundRgbs
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 anstyle segments 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 anstyle segments 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