Vixen
02b45cc4
csharp
public sealed class CodeEditor

A 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 Overscan

    How many lines are realised above and below the viewport.

  • protected override string TagName

    The element name this type answers to when a caller does not choose one.

  • protected override bool AcceptsFocus

    Whether the focus can rest on this kind of control at all.

  • public CodeBuffer Buffer

    The text being edited.

  • public ICodeTokenizer Tokenizer

    What turns a line into colours.

  • public string Source

    The whole text, for the caller who does not want the buffer.

  • public UiElement Gutter

    The column of numbers and markers down the left.

  • public ScrollView Scroller

    The scroller the lines live in.

  • public UiElement Lines

    Where the realised lines go.

  • public UiElement Completion

    The autocomplete popup.

  • public IReadOnlyList<CodeLine> Pool

    The lines that exist as elements, including the parked ones.

  • public IReadOnlyList<int> Rows

    Which buffer line each visible row shows, folding taken out.

  • public TextPosition Caret

    Where the caret is.

  • public TextPosition Anchor

    The other end of the selection. Equal to the caret when nothing is selected.

  • public bool HasSelection

    Whether anything is selected.

  • public string SelectedText

    The selected text, or an empty string.

  • public IReadOnlyList<CodeDiagnostic> Diagnostics

    What is being said about the file.

  • public IReadOnlyList<CodeFold> Folds

    The regions that can be collapsed.

  • public IReadOnlyList<CompletionItem> Completions

    What the popup is offering, filtered.

  • public int CompletionIndex

    Which of them is highlighted.

  • public bool IsCompleting

    Whether the popup is up.

  • public int TabSize
  • public bool AutoIndent
  • public bool AutoFold
  • public bool ReadOnly
  • public Func<CodeEditor, string, IReadOnlyList<CompletionItem>>? CompletionProvider

    What to offer when a completion is asked for.

  • public float RowHeight

    How tall one row of the virtualiser is.

  • public float CharacterWidth

    How wide one character is.

  • public static readonly UiPropertyKey TabSizeProperty

    Identity for TabSize.

  • public static readonly UiPropertyKey AutoIndentProperty

    Identity for AutoIndent.

  • public static readonly UiPropertyKey AutoFoldProperty

    Identity for AutoFold.

  • public static readonly UiPropertyKey ReadOnlyProperty

    Identity for ReadOnly.

Events (3)

  • public Action<CodeEditor>? CaretMoved

    Raised after the caret or the selection moves.

  • public Action<CodeEditor>? TextChanged

    Raised after the text changes.

  • public Action<CodeEditor, CompletionItem>? CompletionAccepted

    Raised 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