feat: grammar-based value coloring behind the experimental flag#497
Open
SJrX wants to merge 5 commits into
Open
feat: grammar-based value coloring behind the experimental flag#497SJrX wants to merge 5 commits into
SJrX wants to merge 5 commits into
Conversation
…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.
Unit Test Results (grammar engine true)1 167 tests 1 167 ✅ 48s ⏱️ Results for commit 6cd5561. |
Unit Test Results (grammar engine false)1 167 tests 1 167 ✅ 50s ⏱️ Results for commit 6cd5561. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Labeledspans (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:
Labeledwrapper,Region/Role,Parse.regions(a defaulted param, so backward-compatible), threaded through the repetition/sequence combinators.GrammarValueColorAnnotator+ color-settings entries + wrappingIPV4_ADDR/IPV6_ADDRetc. inLabeled(Role.LITERAL, …).RegexTerminalcontent isn't recolored).Gating
GrammarValueColorAnnotatorreturns early unlessExperimentalSettings.useGrammarParseEngineis set — off by default, like the other grammar-engine features. No change to default coloring.Scope notes
Labeledregions to find "the whole IPv6 address" within a value.Tests
ColoringTest(engine: regions/roles fromcolorize) andGrammarValueColorAnnotatorTest(end-to-end).GrammarParseEngineInspectionTestnow filtersINFORMATIONhighlights so it counts inspection problems, not coloring.Verification
./gradlew testand./gradlew test -Dsystemd.unit.grammarParseEngine=trueboth pass.🤖 Generated with Claude Code