Files

388 lines
21 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# inlay-hints
M8 of the refactoring plan (`docs/features/refactor/README.md`): **port-name
hints on ordered connections**.
## Goal
An ordered connection list writes the *values* and leaves the *names* to the
reader's memory: `u1 (clk, rst_n, data)` says nothing about which port each
expression lands on, and getting that wrong is one of the classic ways to
break a design without breaking the build. The module's own declaration knows,
and lucisv's workspace index knows where that declaration lives, so
`textDocument/inlayHint` draws each port's name as a ghost label before the
expression it belongs to — `clk:`, `rst_n:`, `data:` — and does the same for a
positional `#( ... )` override against the module's parameters. Nothing is
written into the buffer and nothing new is analyzed: the facts come from the
same `vlog_refactor::instantiation_plan` M4's completion and signature help
already read.
Hints are decoration. Every uncertainty — a module the index does not hold, a
name two files declare, a connection past the last port, a buffer that does
not parse — answers with *no hint* rather than a guess or an error. A wrong
port name drawn in ghost text is worse than no port name, because the reader
has no way to tell it is wrong.
## Assumptions
- **The parse tree is the right source, unlike signature help's token
stream.** `signature.rs` reads tokens because it answers about a list that
is *being typed* and therefore does not parse. A hint decorates a file that
is being *read*: the tree already says which lists are ordered, which
positions were omitted, and exactly where each expression starts, and
re-deriving that by counting commas is how a nested `f(a, b)` shifts every
later hint one port along. A buffer that does not parse gets no hints, which
is the right failure for decoration and the wrong one for signature help.
- **A positional parameter override is a `Connection` with an empty name.**
Unlike a connection list, whose `Connections::Ordered`/`Named` says which
spelling the source used, a parameter override list carries no such node —
each entry either wrote a `.name` or did not. Pinned by a test
(`a_positional_parameter_override_writes_no_name_of_its_own`) so a parser
change cannot silently turn every override into a named one.
- **A mixed parameter list gets nothing.** `#(8, .DEPTH(16))` is illegal
SystemVerilog and perfectly typeable. Hinting only the positional half would
be claiming to know which entry `8` lands on in a list whose own author has
stopped counting; the whole list is left alone.
- **An instance array hints like a single instance.** `sub u [3:0] (a, b)`
connects the same ports, one slice per instance; the names do not change.
- **A gate primitive gets nothing.** `and (q, a, b)` is a
`GateInstantiation` — a different AST node, never reached by this module —
and the language gives its terminals no names to show.
- **`.*` is a named list.** The parser spells it as a `Connections::Named`
entry, so it is skipped with every other named list; there is no position
for it to name anyway.
- **A plan is fetched per request, not cached across them.** Measured before
choosing — see *Cost* below. The `Plans` cache lives for the length of one
request, so a screen instantiating the same module ten times parses its
declaration once, and a defining file edited between two requests is never
read from a stale copy.
- **A hint sitting exactly on the requested range's last byte is in range.**
`TextRange::contains` excludes the end, which is right for a span of text
and wrong for a viewport.
- **A port or parameter list carrying compiler directives gets nothing.**
`instantiation_plan` unions every `` `ifdef`` arm into one flat,
declaration-ordered list (the right answer for a skeleton the user edits —
see `docs/features/refactor-instantiate/README.md`'s `` `ifdef`` policy).
No single configuration has all of those entries, so a positional hint
drawn against the union names the wrong port under any one configuration —
found in QA (2026-08-24) and fixed the same day; see the QA log.
## Handshakes
- **Consumes `vlog_refactor`** — `module_definition` and `instantiation_plan`,
both unchanged, both already public for M4. Nothing new is asked of the
crate: this feature needs exactly the `PlanPort::name`/`PlanParam::name`
lists signature help already reads, and every refusal
`instantiation_plan` can return is swallowed silently here rather than
shown, because a hint has no place to show a sentence.
- **Consumes `vlog_parser::ast`** — `Instantiation`, `Instance`,
`Connections`, `OrderedConn`, `Connection`, and the generate-block nodes the
walk descends through. Read-only; `Expr::span` is what a hint's position is
computed from.
- **Consumes `lucisv_server::state`** — `Workspace::document` for the buffer's
tree, `Workspace::index`/`text_of` for the defining file, exactly as
`signature.rs` does. Nothing in `state.rs` was changed: no cross-request
cache was added, so the workspace stays immutable through this handler.
- **Promises `lucisv_server`** — `inlay::inlay_hints` never panics, never
returns a hint outside the requested range, and never returns a hint whose
label is not a port or parameter name the module's declaration actually
writes.
- **`editors/vscode` needs no change.** `vscode-languageclient` provides the
inlay-hint feature off the advertised capability; the extension sets no
`initializationOptions` that would suppress it. `editors/vscode/smoke.js`
checks a fixed list of capability names and was deliberately left untouched
(it is editor-packaging's file, not this feature's) — it still passes. If
the lead wants `inlayHintProvider` in that list, it is a one-line addition
there.
## What it looks like
`sub.sv` declares:
```systemverilog
module sub #(
parameter int WIDTH = 8,
parameter int DEPTH = 16
) (
input logic clk,
input logic rst_n,
input logic [WIDTH-1:0] data
);
```
and `top.sv` writes:
```systemverilog
sub #(4, 8) u1 (clk_i, rst_ni, data_q);
```
The editor draws:
```
sub #(WIDTH: 4, DEPTH: 8) u1 (clk: clk_i, rst_n: rst_ni, data: data_q);
^^^^^^^ ^^^^^^ ^^^^ ^^^^^^ ^^^^^
ghost labels, `paddingRight`, `InlayHintKind::PARAMETER`
```
Nothing is drawn on `sub u2 (.clk(clk_i), ...)`, on `sub u3 (.*)`, on the
empty position of `sub u4 (a, , c)` — where `c` is still labelled `data`,
because an omitted position still counts — or on the fourth connection of a
three-port module.
## Cost
Inlay hints fire on every scroll, so the number was measured before the design
was settled rather than after. Release build, warm workspace, this machine:
| scenario | mean |
|---|---|
| `corpus/opentitan/.../uart_reg_top.sv` — 1916 lines, 63 instantiations, all **named** | **0.005 ms** |
| synthetic: 100 **distinct** modules ordered-instantiated, median-sized (~90-line) definitions, whole file at once, 700 hints | **30.8 ms** |
| the same, one 40-line screen — 40 distinct modules, 280 hints | **12.2 ms** |
| the same, with 400-line definitions instead (≈4.5× the corpus median) — one 40-line screen | 52.2 ms |
Two things the table says. First, the cost is *entirely* the plan fetch: a
file with no ordered lists costs one tree walk and nothing else, and real RTL
is overwhelmingly named. Counted over the whole corpus with this module's own
walk (throwaway test, run and deleted): **860 files, 45 non-empty ordered
connection lists in 4 files against 945 named ones, and 69 ordered parameter
override lists against 668 named**. Second, what a request pays for is the
number of **distinct modules visible**, not the number of hints: the plan
cache collapses repeats, and the range scope means an instantiation off-screen
is never looked up at all.
So: parse-on-request, no cross-request cache. The realistic worst case — a
screen showing forty different ordered instantiations, which no RTL file in
the corpus comes near — is 12 ms, well inside the budget. The last row is the
honest cliff: definitions 4.5× the corpus median, forty of them on one screen,
would cost 52 ms. If that shape is ever met in the wild, the fix is a plan
cache keyed on the index generation, which needs a `&mut Workspace` in the
request path and brings a staleness question (an unsaved edit to the defining
buffer) that is not worth paying for today.
## Status / TODO
- [x] `crates/lucisv_server/src/inlay.rs`: `inlay_hints`, the ordered-list site
collection for both ports and parameters, the per-request `Plans` cache,
the instantiation walk
- [x] `inlayHintProvider` in `main.rs`, no `resolveProvider`; the dispatch arm
in `server.rs`
- [x] Range scoping, end-inclusive
- [x] Silence on every uncertainty: unknown module, ambiguous duplicates,
named/`.*`/empty/mixed lists, connections past the last port, a buffer
that does not parse, a directive-carrying (`` `ifdef``) port or
parameter list (found in QA, fixed same day — see QA log)
- [x] Gate primitives skipped; instance arrays and multi-instance declarations
hinted; `bind`'s instantiation reached
- [x] Unit tests (26) and one e2e stdio round trip asserting exact labels,
positions, kind and padding
- [x] Cost measured over a corpus file and a synthetic worst case; harness run
and deleted
- [x] `cargo fmt --all -- --check`, `cargo clippy --all-targets`,
`cargo test --workspace`, `editors/vscode/smoke.js`
- [x] `docs/features/lucisv-server/README.md` capability table + work log
- [ ] Not done, backlog: a cross-request plan cache keyed on the index
generation (see *Cost*); type hints, value hints and any other hint kind
(deliberately out of scope — this milestone is exactly the two).
## Work log
- **2026-08-24 (dev)** — read M4's README, `signature.rs`, `ports.rs` and
`convert.rs` first, then the parser's `Instantiation`/`Connections` nodes.
**Design decision 1 — the tree, not the token stream.** The brief points at
`signature.rs` as the closest cousin, and it is, but its comma-counting is
the wrong machine for this job. Signature help answers mid-keystroke about
text that does not parse yet; a hint request arrives about text the user is
reading, where `doc.tree` already holds `Connections::Ordered(...)` with a
span per entry. Counting commas would have re-derived — and eventually got
wrong — what the parser already decided: which lists are ordered, where an
omitted position is, and that the commas inside `f(a, b)` are not
connection separators. The nested-call test that pins this
(`commas_inside_a_connected_expression_do_not_shift_a_position`) passes by
construction rather than by a depth counter, which is the point.
**Design decision 2 — the plan is fetched only for sites that are on
screen.** The range filter runs *before* the lookup, not after, so scrolling
past an instantiation of a module never parses that module's file. This is
what makes the per-screen number a third of the whole-file one in the table
above rather than equal to it.
**Design decision 3 — a per-request cache, and no more.** Measured both
ends (see *Cost*) before deciding. A cross-request cache would need a
`&mut Workspace` in a handler that has no other reason to want one, plus an
invalidation story for a defining file being edited in another tab; the
measurement says it buys nothing on any workload the corpus contains, so it
is recorded as backlog with the number that would justify it.
**A mixed parameter list is silent, which is a judgement call.** The brief
says "mixed illegal lists → sane". Hinting the positional half of
`#(8, .DEPTH(16))` was the alternative; it is exactly the case where the
reader cannot tell whether the author meant the entries to shift, so lucisv
says nothing rather than something confident.
**`bind` is included.** `bind u_dut sub u_chk (a, b);` carries an ordinary
instantiation, connection list and all, so the walk descends into it. One
match arm; nothing else about `bind` is touched.
**Files touched.** New: `crates/lucisv_server/src/inlay.rs`, this README.
Edited: `crates/lucisv_server/src/main.rs` (the module declaration, the
capability, two tests), `crates/lucisv_server/src/server.rs` (one dispatch
arm and its imports), `crates/lucisv_server/tests/e2e.rs` (one new test),
`docs/features/lucisv-server/README.md` (capability table and work log).
**Not touched, deliberately:** `TODO.md`, the umbrella checklist in
`docs/features/refactor/README.md`, `signature.rs`, `completion.rs`,
`state.rs`, `vlog_refactor`, `editors/vscode`. No version bump — 0.5.0 comes
with M14.
**Gates.** `cargo fmt --all -- --check` clean, `cargo clippy --all-targets`
zero warnings, `cargo test --workspace` green at **1358 passed, 0 failed**
(1330 before; 26 unit tests in `inlay.rs`, 1 capability test in `main.rs`,
1 e2e round trip). `node editors/vscode/smoke.js` against the release
binary: OK. No new dependencies. No commits.
- **2026-08-25 (dev, M11 hierarchy)** — the private instantiation walk at the
bottom of `inlay.rs` moved to `crates/lucisv_server/src/instantiations.rs`, so
the call hierarchy can read the same one instead of writing a second
(`docs/features/hierarchy/README.md`). The lifted version threads the
innermost enclosing module declaration through the recursion; `inlay_hints`
maps straight past it, and nothing else in this module changed —
`instantiations(&doc.tree)` became `instantiations::sites(&doc.tree.items)`
and the four walk functions were deleted. Every test in this file was left
untouched and is green, which is the evidence the behavior is identical.
**Handshake note:** the *Handshakes* bullet about consuming
`vlog_parser::ast` now goes through `crate::instantiations`; the AST nodes
read are the same ones.
## QA log
- **2026-08-24 (QA)** — read this README, `refactor-instantiate/README.md`
(for `instantiation_plan`'s `` `ifdef`` policy and non-ANSI handling), and
`inlay.rs` end to end before touching anything.
**Gates.** `cargo fmt --all -- --check`, `cargo clippy --all-targets
--workspace`, `cargo test --workspace` all clean before any QA change —
**1358 passed, 0 failed**, matching the dev's number exactly. `node
editors/vscode/smoke.js target/release/lucisv-server` — OK; confirmed the
script's fixed capability list (`main.rs`'s check, mirrored in the smoke
script) does not include `inlayHintProvider`, matching the disclosed,
deliberate handshake gap.
**Finding — HIGH, fixed.** A module whose port or parameter list carries
`` `ifdef``/`` `else`` directives got hints built from
`InstantiationPlan`'s **union of every arm**, which is a shape no single
build configuration ever has. Repro: `crates/lucisv_server/src/inlay.rs`,
test `an_ifdef_carrying_declaration_gets_no_hints_rather_than_a_wrong_one`
— `module sub (`ifdef CFG_A input logic a, `else input logic b, `endif
input logic clk);` gives `plan.ports == [a, b, clk]` (verified directly
against `vlog_refactor::instantiation_plan`, `crates/vlog_refactor/src/
instantiate.rs:144`). Before the fix, `sub u1 (x, y)` was hinted `a: x, b:
y` — wrong under *both* real configurations, since the true second port is
always `clk`, never `b`. This is exactly the case the feature's own Goal
section rules out ("A wrong port name drawn in ghost text is worse than no
port name, because the reader has no way to tell it is wrong"), and it was
undisclosed: no Assumption, Handshake, or test mentioned `` `ifdef`` at
all, despite `instantiation_plan` warning about exactly this
(`plan.warnings`) and M4's own README treating the same directive shape as
a first-class case worth a warning. `Plans::read`
(`crates/lucisv_server/src/inlay.rs:192`) called
`vlog_refactor::instantiation_plan(...).ok()` and discarded `plan.warnings`
without ever inspecting it. **Fix applied**: `Plans::read` now returns
`None` when `plan.warnings` is non-empty, so a directive-carrying list is
silent exactly like an unknown module. Two regression tests added
(`an_ifdef_carrying_declaration_gets_no_hints_rather_than_a_wrong_one`,
`an_ifdef_carrying_parameter_list_gets_no_hints_either`); one Assumptions
bullet added above documenting the behavior. Full suite re-green after the
fix: **1366 passed, 0 failed** (1358 + 8 new permanent tests; 2 more perf
tests added `#[ignore]`d, not counted in "passed"). `cargo fmt`/`clippy`
clean, smoke.js re-run OK against a rebuilt release binary.
**Everything else tried to break, and did not:**
- *Omitted position, off-by-one.* `u (a, , c)` — already covered by the
dev's own test; independently re-derived by hand against `plan.ports`
and confirmed the third port's name lands on `c`, not shifted.
- *More connections than ports.* Extras correctly unhinted (dev's test,
re-verified).
- *Non-ANSI header.* New test `a_non_ansi_header_definition_hints_correctly`
— `module nasub (clk, rst_n, data); input clk; ...` hints exactly
`["clk:", "rst_n:", "data:"]`. `ports()` in `instantiate.rs:386` branches
on `PortList::NonAnsi` correctly; nothing inlay-specific needed.
- *Parameterized instantiation, port type depends on a parameter.* New
test `a_port_whose_width_depends_on_a_parameter_still_hints_by_name_only`
— `[WIDTH-1:0] data` port, `#(16)` override — no crash, correct labels;
the label is only ever `port.name`, never the type.
- *Recursive/self instantiation.* New test
`a_self_instantiating_module_does_not_crash` — `module top; top u1
(clk); endmodule` does not loop or panic (the module has no declared
ports, so the honest answer is no hints; the point was survival, not a
label).
- *`defparam`.* No `#( ... )` node exists for `defparam u1.WIDTH = 4;`, so
`param_sites` never sees it; port hints on the same instantiation are
unaffected. Confirmed with an ad hoc probe (not kept as a permanent
test — it exercises no code path the existing suite doesn't already
cover).
- *Emoji inside a nested call argument.* New test
`a_comment_nested_inside_a_connected_expression_does_not_shift_utf16_columns`
— `f(/* 🎉é */ a, b)` as one connection: the comma inside stays a
non-separator (per the dev's existing nested-call design) and the next
hint's column (29) is correct in UTF-16 units, surrogate pair included.
- *Mixed positional/named recovery shapes.* Dev's existing test already
covers both parser-recovery shapes and asserts the weaker "no hint on a
named entry" invariant; agree that is the right assertion strength.
**Range scoping.**
- New test
`a_range_starting_mid_instantiation_hints_only_the_visible_connections` —
a range starting after `clk,` on `sub u1 (clk, rst_n, data);` hints
`rst_n` and `data` only. The filter is **per-site**, not
per-instantiation: confirms the code's actual behavior (`Hints::paint`
filters each `Site` independently) is a bit finer-grained than the
README's Design-decision-2 prose implies ("a scroll cost the visible
instantiations"), but it is the more correct behavior for a viewport, so
no fix — just documented here since the prose undersells it.
- Empty range and whole-file: covered by the dev's own
`only_the_requested_range_is_hinted` test; re-run and agree.
**Perf.** Rebuilt a cheap harness (`inlay::tests::qa_perf`, `#[ignore]`d,
not part of the default suite — run with `cargo test --release -p
lucisv_server --bin lucisv-server qa_perf -- --ignored --nocapture`) rather
than driving the real binary over stdio, since the library call is what
the dev's own numbers measure too:
- 40 distinct ~90-line ordered modules, one 40-line screen, 840 hints:
**~2.8ms** release / this machine — same order of magnitude as the
README's 12.2ms for the same shape (this run came in faster; not a
concern, the direction that matters is "small," and it is).
- The same module instantiated 100× ordered, whole file, 300 hints:
**~163µs** — confirms the plan cache collapses repeats to one parse
(**O(distinct modules)**, not O(instances)), matching the Cost section's
claim.
**Staleness.** New test
`an_open_unsaved_buffer_of_the_defining_file_wins_over_disk` — an unsaved
`didChange`-style edit to the *defining* module's open buffer (renaming
`clk` to `clk_renamed`, disk left untouched) is what a hint request in the
instantiating file reads. Matches the Assumptions claim ("a defining file
edited between two requests is never read from a stale copy") and is now
pinned by a test rather than only asserted in prose.
**Verdict: ship-after-fixes — the ifdef finding above is fixed and tested
in this same change; nothing else found rises above a documentation nit,
and those are addressed inline (the Assumptions bullet above, this log).**
## Review
**2026-08-24 — lead.** Reviewed after QA; gates run independently (1366
tests, 0 failures, clippy silent). The tree-walk-over-comma-counting
argument is correct and well made — hints decorate parsed text, so the
tree already answers what signature help must guess. QA's HIGH find was
the round's real work: the `ifdef` union that is right for an editable
skeleton is confidently wrong for a read-only ghost label, and the
applied fix (a plan with warnings is treated like every other
uncertainty — silence) is exactly the feature's own stated invariant;
the in-code comment explaining the asymmetry with M4 is the kind of
comment worth keeping. Verified the fix and its two regression tests
myself. O(distinct-modules) caching confirmed by QA at 163µs for 100
instances of one module. Accepted; M8 checked off in the umbrella plan
and TODO.md §8.