Table of Contents

Class CodeEditor

Namespace
CodeEdit.Maui
Assembly
CodeEdit.Maui.dll

A native source-code editor with syntax colors, line numbers, and atomic editing commands.

public sealed class CodeEditor : ContentView, INotifyPropertyChanged, IEffectControlProvider, IToolTipElement, IContextFlyoutElement, IAnimatable, IViewController, IVisualElementController, IElementController, IGestureController, IGestureRecognizers, IPropertyMapperView, IHotReloadableView, IReplaceableView, ILayout, ILayoutController, IVisualTreeElement, IContentView, IView, IElement, ITransform, IPadding, ICrossPlatformLayout
Inheritance
CodeEditor
Implements
Inherited Members

Remarks

Uses an internal NativeEditorView through its public APIs. Syntax colors are view-owned decorations; they do not change source, saved state, or history. Persist source using Document.Text.

Constructors

CodeEditor()

Creates an editor configured for source code.

public CodeEditor()

Fields

AllowVirtualSpaceProperty

Identifies AllowVirtualSpace.

public static readonly BindableProperty AllowVirtualSpaceProperty

Field Value

BindableProperty

AutoIndentProperty

Identifies AutoIndent.

public static readonly BindableProperty AutoIndentProperty

Field Value

BindableProperty

DocumentProperty

Identifies Document.

public static readonly BindableProperty DocumentProperty

Field Value

BindableProperty

FontFamilyProperty

Identifies FontFamily.

public static readonly BindableProperty FontFamilyProperty

Field Value

BindableProperty

FontSizeProperty

Identifies FontSize.

public static readonly BindableProperty FontSizeProperty

Field Value

BindableProperty

IndentSizeProperty

Identifies IndentSize.

public static readonly BindableProperty IndentSizeProperty

Field Value

BindableProperty

IsReadOnlyProperty

Identifies IsReadOnly.

public static readonly BindableProperty IsReadOnlyProperty

Field Value

BindableProperty

LineCommentPrefixProperty

Identifies LineCommentPrefix.

public static readonly BindableProperty LineCommentPrefixProperty

Field Value

BindableProperty

MaxLengthProperty

Identifies MaxLength.

public static readonly BindableProperty MaxLengthProperty

Field Value

BindableProperty

PlaceholderProperty

Identifies Placeholder.

public static readonly BindableProperty PlaceholderProperty

Field Value

BindableProperty

SelectedRangeProperty

Identifies SelectedRange.

public static readonly BindableProperty SelectedRangeProperty

Field Value

BindableProperty

SelectionStateProperty

Identifies the directional SelectionState.

public static readonly BindableProperty SelectionStateProperty

Field Value

BindableProperty

SelectionsProperty

Identifies the two-way bindable Selections property.

public static readonly BindableProperty SelectionsProperty

Field Value

BindableProperty

ShowLineNumbersProperty

Identifies ShowLineNumbers.

public static readonly BindableProperty ShowLineNumbersProperty

Field Value

BindableProperty

TabSizeProperty

Identifies the native tab-display width, independently of indentation insertion.

public static readonly BindableProperty TabSizeProperty

Field Value

BindableProperty

TextColorProperty

Identifies TextColor.

public static readonly BindableProperty TextColorProperty

Field Value

BindableProperty

UseTabsProperty

Identifies UseTabs.

public static readonly BindableProperty UseTabsProperty

Field Value

BindableProperty

WordWrapProperty

Identifies WordWrap.

public static readonly BindableProperty WordWrapProperty

Field Value

BindableProperty

Properties

Adornments

Gets source-anchored views whose content and actions are supplied by the application.

public RichTextAdornments Adornments { get; }

Property Value

RichTextAdornments

AllowVirtualSpace

Gets or sets whether caret movement and rectangular gestures can extend beyond logical line ends.

public bool AllowVirtualSpace { get; set; }

Property Value

bool

AutoIndent

Gets or sets whether Enter copies the current line's leading whitespace.

public bool AutoIndent { get; set; }

Property Value

bool

CanRedo

Gets whether a redo unit is available.

public bool CanRedo { get; }

Property Value

bool

CanUndo

Gets whether an undo unit is available.

public bool CanUndo { get; }

Property Value

bool

CaretPosition

Gets the zero-based logical line and UTF-16 column of the active endpoint.

public TextPosition CaretPosition { get; }

Property Value

