Skip to content

Cap rendering and editing of extremely long lines - #607

Merged
maxkatz6 merged 2 commits into
AvaloniaUI:masterfrom
ollieb-unity:cap-long-line-rendering
Oct 26, 2026
Merged

maxkatz6 merged 2 commits into
AvaloniaUI:masterfrom
ollieb-unity:cap-long-line-rendering

Conversation

@ollieb-unity

Copy link
Copy Markdown
Contributor

Problem

A single very long line — e.g. a minified blob, or a Unity YAML asset with an inline binary payload serialized as hex — freezes the UI thread as it scrolls into view. Two full-line passes run synchronously: TextMate tokenizes and colorizes the whole line, and TextFormatter shapes every character.

Change

  • VisualLine shapes only the first LENGTH_LIMIT characters and collapses the remainder into a short ... marker via a FormattedTextElement — the same mechanism already used for collapsed fold regions. Document offsets are unchanged, so caret, selection and highlighting keep working.
  • TextMateColoringTransformer skips tokenizing/colorizing lines past LENGTH_LIMIT.
  • New LongLineEditProtection disallows edits inside the collapsed tail; TextArea routes CanInsert / GetDeletableSegments through it so callers cannot insert into or delete the hidden region (deleting the line delimiter would otherwise merge the next line into the hidden text).

This builds on the existing VisualLine.LENGTH_LIMIT (3000) short-circuit, which previously still added a single full-length VisualLineText for over-limit lines.

Builds cleanly against net8.0 and net10.0.

A single very long line (e.g. a minified blob or a Unity YAML asset with an
inline binary payload serialized as hex) froze the UI thread as it scrolled
into view: TextMate tokenized and colorized the whole line, and TextFormatter
shaped every character.

- VisualLine shapes only the first LENGTH_LIMIT characters and collapses the
  remainder into a short "..." marker (a FormattedTextElement, the same
  mechanism used for collapsed fold regions). Document offsets are unchanged,
  so caret, selection and highlighting keep working.
- TextMateColoringTransformer skips tokenizing/colorizing lines past
  LENGTH_LIMIT.
- New LongLineEditProtection disallows edits inside the collapsed tail, and
  TextArea routes CanInsert/GetDeletableSegments through it so callers cannot
  insert into or delete the hidden region (deleting the delimiter would merge
  the next line into the hidden text).

Co-authored-by: Daniel Penalba <daniel.penalba@unity3d.com>
@danipen
danipen self-requested a review August 7, 2026 11:17

@danipen danipen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@danipen

danipen commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

@maxkatz6 can you merge this one? Solving the conflicts now...

Resolve VisualLine conflict: long lines run the RunsOnLongLines generators
(tab layout) over the first LENGTH_LIMIT characters, then collapse the rest
into the LENGTH_LIMIT_MARKER element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danipen

danipen commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Done!

@maxkatz6
maxkatz6 merged commit 7b9835c into AvaloniaUI:master Oct 26, 2026
5 checks passed
OleRoss added a commit to rosslight/Darp.CodeEditor that referenced this pull request Nov 5, 2026
## Summary

The editor package had its document model but no control. This PR
implements `CodeEditor`, `CodeEditorStyles` and `CodeEditorViewState` on
AvaloniaEdit, with TextMate highlighting that colors the visible lines
first and tokenizes the rest in the background.

```xml
<Application.Styles>
  <FluentTheme />
  <ce:CodeEditorStyles />
</Application.Styles>
```

```csharp
var editor = new CodeEditor { Document = CodeDocument.Create(CodeLanguage.Json, json) };
CodeEditorViewState state = editor.SaveViewState();   // before switching documents
editor.RestoreViewState(state);                       // follows the edits made since
```

