Architecture
AvaloniaEdit is composed of several layers. Understanding this hierarchy is helpful for advanced customizations.
The Hierarchy
- TextEditor: The high-level control. It acts as a facade, wrapping the
TextAreaand handling the scroll bars. This is what you typically use in XAML. - TextArea: The main editing control. It handles user input (keyboard/mouse), the
Caret, theSelection, and input methods (IME). - TextView: The presentation layer. It is responsible for visual rendering. It converts the logical
TextDocumentintoVisualLinesandTextLines for drawing. - TextDocument: The data model. It stores the actual characters using a
Rope<char>data structure (for efficient insertion/deletion) and manages theUndoStack.
Accessing Components
From the TextEditor instance, you can access sub-components:
var editor = new TextEditor();
// Access the document (Data)
TextDocument doc = editor.Document;
// Access the editing area (Input/Selection/Caret)
TextArea area = editor.TextArea;
// Access the view (Rendering)
TextView view = editor.TextArea.TextView;
The Coordinate Systems
AvaloniaEdit uses several coordinate systems:
- Offset: The absolute character index in the document (0 to
TextLength). - TextLocation: A
{Line, Column}pair. Note thatColumnis 1-based. - VisualLocation: A visual
{Line, Column}. Visual columns differ from text columns when tabs are expanded or when characters have variable width.
Service Container
The TextView and TextDocument implement IServiceProvider. This allows components to register services (like syntax highlighters or background renderers) that other parts of the editor can retrieve.