public sealed class CodeEditorA text editor for source: coloured, numbered, foldable and virtualised.
No guide page documents this yet — the page shows what the code says about itself.
Remarks
Monospace by construction. A column is turned into an x by multiplying, which is what makes hit testing, the caret, the selection and the scroll width arithmetic rather than a per-line measurement — and it is what every code editor has ever done. ⚠ Given a proportional font the caret lands in the wrong place, and the theme therefore names one: a game that overrides code-editor's font-family with a proportional face has broken the caret, not the colours.
Virtualised on lines, like TreeView is on rows. A fifty-thousand-line shader is fifty thousand strings and about forty CodeLines. Folding is expressed in the same place: a collapsed region is lines missing from the row list, so nothing below has to know that folding exists.
⚠ Highlighting state is cached per line and invalidated from the edit downwards. A block comment opened on line 3 changes what line 4 000 is, so the state has to be carried forward — and recomputing the whole file on every keystroke is what makes a highlighter feel slow. Editing line n throws away the states from n on and nothing above it.
⚠ No undo. See CodeBuffer — an undo stack inside a text control can only undo typing, and every application that has one wants it to cover more than that. Changed is the seam.
Fields and properties (32)
public const int OverscanHow many lines are realised above and below the viewport.
protected override string TagNameThe element name this type answers to when a caller does not choose one.
protected override bool AcceptsFocusWhether the focus can rest on this kind of control at all.
public CodeBuffer BufferThe text being edited.
public ICodeTokenizer TokenizerWhat turns a line into colours.
public string SourceThe whole text, for the caller who does not want the buffer.
public UiElement GutterThe column of numbers and markers down the left.
public ScrollView ScrollerThe scroller the lines live in.
public UiElement LinesWhere the realised lines go.
public UiElement CompletionThe autocomplete popup.
public IReadOnlyList<CodeLine> PoolThe lines that exist as elements, including the parked ones.
public IReadOnlyList<int> RowsWhich buffer line each visible row shows, folding taken out.
public TextPosition CaretWhere the caret is.
public TextPosition AnchorThe other end of the selection. Equal to the caret when nothing is selected.
public bool HasSelectionWhether anything is selected.
public string SelectedTextThe selected text, or an empty string.
public IReadOnlyList<CodeDiagnostic> DiagnosticsWhat is being said about the file.
public IReadOnlyList<CodeFold> FoldsThe regions that can be collapsed.
public IReadOnlyList<CompletionItem> CompletionsWhat the popup is offering, filtered.
public int CompletionIndexWhich of them is highlighted.
public bool IsCompletingWhether the popup is up.
public int TabSizepublic bool AutoIndentpublic bool AutoFoldpublic bool ReadOnlypublic Func<CodeEditor, string, IReadOnlyList<CompletionItem>>? CompletionProviderWhat to offer when a completion is asked for.
public float RowHeightHow tall one row of the virtualiser is.
public float CharacterWidthHow wide one character is.
public static readonly UiPropertyKey TabSizePropertyIdentity for TabSize.
public static readonly UiPropertyKey AutoIndentPropertyIdentity for AutoIndent.
public static readonly UiPropertyKey AutoFoldPropertyIdentity for AutoFold.
public static readonly UiPropertyKey ReadOnlyPropertyIdentity for ReadOnly.
Events (3)
public Action<CodeEditor>? CaretMovedRaised after the caret or the selection moves.
public Action<CodeEditor>? TextChangedRaised after the text changes.
public Action<CodeEditor, CompletionItem>? CompletionAcceptedRaised when a completion is accepted.
Methods (22)
protected override void OnCreated()Builds whatever this element is made of, once, as it joins a document.
public void Refresh()Rebuilds the row list and realises the lines for it.
public void SetDiagnostics(params ReadOnlySpan<CodeDiagnostic> items)Replaces what is being said about the file.
public CodeFold? FoldAt(int line)The fold that starts on a line, if one does.
public bool IsCollapsed(int line)Whether a fold is collapsed.
public bool ToggleFold(int line)Collapses or expands the fold that starts on a line.
public void SetFolds(params ReadOnlySpan<CodeFold> regions)Replaces the foldable regions, turning the automatic ones off.
public int RowOf(int line)Which visible row a buffer line is on, or -1 if it is folded away.
public Vector2 ToScreen(TextPosition position)Where a place in the text is, in document space.
public TextPosition ToPosition(float x, float y)Which character a point is over.
public void Move(TextPosition position, bool extend = false)Puts the caret somewhere, optionally dragging the selection with it.
public void SelectAll()Selects everything.
public void Insert(string text)Replaces the selection, or inserts at the caret if there is none.
public void Erase(bool forward)Removes the selection, or one character either side of the caret.
public void Reveal()Scrolls until the caret is on screen.
public void ShowCompletion()Asks the provider what fits and shows the popup, if anything does.
public void HideCompletion()Takes the popup down.
public bool AcceptCompletion()Puts the highlighted completion in, replacing the word being typed.
public void ClearTabSize()Unsets TabSize, so it takes its default or its ancestor's value again.
public void ClearAutoIndent()Unsets AutoIndent, so it takes its default or its ancestor's value again.
public void ClearAutoFold()Unsets AutoFold, so it takes its default or its ancestor's value again.
public void ClearReadOnly()Unsets ReadOnly, so it takes its default or its ancestor's value again.
Used by (7)
- CodeEditorTestsVixen.Ui.Controls.Advanced.Tests
- CodeOverlayVixen.Ui.Controls.Advanced
- ResizeTestsVixen.Ui.Controls.Advanced.Tests
- CodeEditorViewVixen.Editor.AssetEditors
- ShaderGraphSurfaceTestsVixen.Editor.App.Tests
- ShaderGraphViewVixen.Editor.AssetEditors
- ShaderGraphViewTestsVixen.Editor.AssetEditors.Tests