CodeEditor
The CodeEditor widget extends Editor with syntax highlighting, bracket matching, configurable indentation, and auto-close of bracket pairs.
Overview
Section titled “Overview”CodeEditor is built on top of Editor, so it inherits multi-line editing, line numbers, selection, and undo/redo. It adds a pluggable syntax highlighting system backed by PluginManager, which ships with built-in rules for csharp, javascript, and python, and a default theme. Additional languages and themes can be supplied through plugin packages (see Ambystech.Elaris.UI.CodeEditor.Plugins).
Properties
Section titled “Properties”Language (string)
Section titled “Language (string)”The programming language used for syntax highlighting. Falls back to "csharp" if the requested language isn’t registered.
codeEditor.Language = "csharp";Theme (string)
Section titled “Theme (string)”The theme name used for token colors. Falls back to "default" if the requested theme isn’t registered.
codeEditor.Theme = "default";SyntaxHighlighting (bool)
Section titled “SyntaxHighlighting (bool)”Whether syntax highlighting is enabled. When false, CodeEditor renders like a plain Editor.
codeEditor.SyntaxHighlighting = true;ShowBracketMatching (bool)
Section titled “ShowBracketMatching (bool)”Whether to highlight the bracket that matches the one at the cursor.
codeEditor.ShowBracketMatching = true;TabSize (int)
Section titled “TabSize (int)”The number of spaces per tab. Minimum is 1.
codeEditor.TabSize = 4;UseTabs (bool)
Section titled “UseTabs (bool)”If true, the Tab key inserts a literal \t; otherwise it inserts TabSize spaces.
codeEditor.UseTabs = false;AutoIndent (bool)
Section titled “AutoIndent (bool)”When enabled, pressing Enter preserves the current line’s indentation (and adds one extra level after an opening bracket), and typing an opening bracket ((, [, {) automatically inserts the matching closing bracket.
codeEditor.AutoIndent = true;Methods
Section titled “Methods”SetLanguage(string language)
Section titled “SetLanguage(string language)”Sets the highlighting language. Equivalent to assigning to Language.
codeEditor.SetLanguage("javascript");FindMatchingBracket(int line, int column)
Section titled “FindMatchingBracket(int line, int column)”Returns the Point of the bracket that matches the one at the given position, or null if none is found or the character is not a bracket.
var match = codeEditor.FindMatchingBracket(codeEditor.CursorLine, codeEditor.CursorColumn);GetIndentationLevel(int line)
Section titled “GetIndentationLevel(int line)”Returns the leading-whitespace width of a line, measured in spaces (tabs count as TabSize).
int indent = codeEditor.GetIndentationLevel(codeEditor.CursorLine);Keyboard Behavior
Section titled “Keyboard Behavior”In addition to all Editor shortcuts:
- Tab: Inserts indentation (
TabSizespaces or\tdepending onUseTabs). WithAutoIndentenabled, uses the line’s current indent level. - Shift+Tab: Removes one indentation step before the cursor.
- Enter: With
AutoIndentenabled, preserves the current indent and adds an extra level after an opening{,[, or(. {,[,(: WithAutoIndentenabled, inserts the matching closing bracket and places the cursor between the pair.
Built-in Languages and Themes
Section titled “Built-in Languages and Themes”| Language | Keywords include |
|---|---|
csharp | class, public, async, await, … |
javascript | function, const, async, … |
python | def, class, import, … |
The default theme maps token types (keyword, string, number, comment, identifier, operator) to colors. Additional themes and languages can be registered through the PluginManager plugin API.
Usage Example
Section titled “Usage Example”var codeEditor = new CodeEditor{ X = 2, Y = 2, Width = 80, Height = 24, ShowLineNumbers = true, SyntaxHighlighting = true, ShowBracketMatching = true, AutoIndent = true, TabSize = 4, Language = "csharp", Theme = "default", ForegroundColor = Color.White, BackgroundColor = Color.Black, LineNumberColor = ColorHelper.FromRgb(100, 100, 100), Text = @"using System;
namespace Elaris.Examples{ public class Program { public static void Main(string[] args) { Console.WriteLine(""Hello, Elaris!""); } }}"};
codeEditor.CursorMoved += (line, col) =>{ statusBar.CenterText = $"Line {line + 1}, Col {col + 1} | {codeEditor.Language}";};Related Widgets
Section titled “Related Widgets”- Editor — Plain multi-line text editor base class
- TextField — Single-line text input
- TextView — Read-only multi-line text display
- Base Widget — Base class documentation