Files
lucisv/docs/features/port-detail-spans
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00
..

port-detail-spans

Goal

A non-ANSI port is declared in two places: the header names it (data_in,) and a body declaration finishes it (input signed [31:0] data_in;). Every fact a reader wants — direction, signedness, width — is in the second place, and until this round hover and go-to-definition both pointed only at the first, so hovering a port of an older-style module showed a bare name and a comma. vlog_semantic now records, on the header symbol, the span of the body declaration that completes it (Symbol::detail_span); the server shows both in one hover card and offers both as definition locations, detail first. The same field travels through the workspace index and its on-disk facts, so a cross-file hover into a closed file gets the same two sections. Attached to that, because a card that shows a declaration should show what the author wrote about it: each section carries the comment that documents it.

Assumptions

  • One name, one symbol, per scope. bind::declare already turns a second declaration of a name into a reference to the first, which is exactly how a non-ANSI body declaration attaches to its header port. detail_span rides on that existing mechanism; nothing new decides what "the same port" means.
  • First writer wins. If a header somehow names a port twice, the first body declaration to arrive records its span and later ones are ignored. The result is deterministic, which is what matters here; a duplicated port is the linter's complaint to make, not hover's.
  • detail_span is None for everything else. An ANSI port, a wire, a module — anything complete where it is named — has no detail site, and both consumers render exactly the card they always did.
  • A real span is never empty, so (0, 0) is free to mean None in the facts codec. bind_port_decl only ever records a declaration's own span.
  • Comment attachment is textual, and errs towards attaching nothing. The disk path of a cross-file hover has no parse to ask, so the attachment rules work on lines. Only // lines and single-line /* … */ count as comment-only; the interior of a multi-line block comment is indistinguishable from code without lexing, and a hover card that guessed would be the wrong-line lie the snippet guards exist to prevent.
  • A comment below a declaration belongs to the next declaration unless nothing else could claim it. only_a_closer_on answers that with a whitelist, not a blacklist: the block attaches only when every word on the line after it is a block terminator (end, endmodule, endinterface, endtask, endfunction) — which includes a line with no words at all ();, a blank line, punctuation) — or when there is no line after it at all. Everything else, a bare input on the first line of a wrapped declaration included, reads as another declaration. The bias is the whole point: a false "another declaration is here" only ever declines to attach a comment, the direction that cannot lie, while a false "nothing here" shows one declaration's comment on another's card. (QA, PDS-1: the previous keyword-blacklist form got that backwards and did lie.)
  • The three comment sources are independent, as the owner's rule states them: a declaration takes the comment on its own line and the block above it and the block below it, each guarded on its own. Order is source order. (QA, PDS-2: the first implementation ran them as a priority chain and dropped a below-comment nothing else claimed.)
  • Comment attachment renders, it never rewrites. A CRLF file's line ends before its \n but after its \r, so attached_lines strips that \r as it composes each line — display text only; no buffer is touched (QA, PDS-4).

Handshakes

  • vlog_semantic::Symbol gains a field — detail_span: Option<TextRange>. Additive, like refactor-rename's Target::Declaration before it (§2.6 of topo.md): every existing reader of Symbol compiles and behaves identically, and the two struct literals that build a Symbol (bind.rs) set it. bind_port_decl is where it is written.
  • vlog_semantic::Def gains the same field, carried by the index's internal Decl. Def is what lucisv_server navigates by, so this is the wire that makes a cross-file hover show the body declaration. Def derives PartialEq; two Defs that differ only in detail_span cannot arise, because both come from the same Decl.
  • facts.rs's on-disk format changes — every declaration record now carries two more numbers, written unconditionally (0 0 for the None that every ordinary declaration has) so the stream stays flat and the reader stays a straight sequence of num()s. FACTS_FORMAT is bumped (2 → 3), so a store written by an earlier build is discarded whole: index_cache.rs's header line carries the number, and a header that is not this build's means the file is not read at all. Without the bump an old record would be reinterpreted — every field after a declaration's name span shifted by two — rather than dropped, and only the reader's done() check would stand between a shifted parse and a wrong answer. Dropping the store costs one rescan, once, which is what the persistent index is for (docs/features/index-persistence/README.md); it is the vlog_semantic/src/workspace.rs choke-point rule in topo.md §3.3.
  • nav.rs and hover.rs agree, by construction. Hover shows two sections — header, then detail — and nav::definition returns two locations in the same order, detail first, so "jump" lands where the card's second block is and the card's first block is the second location. Both read symbol.detail_span; there is no second rule for what a port's declaration is. An empty detail span is filtered out in nav.rs rather than trusted.
  • Consumes hover-declarations' guards unchanged. spells_exactly still gates every snippet read out of a file the index may be stale against; fenced_sections is applied after that guard on the disk path, and after live_symbol on the live path.

