# completion-templates M6 of the refactoring plan (`docs/features/refactor/README.md`): **statement and item templates** in the server's completion response, plus the **0.4.0 version bump**. ## Goal Make the completion list finish the *construct*, not the word. Typing `if` inside an `always` block should offer `if begin/end` and, on accept, write ```systemverilog if (cond) begin end ``` at the line's own indentation, with `cond` selected and TAB moving into the body — and the same for `if/else`, `else if`, `case`, the loops, the `always` family, `initial` and the everyday declarations. This is M5's completion machinery with one tier added: the templates are a sixth source of items, ranked between what the code declares and the bare keywords, and gated by two questions the server can already answer from the token stream — *may a statement be written here?* and *may a module item?* Nothing is offered that the buffer's own dialect would reject, and every template's untouched expansion is fed to lucisv's own parser by the test suite, because a template that hands the user a red squiggle from our own server is worse than no template. Because this lives in the server, Vim gets it too — which is the reason it is here rather than in `editors/vscode/snippets/*.json`, where nine of these templates used to live. Those are gone; see *Reconciliation* below. ## Assumptions - **A label that is not a keyword is a feature, not a workaround.** Dedup in `Items` is by label, so a template labelled `if` would have swallowed the keyword `if`. Every label is prose (`if begin/end`, `always_ff with reset`, `logic [N:0] name;`) and `filterText` carries the word the user actually types, so both items appear and the client still matches the template on `if`. `no_label_is_a_bare_keyword_and_no_label_repeats` pins it. - **Position is judged over the token stream, not a parse.** The statement gate reuses M4's `pairs::analyze` scope chain — the same heuristic `at_module_item` has always run on — so a half-typed buffer costs nothing extra and an unbalanced one still answers. Both gates are wrong in the same directions the M4 README already records; the failure mode is a template not offered, never a template offered inside a port list. - **A template's defaults are chosen so the untouched expansion parses.** `if (${1:cond})`, not `if ($1)`: a hole with no default expands to nothing, and `if () begin` is not Verilog. This is the editor-snippets assumption restated for generated text, and it is verified rather than assumed — `every_untouched_expansion_parses` plants every template where its gate says it may be written and asserts `parse_source_tolerant` reports nothing. - **`$` in a body is snippet syntax, not Verilog.** The other editor-snippets lesson: VS Code substitutes an unknown `$NAME` with the empty string and says nothing, so an unescaped `$finish` in a body would vanish invisibly. No system task appears in any body today, and `every_hole_is_a_tabstop_the_snippet_grammar_reads` fails the build if one is added without an escape. - **`lucisv.indent` (default 2) is the indent step**, and the line's own leading whitespace is the base — the same `Layout` M4's skeletons use, resolved the same way and with the same deviation from `lucisv.toml` recorded there. Because the base is baked in, the item must tell the client not to indent it again: see *The indentation contract* below. - **A template is offered on prefix, not on fuzzy match.** The server filters with `starts_with_ignore_case` on the template's prefix exactly as the other tiers filter on a name, so `alw` finds every `always*` template and `wh` finds `while`. What the *client* does with the list on top of that is the client's business (M5's "`sortText` is a nudge" assumption, unchanged). ## Handshakes - **New module `crates/lucisv_server/src/templates.rs`** — the registry, the dialect rule and the renderer. It is data plus one function; nothing in it knows about LSP. `completion.rs` owns the tier, the gates and the items. - **Consumes `vlog_refactor::Layout`** — M4's `{ base, step }`, reused rather than re-declared, so a skeleton and a template are indented by the same type. Nothing in `vlog_refactor` was touched. - **Consumes `lucisv_server::pairs`** through `completion::enclosing_scopes`, exactly as M4 does. No change to `pairs.rs`. - **Changed inside `completion.rs`, for other tiers to know about:** - **The tier digits moved.** Templates are tier `3`; keywords are now `4` and project words `5`. The only assertion outside this module that reads a digit is `e2e.rs`'s word-tier check, updated with a comment saying why. - `at_module_item` now takes the scope chain instead of computing it, so the two gates share one `pairs::analyze` call; its logic is unchanged. - `Word` grew `statement_head` (see *The gates*). - **`editors/vscode`**: nine static snippets removed (below). No manifest change, no `extension.js` change, no new setting, no new capability — `snippetSupport` is read exactly where M4 read it. - **No new dependency.** ## The templates `label` is what the list shows, `prefix` is what the user types, and the body is written as indented lines that the renderer lays out at the site's own indentation. `$0` is where the cursor lands, which is the empty body line in every block template. **Both dialects:** | Label | Prefix | Gate | Body | |---|---|---|---| | `if begin/end` | `if` | statement | `if (${1:cond}) begin` / `$0` / `end` | | `if/else` | `if` | statement | `if (${1:cond}) begin` / `$2` / `end else begin` / `$0` / `end` | | `else if` | `else` | statement (else-tail) | `else if (${1:cond}) begin` / `$0` / `end` | | `else begin/end` | `else` | statement (else-tail) | `else begin` / `$0` / `end` | | `case/endcase` | `case` | statement | `case (${1:sel})` / `${2:value}: $0;` / `default: ;` / `endcase` | | `casez/endcase` | `casez` | statement | as `case`, with `casez` | | `while begin/end` | `while` | statement | `while (${1:cond}) begin` / `$0` / `end` | | `initial begin/end` | `initial` | item | `initial begin` / `$0` / `end` | **SystemVerilog only (`.sv`, `.svh`):** | Label | Prefix | Gate | Body | |---|---|---|---| | `for loop` | `for` | statement | `for (int ${1:i} = 0; $1 < ${2:N}; $1++) begin` / `$0` / `end` | | `always_ff @(posedge clk)` | `always_ff` | item | `always_ff @(posedge ${1:clk}) begin` / `$0` / `end` | | `always_ff with reset` | `always_ff` | item | `always_ff @(posedge ${1:clk} or negedge ${2:rst_n}) begin` / `if (!$2) begin` / `${3:q} <= '0;` / `end else begin` / `$0` / `end` / `end` | | `always_comb begin/end` | `always_comb` | item | `always_comb begin` / `$0` / `end` | | `logic name;` | `logic` | declaration | `logic ${1:name};` | | `logic [N:0] name;` | `logic` | declaration | `logic [${1:7}:0] ${2:name};` | | `localparam int NAME = 0;` | `localparam` | declaration | `localparam int ${1:NAME} = ${2:0};` | | `parameter int NAME = 0;` | `parameter` | declaration | `parameter int ${1:NAME} = ${2:0};` | **Verilog only (`.v`, `.vh`, and any buffer with no file name):** | Label | Prefix | Gate | Body | |---|---|---|---| | `for loop` | `for` | statement | `for (${1:i} = 0; $1 < ${2:N}; $1 = $1 + 1) begin` / `$0` / `end` | | `always @(posedge clk)` | `always` | item | `always @(posedge ${1:clk}) begin` / `$0` / `end` | | `always @(posedge clk or negedge rst_n)` | `always` | item | the reset flavour, `${4:1'b0}` instead of `'0` | | `always @(*)` | `always` | item | `always @(*) begin` / `$0` / `end` | | `reg name;` | `reg` | declaration | `reg ${1:name};` | | `reg [N:0] name;` | `reg` | declaration | `reg [${1:7}:0] ${2:name};` | | `wire name;` | `wire` | declaration | `wire ${1:name};` | | `wire [N:0] name;` | `wire` | declaration | `wire [${1:7}:0] ${2:name};` | | `localparam integer NAME = 0;` | `localparam` | declaration | `localparam integer ${1:NAME} = ${2:0};` | | `parameter integer NAME = 0;` | `parameter` | declaration | `parameter integer ${1:NAME} = ${2:0};` | Sixteen templates in a `.sv` file, eighteen in a `.v` one. Two deliberate details in that table. **The parameter templates name a type** (`int` / `integer`) because lucisv's own `explicit-parameter-storage-type` rule flags `localparam NAME = 0;` — a template that trips our own linter on insert is not a template worth shipping, and it was the untouched-expansion check that surfaced it. **`casez` but not `casex`**: `casez` costs one registry entry and is what a decoder is actually written with, while `casex` is the one most style guides tell you not to use, and an unused item is noise in the list. ## The gates Three gates, all decided before any item is built, so a request at a position where no template belongs costs one `pairs::analyze` and nothing else — and a request in the middle of an expression does not even cost that, because neither gate can be true unless the word starts a statement. **Item position** is M4's `at_module_item`, unchanged: the innermost open scope is a `module`/`interface`/`program`/`generate`/`begin`, no `function`/`class`/ `case`/`fork` is in the chain, the outermost `begin` is not procedural, and the parentheses are balanced. An `always_ff` inside an `always_comb` is exactly what that gate already refuses. **Statement position** is its mirror image, new in `at_statement`: the innermost open scope is a `function`/`task` — whose body is statements all the way down — or a `begin`/`fork` whose outermost `begin` *is* procedural. Everything else is refused, `case` arms included: a bare `if` is not a legal case item, and a gate that guessed at one would offer a template that does not compile. **Declaration position** is either of the two: a `logic`/`reg`/`wire`/ `localparam` declaration is a module item and a block item both, and IEEE 1800's `block_item_declaration` includes the parameter forms as well. On top of the scope chain, a template is only offered where a statement can *start* on the line: nothing but whitespace before the typed word, or nothing but `end`, `else` or `end else` — the two words a statement legitimately follows on one line. That is what makes `end else if` complete, and what keeps a template out of `x = if`. It is `Word::statement_head`, a list of three strings, and it is deliberately the whole of the rule: anything else on the line means the word is part of an expression. One honesty note about the owner's phrasing ("typing `if(` then accepting a suggestion"): the list appears while the word `if` is being typed, and the template is accepted from *there*. Once the `(` is typed, the word being completed is empty and no longer starts a line, so the templates are gone — the user is inside an expression by then, and the item they wanted was on screen one keystroke earlier. Offering `if (…) begin` as a replacement for text that already contains `if(` would mean editing backwards over punctuation the user typed, which is the same line M5's backtick ruling drew. Comments and strings need no new gate at all. M5's `in_comment_or_string` returns `None` for the whole request before any tier runs, so a `// if` offers nothing, and the same is true of a buffer that does not lex. ## Ranking `sortText` is `