Files
lucioandClaude Fable 5.1 acf20f7e62 Extract module: dataflow-inferred ports, a new file where the client can create one, and a verifier ledger that follows the move
TODO §53.1 (docs/features/extract-inline-module), the fourth §8
refactoring; §53.2 inline module is unstarted and runs as its own lane.

`vlog_refactor::extract` (region, flow, boundary, render): a selection
of whole module items becomes a new module — a name declared outside
and read inside is an `input`, written inside an `output` (assign and
procedural LHS, for init/step, ++/--, force/release/deassign, `-> ev`,
an initialiser, an output/inout connection through the index), written
on both sides refused; parameters passed through `#(.P(P))`, used
imports copied, the port's type taken from the declaration's own text;
the selection replaced by an instantiation rendered from the new
module's text. Forty-three refusal sentences. `RefactorEdit::created`;
the server emits `CreateFile` document changes when the client reads
both halves of the capability, else writes the module below its source;
`refactor.extract` action carrying `lucisv.extractModule [uri, range,
name]`, and a name prompt in the VS Code extension through the language
client's middleware.

The verifier's never-dropped ledger follows the move (review rework of
QA's two HIGHs): `verify_move(moving: &[(file, region)])` — a piece
deleted inside the moved region must land exactly once in the file the
move names, a deletion outside the region stays an ordinary deletion,
four equations, twelve attack pairs through the production entry;
`check.rs` split (pure move) into `check/{mod,ledger}.rs`. `interface`
and `program` bodies are refused (one AST node, gated on `kw`). QA's
re-attack: PASS; its MEDIUM (a same-file relocation as two edits) is
filed as topo §2.9-32.

Trials: corpus 5 extracted / 6 refused, opentitan `hw/ip/{aes,uart,
spi_host,hmac}` 5 / 5, the unresolved-name census held for every
accepted extraction. 0 of 820 corpus files differ from 0.46.0.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-05 23:04:14 -05:00

75 lines
3.2 KiB
JavaScript

// The one thing "extract into module" needs that LSP has no request for:
// asking the user what to call the new module.
//
// The server offers the code action with a `Command` rather than an edit —
// `lucisv.extractModule [uri, range, defaultName]` — precisely so a client can
// step in between the click and the edit. This file is that step.
//
// **Through the language client's middleware, not through a second command.**
// `vscode-languageclient` registers every command the server advertises in
// `executeCommandProvider` with `vscode.commands.registerCommand`, so an
// extension that registered `lucisv.extractModule` itself would collide with
// it at activation. `middleware.executeCommand` is the seam the client provides
// for exactly this: it sees the command on its way out, may change the
// arguments, and hands them to `next`. One command id, one server contract,
// and every other client — Vim, a CLI — runs the same command with the default
// name and gets the same edit.
//
// Nothing here knows about `vscode`: the dependencies come in as an object, so
// `extract_test.js` drives the whole flow headlessly.
// See docs/features/extract-inline-module/README.md.
/** The command the server's code action carries. */
const EXTRACT_MODULE = 'lucisv.extractModule';
/**
* A plain SystemVerilog identifier, which is the only thing the engine will
* name a module — and a file — with. Checked here as well as in the engine so
* the user is told while the box is open rather than by an error afterwards.
*/
const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_$]*$/;
/**
* What the input box says about what has been typed so far, or `undefined`
* when it is a name lucisv would accept.
*
* The message is the one the engine would refuse with, in the present tense:
* an editor's validation is read while typing, not after.
*/
function validateModuleName(value) {
const name = (value || '').trim();
if (name === '') return 'The new module needs a name.';
if (!IDENTIFIER.test(name)) {
return 'A module name is a plain SystemVerilog identifier: a letter or `_`, then letters, digits, `_` or `$`.';
}
return undefined;
}
/**
* The language client's `executeCommand` middleware.
*
* - `deps.showInputBox` — `vscode.window.showInputBox`, which resolves to the
* typed string or to `undefined` when the user pressed Escape.
* - `command`, `args`, `next` — what the client hands every middleware.
*
* Every command but this one goes straight through. This one asks for a name,
* and a cancelled box resolves to `undefined` without ever reaching the
* server: an extraction nobody named is one nobody asked for.
*/
async function executeCommand(deps, command, args, next) {
if (command !== EXTRACT_MODULE) return next(command, args);
const [uri, range, suggested] = args || [];
const name = await deps.showInputBox({
title: 'Extract into module',
prompt: 'Name for the new module',
value: typeof suggested === 'string' ? suggested : '',
validateInput: validateModuleName,
});
if (name === undefined) return undefined;
const trimmed = name.trim();
if (validateModuleName(trimmed) !== undefined) return undefined;
return next(command, [uri, range, trimmed]);
}
module.exports = { EXTRACT_MODULE, executeCommand, validateModuleName };