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:
lucioandClaude Fable 5 committed 2026-08-24 01:42:48 -07:00
1 parent fda6d20d9f
commit de8a307e00
6 files changed
+600 -6

No files matched your search

+3 -1
View File
@@ -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
+184
View File
@@ -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)_
+46 -5
View File
@@ -1,12 +1,14 @@
# vfmt for VS Code
A thin extension: it registers the Verilog/SystemVerilog file types and starts
`vfmt-server`. Every feature you see — formatting, diagnostics, semantic
highlighting, hover, go-to-definition, references, symbols — comes from the
server, so this directory contains no language logic and no editor-specific
commands. See `docs/features/vfmt-server/README.md` for the capability list and
`vfmt-server`. Every *analysis* feature you see — formatting, diagnostics,
semantic highlighting, hover, go-to-definition, references, symbols — comes
from the server, so this directory contains no language logic and no
editor-specific commands. The one exception is the snippet library below, which
is static data VS Code reads straight from the manifest. See
`docs/features/vfmt-server/README.md` for the capability list,
`docs/features/editor-packaging/README.md` for why the extension looks like
this.
this, and `docs/features/editor-snippets/README.md` for the snippets.
## 1. Build the server
@@ -79,6 +81,45 @@ the CLI does; when it names a setting, it wins over VS Code's tab size (an
editor default is not a deliberate override). Without a config, VS Code's tab
size governs the indent and the line length is the built-in 100.
## Snippets
Type a prefix and pick the snippet from the completion list; `Tab` walks the
placeholders. These are a static manifest contribution, so they work with no
server on `PATH` and before the extension has activated. Bodies are
dialect-specific — `module` gives `input wire` in a `.v` file and `input logic`
in a `.sv` one.
| Verilog (`.v`, `.vh`) | |
|---|---|
| `module` | module with a port list |
| `always` / `alwayscomb` | clocked block with async active-low reset / `always @(*)` |
| `case` | `case` with a `default` arm |
| `function` / `task` | Verilog-2001 declaration with an ANSI argument list |
| `genfor` / `for` | `generate`+`genvar` loop with a named block / plain loop |
| `initial` | `initial begin … end` |
| `tb` | testbench module: clock, reset, `$display`, `$finish` |
| `guard` | `` `ifndef ``/`` `define `` include guard from the file name |
| SystemVerilog (`.sv`, `.svh`) | |
|---|---|
| `module` / `modulep` | module with ports / with a parameter list too |
| `always_ff` / `always_comb` | clocked block with async active-low reset / combinational |
| `case` / `uniquecase` | `case` with a `default` arm / `unique case` |
| `fsm` | two-process state machine: enum, next-state logic, state register |
| `enum` / `struct` | `typedef enum` / `typedef struct packed` |
| `function` / `task` | `automatic` declaration |
| `interface` | interface with two modports |
| `package` / `class` | package named after the file / class with a constructor |
| `assertp` | labelled concurrent assertion with `disable iff` and `$error` |
| `genfor` | `for (genvar …)` with a named block |
| `initial` | `initial begin … end` |
| `tb` | testbench module: clock, reset, `$finish` |
| `guard` | `` `ifndef ``/`` `define `` include guard from the file name |
To add your own without editing the extension, use VS Code's
**Snippets: Configure Snippets** command and pick `verilog` or
`systemverilog`; user snippets sit alongside these, they do not replace them.
## Format on save
The extension registers no formatter of its own — the server advertises
+10
View File
@@ -47,6 +47,16 @@
]
}
],
"snippets": [
{
"language": "verilog",
"path": "./snippets/verilog.json"
},
{
"language": "systemverilog",
"path": "./snippets/systemverilog.json"
}
],
"configuration": {
"title": "vfmt",
"properties": {
+226
View File
@@ -0,0 +1,226 @@
{
"Module": {
"prefix": "module",
"body": [
"module ${1:name} (",
" input logic ${2:clk},",
" input logic ${3:rst_n}$4",
");",
" $0",
"endmodule"
],
"description": "Module with a port list"
},
"Parameterized module": {
"prefix": "modulep",
"body": [
"module ${1:name} #(",
" parameter int ${2:WIDTH} = ${3:8}",
") (",
" input logic ${4:clk},",
" input logic ${5:rst_n}$6",
");",
" $0",
"endmodule"
],
"description": "Module with parameters and ports"
},
"always_ff": {
"prefix": "always_ff",
"body": [
"always_ff @(posedge ${1:clk} or negedge ${2:rst_n}) begin",
" if (!$2) begin",
" ${3:q} <= '0;",
" end else begin",
" $0",
" end",
"end"
],
"description": "Sequential block with async active-low reset"
},
"always_comb": {
"prefix": "always_comb",
"body": [
"always_comb begin",
" $0",
"end"
],
"description": "Combinational always_comb block"
},
"Case with default": {
"prefix": "case",
"body": [
"case (${1:sel})",
" ${2:value}: ${3:statement};",
" default: ${4:statement};",
"endcase"
],
"description": "Case statement with a default branch"
},
"Unique case": {
"prefix": "uniquecase",
"body": [
"unique case (${1:sel})",
" ${2:value}: ${3:statement};",
" default: ${4:statement};",
"endcase"
],
"description": "unique case with a default branch"
},
"FSM skeleton": {
"prefix": "fsm",
"body": [
"typedef enum logic [${1:1}:0] {",
" ${2:Idle},",
" ${3:Busy}",
"} state_e;",
"",
"state_e state_q, state_d;",
"",
"always_comb begin",
" state_d = state_q;",
" unique case (state_q)",
" $2: state_d = ${6:$3};",
" $3: state_d = $2;",
" default: state_d = $2;",
" endcase",
"end",
"",
"always_ff @(posedge ${4:clk} or negedge ${5:rst_n}) begin",
" if (!$5) state_q <= $2;",
" else state_q <= state_d;",
"end"
],
"description": "Two-process state machine: enum, next-state logic, state register"
},
"typedef enum": {
"prefix": "enum",
"body": [
"typedef enum logic [${1:1}:0] {",
" ${2:ValueA},",
" ${3:ValueB}",
"} ${4:name_e};"
],
"description": "typedef enum"
},
"typedef struct": {
"prefix": "struct",
"body": [
"typedef struct packed {",
" logic ${1:[7:0]} ${2:field};",
"} ${3:name_t};"
],
"description": "typedef struct packed"
},
"Function": {
"prefix": "function",
"body": [
"function automatic ${1:logic [7:0]} ${2:name}(input ${3:logic [7:0]} ${4:arg});",
" $0",
" return ${5:result};",
"endfunction"
],
"description": "Automatic function"
},
"Task": {
"prefix": "task",
"body": [
"task automatic ${1:name}(input ${2:logic [7:0]} ${3:arg});",
" $0",
"endtask"
],
"description": "Automatic task"
},
"Interface": {
"prefix": "interface",
"body": [
"interface ${1:name}_if (input logic ${2:clk});",
" logic ${3:valid};",
" logic ${4:ready};",
"",
" modport ${5:master} (output $3, input $4);",
" modport ${6:slave} (input $3, output $4);",
"endinterface"
],
"description": "Interface with two modports"
},
"Package": {
"prefix": "package",
"body": [
"package ${1:${TM_FILENAME_BASE}};",
" $0",
"endpackage"
],
"description": "Package named after the file"
},
"Class": {
"prefix": "class",
"body": [
"class ${1:name};",
" function new();",
" $0",
" endfunction",
"endclass"
],
"description": "Class with a constructor"
},
"Assert property": {
"prefix": "assertp",
"body": [
"${1:label}: assert property (@(posedge ${2:clk}) disable iff (!${3:rst_n})",
" ${4:antecedent} |-> ${5:consequent})",
" else \\$error(\"${6:$1 failed}\");"
],
"description": "Labeled concurrent assertion with disable iff"
},
"Generate for": {
"prefix": "genfor",
"body": [
"for (genvar ${1:i} = 0; $1 < ${2:N}; $1++) begin : ${3:gen_$1}",
" $0",
"end"
],
"description": "Generate for loop with a named block"
},
"Initial block": {
"prefix": "initial",
"body": [
"initial begin",
" $0",
"end"
],
"description": "Initial block"
},
"Testbench skeleton": {
"prefix": "tb",
"body": [
"module ${1:${TM_FILENAME_BASE}};",
" logic clk = 0;",
" logic rst_n;",
"",
" always #${2:5} clk = ~clk;",
"",
" initial begin",
" rst_n = 0;",
" repeat (2) @(posedge clk);",
" rst_n = 1;",
" $0",
" \\$finish;",
" end",
"endmodule"
],
"description": "Testbench with clock and reset"
},
"Include guard": {
"prefix": "guard",
"body": [
"`ifndef ${1:${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_SVH}",
"`define $1",
"",
"$0",
"",
"`endif // $1"
],
"description": "ifndef/define include guard from the file name"
}
}
+131
View File
@@ -0,0 +1,131 @@
{
"Module": {
"prefix": "module",
"body": [
"module ${1:name} (",
" input wire ${2:clk},",
" input wire ${3:rst_n}$4",
");",
" $0",
"endmodule"
],
"description": "Module with a port list"
},
"Clocked always block": {
"prefix": "always",
"body": [
"always @(posedge ${1:clk} or negedge ${2:rst_n}) begin",
" if (!$2) begin",
" ${3:q} <= ${4:1'b0};",
" end else begin",
" $0",
" end",
"end"
],
"description": "Sequential always block with async active-low reset"
},
"Combinational always block": {
"prefix": "alwayscomb",
"body": [
"always @(*) begin",
" $0",
"end"
],
"description": "Combinational always @(*) block"
},
"Case with default": {
"prefix": "case",
"body": [
"case (${1:sel})",
" ${2:value}: ${3:statement};",
" default: ${4:statement};",
"endcase"
],
"description": "Case statement with a default branch"
},
"Function": {
"prefix": "function",
"body": [
"function ${1:[7:0]} ${2:name}(input ${3:[7:0]} ${4:arg});",
" begin",
" $2 = ${5:$4};",
" end",
"endfunction"
],
"description": "Function declaration"
},
"Task": {
"prefix": "task",
"body": [
"task ${1:name}(input ${2:[7:0]} ${3:arg});",
" begin",
" $0",
" end",
"endtask"
],
"description": "Task declaration"
},
"Generate for": {
"prefix": "genfor",
"body": [
"genvar ${1:i};",
"generate",
" for ($1 = 0; $1 < ${2:N}; $1 = $1 + 1) begin : ${3:gen_$1}",
" $0",
" end",
"endgenerate"
],
"description": "Generate for loop with a named block"
},
"For loop": {
"prefix": "for",
"body": [
"for (${1:i} = 0; $1 < ${2:N}; $1 = $1 + 1) begin",
" $0",
"end"
],
"description": "For loop"
},
"Initial block": {
"prefix": "initial",
"body": [
"initial begin",
" $0",
"end"
],
"description": "Initial block"
},
"Testbench skeleton": {
"prefix": "tb",
"body": [
"module ${1:${TM_FILENAME_BASE}};",
" reg clk = 1'b0;",
" reg rst_n;",
"",
" always #${2:5} clk = ~clk;",
"",
" initial begin",
" rst_n = 1'b0;",
" repeat (2) @(posedge clk);",
" rst_n = 1'b1;",
" $0",
" \\$display(\"${3:done}\");",
" \\$finish;",
" end",
"endmodule"
],
"description": "Testbench with clock and reset"
},
"Include guard": {
"prefix": "guard",
"body": [
"`ifndef ${1:${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_VH}",
"`define $1",
"",
"$0",
"",
"`endif // $1"
],
"description": "ifndef/define include guard from the file name"
}
}