Status / TODO

  • Symbol::detail_span, written by bind_port_decl, first writer wins — for a module's non-ANSI port and for an old-style task/function argument, which merge through the same bind::declare.
  • Def::detail_span + the index's Decl carrying it.
  • facts.rs codec: unconditional pair, (0,0) = None, FACTS_FORMAT bumped so a stale store is not read as a fresh one.
  • hover::fenced_sections — one section, or header + detail.
  • nav::definition returns the detail location first.
  • Comment attachment (attached_lines): same-line, above and below, each guarded on its own, all three at once where all three are written.
  • Tests: vlog_semantic records the span for a split port and not for an ANSI one; hover shows two sections from either end of the port; comments attach above, below, and are not stolen from the next port; a cross-file split port shows both sections from the index.
  • 2026-08-28: the below-comment guard is a closer whitelist, so a wrapped next declaration keeps its own leading comment (QA, PDS-1).
  • 2026-08-28: above / beside / below are independent (QA, PDS-2).
  • 2026-08-28: an old-style task or function argument gets a detail_span too (QA, PDS-3).
  • 2026-08-28: a CRLF file's snippet carries no \r into the fence (QA, PDS-4).
  • Not done: detail_span for anything other than a port or a task/function argument. An extern method prototype and its out-of-body definition are the same two-places-one-symbol shape and would fit the field exactly, but no hover complaint has been filed about them and speculative generality is what CLAUDE.md says not to build.
  • Not done: a doc-comment model. The attachment above is positional and deliberately dumb — no /// convention, no markdown rendering of the comment body, no stripping of //. That is a feature of its own if it is ever wanted.

Work log

2026-08-28 — dev. Implemented as part of the LSP performance round (plan: "Improve LSP performance + hover accuracy", part B), continued by a second dev after the first stopped mid-round; the code below is the first dev's except where noted.

  • bind_port_decl looks the declarator's name up in the scope it is binding into before bind_declarators runs, because declare will turn the name into a reference and there would be no symbol id to write on afterwards. The lookup is the scope's own names map — the same map declare consults — so "the same port" is decided in exactly one place.
  • The codec writes the pair unconditionally rather than with a presence flag. A flag would have kept old records readable at the cost of a branch in the hot reader and a second thing to get wrong; dropping a stale record costs one reparse of that file, once.
  • Comment attachment was not in the plan. It is here because the first card the two-section change produced looked wrong without it: a port's comment sits on the line above or beside it, and showing the declaration without it loses the only prose the author wrote. The rules are the narrowest ones that never steal another declaration's comment, and each has a test.
  • Second dev, 2026-08-28: bumped FACTS_FORMAT 2 → 3, which the first dev's change owed and did not take. Its comment argued that a pre-field record is "one field short, fails done(), and is dropped" — true for the last declaration in a record, but a record with a declaration followed by anything else is not short, it is shifted, and relying on a trailing-length check to catch a field-order change is exactly what the format number exists to make unnecessary. The workspace VERSION is also in the store's header, but it has not moved this round, so without the bump a store written by the released 0.12.1 would have been read by the new codec. Otherwise no behavioral change: this README is the documentation the round owed, and cargo fmt was run over hover.rs.

