Skip to content

Add a Code mode to the Edit Text Window (#645) - #710

Merged
TheJoeFin merged 17 commits into
devfrom
spike/645-code-mode
Nov 14, 2026
Merged

TheJoeFin merged 17 commits into
devfrom
spike/645-code-mode

Conversation

@TheJoeFin

Copy link
Copy Markdown
Owner

Refs #645

Adds a Code editor mode to the Edit Text Window, beside Raw Text, Spreadsheet and Markdown: the text in an AvalonEdit editor with syntax highlighting, line numbers, bracket and tag folding, and the same smooth scrolling as the other editors. It started as a spike, so the gaps are listed at the end.

What it does

  • Format → Code, plus an "Enter Code Mode" bottom-bar button. In Code mode, Format → Code Language and a language button in the status bar pick the highlighting.
  • Language comes from, in order: the opened file's extension, the history entry, the person's pick, or a heuristic detector for pasted and OCR'd code (CodeLanguageDetector in Core). If nothing scores high enough, it stays plain text.
  • Code files open in Code mode: .cs, .json, .xml, .xaml, .py, .js, .ps1, .sql and the other extensions in CodeLanguages. This changes behavior: those files opened as Raw Text before. IoUtilitiesTests had a row pinning .json to Text; it now expects Code.
  • Dark theme: AvalonEdit's colors are made for white backgrounds. On dark, each color keeps its hue and is lightened until it reaches 4.5:1 contrast (CodeHighlightColors).
  • Folding: brackets for C#, Java, C/C++, JavaScript, JSON, CSS, PHP and PowerShell (strings, comments and character literals are skipped), and tags for XML/XAML and HTML. The tag rule is a forgiving scanner, not AvalonEdit's XmlFoldingStrategy, which stops at the first XML error and so fails on ordinary HTML and on OCR'd markup. Showing a range (Find & Replace) expands any collapsed region hiding it.
  • Shortcuts: AvalonEdit's Ctrl+D (delete line) and Ctrl+I (indent) are removed so the window's Duplicate Line and Isolate Selection keep working.

How it fits the editor session (E1)

The raw text box stays the document, as in every mode. The code editor holds the same characters, so:

  • an edit typed in the code editor is applied to the text box as the same replacement (EditorSession.ApplyEditFromCodeEditor), not a whole-text copy per keystroke;
  • a change made through the session (transforms, Find & Replace, grabs, transcription) reaches the code editor as the smallest replacement (TextEdit.Between), so its caret, scroll position, undo history and collapsed regions survive;
  • the selection is mirrored both ways, so every existing selection-based command works on what's selected in the code editor.

CodeSurface is the fourth IEditorSurface, and EtwEditorMode gains Code. This is relevant to decision D3 in docs/v5/README.md: AvalonEdit can sit behind the session as one more surface without touching the rest of the window.

Needs checking before release

  • New NuGet package: AvalonEdit 6.3.1.120 (MIT, ~620 KB). The MSIX package build has to be checked in Visual Studio; it can't be built from the CLI. ThirdPartyNotices / the licenses window need an entry.
  • History in older builds: history is saved with string enums, so an entry saved with EditorMode: "Code" can't be read by an older build. From the code, its item-by-item recovery should drop just those entries; not tested.
  • Touch panning on the code editor (it now has the same touch handling as the text box, but AvalonEdit does its own mouse-based selection).

Not done yet

  • Calc pane scrolling doesn't follow the code editor.
  • Use Margins has no effect in Code mode.
  • No spell check.
  • Shorter right-click menu: a fixed list, without the spelling, link and email items the text box adds.
  • Font: Code mode always uses a monospace font (Cascadia Mono); it follows the window's font size and Ctrl+scroll zoom.
  • Folding gaps:
    • No rules for Python, SQL or Visual Basic, and no #region folding.
    • No keyboard shortcuts or Collapse All / Expand All.
    • C# @$"…" strings, PowerShell here-strings and C++ raw strings can confuse the bracket rule if they contain an unmatched bracket.
  • Short snippets aren't detected: a few lines (class A { int x; }) stay plain text until a language is picked.

Tests

  • New tests:
    • Core: language detection, extension lookup, the minimal text diff, the dark-theme colors, bracket and tag folding.
    • App: typing in the code editor updates the text and caret; a transform applies to the code editor's selection; a whole-text change is one undoable edit; selection mirrors both ways; mode switches keep edits; history restores the language; Ctrl+D/Ctrl+I go to the window; folding follows edits and keeps collapsed regions; showing a range expands it; the editor scrolls in pixels.
  • Totals: Tests.exe 1,020 (3 existing skips), Tests.Core.exe 1,254, Tests.Core.Windows.exe 322 (4 existing skips), all passing.
  • Manual: opened a .cs file in the app (isolated automation profile, dark theme): it opened in Code mode with C# detected, highlighting readable. Light-theme highlighting and folding were checked from offscreen renders, not in the running app.

🤖 Generated with Claude Code

TheJoeFin and others added 17 commits November 11, 2026 22:14
A fourth editor mode beside Raw Text, Spreadsheet and Markdown: the text in
an AvalonEdit editor with syntax highlighting and line numbers.

- The raw text box stays the document. The code editor holds the same
  characters, so the two sync edit by edit in both directions, and the
  selection is mirrored, so every existing command works in code mode.
- The language comes from the file extension, history, the person's pick
  (Format > Code Language, or the status-bar button), or a heuristic
  detector in Core for pasted and OCR'd code.
- On the dark theme AvalonEdit's light-theme colors are remapped to keep
  their hue and reach 4.5:1 contrast.
- Code file extensions (.cs, .json, .py, .xml, ...) now open in Code mode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AvalonEdit draws line numbers against the left edge and the code against
the gutter's dotted line, and its current-line border defaults to a
translucent green that setting null never replaces.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collapse/expand markers in the code editor's gutter, using AvalonEdit's
folding manager with folding rules written in Core:

- Brackets for C#, Java, C/C++, JavaScript, JSON, CSS, PHP and PowerShell.
  Brackets in strings, comments and character literals are skipped. When a
  brace starts its own line, the fold starts at the end of the header above
  it, so the collapsed line still shows the signature.
- Tags for XML and HTML: elements, multi-line start and self-closing tags,
  and comments. A forgiving scanner rather than AvalonEdit's
  XmlFoldingStrategy, which stops at the first XML error and so fails on
  ordinary HTML and on OCR'd markup.

Regions are recomputed 400 ms after the text stops changing, and once on
entering Code mode. Showing a range (Find, Go to) expands any collapsed
region hiding it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Edit Text Window's SmoothScrollBehavior (eased wheel scrolling, touch
panning with flick inertia) works on AvalonEdit unchanged: its text area
scrolls in pixels, so each frame of the ease moves it a little instead of
AvalonEdit's default three-line jump per wheel notch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… Code mode (#645)

