TODO §69: colour module instantiations by role, with # matching parameters

Add AST-guided semantic token overrides in lucisv_server/src/tokens/instantiation.rs
and cross-file overlay lookup in tokens.rs without changing the 14-type wire legend:
- module/interface/extends names -> class (pkg:: prefixes -> namespace)
- parameter-list # (never delays), .PARAM overrides and parameter values -> parameter
- instance names (declarations, gate instances, bind lists, hierarchical uses) -> function
- named port connections (.port, implicit .port, .*) -> property
- port connection signals -> variable

Enable editor.semanticHighlighting.enabled by default for [verilog] and
[systemverilog] in editors/vscode/package.json and contribute semanticTokenScopes
so every role renders with a distinct, non-white colour across VS Code themes.
Add editors/vscode/semanticTokens_test.js (75 checks, passing under node --test).
Rust tests/fmt/clippy deferred to Gitea CI (no cargo toolchain on this Windows host).
This commit is contained in:
lucio committed 2026-11-19 02:38:42 -08:00
1 parent 857b82d58a
commit e4db0ac1fc
9 files changed
+895 -49

No files matched your search

@@ -0,0 +1,126 @@
# instantiation-highlighting
## Goal
Give module and interface instantiations distinct, recognizable syntax colours
instead of painting every part of an instantiation with the default variable
foreground. Borrowing the roles other languages assign to constructor and
component calls (C++, Rust, TypeScript/JSX), each syntactic piece of an
instantiation takes its own semantic token class and theme-safe TextMate scope:
1. **Module / interface name** (`uart_core` in `uart_core #(...) u_uart (...)`,
`pkg::uart_core` with `pkg` as `namespace`, and lone `bind` targets) →
`class` (`entity.name.type.class`).
2. **`#` sign and parameters** (`#` before parameter port or override lists,
`.BAUD_DIV` dot + parameter-port name, `parameter`/`localparam` symbols, and
unresolved constant identifiers inside `#(...)`) → `parameter`
(`keyword.control` / `constant.language` / `variable.parameter`), sharing
one colour that stays distinct from bracket-pair-coloured `()`.
3. **Instance name** (`u_uart`, gate instances, `bind` target instances, and
uses of `SymbolKind::Instance` such as `u_uart.sig`) → `function`
(`entity.name.function`).
4. **Port name** (`.clk_i` dot + port name, implicit `.clk_i`, and wildcard
`.*` in an instance's connection list) → `property`
(`entity.name.tag.css` / `entity.other.attribute-name` /
`variable.other.property`).
5. **Port connection** (`clk` inside `.clk_i(clk)` or positional `(clk)`) →
`variable` (`support.variable` / `entity.other.attribute-name` /
`variable.other.readwrite`), keeping signal colouring consistent across the
file and non-white in themes that leave bare `variable` uncoloured.
## Assumptions
- **The 14-type wire legend is unchanged.** Every role above maps onto the
existing `SemanticTokenType` indices (`namespace`, `class`, `parameter`,
`variable`, `property`, `function`), so `token_types().len() == 14` and
`token_modifiers().len() == 1` stay untouched on the wire.
- **AST spans decide instantiation structure; single-file + overlay semantics
decide expressions.** A buffer opened before workspace indexing finishes, a
standalone file, or an instantiation of an unindexed vendor macro still
highlights its module name, `#`, `.PARAM`, instance name, and `.port` from
`Document.tree` alone. Identifiers that resolve through `Document.external`
also take their cross-file `SymbolKind` instead of degrading to `variable`.
- **Parameter `#` only, never delay `#`.** Only the `#` introducing a parameter
port list, parameter override list, or parameterized `DataType` receives
`Highlight::Parameter`; `#5` and `#(1:2:3)` net, assignment, gate, and
procedural delays remain unemitted punctuation.
- **Large-file fast path (`Document::large`) stays lexer-only.** Because the
fast path builds no AST or symbol table, AST-driven `#`, `.`, and `.*` tokens
are emitted only by the full pipeline, alongside identifier refinement.
- **Editor defaults remain user-overridable.** Setting
`editor.semanticHighlighting.enabled = true` in `configurationDefaults` for
`[verilog]` and `[systemverilog]` activates semantic tokens in themes whose
own `semanticHighlighting` flag is unset or `false`, while any explicit user
or workspace setting still wins.
## Handshakes
- **Consumed from `vlog_parser`:** `SourceFile`, `Item`/`ItemKind`, `Module`,
`Instantiation`, `Instance`, `Connections`, `Connection`, `BindDirective`,
`GateInstantiation`, `ClassDecl`, `ClassExtends`, `DataType::params`,
`ListElem`, `RawText`, `Token`, `TokenKind`, `TextRange`. No parser changes.
- **Consumed from `vlog_semantic`:** `Semantics`, `SymbolKind`,
`ExternalResolutions::definition_of`, `Definition`. No semantic changes.
- **Promised to LSP clients (`crates/lucisv_server/src/tokens.rs`):** identical
14-entry `token_types` legend and 1-entry `token_modifiers` legend; sorted,
non-overlapping single-line UTF-16 tokens; exact `full/delta` round-trip.
- **Shared with `editors/vscode/package.json`:** `contributes.semanticTokenScopes`
keys for `verilog` and `systemverilog` are a subset of `tokens::token_types()`,
checked by `editors/vscode/semanticTokens_test.js`.
## Status / TODO
- [x] Add `crates/lucisv_server/src/tokens/instantiation.rs` for AST-guided
instantiation and parameter-header highlight overrides.
- [x] Update `crates/lucisv_server/src/tokens.rs` to apply overrides, consult
`Document.external` via `definition_of`, map `SymbolKind::Instance` to
`Highlight::Function`, and add unit tests covering instantiation shapes,
delay `#` exclusion, recovered entries, and `large-file-mode` window
agreement.
- [x] Configure `editor.semanticHighlighting.enabled` and
`contributes.semanticTokenScopes` for `verilog` and `systemverilog` in
`editors/vscode/package.json`, with headless verification in
`editors/vscode/semanticTokens_test.js` (and `.vscodeignore` exclusion).
- [x] Update `docs/features/lucisv-server/README.md` and
`docs/features/large-file-mode/README.md` for the refined mapping.
## Work log
### 2026-11-17 — dev
- Audited `tokens.rs`, `editors/vscode/package.json`, and VS Code theme scope
tables across Dark Modern/Dark+, Light Modern/Light+, GitHub Dark, One Dark
Pro, Monokai, and Visual Studio Dark. Found two root causes for flat white
instantiations: (1) `tokens.rs` consulted only single-file `Semantics`
(leaving external module names, `.PARAM` overrides, `.port` names, and
`SymbolKind::Instance` all on `Highlight::Variable`, and leaving `#`
unemitted); (2) `package.json` neither defaulted
`editor.semanticHighlighting.enabled` on nor mapped semantic token types to
distinct TextMate scopes for themes where `variable`/`parameter`/`property`
collapse to white or one shared colour.
- Added `crates/lucisv_server/src/tokens/instantiation.rs` (~280 total lines)
and wired `crates/lucisv_server/src/tokens.rs` without changing the 14-type
wire legend:
- Module/interface/`extends` names → `Highlight::Class` (`pkg::` prefixes →
`Highlight::Namespace`);
- Parameter-list `#` (headers, instantiations, parameterized `DataType`s,
never delays), `.PARAM` overrides (`.` + name), and unresolved constant
identifiers inside `#(...)` → `Highlight::Parameter`;
- `SymbolKind::Instance`, `Instance.name_span`, gate instances, and `bind`
instance lists → `Highlight::Function`;
- Named port connections (`.` + name, implicit `.name`, wildcard `.*`),
including recovered unfinished entries while typing → `Highlight::Property`;
- Port connection expressions stay on `identifier_type` (`Highlight::Variable`
for signals), now backed by `ExternalResolutions::definition_of`.
- Added `editor.semanticHighlighting.enabled: true` under
`configurationDefaults` and `semanticTokenScopes` for `verilog` and
`systemverilog` in `editors/vscode/package.json`; added
`editors/vscode/semanticTokens_test.js` (75 checks, all green under `node`)
and listed it in `editors/vscode/.vscodeignore`.
- Cargo/Rust toolchain is not installed on this Windows host; Rust formatting
(`use_small_heuristics = "Max"`, ≤100 cols), clippy rules, and test
invariants were audited manually for Gitea CI execution.
## QA log
## Review
+6 -5
View File
@@ -184,11 +184,12 @@ documented difference below.
### What it gets wrong, honestly
- **A plain identifier is always `variable`, with no `declaration` modifier.**
There is no `Semantics` to refine it with, so a module name is not a `class`
and a parameter is not a `parameter`. Keywords, numbers, strings, comments,
directives, macro uses, `$system` tasks and attributes are all exactly what
the full pipeline says.
- **A plain identifier is always `variable`, with no `declaration` modifier, and
AST-only instantiation punctuation (`#`, `.`, `.*`) is unemitted.** There is
no `Semantics` or `Document.tree` to refine them with, so a module name is
not a `class` and a parameter or `#` is not a `parameter`. Keywords, numbers,
strings, comments, directives, macro uses, `$system` tasks and attributes are
all exactly what the full pipeline says.
- **A `/*` inside a string or after a `//` fools the backward scan**, and so
does a block comment whose opener is more than 64 KiB back — the documented
answer there is "code", so a multi-megabyte block comment mis-highlights.
+24 -14
View File
@@ -573,25 +573,35 @@ Modifiers: `0 declaration`.
| `TokenKind::SystemIdent` (`$display`) | `function` |
| `TokenKind::Attribute` (`(* ... *)`) | `decorator` |
| `Comment` trivia (leading and trailing, incl. on the EOF token) | `comment` |
| `TokenKind::Symbol` | **not emitted** |
| `TokenKind::Ident`, otherwise | from `Semantics`, below |
| `TokenKind::Symbol` (`#` before parameter lists; `.` / `.*` on named connections) | `parameter` / `property` |
| `TokenKind::Symbol`, otherwise | **not emitted** |
| `TokenKind::Ident`, otherwise | from `Document.tree` + `Semantics` + `ExternalResolutions`, below |
A plain identifier is looked up in the file's `Semantics` at its start offset:
a declaration (`symbol_at`) gets its symbol's type **plus the `declaration`
modifier**; a use (`reference_at`) that resolved in-file gets the resolved
symbol's type; anything else — an external candidate, a not-found name, a name
a macro would have produced — falls back to `variable`.
A plain identifier first checks the AST overrides built by
`tokens/instantiation.rs` (`docs/features/instantiation-highlighting/README.md`):
an instantiated module or `extends` base takes `class` (`pkg::` prefixes take
`namespace`), an instance name takes `function`, a `.PARAM` override and any
unresolved constant identifier inside `#(...)` take `parameter` alongside `#`,
and a `.port` connection takes `property`. Otherwise it is looked up through
`ExternalResolutions::definition_of` (`Semantics` first, then the workspace
overlay): a declaration (`symbol_at`) carries the `declaration` modifier, a
resolved local or external reference takes its target's `SymbolKind`, and
anything else falls back to `variable`.
`SymbolKind` → type: `Module`→`class`, `Package`→`namespace`,
`Typedef`→`type`, `EnumVariant`→`enumMember`, `Param`/`TfArg`→`parameter`,
`Function`/`Task`→`function`, `StructMember`→`property`, and
`Port`/`Net`/`Var`/`Genvar`/`Instance`/`Block`→`variable`.
`SymbolKind` → type: `Module`/`Interface`/`Modport`/`Class`/`Covergroup`→`class`,
`Package`→`namespace`, `Typedef`→`type`, `EnumVariant`→`enumMember`,
`Param`/`LocalParam`/`TfArg`→`parameter`,
`Function`/`Task`/`Instance`/`Property`/`Sequence`→`function`,
`StructMember`/`Constraint`/`Coverpoint`/`Cross`→`property`, and
`Port`/`Net`/`Var`/`Genvar`/`Clocking`/`Block`→`variable`.
Two rules that are easy to get wrong and are tested:
- **Punctuation is not emitted.** Every editor already colors brackets from
its TextMate grammar, and emitting one token per `;` roughly triples the
payload for no visible gain.
- **Ordinary punctuation (including `()`) is not emitted.** Every editor
already colours brackets from its bracket-pair colorizer or TextMate grammar,
and emitting one token per `;` roughly triples the payload for no visible
gain; only the parameter-list `#` and named-connection `.`/`.*` carry AST
highlights.
- **No token ever spans a line break.** The spec requires it, and Verilog has
three multi-line token shapes that would violate it — a block comment, a
continued `` `define``, and a multi-line macro invocation. Every span is