2026-08-28 — dev (QA fixes, third dev). Every finding in the QA log below is addressed; each is marked fixed or left, with the reason.

  • PDS-1 — fixed. another_port_on/is_port_grammar_word are gone, replaced by only_a_closer_on/is_closer_word (hover.rs). The old predicate asked "does this line carry an identifier the port grammar cannot account for?", which is a blacklist: a line that is only keywords — the first line of a wrapped input\n [7:0] result; — answered "no other port here" and handed result's comment to data_in. The new one asks the question the other way round: every word on the line must be a block terminator, so a line with any word the whitelist does not name (a bare input included) reads as another declaration and the comment stays put. A line with no words at all — );, blank, punctuation — passes, which is what a last port's trailing comment is really followed by, and so does the end of the file, where there is no line left to claim anything. The doc comment now states that bias and why only that direction is safe.
    • name fell out of the predicate, and with it out of attached_lines and fenced_sections — the guard no longer needs to know which port it is guarding, only whether anything follows the comment. Three call sites simplified; no behaviour rides on the removal.
    • Pinned: a_wrapped_next_declaration_keeps_its_own_leading_comment (the QA repro verbatim, plus the assertion that the comment does reach result's own card) and a_wrapped_declaration_keeps_the_comment_below_it (the same wrapped shape as the last declaration still takes it). The existing a_comments_below_a_declaration_attach_only_when_no_port_follows_them — comment block above endmodule — still passes unchanged.
  • PDS-2 — fixed. attached_lines no longer runs a priority chain: the above walk, the declaration's own lines (which carry the same-line comment inherently) and the below walk are three independent collections joined in source order. has_comment_after was the chain's only reader and is deleted. Each guard is exactly as strict as before — the above walk still stops at the first line that is not comment-only, and the below guard got stricter with PDS-1 — so nothing new can be stolen; what changes is only that a below-comment nothing else claims is no longer silently dropped because something was also written above.
    • Pinned: a_declaration_shows_the_comments_above_beside_and_below_it_at_once (all three on one body declaration) and a_header_port_shows_the_comments_above_and_below_it_at_once, which is QA's repro 1 and repro 2 in one header — with the negative half (the same block is left alone when another port follows it) in the same test.
  • PDS-3 — fixed, not documented as a gap. Checked the binder first, as the brief asked: bind_tf_args declares task t(a);'s a as a TfArg in the task's own scope, and input [7:0] a; at statement position reaches bind_port_decl with that same scope, so declare turns it into a reference to the header symbol — the identical header→body merge a module port gets, one symbol and not two (verified by probe before changing anything). So the recording is extended rather than parked: the kind == SymbolKind::Port gate is gone and the inner test is symbol.kind == kind, which is the same condition for a port and now also holds for an argument. Pinned in vlog_semantic (an_old_style_task_argument_records_its_body_declaration_too, including that an ANSI-style argument still has none) and in hover (an_old_style_task_argument_hovers_as_two_sections_too).
  • PDS-4 — fixed. attached_lines's line reader strips a trailing \r from every line it composes, so a CRLF file's fenced block is \n-joined like any other. It is done there, at the one place all three collections (above, declaration, below) pass through, rather than in cut_to_size, which would have had to re-split the joined string. Display text only — nothing writes to the buffer. QA's pinned test a_crlf_multiline_snippet_keeps_interior_carriage_returns was the regression test for the old behaviour; it is now a_crlf_multiline_snippet_carries_no_carriage_returns, asserting no \r survives and that the lines read as written. (Editing a QA-authored test rather than deleting it: the finding it pins is the one being closed, and the same file's local_text path goes through the same helper, so it covers both branches QA named.)

QA log