Install AvalonEdit's SearchPanel (Ctrl+F), add a Show Whitespace toggle,
shade other occurrences of the selection, outline matching brackets
(BracketMatcher in Core, with tests), and use brace-aware auto-indent
for C-like languages.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
A JSON array of objects folded only every other item: an object whose brace
sits on its own line started its fold at the end of the previous line, which
for array siblings is the previous item's closing brace, so neighbouring folds
overlapped. A line ending in a comma or an opening bracket is no longer treated
as a header to keep visible.

The folding scan now runs on a worker thread and is applied only if the text
is unchanged, so large documents no longer freeze the UI (#645).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Replace AvalonEdit's find bar template with a themed one: match case, whole
word and regex toggles plus vector previous/next/close icons, colored from the
editor's palette. The editor's Transparent background is #00FFFFFF, so it is
only used as a base when opaque; otherwise a dark or light default applies.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Opening a file whose text was already showing left the code editor
holding older text, which OpenPath then copied back into the document
and recorded as saved. Opening now always loads the code editor.

Entering Code mode and opening a file also left the fill on AvalonEdit's
undo stack, so Ctrl+Z emptied the document or brought back the previous
file. Both now start the code editor with no undo history.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Entering Code mode always folded on the UI thread, whatever the size,
and setting the language scanned the editor's old text first, with a
third scan after the typing delay. UpdateFoldings now sends documents
over 50k characters to a worker thread and skips a scan already done or
under way for the same text, language and folding manager.

Entering Code mode and opening a file load the text before setting the
language, so each does one scan, of the text being shown. A background
scan's result is also dropped if the language changed meanwhile.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
History saved Plain Text as a null language, the same as no choice, so
a restored window detected a language again and overrode the person's
choice. Plain Text is now saved as "". Restoring a Recent text history
item into an empty window also ignored the saved language entirely;
it now restores it the same way the history constructor does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A line of 1.6M characters (a report's embedded data) froze the editor
for good when it scrolled into view: AvalonEdit highlighted it into
some 100,000 sections in seconds, then applied them in quadratic time,
and WPF alone takes 16 s to lay out a line that long.

Lines over 10,000 characters now show only that much, followed by a
marker for the rest; the text stays in the document. They are also not
highlighted: LongLineHighlighter, adapted from AvalonEdit's
DocumentHighlighter, leaves them plain and carries the highlighting
state across them unscanned. Folds starting in a hidden part are not
offered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ApplyEditFromCodeEditor left the text box caret at the end of the edit,
a guess that is wrong for any change that doesn't move the caret, such
as text inserted after the selection. It now takes the code editor's
own selection, which AvalonEdit has already updated when the window
hears of the change.

In practice AvalonEdit 6.3 hid the wrong guess: its caret never resets
the flag that raises PositionChanged after an update, so the window
re-mirrored the real selection after every edit. The sync no longer
depends on that.

Adds TextSpan.ClampTo for the clamping that was written out in three
places.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With the Code mode find bar open, Shift+F3 goes to the previous match,
as the find bar's own button says, but the Code context menu and the
Edit menu still showed it as Toggle Case's shortcut. Both menus now
leave the shortcut out while the find bar is open. The key itself is
unchanged: taking it from the find bar would change the text of
someone stepping back through matches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The bracket highlight redraws on every caret move, keystroke and
scroll, and each draw copied up to 40,000 characters around the caret
into a new string before checking for a bracket. BracketMatcher.Find
now also reads through a character accessor, and the renderer hands it
the document's GetCharAt: most draws read just the two characters
beside the caret, and none copies the text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
EditorSession.ScrollToEnd checked for the concrete CodeSurface type to
scroll the code editor, so any other code surface, such as a test fake,
was silently skipped. ScrollToEnd is now on IEditorSurface, and the
session scrolls the text box and then the visible editor. Markdown and
spreadsheet do nothing, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drops the editor's own brace-language list, which left PowerShell folded
by braces but not indented by them. A test keeps the two in step.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@TheJoeFin
TheJoeFin merged commit 2bc0599 into dev Nov 14, 2026
1 check passed
@TheJoeFin
TheJoeFin deleted the spike/645-code-mode branch November 14, 2026 15:24
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.

1 participant