All three vfmt.toml keys, pushed over workspace/didChangeConfiguration (initial push verified against the real vscode-languageclient 9.0.1). Precedence: vfmt.toml > vfmt.* settings > editor tabSize > built-in default — a checked-in config stays authoritative so format-on-save never diverges from CI. Invalid values are ignored, never clamped or coerced; exclude is appended to the config's excludes and re-indexes the workspace on change. Untitled buffers and toml-less projects gain an editor-set line length, previously impossible over LSP. Co-Authored-By: Claude Fable 5 <[email protected]>
184 lines
7.1 KiB
JavaScript
184 lines
7.1 KiB
JavaScript
// The whole VS Code extension: start `vfmt-server` over stdio, let the
|
|
// language client do the rest, and paint one thing the protocol has no request
|
|
// for — the `begin`/`end` rainbow, whose ranges the server computes and hands
|
|
// over through the custom `vfmt/scopePairs` request.
|
|
//
|
|
// Every *answer* still comes from the server: nothing here parses Verilog, and
|
|
// the only reason this file is longer than the language client's setup is that
|
|
// LSP has no "decorate these ranges" notification, so the painting has to be
|
|
// driven from the client side (docs/features/editor-scopes).
|
|
|
|
const vscode = require('vscode');
|
|
const { LanguageClient, TransportKind } = require('vscode-languageclient/node');
|
|
|
|
/** The single client, kept only so `deactivate` can stop it. */
|
|
let client;
|
|
|
|
/** The languages this extension attaches to. */
|
|
const LANGUAGES = ['verilog', 'systemverilog'];
|
|
|
|
/**
|
|
* How many colours the rainbow cycles through. Six is what VS Code's own
|
|
* bracket colouring defines (`editorBracketHighlight.foreground1..6`), and
|
|
* borrowing that palette is what makes this follow the user's theme instead of
|
|
* inventing colours that clash with it.
|
|
*/
|
|
const RAINBOW_COLORS = 6;
|
|
|
|
/** How long to let typing settle before re-asking for pairs. */
|
|
const REPAINT_DEBOUNCE_MS = 250;
|
|
|
|
/** One decoration type per colour, created once and reused. */
|
|
let decorationTypes = [];
|
|
|
|
/** Whether the rainbow is on, per `vfmt.rainbowBeginEnd`. */
|
|
let rainbowEnabled = true;
|
|
|
|
/** The pending debounced repaint, if any. */
|
|
let repaintTimer;
|
|
|
|
/** Spawn the server and attach it to Verilog/SystemVerilog documents. */
|
|
function activate(context) {
|
|
const serverPath = vscode.workspace.getConfiguration('vfmt').get('serverPath') || 'vfmt-server';
|
|
|
|
// No arguments: `vfmt-server` takes none and speaks LSP on stdin/stdout.
|
|
// `editors/vscode/smoke.js` spawns it exactly this way.
|
|
const serverOptions = {
|
|
command: serverPath,
|
|
args: [],
|
|
transport: TransportKind.stdio,
|
|
};
|
|
|
|
// No `scheme` filter: the server serves `untitled:` buffers too, for
|
|
// everything single-file (docs/features/vfmt-server/README.md).
|
|
const clientOptions = {
|
|
documentSelector: LANGUAGES.map((language) => ({ language })),
|
|
// Push the whole `vfmt` section to the server as
|
|
// `workspace/didChangeConfiguration` — once when the client initializes
|
|
// and again on every change. The server reads `vfmt.lineLength`,
|
|
// `vfmt.indent` and `vfmt.exclude` from it and ignores the rest
|
|
// (docs/features/editor-settings/README.md).
|
|
synchronize: { configurationSection: 'vfmt' },
|
|
};
|
|
|
|
client = new LanguageClient('vfmt', 'vfmt', serverOptions, clientOptions);
|
|
context.subscriptions.push(client);
|
|
|
|
// The colours come from the theme, so the decoration carries no fallback of
|
|
// its own: an unset `editorBracketHighlight.foregroundN` leaves the text its
|
|
// normal colour, which is the right failure.
|
|
decorationTypes = Array.from({ length: RAINBOW_COLORS }, (_, index) =>
|
|
vscode.window.createTextEditorDecorationType({
|
|
color: new vscode.ThemeColor(`editorBracketHighlight.foreground${index + 1}`),
|
|
}),
|
|
);
|
|
context.subscriptions.push(...decorationTypes);
|
|
|
|
rainbowEnabled = readRainbowSetting();
|
|
|
|
context.subscriptions.push(
|
|
// A new editor has no decorations of its own; paint it as it appears.
|
|
vscode.window.onDidChangeActiveTextEditor(() => repaintVisible()),
|
|
// Covers splits, tab drags, and an editor becoming visible again.
|
|
vscode.window.onDidChangeVisibleTextEditors(() => repaintVisible()),
|
|
// Typing moves every range after the cursor, so the answer is stale the
|
|
// moment it arrives; debounce rather than ask on every keystroke.
|
|
vscode.workspace.onDidChangeTextDocument((event) => {
|
|
if (isVerilog(event.document)) scheduleRepaint();
|
|
}),
|
|
vscode.workspace.onDidChangeConfiguration((event) => {
|
|
if (!event.affectsConfiguration('vfmt.rainbowBeginEnd')) return;
|
|
rainbowEnabled = readRainbowSetting();
|
|
// Honoured immediately, both ways: turning it off has to take the
|
|
// colours away, or "off" would only mean "off after a reload".
|
|
repaintVisible();
|
|
}),
|
|
);
|
|
|
|
// Closing an editor disposes its decorations with it, so there is nothing to
|
|
// clear on close; the only explicit clearing is turning the setting off,
|
|
// which `repaintVisible` does by painting empty ranges.
|
|
|
|
client.start().then(
|
|
() => repaintVisible(),
|
|
// The client reports a server that will not start, in its own output
|
|
// channel and its own words. Decorations are cosmetic; there is nothing
|
|
// useful to add here, and a second error popup would only be noise.
|
|
() => {},
|
|
);
|
|
}
|
|
|
|
/** Stop the server; VS Code waits on the returned promise. */
|
|
function deactivate() {
|
|
clearTimeout(repaintTimer);
|
|
return client ? client.stop() : undefined;
|
|
}
|
|
|
|
/** True for the documents this extension knows about. */
|
|
function isVerilog(document) {
|
|
return LANGUAGES.includes(document.languageId);
|
|
}
|
|
|
|
function readRainbowSetting() {
|
|
return vscode.workspace.getConfiguration('vfmt').get('rainbowBeginEnd') !== false;
|
|
}
|
|
|
|
/** Coalesce a burst of edits into one repaint. */
|
|
function scheduleRepaint() {
|
|
clearTimeout(repaintTimer);
|
|
repaintTimer = setTimeout(() => repaintVisible(), REPAINT_DEBOUNCE_MS);
|
|
}
|
|
|
|
/** Repaint every visible Verilog editor. */
|
|
function repaintVisible() {
|
|
for (const editor of vscode.window.visibleTextEditors) {
|
|
if (isVerilog(editor.document)) paint(editor);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ask the server where this buffer's blocks are and colour them by depth.
|
|
*
|
|
* Turning the rainbow off paints empty ranges rather than skipping the editor:
|
|
* `setDecorations` with `[]` is how a decoration type is removed, so "off" and
|
|
* "no blocks here" are the same operation.
|
|
*/
|
|
async function paint(editor) {
|
|
const document = editor.document;
|
|
const askedAbout = document.version;
|
|
const buckets = decorationTypes.map(() => []);
|
|
if (rainbowEnabled) {
|
|
let result;
|
|
try {
|
|
result = await client.sendRequest('vfmt/scopePairs', {
|
|
textDocument: { uri: document.uri.toString() },
|
|
});
|
|
} catch {
|
|
// The server is gone, restarting, or older than this extension. The
|
|
// client says so where it says everything else; leaving the old colours
|
|
// alone is better than blanking the file over a transient failure.
|
|
return;
|
|
}
|
|
// The answer describes the version we asked about. If the user typed while
|
|
// it was in flight, every range after the edit is now wrong — drop it and
|
|
// let the debounced repaint that the edit already scheduled do the work.
|
|
if (document.version !== askedAbout) return;
|
|
// The editor may have been closed while the request was in flight.
|
|
if (!vscode.window.visibleTextEditors.includes(editor)) return;
|
|
for (const pair of (result && result.pairs) || []) {
|
|
const ranges = buckets[pair.depth % RAINBOW_COLORS];
|
|
ranges.push(asRange(pair.open), asRange(pair.close));
|
|
}
|
|
}
|
|
for (const [index, ranges] of buckets.entries()) {
|
|
editor.setDecorations(decorationTypes[index], ranges);
|
|
}
|
|
}
|
|
|
|
/** An LSP range (UTF-16, which is what the server advertises) as a VS Code one. */
|
|
function asRange(range) {
|
|
return client.protocol2CodeConverter.asRange(range);
|
|
}
|
|
|
|
module.exports = { activate, deactivate };
|