2026-08-28 — QA. Verified against the owner's own spec ("two sections … same line, or above it (if no other port on the line above), or below it (if no other port on the line below)"), and attacked with the shapes named in the QA brief. Gates green (see change-debounce's QA log for the shared run). All reproductions used temporary #[test]s in hover.rs, run once, and removed after confirming — none are left in the tree, except the one noted below as a kept, permanent regression test.

  • Finding PDS-1 (high — a wrong comment shown, the one invariant this feature explicitly promises never to violate). another_port_on (hover.rs:355) — the guard that decides whether a below-comment block belongs to this declaration or the next one — inspects only the single line immediately following the comment block. When the next port's own declaration wraps, with its direction keyword alone on the first line and its name on a later line, every word on that first line is in is_port_grammar_word's list (hover.rs:368, which includes bare input/output/etc.), so another_port_on returns false — "no other port here" — and the comment attaches to the wrong declaration.

    • Repro:
      module alu (data_in, result);
      input [7:0] data_in;
      // about result
      input
          [7:0] result;
      endmodule
      
      Hovering data_in shows a detail section reading input [7:0] data_in;\n// about result — result's own leading comment, attached to data_in's card. Confirmed live (temporary test asserting the comment is not present failed with exactly this text).
    • This is precisely the failure mode docs/features/port-detail-spans/ README.md's own Assumptions section rules out: "a false 'yes' only ever declines to attach a comment — the direction that cannot lie." Here a false no (failing to recognize result's declaration as a port) makes it lie. Left unfixed — not small/unambiguous; the honest fix wants another_port_on (or its caller) to look past a line that is entirely grammar words with no other identifier, to the line(s) after it, which is a real design change to a predicate three call sites and several tests depend on.
  • Finding PDS-2 (medium — divergence from the stated spec, not corruption; the one the QA brief asked to check explicitly). The owner's rule reads as three independently-checked sources (same line, above, below). The implementation instead runs a strict priority chain in attached_lines (hover.rs:297): below-comment collection is skipped entirely whenever there is any above-comment, or any same-line trailing comment (hover.rs:313, if above.is_empty() && !decl.last()...has_comment_after) — even when the below comment is otherwise unclaimed by anything else (no other port follows it).

    • Repro 1 (above blocks below):
      module alu (
          // above
          data_in
          // below
      );
      input [7:0] data_in;
      endmodule
      
      Header section shows // above + data_in only; // below is silently dropped, though nothing else claims it (the next line is );).
    • Repro 2 (same-line trailing comment blocks below):
      module alu (
          data_in  // right here
          // below
      );
      input [7:0] data_in;
      endmodule
      
      Header section shows data_in // right here only; // below dropped.
    • Not unsafe (never shows the wrong comment — just fewer than the literal spec asks for), so lower severity than PDS-1, but a real, reproducible divergence. Left unfixed — a design call (should a declaration ever show two attached comments at once?) belongs to the dev/lead, not QA.
  • Finding PDS-3 (low — silent feature-completeness gap, not a bug). bind_port_decl's detail_span-recording branch is gated on kind == SymbolKind::Port (bind.rs:512), which excludes SymbolKind::TfArg — old-style (non-ANSI) task/function port declarations (task t(a);\n input a;\n ...\nendtask) never get a detail_span, even though they are exactly the same two-places-one-symbol shape a module's non-ANSI port is, and bind_port_decl is already reached at statement position for them (bind.rs:1030, StmtKind::PortDecl). The feature's own "Not done" list names only extern method prototypes as the deliberately parked case; task/function old-style ports are a second, unmentioned one. Falls back to the single-section card exactly as before this round — no regression, just an undocumented gap, worth a line in Status/TODO if the dev wants it.

  • Finding PDS-4 (low/cosmetic — pre-existing, amplified by this round, not introduced by it). On a CRLF file, a multi-line fenced snippet embeds a raw \r byte in the middle of the code fence for every joined line except the last (whose trailing \r happens to be stripped because cut_to_size's trim_end() only trims the very end of the whole joined string, not each line). This is pre-existing behavior of Positions::whole_lines / cut_to_size — local_text already had the same characteristic for any multi-line decl_span before this round — but comment attachment now makes multi-line snippets routine (any port with a nearby comment, not just a rare multi-line declaration), so the gap is hit far more often in practice. Repro: a CRLF split-port module with an above-comment; the header section's rendered text contains a literal \r between the comment line and the declaration line. Cosmetic only (a stray control character, not wrong text); not fixed, flagged for whoever touches cut_to_size next.

  • Attack angles checked and clean (no finding):

    • A port declared twice — first-writer-wins confirmed: the second body declaration's comment (// second (duplicate...)) never appears; only the first's (// first) does, matching bind_port_decl's documented policy.
    • ANSI mixed with non-ANSI in one header — a full ANSI port (input wire clk,) and a split non-ANSI port (data_in) in the same header list: clk renders exactly one fenced section, data_in exactly two, no cross-contamination.
    • A comment containing // inside a string on the declaration line — the whole line is shown verbatim regardless (the attachment logic never parses the line's meaning, only whether has_comment_after sees /// /* after the name), so there is no wrong-line risk, only the expected line reproduced as written.
    • `ifdef/ `endif between the header and the body declaration — correctly ignored as neither comment-only nor a stolen declaration; the detail section is exactly the body decl's own line(s). (Note: a `endif/ `ifdef/ `define line sitting where "no other port" is required is itself treated as "another port" by another_port_on's conservative word-matching — a false positive that only declines to attach, so it is safe, just another instance of the documented conservative-below-the-fold behavior, not a new bug.)
    • An attribute ((* foo *)) prefixing the next port's declaration — correctly detected as "another port" (the attribute's own words split out as ordinary non-grammar identifiers), so the comment is not stolen.
    • A block comment /* … */ spanning multiple lines — the interior lines are (by design, and correctly) not recognized as comment-only, so the walk stops there and nothing is attached from inside the block — safe (declines rather than guesses), matching the documented limitation.
  • Facts-format bump (2 → 3): verified the drop, not a shifted read. Wrote and kept a permanent regression test, a_store_from_the_previous_facts_format_is_dropped_not_misread (crates/lucisv_server/src/index_cache.rs, next to the existing a_store_from_another_version_is_dropped_whole_and_replaced and a_store_from_the_previous_store_format_is_dropped_whole_and_replaced, which cover a VERSION mismatch and a CACHE_FORMAT mismatch but, before this, never a FACTS_FORMAT-only mismatch). It writes a store whose header says FACTS_FORMAT 2 with today's CACHE_FORMAT and VERSION otherwise unchanged, opens it, and asserts facts_for returns None (the whole store is unread, not field-shifted) and that the first write since replaces it under today's header, readable afterward. Passes. This closes a real test gap the round left — the design (the format number lives in the header line IndexCache::open compares byte-for-byte, index_cache.rs:103) was sound but untested for this exact axis.

  • A3 tie-break: confirmed correct, both via the shipped looking_a_name_up_answers_exactly_what_scanning_every_symbol_would (passes) and by re-deriving it: by_name is built with a stable sort (Vec::sort_by, lib.rs), so symbols sharing a name keep declaration order, and symbols_named's two partition_point binary searches over that name-sorted table can only reorder within a name if the sort were unstable — it is not.

  • nav::definition / hover agreement: confirmed the detail location is filtered by !span.is_empty() (nav.rs) exactly as documented, and that a detail-less symbol (ANSI port, ordinary declaration) still returns exactly one location, unchanged from before this round.

