// Author: Guoliang Chen, a Senior Digital Design Engineer // 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 }; // Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com