VS Code: snippet templates for common Verilog/SV constructs
30 snippets across both language ids: module, always/always_ff/always_comb with reset, case-with-default, FSM skeleton, interface with modports, package, class, assert property, testbench, include guard, and more. All 30 rendered bodies parse with vfmt itself; $-escaping verified by round-tripping through the VS Code snippet grammar. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
fda6d20d9f
commit
de8a307e00
6 files changed
+600
-6
No files matched your search
@@ -12,7 +12,9 @@ below the protocol layer"* — read in the other direction: no *language* logic
|
||||
above it either. Every feature a user sees comes from the server, so this
|
||||
feature ships one 41-line JavaScript file, one setting, two markdown guides,
|
||||
and a dependency-free smoke test that proves the extension's spawn contract
|
||||
matches the binary's reality without opening an editor.
|
||||
matches the binary's reality without opening an editor. (One later exception:
|
||||
static snippet templates, which LSP has no channel for — see
|
||||
`docs/features/editor-snippets/README.md`.)
|
||||
|
||||
## Assumptions
|
||||
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
# 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 `vfmt-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 vfmt'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 `vfmt.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 `vfmt-server`. This is deliberate: snippets are a
|
||||
static manifest contribution, so they work before the server starts, if the
|
||||
server is missing, and if `vfmt.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.
|
||||
- [ ] 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 **vfmt's own parser**
|
||||
(`target/debug/vfmt --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 vfmt 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.
|
||||
|
||||
## QA log
|
||||
|
||||
_(empty — no QA pass yet)_
|
||||
|
||||
## Review
|
||||
|
||||
_(reserved for the lead)_
|
||||
Reference in new issue
Block a user