![JSON in Light+, YAML in Dark+ inside a ThemeVariantScope, and XML in
Light+](https://github.com/user-attachments/assets/7ec39bda-8d02-4d7c-8c28-09faf7a0a545)

| The context menu takes the editor's theme variant | Typing recolors at
once |
| --- | --- |
| ![The context menu in the dark editor, in Fluent's dark
look](https://github.com/user-attachments/assets/89ab3275-5853-4dba-99d8-c252e94780da)
| ![Typing a property, an array and a comment in the JSON
editor](https://github.com/user-attachments/assets/1238e731-719d-489e-8968-0a8a78816822)
|

## Changes

- Where to look: `CodeEditor.cs` (showing a document only while in the
visual tree, disposal, view state, `ScrollTo`), `Highlighting/` (the
background highlighter, one TextMate registry per editor) and
`CodeEditorStyles.axaml`.
- AvaloniaEdit comes from a preview feed, `12.0.999-ci3-alpha`, with
three fixes not yet released upstream: each control uses its own
dispatcher, the undo history no longer keeps editors alive, and the last
focused element is held weakly. Restoring it needs a token with
`read:packages` (`CONTRIBUTING.md`); CI reads
`AVALONIAEDIT_PACKAGES_TOKEN`.
- Long lines: the background tokenizes any line within 50 ms; a line
that runs out of time stays uncolored and the next one starts from the
grammar's start. One slow call on the UI thread leaves only that line to
the background.
- The built-in XML grammar now tokenizes: VS Code's file has begin rules
without an end, which are read the way vscode-textmate reads them.
TextMateSharp is updated to 2.0.5.
- `ScrollTo`'s doc now says "taller or wider than the view".

### Benchmarks

The latency budgets of `docs/design.md` 1.2 are tracked by benchmarks,
not asserted by tests (decision 102). Pull requests compare only the
`CI` category against the target branch, six cases instead of 21:

| Benchmark | Guards |
| --- | --- |
| `FirstFrameBenchmarks.FirstColoredFrame` | highlighted text soon after
creating an editor |
| `TypingBenchmarks.KeystrokeIn5000Lines` | the UI thread while typing |
| `TypingBenchmarks.KeystrokeOnA20000CharacterLine` | the long-line
freeze the prototype had |
| `ScrollingBenchmarks.PageDown` | the UI thread while scrolling |
| `HighlightingBenchmarks.TokenizeAllLinesWarm` | background tokenizing
throughput |
| `DocumentBenchmarks.EditAndSnapshot` | the cost of one edit |

`--filter '*'` still runs all of them locally (`CONTRIBUTING.md`). Main
has no editor yet, so this PR's benchmark comment shows the editor
benchmarks without a comparison; later PRs compare them. The workflow
now packs the target branch outside the checkout, so that it restores
with its own `nuget.config`.

## Testing

- Added tests for the editor's contracts: selection, caret position,
`ScrollTo`, view state across edits, read-only, clipboard, keys reaching
the app, disposal (also while attaching), two editors on one document,
collection after removal, themes by variant and fallback, and
highlighting of each built-in language. They pass with `dotnet test` and
as a NativeAOT executable.
- Ran the editor in a desktop app on Linux, as in the screenshots:
highlighting, typing, the context menu, and a `ThemeVariantScope`.
- Not covered: editors on a second UI thread (decision 100 stays), and
the first editor of a fresh JIT process, which takes about 450 ms,
mostly Avalonia's and AvaloniaEdit's first use (decision 103).

## Impact

- Restoring the repository needs the preview feed's token until
AvaloniaEdit releases the fixes. Pull requests from forks can't restore
it.
- Lines over 3,000 characters show only their start and can't be edited
past it: AvaloniaEdit's upstream AvaloniaUI/AvaloniaEdit#607, which the
preview includes. An upstream option to raise the limit is in progress.

## Checklist

- [x] Documentation (and changelog) is updated if needed
- [x] CI Pipeline is successful
- [x] (Human) Self-review was done

PR generated by claude-opus-5-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants