Files

232 lines
14 KiB
Markdown

# editor-snippets
## Goal
Give the VS Code extension the templates every RTL and verification engineer
retypes: a module header, a clocked `always`/`always_ff`, a `case` with a
`default`, a function/task, a generate-for, an interface with modports, an FSM
skeleton, a testbench with clock and reset, an include guard. This is the last
of the five editor features the owner asked for, and the only one that needs no
server work at all: VS Code's `contributes.snippets` is a static JSON
contribution the editor reads from `package.json` without ever activating the
extension, so the feature costs two data files, ten lines of manifest, and no
new code path in `extension.js` or `lucisv-server`. Two files, one per language
id, because the same prefix wants a different body per dialect: `module` gives
`input wire` in Verilog and `input logic` in SystemVerilog, `function` gives a
`begin`/`end` return-assignment in Verilog and `automatic` + `return` in
SystemVerilog.
## Assumptions
- **`$` in a snippet body is snippet syntax, not Verilog.** VS Code's snippet
grammar reads `$1`/`${1:x}` as tabstops and `$NAME` as a variable, and an
*unknown* variable is substituted with the empty string — silently. So an
unescaped `$finish` inserts nothing at all, and the bug is invisible in the
JSON. Every Verilog system task in these files is written `\\$` in the JSON
(→ `\$` after `JSON.parse` → a literal `$` to the snippet engine). Three
occurrences today: `\$error` (`assertp`), `\$finish` (both `tb`),
`\$display` (Verilog `tb`). The checker described under *Work log* fails the
build on any `$word` that is not a known VS Code variable, precisely so a
future `$random` cannot be added unescaped.
- **The inserted text, with every default accepted, is valid Verilog.** A
snippet whose untouched expansion does not parse is a snippet that hands the
user a red squiggle from our own server. This is verified, not assumed — see
the *Work log*; it is what caught the Verilog `function` body.
- **Placeholder *text* may be prose (`value`, `statement`, `antecedent`) even
where prose is not an expression.** Those are selected on insert and typed
over; they are labels, not code. The line above is about the code the user
keeps, not about placeholder labels — and in practice lucisv's parser accepts
the two we ship (`value: statement;`) as an identifier statement anyway.
- **Two-space indent matches the formatter.** `vlog_formatter::Config::default`
is `indent_width: 2` (`crates/vlog_formatter/src/format.rs:27`), so a
snippet body inserted into a file with no `lucisv.toml` is not immediately
re-indented on the next format-on-save.
- **Prefixes are unique within a language and are not reserved.** VS Code
offers a snippet whose prefix matches the typed word alongside ordinary
completions; a prefix that equals a keyword (`module`, `case`, `for`,
`initial`, `interface`, `class`, `package`, `function`, `task`) is the
normal, expected spelling and is what every other Verilog extension uses.
- **`TM_FILENAME_BASE` is a usable identifier.** `package`, `tb` and the
include guards default to the file's base name. A file named with characters
illegal in a Verilog identifier (a dash, a dot) yields a default the user has
to edit — acceptable, since the value lands in a selected placeholder.
- **The user's file has the language id `verilog` or `systemverilog`.** Same
assumption `editor-packaging` already records; a `.sv` file another extension
claimed gets that extension's snippets, not these.
## Handshakes
- **Consumes** nothing from `lucisv-server`. This is deliberate: snippets are a
static manifest contribution, so they work before the server starts, if the
server is missing, and if `lucisv.serverPath` is wrong. Nothing in
`extension.js` reads or registers them.
- **Consumes** the two language ids declared in
`editors/vscode/package.json` → `contributes.languages` (`verilog`,
`systemverilog`). If those ids ever change, `contributes.snippets[].language`
must change with them; a mismatch is silent (VS Code just never offers the
snippets).
- **Promises** the packaged extension keeps `snippets/*.json`. Verified against
`.vscodeignore` with `vsce ls` — see *Work log*. Anything added to
`.vscodeignore` must not match `snippets/`.
- **Request to the lead (not done here, outside this feature's scope):**
`editors/vscode/README.md` says the extension registers file types and starts
the server and that "every feature you see comes from the server". That is
now false by one item, so this feature adds a *Snippets* section to that file
— but `docs/features/editor-packaging/README.md` makes the same claim in its
*Goal* and enumerates what the extension deliberately does not ship, and that
README belongs to another feature. It wants one row/sentence saying snippets
arrived later, which the lead should place.
## Status / TODO
- [x] Review the two drafted files for JSON validity.
- [x] Review snippet-syntax escaping; confirm every literal `$` round-trips.
- [x] Review dialect correctness (Verilog-2001 vs SystemVerilog bodies).
- [x] Review prefixes for collisions and shadowing.
- [x] Fix what the review found (three changes, listed in the work log).
- [x] Register both files in `package.json` → `contributes.snippets`.
- [x] Verify the packaged `.vsix` includes them (`vsce ls`).
- [x] Feature README.
- [x] **2026-08-24, M6:** hand the statement- and block-level templates to the
server and keep only the file-level ones here — nine snippets removed,
the two READMEs' prefix tables updated. See the work-log entry below and
`docs/features/completion-templates/README.md`.
- [ ] Not done, deliberately: no snippets for UVM (`uvm_component_utils`,
factory registration) — they belong with a UVM-aware feature, not in a
base file the whole ecosystem sees. No `always_latch`, no `covergroup`,
no `sequence`: each is a template somebody wants and nobody agrees on the
shape of, and an unused snippet is noise in the completion list.
- [ ] Not done, deliberately: no `language-configuration.json`. Snippets do not
need it, and `editor-packaging` already recorded the reasoning for
leaving it out.
## Work log
### 2026-08-24 — M1: review, fix and register the drafted snippets
**Reviewed** `editors/vscode/snippets/verilog.json` (10 snippets as drafted)
and `editors/vscode/snippets/systemverilog.json` (19). Both were valid JSON as
drafted and both `\\$` escapes in the SystemVerilog file (`\$error`,
`\$finish`) were already correct — confirmed, not assumed, by parsing.
**How it was verified.** Reading snippet JSON does not tell you what the editor
inserts, so two throwaway checkers were written (in the scratchpad, not
committed — they check data files, and nothing in the repo runs `node` in CI):
1. A parser for the documented VS Code snippet grammar (tabstop, placeholder,
choice, variable, transform, and the `\$`/`\\`/`\}` escapes) that renders
"what is inserted if the user accepts every default", resolving mirrors
(`$2` after `${2:name}`) to the placeholder's text. It hard-fails on `$` +
identifier that is not one of VS Code's known variables — the exact shape an
unescaped `$finish` would take — and warns on tabstop-numbering gaps, unknown
snippet keys, duplicate prefixes within a file and a missing description.
Result after the fixes below: **ALL OK**, 30 snippets, no failures. The
rendered `assertp` contains a literal `else $error("…")`, the two `tb`
snippets a literal `$finish;`, the Verilog `tb` a literal `$display("done")`.
2. The rendered text of all 30 was then fed through **lucisv's own parser**
(`target/debug/lucisv --check`), wrapping statement-level snippets in
`module wrapper_m; … endmodule` and expression-level ones additionally in
`initial begin … end`. Result: **30/30 parse.** Every "would be reformatted"
diff among them is the empty `$0` line or lucisv collapsing a two-port module
onto one line, never an indentation mismatch.
**Three fixes, all found by check 2 or by the dialect review:**
- `verilog.json` / *Function*: the body was `$2 = $0;`, i.e. the inserted text
was `name = ;` — the one snippet of the 30 that **failed to parse**
(`parse error: expected expression (found ";")`). The final-cursor marker was
doing duty as the right-hand side, so the template handed the user a syntax
error and our own server flagged it. Now `$2 = ${5:$4};` → `name = arg;`,
which parses, and the RHS is a selected placeholder mirroring the argument
name. The snippet ends without an explicit `$0`, so the cursor lands after
`endfunction` — the same place `$0` at the end would put it.
- `verilog.json` / *Function*: return/argument widths defaulted to
`[WIDTH-1:0]`, referencing a parameter the snippet does not declare, while
the sibling *Task* snippet used `[7:0]`. Both are now `[7:0]`.
- `systemverilog.json` / *FSM skeleton*: the `unique case` arms were
`$2: $0` / `$3: ;`, so the expansion was `Idle:` followed by `Busy: ;` — an
empty case arm with no placeholder to type over, which reads to a compiler as
a labelled statement rather than as the state's body. Arms are now
`$2: state_d = ${6:$3};` and `$3: state_d = $2;`, which is a real (if
trivial) two-state machine on insert and leaves the interesting arm as a
selected placeholder.
**One addition (deviation from a strict "review only" reading of the plan):**
`verilog.json` had no testbench template while `systemverilog.json` had one, so
Verilog users got no `tb` — an asymmetry in a feature whose whole point is
common templates. Added *Testbench skeleton* (prefix `tb`) mirroring the
SystemVerilog one in `reg`/`1'b0` spelling, with `\$display` and `\$finish`.
It parses and it is the only Verilog-file exercise of the `$`-escaping rule.
**Registration.** `contributes.snippets` added to
`editors/vscode/package.json`, `verilog` → `./snippets/verilog.json`,
`systemverilog` → `./snippets/systemverilog.json`. No `activationEvents`
change: snippet contributions are read from the manifest and do not activate
the extension, which is why they work with no server on `PATH`.
**Packaging.** `npx --no-install @vscode/vsce ls` (already in the npx cache; no
new tooling installed, per the plan) accepts the manifest and lists exactly six
non-dependency files: `package.json`, `extension.js`, `README.md`, `LICENSE`,
`snippets/verilog.json`, `snippets/systemverilog.json`. `.vscodeignore` needed
no change.
**Also updated** `editors/vscode/README.md` with a *Snippets* section (the
prefix tables per language and the "no server needed" note), since that file
told users every feature comes from the server.
**Not touched:** no Rust file, no `crates/`, no `extension.js`. `TODO.md` has
no snippets item to check off — §6's shipped-capabilities list mentions
"declaration snippets" only as part of `hover`, which is a different thing —
so it was left alone.
### 2026-08-24 — M2: nine templates moved server-side in M6
`completion-templates` (M6) makes the server offer the statement and block
templates itself, gated by where the cursor actually is and by the file's
dialect — something a static `contributes.snippets` entry cannot do, since VS
Code offers a snippet on prefix alone, anywhere in the file. Two lists offering
the same prefix would have meant two near-identical items in one completion
list, so the overlapping snippets were **removed here** rather than duplicated:
| File | Snippet | Prefix | Replaced by |
|---|---|---|---|
| `systemverilog.json` | always_ff | `always_ff` | server `always_ff with reset` |
| `systemverilog.json` | always_comb | `always_comb` | server `always_comb begin/end` |
| `systemverilog.json` | Case with default | `case` | server `case/endcase` |
| `systemverilog.json` | Initial block | `initial` | server `initial begin/end` |
| `verilog.json` | Clocked always block | `always` | server `always @(posedge clk or negedge rst_n)` |
| `verilog.json` | Combinational always block | `alwayscomb` | server `always @(*)` |
| `verilog.json` | Case with default | `case` | server `case/endcase` |
| `verilog.json` | For loop | `for` | server `for loop` |
| `verilog.json` | Initial block | `initial` | server `initial begin/end` |
`alwayscomb` is the one removal whose prefix does *not* collide: VS Code
matches a typed `always` against it as a subsequence, so it appeared beside the
server's `always @(*)` with a byte-identical body. Removing it is a judgment
call and is flagged as one.
**Everything file-level stays**, and stays static, so it still works with no
server on `PATH`: `module`, `modulep`, `fsm`, `enum`, `struct`, `function`,
`task`, `interface`, `package`, `class`, `assertp`, `genfor`, `uniquecase`,
`tb`, `guard`. `uniquecase` is kept deliberately — `unique case` is a different
construct from `case`, not a second spelling of it, and the server offers no
template for it. `genfor` is kept for the same reason: a `generate` loop is a
module item, not the procedural `for` the server offers.
**Verified.** Both files re-parsed as JSON; prefixes unique within each file;
the `$`-escaping check re-run over what remains (`\$display`, `\$finish`,
`\$error` in the two `tb` snippets and `assertp` — the three occurrences the
*Assumptions* section names, all still escaped, none of them in a removed
snippet). `npx --no-install @vscode/vsce ls` still accepts the manifest and
still lists both `snippets/*.json`. Counts: `systemverilog.json` 19 → 15,
`verilog.json` 11 → 6. `editors/vscode/README.md`'s two prefix tables were
rewritten to match and now say where the statement templates went.
## QA log
_(empty — no QA pass yet)_
## Review
_(reserved for the lead)_