Class TextMateSyntaxHighlighter
An ISyntaxHighlighter implementation powered by TextMateSharp. Provides syntax highlighting for 50+ languages using VS Code's TextMate grammar engine.
public class TextMateSyntaxHighlighter : ISyntaxHighlighter
- Inheritance
-
TextMateSyntaxHighlighter
- Implements
- Inherited Members
Remarks
Assign an instance to SyntaxHighlighter to enable colorized code blocks in Markdown rendering:
markdownView.SyntaxHighlighter = new TextMateSyntaxHighlighter (ThemeName.DarkPlus);
The highlighter maintains per-line tokenizer state internally. Markdown calls ResetState() at the start of each code block so that multi-line constructs (strings, comments) don't leak across blocks.
Constructors
- TextMateSyntaxHighlighter(ThemeName)
Initializes a new TextMateSyntaxHighlighter with the specified theme.
Properties
- DefaultBackground
Gets the default background color from the active syntax highlighting theme. Used by code block views to fill their viewport background consistently with per-token backgrounds. Returns null if no theme background is available.
- ThemeName
Gets the TextMateSharp.Grammars.ThemeName that is currently active.
Methods
- GetAttributeForScope(MarkdownStyleRole)
Returns a theme-derived Attribute for the given markdown style role, or null if this highlighter has no specific styling for that role.
- GetThemeForBackground(Color)
Returns a ThemeName appropriate for the given terminal background color. Returns TextMateSharp.Grammars.ThemeName.DarkPlus for dark backgrounds and TextMateSharp.Grammars.ThemeName.LightPlus for light backgrounds.
- Highlight(string, string?)
Highlights a single line of code and returns styled segments.
- ResetState()
Resets internal tokenizer state. Called by Markdown at the start of each new code block so that stateful tokenizers (e.g., TextMate) begin fresh.
- SetTheme(ThemeName)
Switches the active theme used for colorization. Clears the grammar cache since theme changes may affect tokenization colors.