Skip to content

feat: grammar-based value coloring behind the experimental flag#497

Open
SJrX wants to merge 5 commits into
242.xfrom
grammar-value-coloring
Open

feat: grammar-based value coloring behind the experimental flag#497
SJrX wants to merge 5 commits into
242.xfrom
grammar-value-coloring

Conversation

@SJrX

@SJrX SJrX commented Jun 27, 2026

Copy link
Copy Markdown
Owner

What

Role-based, one-span coloring of option values, driven by the grammar. When the experimental engine is on, each value is parsed and its Labeled spans (e.g. a whole IPv4/IPv6 address as one literal) and per-token roles are colored — instead of the flat single color the lexer gives today.

Re-cut of the stacked value-coloring PRs against current 242.x:

Gating

GrammarValueColorAnnotator returns early unless ExperimentalSettings.useGrammarParseEngine is set — off by default, like the other grammar-engine features. No change to default coloring.

Scope notes

Tests

ColoringTest (engine: regions/roles from colorize) and GrammarValueColorAnnotatorTest (end-to-end). GrammarParseEngineInspectionTest now filters INFORMATION highlights so it counts inspection problems, not coloring.

Verification

./gradlew test and ./gradlew test -Dsystemd.unit.grammarParseEngine=true both pass.

🤖 Generated with Claude Code

Steve Ramage and others added 5 commits June 27, 2026 08:07
…ed (#467 #342)

Adds the (pure, IntelliJ-free) substrate for grammar-based syntax coloring. Coloring
is automatic and needs NO grammar changes; an optional wrapper handles composite spans.

- Role { ENUM, LITERAL, OPERATOR, IDENTIFIER } + Region(start, end, role).
- defaultRole(terminal): IntegerTerminal -> LITERAL; Literal/FlexibleLiteralChoice ->
  ENUM, or OPERATOR when all choices are punctuation (":", "+", "=", "~", "/", ...);
  RegexTerminal -> IDENTIFIER; whitespace/other -> uncoloured.
- Labeled(role, inner): optional, transparent wrapper. Matching is delegated to inner
  unchanged (SyntacticMatch/SemanticMatch/parse), so it affects only colour — e.g.
  Labeled(LITERAL, IPV4_ADDR) paints 127.0.0.1 as one literal instead of per-octet.
- Parse gains a defaulted `regions` field; the merging combinators (Seq, ZeroOrMore,
  OneOrMore, Repeat) thread it. colorize(value) returns labeled regions plus per-token
  defaults for anything not inside a labeled region.

Role -> TextAttributes mapping is deliberately left to the IntelliJ layer (next MR:
annotator + color settings, behind the experimental flag).

Tests: default roles; automatic colouring of the unchanged RestrictAddressFamilies
grammar (operator/enum, whitespace uncoloured); Labeled collapsing an IPv4 address to
one LITERAL; and Labeled being transparent to validation.

Refs #467 #342

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The rendering half of grammar coloring — now visible in the editor behind the flag.

- UnitFileHighlighter: four user-customizable TextAttributesKeys, one per Role
  (GRAMMAR_ENUM/LITERAL/OPERATOR/IDENTIFIER), defaulting to CONSTANT/NUMBER/
  OPERATION_SIGN/IDENTIFIER colors.
- GrammarValueColorAnnotator: flag-gated; for grammar-backed options it runs colorize()
  and paints each Region with the matching attributes key. No-op when the flag is off.
- UnitFileColorSettings: adds the four roles under "Value" (customizable + themed) and
  extends the preview with grammar examples — RestrictAddressFamilies, SocketBindAllow/
  Deny, IPAddressAllow, RootImagePolicy — via highlighting tags.
- Combinators: IPV4_ADDR / IPV6_ADDR wrapped in Labeled(LITERAL) so an address colours
  as one literal span rather than per-octet/hextet (transparent to matching; full suite
  green confirms validation/completion unchanged). colorize() now dedupes identical
  regions (nested Labeled, e.g. IPv4 suffix inside IPv6).

Tests: GrammarValueColorAnnotatorTest (SocketBindAllow coloured by role; an IP as one
literal span; nothing when the flag is off) and the extended ColoringTest.

Refs #467 #342

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… preview (#467 #342)

The color-settings preview had grammar examples for enum/literal/operator but none for
the Identifier role, so clicking "Value//Identifier (grammar)" highlighted nothing.
Tag the [Unit] Description= value (a config_parse_unit_string_printf identifier) so the
role has a visible example.

Refs #467 #342

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The default heuristic mapped RegexTerminal -> IDENTIFIER, so free-form values like
Description= and interface names (Bond=) got recoloured, which felt wrong — those
should keep the editor's normal value color. Make RegexTerminal uncoloured by default.

- defaultRole: RegexTerminal now falls through to null (no coloring).
- Drop the Identifier descriptor + tag + demo example from the color settings page,
  since nothing uses the IDENTIFIER role by default now. The Role and its key are kept
  for grammars that opt in explicitly via Labeled later (e.g. a single-token User=).

Refs #467 #342

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…Test

The value-color annotator adds INFORMATION-severity coloring highlights when the
experimental flag is on, which inflates doHighlighting() counts. Count only the
inspection problems (severity != INFORMATION), matching the value-coloring annotator.
@github-actions

Copy link
Copy Markdown

Unit Test Results (grammar engine true)

1 167 tests   1 167 ✅  48s ⏱️
  306 suites      0 💤
  306 files        0 ❌

Results for commit 6cd5561.

@github-actions

Copy link
Copy Markdown

Unit Test Results (grammar engine false)

1 167 tests   1 167 ✅  50s ⏱️
  306 suites      0 💤
  306 files        0 ❌

Results for commit 6cd5561.

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