Skip to content

Wire xshd colorizer through TG Scheme code-token VisualRoles (theme/palette layer) #132

@tig

Description

@tig

Overview

Wire the Editor's xshd syntax-highlighting colorizer through Terminal.Gui's Scheme / VisualRole system, so the Editor participates in TG's ThemeManager-driven theme story alongside MarkdownCodeBlock and the new Code view. Closes #99 (theme switching for syntax highlighting) and #128 (unreadable JSON colors on dark terminals).

This is the Editor-side consumer of a larger cross-repo plan. The TG work happens first; this issue captures the Editor work that follows.

Plan & spec

The full plan, including phase ordering, decision log, palette tables, file lists, definitions of done, and out-of-scope notes, lives in specs/syntax-theme/spec.md on the develop branch. Implementation will happen on the existing code-themes branch and worktree at ../Editor-code-themes.

Cross-repo dependencies

This issue is blocked by both:

  • gui-cs/Terminal.Gui#5310 — adds the ConfigurationManager / Scheme / Theme benchmark baseline TG needs before code-role storage can be evaluated for regression.
  • gui-cs/Terminal.Gui#5311 — adds the 12 VisualRole.Code* entries, matching Scheme storage, populated Dark / Light themes, a new public Code view, and unified markdown-highlighter routing. This issue's PR bumps <TerminalGuiVersion> to the release that ships #5311.

Implementation on the code-themes branch in this repo can begin in parallel with TG #5311 — anything that references VisualRole.CodeXxx won't compile until a TG release with #5311 ships, but the role-mapping table, xshd loader plumbing, colorizer rewrite, and tests can all be drafted against the agreed-on enum names.

Scope (this repo, Phase 2 in the spec)

Bridge xshd named colors to VisualRoles

New src/Terminal.Gui.Editor/Highlighting/XshdRoleMap.cs — a built-in Dictionary<string, VisualRole> keyed by xshd <Color name="..."> values, covering common cross-language names (Comment, Keyword/Keywords, String/Char/Character, NumberLiteral/Digits/Number, Operators, Preprocessor, Punctuation, MethodCall/MethodName, ValueTypeKeywords/ReferenceTypeKeywords/DataTypes, FieldName/Variable, TrueFalse/Bool/Null/NullOrValueKeywords, JavaDocTags/KnownDocTags/Attributes, plus the language-categorized variants — see spec §6.2 for the full draft table). Names absent from the table fall through to the xshd-declared color (today's behavior).

Let xshd entries override the bridge per-color

Add an optional category="..." attribute to xshd's <Color> element and a VisualRole? Role property on the runtime HighlightingColor. At load time, populate Role from category= if present; else from XshdRoleMap.TryGetRole(name); else null. Log the new xshd attribute as an intentional fork addition in third_party/AvaloniaEdit/UPSTREAM.md.

Route the colorizer through the active scheme

Rewrite HighlightingColorizer.ToAttribute. Resolution order:

  1. If color.Role is non-null AND the editor's current scheme has an explicit (non-derived) Attribute for that role, return it.
  2. Else fall back to xshd's declared Foreground paired with the editor's scheme background.
  3. Else return the editor's default attribute.

The constructor gains a Func<VisualRole, Attribute>? getRoleAttribute parameter, supplied by Editor as role => GetAttributeForRole(role).

Retire Editor.UseThemeBackground

Remove the property at src/Terminal.Gui.Editor/Editor.cs:231-245, the _useThemeBackground field on HighlightingColorizer, and the useThemeBackground parameter on VisualLineBuildContext. The theme decides backgrounds now; pre-alpha — no compat shim.

Repaint on theme changes

Editor hooks ThemeManager.ThemeChanged (and SchemeChanged if exposed). On either event, invalidate visual-line caches and redraw.

Add a Theme switcher to the ted demo

examples/ted/TedApp.cs gains a Theme Shortcut in the status bar next to the existing Language shortcut. Click cycles ThemeManager.Theme across the TG-shipped theme names; title updates to the current theme.

Files

See spec §"Files in Scope" → "Editor repo (Phase 2)" for the full list. Headlines:

  • Directory.Build.props — bump <TerminalGuiVersion>
  • src/Terminal.Gui.Editor/Highlighting/XshdRoleMap.csNEW
  • src/Terminal.Gui.Editor/Highlighting/HighlightingColor.cs — add VisualRole? Role
  • src/Terminal.Gui.Editor/Highlighting/Xshd/XshdColor.cs — add Category property
  • src/Terminal.Gui.Editor/Highlighting/Xshd/V2Loader.cs (or equivalent) — read category=
  • src/Terminal.Gui.Editor/Rendering/HighlightingColorizer.cs — rewrite ToAttribute
  • src/Terminal.Gui.Editor/Rendering/VisualLineBuildContext.cs — drop useThemeBackground param
  • src/Terminal.Gui.Editor/Editor.cs — remove UseThemeBackground; wire theme/scheme-changed handlers
  • examples/ted/TedApp.cs — add Theme Shortcut to status bar
  • tests/Terminal.Gui.Editor.Tests/HighlightingTests.cs — new tests
  • tests/Terminal.Gui.Editor.IntegrationTests/ — theme-swap smoke test
  • third_party/AvaloniaEdit/UPSTREAM.md — log category= addition

Definition of Done

  • TG #5311 merged and released; <TerminalGuiVersion> bumped to that release
  • dotnet run --project tests/Terminal.Gui.Editor.Tests passes incl. new tests:
    • Colorizer uses scheme code-role when the theme defines it
    • Colorizer falls back to xshd-declared color when the theme does not override
    • XshdRoleMap covers the common cross-language names from spec §6.2
    • category= attribute on an xshd <Color> wins over the default table
  • dotnet run --project tests/Terminal.Gui.Editor.IntegrationTests passes incl. theme-swap smoke test
  • ted foo.cs and ted foo.json are readable on both dark and light terminals (manual)
  • Theme Shortcut in ted cycles ThemeManager.Theme and lines re-render live
  • Editor.UseThemeBackground removed; no callers remain
  • dotnet format Terminal.Gui.Editor.slnx --exclude third_party/ --verify-no-changes clean
  • dotnet jb cleanupcode Terminal.Gui.Editor.slnx --profile="TG.Editor Full Cleanup" clean
  • Issues Add theme/palette layer for syntax highlighting colors #99 and default json colors are unreadable in ted #128 closed

Out of Scope

See spec §"Out of Scope". Highlights:

  • Editing the 14 xshd resource files. The role table covers them. Per-language category= overrides only if a specific case proves wrong; logged in UPSTREAM.md per fork policy when made.
  • Per-Editor HighlightingTheme property. TG's ThemeManager.Theme is the single switch.
  • TextMate grammar palette bridge for Editor. Ships with the post-alpha textmate-grammars work (per DEC-002), not here.
  • Themable Markdown structural tokens (Markup* role family). Deferred until a concrete consumer arises.
  • Bold/italic/underline overrides via theme. Future enhancement.

Working branch

Implementation work happens on branch code-themes, worktree at ../Editor-code-themes. The spec also lives there as specs/syntax-theme/spec.md. Push and PR when TG #5311 ships and the Editor work is ready for review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions