Skip to content

CodeEditor

The CodeEditor widget extends Editor with syntax highlighting, bracket matching, configurable indentation, and auto-close of bracket pairs.

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).

The programming language used for syntax highlighting. Falls back to "csharp" if the requested language isn’t registered.

codeEditor.Language = "csharp";

The theme name used for token colors. Falls back to "default" if the requested theme isn’t registered.

codeEditor.Theme = "default";

Whether syntax highlighting is enabled. When false, CodeEditor renders like a plain Editor.

codeEditor.SyntaxHighlighting = true;

Whether to highlight the bracket that matches the one at the cursor.

codeEditor.ShowBracketMatching = true;

The number of spaces per tab. Minimum is 1.

codeEditor.TabSize = 4;

If true, the Tab key inserts a literal \t; otherwise it inserts TabSize spaces.

codeEditor.UseTabs = false;

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;

Sets the highlighting language. Equivalent to assigning to Language.

codeEditor.SetLanguage("javascript");

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);

Returns the leading-whitespace width of a line, measured in spaces (tabs count as TabSize).

int indent = codeEditor.GetIndentationLevel(codeEditor.CursorLine);

In addition to all Editor shortcuts:

  • Tab: Inserts indentation (TabSize spaces or \t depending on UseTabs). With AutoIndent enabled, uses the line’s current indent level.
  • Shift+Tab: Removes one indentation step before the cursor.
  • Enter: With AutoIndent enabled, preserves the current indent and adds an extra level after an opening {, [, or (.
  • {, [, (: With AutoIndent enabled, inserts the matching closing bracket and places the cursor between the pair.
LanguageKeywords include
csharpclass, public, async, await, …
javascriptfunction, const, async, …
pythondef, 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.

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}";
};
  • 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