Architecture

AvaloniaEdit is composed of several layers. Understanding this hierarchy is helpful for advanced customizations.

The Hierarchy

  1. TextEditor: The high-level control. It acts as a facade, wrapping the TextArea and handling the scroll bars. This is what you typically use in XAML.
  2. TextArea: The main editing control. It handles user input (keyboard/mouse), the Caret, the Selection, and input methods (IME).
  3. TextView: The presentation layer. It is responsible for visual rendering. It converts the logical TextDocument into VisualLines and TextLines for drawing.
  4. TextDocument: The data model. It stores the actual characters using a Rope<char> data structure (for efficient insertion/deletion) and manages the UndoStack.

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 that Column is 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.