wijjit.elements.input.code_editor.SyntaxHighlighter
- class wijjit.elements.input.code_editor.SyntaxHighlighter(language=None, theme='monokai', filename_hint=None)[source]
Manages syntax highlighting tokenization and caching.
This class handles tokenizing source code using Pygments lexers and caching the results for efficient rendering. It supports incremental re-tokenization when edits are made, and full re-tokenization for large changes.
- Parameters:
language (
str, optional) – Programming language name (e.g., “python”, “javascript”). Use “auto” for automatic detection, or None to disable highlighting.theme (
str, optional) – Color theme name (default: “monokai”)filename_hint (
str, optional) – Filename hint for language auto-detection (e.g., “main.py”)
- Variables:
Methods
__init__([language, theme, filename_hint])detect_language(text)Detect the programming language from content and/or filename.
get_line_tokens(line_idx)Get cached tokens for a specific line.
invalidate_from_line(line_idx)Mark lines from the given index as needing re-tokenization.
Check if syntax highlighting is currently enabled.
retokenize_incremental(lines)Re-tokenize only from the dirty line forward.
set_language(language)Set the highlighting language.
set_theme(theme)Set the color theme.
tokenize_document(lines)Tokenize the entire document.
Attributes
Get the Pygments lexer, creating it if needed.
- __getstate__()[source]
Get state for pickling, excluding the lexer.
- Returns:
Picklable state dictionary
- Return type:
- __setstate__(state)[source]
Restore state from pickle, recreating the lexer.
- Parameters:
state (
dict) – Pickled state dictionary- Return type:
None
- __deepcopy__(memo)[source]
Create a deep copy, excluding the lexer.
- Parameters:
memo (
dict) – Memoization dictionary for deepcopy- Returns:
Deep copy of this highlighter
- Return type:
- property lexer: Lexer | None
Get the Pygments lexer, creating it if needed.
- Returns:
The Pygments lexer instance
- Return type:
LexerorNone
- detect_language(text)[source]
Detect the programming language from content and/or filename.
- Parameters:
text (
str) – Source code content- Returns:
Detected language name, or None if detection failed
- Return type:
Notes
Filename-based detection is more reliable for short code snippets. Content-based detection works best with substantial code (50+ lines).
- set_theme(theme)[source]
Set the color theme.
- Parameters:
theme (
str) – Theme name (e.g., “monokai”, “dracula”)- Return type:
None
- tokenize_document(lines)[source]
Tokenize the entire document.
Notes
This performs a full tokenization of the document. For large documents, this may take 100-200ms. Results are cached per line for fast rendering.
- invalidate_from_line(line_idx)[source]
Mark lines from the given index as needing re-tokenization.
- Parameters:
line_idx (
int) – Starting line index for invalidation- Return type:
None
Notes
This is used for incremental updates. When a line is edited, all lines from that point forward may need re-tokenization (due to multi-line constructs like strings and comments).
- retokenize_incremental(lines)[source]
Re-tokenize only from the dirty line forward.
Notes
This is more efficient than full re-tokenization for small edits. However, for safety and simplicity with multi-line constructs, we re-tokenize from the dirty line to the end.