TextPosition

Commands

Gets the stable MVVM commands.

public CodeEditorCommands Commands { get; }

Property Value

CodeEditorCommands

Composition

Gets authoritative native marked-text state.

public RichTextCompositionState Composition { get; }

Property Value

RichTextCompositionState

CompositionSelection

Gets the native provisional selection, or null outside composition.

public TextSelectionState? CompositionSelection { get; }

Property Value

TextSelectionState?

CompositionSnapshot

Gets provisional native source with an invalid edit revision, or null outside composition.

public CodeDocumentSnapshot? CompositionSnapshot { get; }

Property Value

CodeDocumentSnapshot

Decorations

Gets independently owned presentation layers in creation order.

public RichTextDecorations Decorations { get; }

Property Value

RichTextDecorations

Document

Gets or sets the stable live source document. Persist code through Text.

public CodeDocument Document { get; set; }

Property Value

CodeDocument

Folding

Gets explicit source-range folding. The application owns discovery, UI, and expansion policy.

public RichTextFolding Folding { get; }

Property Value

RichTextFolding

FontFamily

Gets or sets the monospaced font family.

public string FontFamily { get; set; }

Property Value

string

FontSize

Gets or sets the font size in device-independent units.

public double FontSize { get; set; }

Property Value

double

IndentSize

Gets or sets the indentation width in spaces, from 1 through 16. This is not a native tab-stop setting.

public int IndentSize { get; set; }

Property Value

int

InputRules

Gets synchronous rules that extend native source input before one atomic commit.

public TextInputRules InputRules { get; }

Property Value

TextInputRules

IsReadOnly

Gets or sets whether user edits and code-editing commands are disabled.

public bool IsReadOnly { get; set; }

Property Value

bool

KeyBindings

Gets the text surface's key bindings, including the editable code defaults.

public EditorKeyBindingCollection KeyBindings { get; }

Property Value

EditorKeyBindingCollection

LineCommentPrefix

Gets or sets the prefix used by the native line-comment shortcut.

public string LineCommentPrefix { get; set; }

Property Value

string

LineCount

Gets the logical line count, including a trailing empty line.

public int LineCount { get; }

Property Value

int

MaxLength

Gets or sets the maximum UTF-16 source length, or -1 for unlimited.

public int MaxLength { get; set; }

Property Value

int

NewLine

Gets or sets the line ending inserted by native Enter and newline commands. Existing source endings are preserved.

public string NewLine { get; set; }

Property Value

string

Placeholder

Gets or sets the empty-source placeholder.

public string Placeholder { get; set; }

Property Value

string

SelectedRange

Gets or sets the selected UTF-16 range, clamped to the source length.

public TextSpan SelectedRange { get; set; }

Property Value

TextSpan

Selection

Gets the source-only live selection facade.

public CodeTextSelection Selection { get; }

Property Value

CodeTextSelection

SelectionGeneration

Gets a generation that changes when this view's selection changes or maps through a source edit.

public long SelectionGeneration { get; }

Property Value

long

SelectionState

Gets or sets the anchor and active offsets.

public TextSelectionState SelectionState { get; set; }

Property Value

TextSelectionState

Selections

Gets or sets every source selection. The primary selection controls native input.

public TextSelectionSet Selections { get; set; }

Property Value

TextSelectionSet

ShowLineNumbers

Gets or sets whether logical line numbers are visible.

public bool ShowLineNumbers { get; set; }

Property Value

bool

TabSize

Gets or sets native tab spacing in the view's space advances, from one through 32. Source tab characters remain unchanged.

public int TabSize { get; set; }

Property Value

int

TextColor

Gets or sets the normal text and line-number color, or null for the native default.

public Color? TextColor { get; set; }

Property Value

Color

TextLayout

Gets visible geometry and hit testing in this composed editor's coordinates.

public RichTextLayout TextLayout { get; }

Property Value

RichTextLayout

UseTabs

Gets or sets whether indentation inserts tabs instead of spaces.

public bool UseTabs { get; set; }

Property Value

bool

WordWrap

Gets or sets whether long lines wrap. The default is horizontal scrolling.

public bool WordWrap { get; set; }

Property Value

bool

Methods

AddCaret(int, bool)

Adds a source caret, optionally making it the primary native selection.

public void AddCaret(int position, bool makePrimary = true)

Parameters

position int
makePrimary bool

AddCaretAbove()