Review

2026-08-28 — lead. Accepted after one fix round. Gates re-run independently: fmt clean, clippy silent, cargo test --workspace green (2 396 passing at the time of review), cargo test -p vlog_formatter green.

detail_span is the right adaptation of the "detail range" idea to this model: the non-ANSI merge already produced one symbol, so the only thing missing was where the declaration is completed, and recording that as an optional second span leaves every complete-where-named declaration untouched. Hover and go-to-definition read the same field, which keeps topo.md's one-matcher rule intact.

Two things in this feature deserved the scrutiny they got.

The facts-format bump. The first implementation added two numbers per declaration record without moving FACTS_FORMAT, on the argument that a short record fails done() and is dropped. That argument only holds for the last declaration in a record; every earlier one would have been read field-shifted by two, which is a persisted cache handing back wrong spans — the exact shape invariant 4 exists to catch, in the same family as the macro-items corruption. Bumping 2→3 is correct and QA's added regression test (index_cache.rs, a store from the previous format is dropped, not misread) closes a gap no existing test covered, since nothing varied FACTS_FORMAT alone.

The comment attachment. This began as scope creep — round 1's plan explicitly deferred doc comments on hover — and then turned out to be precisely what the user asked for, so it stays. But it is a textual heuristic running over disk text on the cross-file path, and QA's PDS-1 found it lying: another_port_on was a keyword blacklist, so a wrapped declaration whose first line is bare input looked like an empty line and the next port's comment attached to the previous port's card. Replacing the blacklist with a closer whitelist (only_a_closer_on) inverts the bias in the one direction that is safe — an unrecognised line now declines to attach rather than mis-attaching — and the doc comment states that bias as the contract. That is the correct resolution; a predicate over source text that guesses must fail closed.

PDS-2 was a genuine divergence from the user's stated rule and is fixed by making the three comment sources independent rather than a priority chain. PDS-3 was worth chasing rather than parking: the binder does perform the same header/body merge for TfArg, so old-style task and function arguments now get detail spans too, and the gap is closed rather than documented. PDS-4's CRLF strip is display-only and does not touch the buffer.

The dev renamed and inverted a QA-authored test, and flagged it. Correct call and correctly surfaced: the behaviour that test pinned was the finding.