Files
lucisv/editors/vscode/referencesView.js
T
lucioandClaude Opus 5.5 5e4af0be3d VS Code: Find All References with whole source lines (lucisv References view)
VS Code's built-in references pane cuts each preview to ~8 characters
before the match. lucisv.references.show runs the same reference and
definition providers and fills a tree view in the lucisv sidebar: one row
per file (current file first), one row per reference with the trimmed full
line and the identifier highlighted, the declaration marked; a click
selects the exact range. The setting lucisv.references.fullLineView
(default true) binds Shift+Alt+F12 to it in verilog/systemverilog editors
only; Shift+F12 peek is untouched. Closed files are read with
workspace.fs, so no didOpen reaches the server.

QA (Sonnet): PASS with 4 lows (recorded). Node tests 8/8. tools/RELEASING.md
must-ship list names every runtime module (lane handshake).
docs/features/vscode-references-view.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 14:14:11 -07:00

157 lines
6.4 KiB
JavaScript

// The lucisv References view: Find All References with every reference's
// whole source line, the identifier highlighted, grouped by file.
//
// VS Code's own References view and peek cut each preview to a few characters
// before the match, so `always_ff @(posedge clk) r <= ~r;` reads `clk) r <= ~r;`.
// The server's ranges are exact; the preview is the client's, so this view is
// the client's too (docs/features/vscode-references-view). The answers still
// come from the server, through VS Code's own provider commands.
const vscode = require('vscode');
const { buildReferenceGroups, linesOf, referencesTitle } = require('./referencesModel');
const VIEW_ID = 'lucisv.references';
/** Shows the view; it stays hidden until the first search fills it. */
const HAS_RESULTS_KEY = 'lucisv.references.hasResults';
class ReferencesView {
constructor(context) {
this.groups = [];
this.request = 0;
this.changed = new vscode.EventEmitter();
this.onDidChangeTreeData = this.changed.event;
this.treeView = vscode.window.createTreeView(VIEW_ID, { treeDataProvider: this, showCollapseAll: true });
context.subscriptions.push(
this.treeView,
this.changed,
vscode.commands.registerCommand('lucisv.references.show', () => this.show()),
vscode.commands.registerCommand('lucisv.references.clear', () => this.clear()),
);
}
/** Find the references at the active editor's cursor and fill the view with them. */
async show() {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const { document } = editor;
const position = editor.selection.active;
const asked = ++this.request;
let locations;
let definitions;
try {
[locations, definitions] = await Promise.all([
vscode.commands.executeCommand('vscode.executeReferenceProvider', document.uri, position),
// Only for the declaration mark: a failure here costs the mark, never the list.
Promise.resolve(vscode.commands.executeCommand('vscode.executeDefinitionProvider', document.uri, position)).catch(() => []),
]);
} catch (error) {
if (asked === this.request) vscode.window.showWarningMessage(`lucisv: Find All References failed: ${(error && error.message) || error}`);
return;
}
const lineText = await readLines(locations || []);
if (asked !== this.request) return;
this.groups = buildReferenceGroups({
locations: locations || [],
definitions: definitions || [],
currentUri: document.uri,
lineText,
relativePath: (uri) => vscode.workspace.asRelativePath(uri),
});
const name = wordAt(document, position);
this.treeView.title = this.groups.length > 0 ? referencesTitle(name, this.groups) : `References: ${name}`;
this.treeView.message = this.groups.length > 0 ? undefined : `No references to ${name}.`;
this.changed.fire();
await vscode.commands.executeCommand('setContext', HAS_RESULTS_KEY, true);
await vscode.commands.executeCommand(`${VIEW_ID}.focus`);
}
/** Empty the view and hide it again. */
async clear() {
this.request += 1;
this.groups = [];
this.treeView.title = 'References';
this.treeView.message = undefined;
this.changed.fire();
await vscode.commands.executeCommand('setContext', HAS_RESULTS_KEY, false);
}
getChildren(node) {
if (!node) return this.groups;
return node.items || [];
}
getTreeItem(node) {
return node.items ? fileItem(node) : referenceItem(node);
}
}
/** A file's row: its workspace-relative path and how many references it holds. */
function fileItem(group) {
const item = new vscode.TreeItem(group.path, vscode.TreeItemCollapsibleState.Expanded);
item.resourceUri = group.uri;
item.iconPath = vscode.ThemeIcon.File;
item.description = String(group.count);
item.tooltip = group.path;
return item;
}
/** A reference's row: the whole line, the identifier highlighted; a click selects it in its file. */
function referenceItem(reference) {
const label = reference.text === ''
? { label: `line ${reference.line}` }
: { label: reference.text, highlights: reference.highlights };
const item = new vscode.TreeItem(label, vscode.TreeItemCollapsibleState.None);
const { uri, range } = reference.location;
item.description = reference.declaration ? `declaration · line ${reference.line}` : `line ${reference.line}`;
if (reference.declaration) item.iconPath = new vscode.ThemeIcon('symbol-key');
item.tooltip = `${vscode.workspace.asRelativePath(uri)}:${reference.line}:${reference.character}`;
item.command = {
command: 'vscode.open',
title: 'Open Reference',
arguments: [uri, { selection: range, preserveFocus: false, preview: true }],
};
return item;
}
/**
* A `lineText(key, line)` reader over every file the answer names. An open
* document is read from its buffer — unsaved edits are what the server
* answered against. A closed file is read from disk with
* `workspace.fs`, not `openTextDocument`: opening five hundred files' worth of
* documents would send the server a `didOpen` for each, and fill Problems with
* their diagnostics, for a preview line.
*/
async function readLines(locations) {
const uris = new Map();
for (const location of locations) {
if (location && location.uri !== undefined) uris.set(String(location.uri), location.uri);
}
const open = new Map(vscode.workspace.textDocuments.map((document) => [document.uri.toString(), document]));
const readers = new Map(await Promise.all([...uris].map(async ([key, uri]) => [key, await fileReader(open.get(key), uri)])));
return (key, line) => {
const reader = readers.get(key);
return reader ? reader(line) : undefined;
};
}
/** One file's line reader, or `undefined` if it cannot be read. */
async function fileReader(document, uri) {
if (document) return (line) => (line >= 0 && line < document.lineCount ? document.lineAt(line).text : undefined);
try {
const lines = linesOf(new TextDecoder().decode(await vscode.workspace.fs.readFile(uri)));
return (line) => lines[line];
} catch {
// Deleted since it was indexed, or a scheme `workspace.fs` cannot read: the
// row still opens the location, and says which line it is.
return undefined;
}
}
/** The identifier under the cursor, for the title. */
function wordAt(document, position) {
const range = document.getWordRangeAtPosition(position);
return range ? document.getText(range) : 'symbol';
}
module.exports = { ReferencesView, VIEW_ID };