Adds a caret on the preceding native visual line.

public bool AddCaretAbove()

Returns

bool

AddCaretBelow()

Adds a caret on the following native visual line.

public bool AddCaretBelow()

Returns

bool

CaptureScrollAnchor()

Captures a live source anchor for restoration in this or another view of the same document.

public CodeScrollAnchor? CaptureScrollAnchor()

Returns

CodeScrollAnchor

CaptureViewport()

Captures the first visible source caret without moving selection or focus.

public CodeViewportSnapshot? CaptureViewport()

Returns

CodeViewportSnapshot

ClearSecondarySelections()

Retains the primary selection and removes all secondary selections.

public void ClearSecondarySelections()

ClearUndoHistory()

Clears undo and redo history.

public void ClearUndoHistory()

CopyAsync()

Copies the selection through the native clipboard.

public Task CopyAsync()

Returns

Task

The clipboard operation.

CutAsync()

Cuts the selection through the native clipboard.

public Task CutAsync()

Returns

Task

The clipboard operation.

Focus()

Focuses the native text surface.

public bool Focus()

Returns

bool

Whether the focus request succeeded.

GetLineRange(int)

Gets the source range for a zero-based logical line, excluding its line terminator.

public TextSpan GetLineRange(int line)

Parameters

line int

The zero-based line number.

Returns

TextSpan

The line's UTF-16 source range.

GetOffset(TextPosition)

Converts a zero-based line and UTF-16 column to a bounded source offset.

public int GetOffset(TextPosition position)

Parameters

position TextPosition

The logical source position.

Returns

int

The UTF-16 source offset.

GetPosition(int)

Converts a bounded UTF-16 offset to a zero-based line and column.

public TextPosition GetPosition(int offset)

Parameters

offset int

The source offset.

Returns

TextPosition

The logical source position.

Indent()

Inserts indentation at the caret, or indents every selected logical line in one undo unit.

public void Indent()

InsertNewLine()

Replaces the selection with a newline and, when enabled, the current line's leading whitespace.

public void InsertNewLine()

MoveSelections(EditorKey, bool, bool)

Moves every selection with native caret navigation. Supports Left, Right, Up, Down, Home, and End.

public bool MoveSelections(EditorKey direction, bool extend = false, bool byWord = false)

Parameters

direction EditorKey
extend bool
byWord bool

Returns

bool

OnPropertyChanged(string?)

protected override void OnPropertyChanged(string? propertyName = null)

Parameters

propertyName string

Outdent()

Removes one indentation level from each selected line in one undo unit.

public void Outdent()

PasteAsync()

Pastes clipboard content as plain source text.

public Task PasteAsync()

Returns

Task

The clipboard operation.

Redo()

Reapplies the most recently undone source edit.

public void Redo()

ReplaceSelections(string)

Replaces all selections in one checked transaction and undo entry.

public bool ReplaceSelections(string text)

Parameters

text string

Returns

bool

RestoreScrollAnchorAsync(CodeScrollAnchor, object?, CancellationToken)

Restores a tracked source anchor using this view's native geometry and scroll limits.

public Task<bool> RestoreScrollAnchorAsync(CodeScrollAnchor anchor, object? causeToken = null, CancellationToken cancellationToken = default)

Parameters

anchor CodeScrollAnchor
causeToken object
cancellationToken CancellationToken

Returns

Task<bool>

False for a disposed/foreign anchor, unavailable layout, or a superseding scroll request.

RevealAsync(int, CodeRevealAlignment, object?, CancellationToken)

Reveals a source position using native scrolling without changing selection or focus.

public Task<bool> RevealAsync(int position, CodeRevealAlignment alignment = CodeRevealAlignment.Nearest, object? causeToken = null, CancellationToken cancellationToken = default)

Parameters

position int
alignment CodeRevealAlignment
causeToken object
cancellationToken CancellationToken

Returns

Task<bool>

True after scrolling settles, including clamping at a document edge; false if superseded or unavailable.

ScrollIntoView(TextSpan)

Reveals source without changing the selection.

public void ScrollIntoView(TextSpan range)

Parameters

range TextSpan

The source range to reveal.

SelectAll()

Selects all source text.

public void SelectAll()

SelectRectangle(Point, Point, bool)

Selects the visual text intersecting a rectangle on visible native rows, including disjoint bidi intervals.

