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
AutoIndentProperty
Identifies AutoIndent.
public static readonly BindableProperty AutoIndentProperty
Field Value
DocumentProperty
Identifies Document.
public static readonly BindableProperty DocumentProperty
Field Value
FontFamilyProperty
Identifies FontFamily.
public static readonly BindableProperty FontFamilyProperty
Field Value
FontSizeProperty
Identifies FontSize.
public static readonly BindableProperty FontSizeProperty
Field Value
IndentSizeProperty
Identifies IndentSize.
public static readonly BindableProperty IndentSizeProperty
Field Value
IsReadOnlyProperty
Identifies IsReadOnly.
public static readonly BindableProperty IsReadOnlyProperty
Field Value
LineCommentPrefixProperty
Identifies LineCommentPrefix.
public static readonly BindableProperty LineCommentPrefixProperty
Field Value
MaxLengthProperty
Identifies MaxLength.
public static readonly BindableProperty MaxLengthProperty
Field Value
PlaceholderProperty
Identifies Placeholder.
public static readonly BindableProperty PlaceholderProperty
Field Value
SelectedRangeProperty
Identifies SelectedRange.
public static readonly BindableProperty SelectedRangeProperty
Field Value
SelectionStateProperty
Identifies the directional SelectionState.
public static readonly BindableProperty SelectionStateProperty
Field Value
SelectionsProperty
Identifies the two-way bindable Selections property.
public static readonly BindableProperty SelectionsProperty
Field Value
ShowLineNumbersProperty
Identifies ShowLineNumbers.
public static readonly BindableProperty ShowLineNumbersProperty
Field Value
TabSizeProperty
Identifies the native tab-display width, independently of indentation insertion.
public static readonly BindableProperty TabSizeProperty
Field Value
TextColorProperty
Identifies TextColor.
public static readonly BindableProperty TextColorProperty
Field Value
UseTabsProperty
Identifies UseTabs.
public static readonly BindableProperty UseTabsProperty
Field Value
WordWrapProperty
Identifies WordWrap.
public static readonly BindableProperty WordWrapProperty
Field Value
Properties
Adornments
Gets source-anchored views whose content and actions are supplied by the application.
public RichTextAdornments Adornments { get; }
Property Value
AllowVirtualSpace
Gets or sets whether caret movement and rectangular gestures can extend beyond logical line ends.
public bool AllowVirtualSpace { get; set; }
Property Value
AutoIndent
Gets or sets whether Enter copies the current line's leading whitespace.
public bool AutoIndent { get; set; }
Property Value
CanRedo
Gets whether a redo unit is available.
public bool CanRedo { get; }
Property Value
CanUndo
Gets whether an undo unit is available.
public bool CanUndo { get; }
Property Value
CaretPosition
Gets the zero-based logical line and UTF-16 column of the active endpoint.
public TextPosition CaretPosition { get; }
Property Value
Commands
Gets the stable MVVM commands.
public CodeEditorCommands Commands { get; }
Property Value
Composition
Gets authoritative native marked-text state.
public RichTextCompositionState Composition { get; }
Property Value
CompositionSelection
Gets the native provisional selection, or null outside composition.
public TextSelectionState? CompositionSelection { get; }
Property Value
CompositionSnapshot
Gets provisional native source with an invalid edit revision, or null outside composition.
public CodeDocumentSnapshot? CompositionSnapshot { get; }
Property Value
Decorations
Gets independently owned presentation layers in creation order.
public RichTextDecorations Decorations { get; }
Property Value
Document
Gets or sets the stable live source document. Persist code through Text.
public CodeDocument Document { get; set; }
Property Value
Folding
Gets explicit source-range folding. The application owns discovery, UI, and expansion policy.
public RichTextFolding Folding { get; }
Property Value
FontFamily
Gets or sets the monospaced font family.
public string FontFamily { get; set; }
Property Value
FontSize
Gets or sets the font size in device-independent units.
public double FontSize { get; set; }
Property Value
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
InputRules
Gets synchronous rules that extend native source input before one atomic commit.
public TextInputRules InputRules { get; }
Property Value
IsReadOnly
Gets or sets whether user edits and code-editing commands are disabled.
public bool IsReadOnly { get; set; }
Property Value
KeyBindings
Gets the text surface's key bindings, including the editable code defaults.
public EditorKeyBindingCollection KeyBindings { get; }
Property Value
LineCommentPrefix
Gets or sets the prefix used by the native line-comment shortcut.
public string LineCommentPrefix { get; set; }
Property Value
LineCount
Gets the logical line count, including a trailing empty line.
public int LineCount { get; }
Property Value
MaxLength
Gets or sets the maximum UTF-16 source length, or -1 for unlimited.
public int MaxLength { get; set; }
Property Value
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
Placeholder
Gets or sets the empty-source placeholder.
public string Placeholder { get; set; }
Property Value
SelectedRange
Gets or sets the selected UTF-16 range, clamped to the source length.
public TextSpan SelectedRange { get; set; }
Property Value
Selection
Gets the source-only live selection facade.
public CodeTextSelection Selection { get; }
Property Value
SelectionGeneration
Gets a generation that changes when this view's selection changes or maps through a source edit.
public long SelectionGeneration { get; }
Property Value
SelectionState
Gets or sets the anchor and active offsets.
public TextSelectionState SelectionState { get; set; }
Property Value
Selections
Gets or sets every source selection. The primary selection controls native input.
public TextSelectionSet Selections { get; set; }
Property Value
ShowLineNumbers
Gets or sets whether logical line numbers are visible.
public bool ShowLineNumbers { get; set; }
Property Value
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
TextColor
Gets or sets the normal text and line-number color, or null for the native default.
public Color? TextColor { get; set; }
Property Value
TextLayout
Gets visible geometry and hit testing in this composed editor's coordinates.
public RichTextLayout TextLayout { get; }
Property Value
UseTabs
Gets or sets whether indentation inserts tabs instead of spaces.
public bool UseTabs { get; set; }
Property Value
WordWrap
Gets or sets whether long lines wrap. The default is horizontal scrolling.
public bool WordWrap { get; set; }
Property Value
Methods
AddCaret(int, bool)
Adds a source caret, optionally making it the primary native selection.
public void AddCaret(int position, bool makePrimary = true)
Parameters
AddCaretAbove()
Adds a caret on the preceding native visual line.
public bool AddCaretAbove()
Returns
AddCaretBelow()
Adds a caret on the following native visual line.
public bool AddCaretBelow()
Returns
CaptureScrollAnchor()
Captures a live source anchor for restoration in this or another view of the same document.
public CodeScrollAnchor? CaptureScrollAnchor()
Returns
CaptureViewport()
Captures the first visible source caret without moving selection or focus.
public CodeViewportSnapshot? CaptureViewport()
Returns
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
lineintThe 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
positionTextPositionThe 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
offsetintThe 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
Returns
OnPropertyChanged(string?)
protected override void OnPropertyChanged(string? propertyName = null)
Parameters
propertyNamestring
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
textstring
Returns
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
anchorCodeScrollAnchorcauseTokenobjectcancellationTokenCancellationToken
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
positionintalignmentCodeRevealAlignmentcauseTokenobjectcancellationTokenCancellationToken
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
rangeTextSpanThe 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
Returns
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
revisionDocumentRevisionThe captured source revision.
editsIReadOnlyList<TextReplacement>Original-coordinate source replacements.
selectionAfterTextSelectionState?The resulting directional selection, or null to map the current selection.
optionsTextEditOptionsUndo and application metadata.
expectedSelectionGenerationlong?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
ContentChanged
Occurs after an atomic source-document change.
public event EventHandler<TextDocumentChangedEventArgs>? ContentChanged
Event Type
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
DocumentChanged
Occurs after a replacement source document is attached and view state is synchronized.
public event EventHandler<CodeDocumentReplacedEventArgs>? DocumentChanged
Event Type
InputRejected
Occurs when rule failure or a conflicting plan rejects native input.
public event EventHandler<RichTextInputRejectedEventArgs>? InputRejected
Event Type
KeyDown
Occurs before built-in hardware-key handling, except while native text composition is active.
public event EventHandler<EditorKeyEventArgs>? KeyDown
Event Type
Pasting
Occurs before committing plain source text from the clipboard.
public event EventHandler<TextPastingEventArgs>? Pasting
Event Type
PointerExited
Occurs when a hovering pointer exits the text surface.
public event EventHandler? PointerExited
Event Type
PointerMoved
Observes local pointer movement without changing native input behavior.
public event EventHandler<RichTextPointerEventArgs>? PointerMoved
Event Type
PointerPressed
Observes local pointer presses without suppressing selection or focus.
public event EventHandler<RichTextPointerEventArgs>? PointerPressed
Event Type
SelectionChanged
Occurs after the caret or selection changes.
public event EventHandler<RichTextSelectionChangedEventArgs>? SelectionChanged
Event Type
SelectionsChanged
Occurs after primary or secondary selections change.
public event EventHandler? SelectionsChanged
Event Type
TextChanged
Occurs after logical source text changes.
public event EventHandler<TextDocumentChangedEventArgs>? TextChanged
Event Type
ViewportChanged
Occurs when native scrolling, layout, or edits change the source-based viewport.
public event EventHandler<CodeViewportChangedEventArgs>? ViewportChanged