Files
lucisv/docs/features/hover-declarations/README.md
T
lucioandClaude Fable 5 efe67bde65
ci / checks (push) Successful in 48s
ci / corpus (push) Successful in 21s
Hover shows the cross-file declaration itself, not just where it lives
The Definition::External card now renders the declaration's source
line(s) fenced under the "declared in file:line" heading — direction,
type and width for a port, the header line for a module, the
declaration line for package members. No facts change: snippets are
fetched at hover time, from the live buffer via nav's logical matcher
when the target is open, from disk (what the index was built from)
otherwise. A boundary-checking guard (spells_exactly) drops the
snippet rather than ever showing a wrong line — QA's attack proved
prefix matching alone was forgivable (addr vs addr2) and the fix is
pinned. Caps at 5 lines / 400 chars with notes. 18 new tests.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-27 22:32:59 -07:00

330 lines
20 KiB
Markdown

# hover-declarations
## Goal
Hovering a name declared in **this** file has always shown the declaration
itself — `local_text` prints the kind, the name and the source lines
`decl_span` covers. Hovering a name declared in *another* file showed only
`**port** — declared in `sub.sv:3``: enough to navigate to, not enough to read.
The owner's ask (2026-08-27) is that the cross-file card carry the declaration
too — for a port its direction, type and width, `input logic [7:0] addr` — so
that reading a connection list does not require jumping out of the file. This
round adds a fenced `systemverilog` snippet of the declaration's source line(s)
under each candidate's existing heading, fetched at hover time and omitted
entirely whenever it could be the wrong line.
## Assumptions
- **The index stays text-free.** `Def` is `{file, name_span, kind}` and the
persisted facts and `FACTS_FORMAT` are untouched
(`docs/features/workspace-index/README.md`,
`docs/features/index-persistence/README.md`). A hover is a rare, one-name,
human-speed request; reading one file to answer it is the same trade
`locate::line_number` has always made for the `file:line` half of the card.
- **`reference.name` is the declared name.** Every `Definition::External`
answer comes from a reference (`ExternalResolutions::definition_of` looks the
target up by `ReferenceId`), and `resolve_external` finds the `Def` by
passing exactly that `name` to `WorkspaceIndex::units` / `member`. So the
name is the bare declared spelling — `WIDTH` for a `p::WIDTH` use, `clk` for
a `.clk(...)` connection — never a qualified path. That is what makes it
usable as the guard's expected text.
- **Disk text is what the index was built from.** `Workspace::text_of` prefers
an open buffer, which is right for a location and wrong for a snippet, so the
open case is handled separately and never reaches `text_of`.
- **A `decl_span` may be enormous.** A module's `decl_span` is its whole body
(`bind_module` passes the item span), and one netlist line has no bound at
all — hence the two caps below.
## Handshakes
**Consumed**
- `crate::nav::indexed_def_for_symbol` — raised from private to `pub(crate)`,
behaviour byte-for-byte unchanged. `nav.rs` walks it live-symbol → indexed
`Def`; this feature walks the same relation the other way (scan the open
buffer's symbols for the one that maps to this `Def`), which is precisely
what `nav.rs`'s own `include_declaration` block already does for the
declaration location. One matcher, so a hover snippet and a
go-to-definition can never disagree about which declaration is which.
- `Workspace::document_for_path`, `Workspace::text_of`, `WorkspaceIndex::path`.
- `crate::convert::Positions::whole_lines`, `vlog_parser::LineIndex`.
- `vlog_semantic::{Def, Symbol}` — read only; nothing added to either.
**Promised**
- `nav.rs` keeps its behaviour: the visibility change is the only edit to it,
and its whole test module stays green unmodified.
- The `` ```systemverilog `` fence tag is used for the new block, as the
fence-tag contract in `docs/features/lucisv-server/README.md` requires — it
is the only thing that colours a hover card.
- Nothing is added to `Def`, to the facts file, or to `FACTS_FORMAT`; a cache
written before this round is still current after it.
- The heading line `**label** — declared in `file:line`` is unchanged, so a
client (or a test) reading only that half sees exactly what it saw before.
## How the snippet is fetched
`declaration_snippet(ws, def, name)` in `hover.rs`, once per candidate:
1. **The target file is open** (`ws.document_for_path`). The index's span is
against that file's *last saved* text, so an unsaved edit above the
declaration has already moved it and reading the buffer at `def.name_span`
would print whatever those offsets now cover. Match the `Def` to a live
symbol **logically** instead — same name, and `indexed_def_for_symbol` maps
it back to this same `Def` — and widen that symbol's live `decl_span` to
whole lines.
2. **Not open**: `ws.text_of(path)` (disk, since no buffer backs it), a fresh
`LineIndex`, and `def.name_span` widened to whole lines.
3. **Guard, both paths.** Slice the chosen text at the chosen name span and
require it to equal the hovered name. `str::get` returns `None` for a span
that runs past the text or lands mid-character, so a stale or shifted span
fails the check rather than panicking. Any failure — unreadable file, no
live symbol, text that does not spell the name — drops the snippet and
leaves today's card exactly as it was. **A hover must never lie; less
information beats wrong information.**
Note the asymmetry, and that it is deliberate: the open path widens
`decl_span` (mirroring `local_text`, which is the only span a live symbol
offers that covers a whole multi-line declaration), the disk path widens
`def.name_span` (the only span the index holds). For the declarations the ask
is about — ports, parameters, typedefs, a module header — both are one line
and the two paths print the same thing.
## Cutting it down
`cut_to_size` trims trailing blank space (as `local_text` does; leading
indentation stays, so the line reads as written) and then applies two caps,
each with its own note under the fence:
| Cap | Value | Why |
|---|---|---|
| lines | `SNIPPET_LINES` = 5 | a module's `decl_span` is its whole body |
| characters | `SNIPPET_CHARS` = 400 | one netlist line can be megabytes |
A cut appends ` …` inside the fence and `*shown to N lines*` /
`*shown to N characters*` after it — the same shape as the macro card's
`*truncated at N characters*`, so the two footnotes read as one convention.
The character cap counts `char`s and cuts on a boundary.
## Status / TODO
- [x] `declaration_snippet` with the open-buffer and disk paths.
- [x] Guard on both paths; snippet omitted on any mismatch.
- [x] Line and character caps with a note.
- [x] One snippet per candidate for an ambiguous name.
- [x] `indexed_def_for_symbol` shared with `nav.rs`, behaviour unchanged.
- [x] Tests (9 new, in `hover.rs`'s own test module).
- [x] Hover row + hover section in `docs/features/lucisv-server/README.md`.
- [x] `cargo fmt` / `cargo clippy --all-targets` / `cargo test --workspace`.
- Not done, deliberately: no facts/cache change, nothing in `vlog_semantic`,
no TODO.md edit (no existing checklist item covers this; §8's hover items
are all already checked, and inventing one is out of scope).
## Work log
### 2026-08-27 — dev
- Read the brief, `hover.rs`, `nav.rs`, `state.rs` and `convert.rs`. Confirmed
the two facts the design rests on: `set_document` does **not** rebuild the
index (so an open buffer really can hold a span the index does not know
about — the case step 1 exists for), and `follow_one` looks a `Def` up by
`reference.name`, which is what licenses using that name as the guard's
expected text rather than trying to recover a name from `Def`.
- Added `declaration_snippet`, `live_symbol`, `span_text`, `fenced` and
`cut_to_size` to `hover.rs`, plus the two caps as consts. `symbol_hover`'s
`External` arm now appends the snippet to each candidate's part; the heading
it appends to is untouched.
- Raised `nav::indexed_def_for_symbol` to `pub(crate)` with a doc line naming
the second caller. No other change to `nav.rs`.
- Decision: the open path uses `decl_span` and the disk path `name_span`,
per the brief. Considered using `name_span` on both for symmetry — it would
make a big module's card one header line rather than five — but that would
drop the live-buffer case's only handle on a multi-line declaration, and the
line cap already bounds the damage. Recorded in *How the snippet is fetched*
so the asymmetry is a decision and not an accident.
- Decision: no fallback from the open path to disk text. If a file is open and
no live symbol matches, the buffer has been edited past the index and the
indexed span is exactly the thing that cannot be trusted; falling back would
re-introduce the wrong-line risk the guard exists to remove.
- Nine tests added (listed below). Ran the gates: fmt
clean, clippy `--all-targets` clean, `cargo test --workspace` green at 2372
passed / 0 failed / 12 ignored.
**Tests added** (all in `crates/lucisv_server/src/hover.rs`, `mod tests`):
| Test | Pins |
|---|---|
| `a_cross_file_port_connection_shows_the_ports_own_declaration_line` | the ask: direction + type + width, and only that port's line |
| `a_cross_file_module_instance_shows_its_header_line` | the module header, disk path |
| `a_package_member_shows_its_declaration_line` | a `localparam` and a `typedef` through `import p::*` |
| `an_unsaved_edit_that_moved_the_declaration_is_read_at_its_live_span` | live line, not the stale-span line (asserts the *new* width and the absence of the old) |
| `a_stale_indexed_span_drops_the_snippet_rather_than_showing_the_wrong_line` | guard, disk half: file rewritten under a live index → no fence at all, heading intact |
| `an_open_buffer_that_no_longer_declares_the_name_drops_the_snippet` | guard, live half: the port renamed away in the buffer |
| `a_very_long_declaration_line_is_cut_with_a_note` | the character cap and its note |
| `a_declaration_spanning_many_lines_is_cut_at_the_line_limit` | the line cap, via an open module whose `decl_span` is its body |
| `two_files_declaring_one_name_each_get_their_own_snippet` | ambiguous name: both headings, both snippets |
## QA log
### 2026-08-27 — QA
Read the brief, the dev's Work log, and `crates/lucisv_server/src/hover.rs` /
`nav.rs` in full. Ran the gates first (all green before any probing): `cargo
fmt --check` clean, `cargo clippy --all-targets --workspace -- -D warnings`
clean, `cargo test --workspace` green — 1173 passed / 0 failed / 12 ignored
(pre-existing) before this round's own probes were added.
Then attacked `declaration_snippet`, `live_symbol`, `span_text` and
`cut_to_size` along the seven angles in the charter. Eight probe tests added
to a new `mod qa_hover_declarations` in `hover.rs` (one `#[ignore]`d perf
measurement); one real defect found and fixed with a ninth test pinning it.
**Finding 1 — MEDIUM, fixed.** *The guard was a fixed-length substring check,
not a whole-identifier check, so an edit that only **extends** a name defeats
it.* `declaration_snippet`'s guard (`hover.rs:243` and `:252`, before the fix)
compared `span_text(text, span)` — a slice of exactly `name.len()` bytes at
the *old, recorded* offset — against `name`. `def.name_span`'s length is
whatever the name was when the index was last built; it does not grow if the
file is edited without a reindex. So: index built on `input logic [7:0]
addr;`, then the file is rewritten on disk to `input logic [7:0] addr2;`
*without* triggering a reindex (an ordinary edit-then-hover-before-save-event
race, exactly the staleness window this feature's guard exists for). The old
span still covers exactly the four bytes `"addr"` — a *prefix* of the new
`"addr2"` — so `span_text` returns `Some("addr")`, equals the hovered name,
and the guard passed. The card for `.addr(...)` then showed
`` input logic [7:0] addr2 `` under a heading that says `addr`'s declaration,
one line pretending to be a name it is not — exactly the masked-corruption
shape CLAUDE.md's invariant 4 is about, applied to this round's new safety
net rather than `verify.rs`.
- Repro (was a failing/panicking assertion, now a passing regression test):
`a_disk_edit_that_only_appends_to_the_name_defeats_the_guard`,
`hover.rs:1060`.
- Fix: added `spells_exactly` (`hover.rs:293`) and `continues_a_name`
(`hover.rs:306`), and switched both guard call sites
(`declaration_snippet`'s live and disk branches) to it. It keeps the
existing text-equality check and additionally requires that the
character immediately outside the span on either side (if any) is not an
identifier-continuation character (`[A-Za-z0-9_$]`) — turning a
prefix/suffix match back into a miss. Applied to *both* branches for one
guard, one guarantee, even though the live branch's `symbol.name_span`
(a token span from a fresh parse) could not actually suffer this —
`symbol.name == name` was already checked before the span comparison, so
the length always matched there. No behavior change for any span that is
a genuine whole identifier, which is every case the dev's 9 tests and the
existing corpus tests exercise — full `cargo test --workspace` stayed
green (1173 passed / 0 failed / 13 ignored, the 13th being this round's
own perf probe) after the fix.
- This does **not** close every staleness gap by design — a same-length
edit that changes an identifier into a different same-length identifier,
or that changes what a name's *kind* means without renaming it, is still
the accepted risk window the Assumptions section already names ("a hover
is a rare, one-name, human-speed request"). What the fix removes is
specifically the case where the *shown text visibly disagrees with the
name in the heading* — the one the guard's stated promise ("never show a
wrong line") is about.
**Finding 2 — LOW, filed, not fixed.** *A CRLF file's multi-line snippet
keeps the embedded `\r` on every interior line, only the last one is
trimmed.* `cut_to_size` (`hover.rs`, unchanged by QA) calls `trim_end()` once
on the whole cut string, which strips a *trailing* `\r` (and `\n`) but not an
interior one before a kept newline. `local_text` (the in-file branch,
pre-existing, not part of this round) has the identical property via the same
`whole_lines`/`trim_end` shape, so this is not new behavior introduced by
this feature, just newly reachable from a second call site. Cosmetic, not a
correctness break: the bytes shown are exactly the file's own bytes, and a
fenced code block renders them close to verbatim either way. Pinned as a
permanent regression test rather than fixed, since a real fix (stripping
`\r` before every kept `\n`, in both `local_text` and here) is a
cross-cutting change to shared rendering, out of this round's stated scope
(`hover.rs`'s `External` arm and its shared helper with `nav.rs`).
- Repro: `a_crlf_multiline_snippet_keeps_interior_carriage_returns`,
`hover.rs:1156`.
**Finding 3 — LOW/accepted, pinned as a regression test, not a defect of this
round.** *Two live declarations of the same name+kind in one open buffer (a
transient mid-edit state, e.g. a duplicate line not yet deleted) make
`live_symbol`'s `.find()` pick whichever comes first in `Semantics::symbols()`
order, silently.* `indexed_def_for_symbol` (`nav.rs:164`, visibility-only
change this round) maps *any* live symbol with a matching name/kind/owner
back to the single persisted `Def` for that name when the index itself has
only one — it does not know or care that the *live* buffer momentarily has
two. This is inherited, unchanged behavior from `nav.rs`'s existing
go-to-definition matcher (the Handshakes section calls this out on purpose:
"one matcher, so a hover snippet and a go-to-definition can never disagree").
Not a lie in the sense the guard is about — the guard still requires the
*chosen* symbol's own span to spell the name exactly, which it always does
here by construction — but it is a silent, unsignaled choice between two
candidates. Filed rather than fixed: fixing it means changing
`indexed_def_for_symbol`'s matching rule, which is nav.rs's and explicitly
out of this round's scope ("nav tests must stay green," brief line 27).
- Repro: `duplicate_live_declarations_of_one_name_pick_the_first_one_silently`,
`hover.rs:1126`.
**Finding 4 — perf note, not a defect, no action taken.** *A hover resolving
into a large closed file reads the file and builds a `LineIndex` over it
twice*: once inside `locate::line_number` (for the `file:line` heading — this
predates this round) and again inside `declaration_snippet` (for the
snippet — new this round), independently, from the same disk path. Measured
in release mode: a 31.7 MB synthetic file (600k filler lines behind a small
port declaration, `maxFileSize` raised so it indexes at all) took 34 ms for
one hover — acceptable for a rare, single-shot request, but a real, avoidable
doubling of I/O and linear scan work per disk-path candidate. A fix would
mean threading one read/`LineIndex` through both `line_number` and
`declaration_snippet`, which touches `locate.rs` (also used by `macro_hover`)
and is a small refactor rather than a one-line change — noted per the
charter's "note it, don't redesign" rather than attempted.
- Measurement, `#[ignore]`d (writes ~30 MB, not a correctness assertion):
`hovering_into_a_large_closed_file_reads_it_twice`, `hover.rs:1325`. Run
with `cargo test --release -p lucisv_server large_closed -- --ignored
--nocapture`.
**Verified, no finding** (angles from the charter that turned up nothing):
UTF-8 multi-byte content before a declaration, on the same file, does not
panic `span_text`/`whole_lines` (`multibyte_utf8_before_the_declaration_does_not_panic`,
`hover.rs`) — both are `.get()`-based and this stayed a belt-and-braces pin.
A declaration at byte offset 0 and a declaration on the file's last line with
no trailing newline both render correctly, no underflow
(`a_declaration_at_the_very_start_of_the_file_is_shown`,
`a_declaration_on_the_last_line_with_no_trailing_newline_is_shown`). The line
cap (`SNIPPET_LINES` = 5) and character cap (`SNIPPET_CHARS` = 400) are both
exact-boundary-safe — 5 lines / 400 chars is not cut, 6 lines / 401 chars is,
and a 400-char boundary landing mid multi-byte character is not split because
`cut_to_size` counts with `char_indices`, never raw bytes
(`exactly_the_line_cap_is_not_cut_one_more_is`,
`exactly_the_char_cap_is_not_cut_one_more_is_and_boundary_is_safe`). A
case-mismatched open buffer (same file, different-case path on a
case-insensitive filesystem) is not recognized as "the target file open" by
`document_for_path`'s exact `PathBuf` equality, so it falls back to the disk
path rather than mixing the two identities — the safe direction, confirmed
with unsaved edits in the mismatched buffer never leaking into the card
(`a_case_mismatched_open_buffer_is_not_recognized_as_the_target_falls_back_to_disk`,
macOS/Windows-gated). Multiple ambiguous candidates each render from their
own file with no cross-contamination — the dev's own
`two_files_declaring_one_name_each_get_their_own_snippet` already covers
this and reads fine without a divider between candidates (each fenced block
is its own visual unit).
**Gates, final.** `cargo fmt --check` clean. `cargo clippy --all-targets
--workspace -- -D warnings` clean. `cargo test --workspace`: 1173 passed / 0
failed / 13 ignored (12 pre-existing + this round's one perf probe).
## Review
**2026-08-27 — lead.** Reviewed the full diff after QA's SHIP verdict;
gates re-run independently (fmt clean, clippy silent, 1173 server tests
green). The shape is right: no widening of persisted facts, snippets
fetched at hover time from whichever text the span is valid against
(live logical match through `nav.rs`'s one matcher, disk otherwise),
and a guard that prefers no snippet to a wrong one. QA's MEDIUM find —
the fixed-length substring guard passing on a name that had merely been
*extended* in place — is precisely the masked-corruption shape
invariant 4 exists for, applied correctly to a new forgiveness path
outside `verify.rs`; the `spells_exactly` boundary check closes it on
both the live and disk paths and the attack test pins it. The
deliberate asymmetry (live path shows `decl_span`, disk path the name's
line) is documented and bounded by the caps; the duplicate-live-symbol
tie-break is inherited from the shared matcher and correctly pinned
rather than re-designed. The CRLF interior-`\r` cosmetic and the
double-read of large closed files are accepted as filed. Accepted;
committed by the lead.