public bool SelectRectangle(Point anchor, Point active, bool virtualSpace = false)

Parameters

anchor Point
active Point
virtualSpace bool

Returns

bool

Remarks

Points use this control's coordinates. Native glyph extents determine selection; virtual space is available only beyond a logical line end. Alt-pointer drags retain their source anchor and scroll when the pointer leaves the viewport.

ToggleLineComment()

Adds or removes the line-comment prefix after leading whitespace on selected nonempty lines.

public void ToggleLineComment()

TryApplyEdits(DocumentRevision, IReadOnlyList<TextReplacement>, TextSelectionState?, TextEditOptions, long?)

Applies a checked source edit through its source session.

public bool TryApplyEdits(DocumentRevision revision, IReadOnlyList<TextReplacement> edits, TextSelectionState? selectionAfter = null, TextEditOptions options = default, long? expectedSelectionGeneration = null)

Parameters

revision DocumentRevision

The captured source revision.

edits IReadOnlyList<TextReplacement>

Original-coordinate source replacements.

selectionAfter TextSelectionState?

The resulting directional selection, or null to map the current selection.

options TextEditOptions

Undo and application metadata.

expectedSelectionGeneration long?

An optional captured selection generation, to reject results after the user moves a caret.

Returns

bool

Whether the current view accepted the revision and edits.

TryCompleteComposition()

Requests native composition completion across views of this document without moving focus.

public bool TryCompleteComposition()

Returns

bool

False when the native input method must finish through its own interaction.

Undo()

Undoes the most recent source edit.

public void Undo()

Events

CompositionChanged

Occurs when native marked text starts, changes, or ends.

public event EventHandler<RichTextCompositionChangedEventArgs>? CompositionChanged

Event Type

EventHandler<RichTextCompositionChangedEventArgs>

ContentChanged

Occurs after an atomic source-document change.

public event EventHandler<TextDocumentChangedEventArgs>? ContentChanged

Event Type

EventHandler<TextDocumentChangedEventArgs>

ContextMenuOpening

Customizes the text surface's native context or selection menu. Apple platforms require version 16 or later.

public event EventHandler<EditorContextMenuEventArgs>? ContextMenuOpening

Event Type

EventHandler<EditorContextMenuEventArgs>

DocumentChanged

Occurs after a replacement source document is attached and view state is synchronized.

public event EventHandler<CodeDocumentReplacedEventArgs>? DocumentChanged

Event Type

EventHandler<CodeDocumentReplacedEventArgs>

InputRejected

Occurs when rule failure or a conflicting plan rejects native input.

public event EventHandler<RichTextInputRejectedEventArgs>? InputRejected

Event Type

EventHandler<RichTextInputRejectedEventArgs>

KeyDown

Occurs before built-in hardware-key handling, except while native text composition is active.

public event EventHandler<EditorKeyEventArgs>? KeyDown

Event Type

EventHandler<EditorKeyEventArgs>

Pasting

Occurs before committing plain source text from the clipboard.

public event EventHandler<TextPastingEventArgs>? Pasting

Event Type

EventHandler<TextPastingEventArgs>

PointerExited

Occurs when a hovering pointer exits the text surface.

public event EventHandler? PointerExited

Event Type

EventHandler

PointerMoved

Observes local pointer movement without changing native input behavior.

public event EventHandler<RichTextPointerEventArgs>? PointerMoved

Event Type

EventHandler<RichTextPointerEventArgs>

PointerPressed

Observes local pointer presses without suppressing selection or focus.

public event EventHandler<RichTextPointerEventArgs>? PointerPressed

Event Type

EventHandler<RichTextPointerEventArgs>

SelectionChanged

Occurs after the caret or selection changes.

public event EventHandler<RichTextSelectionChangedEventArgs>? SelectionChanged

Event Type

EventHandler<RichTextSelectionChangedEventArgs>

SelectionsChanged

Occurs after primary or secondary selections change.

public event EventHandler? SelectionsChanged

Event Type

EventHandler

TextChanged

Occurs after logical source text changes.

public event EventHandler<TextDocumentChangedEventArgs>? TextChanged

Event Type

EventHandler<TextDocumentChangedEventArgs>

ViewportChanged

Occurs when native scrolling, layout, or edits change the source-based viewport.

public event EventHandler<CodeViewportChangedEventArgs>? ViewportChanged

Event Type

EventHandler<CodeViewportChangedEventArgs>