TODO §71.7. Three server bugs, all older than this series. - A caret just past an identifier's last letter resolved the NEXT character (spans are end-exclusive): references, definition, hover and highlight came back empty where VS Code considers the caret on the word — the owner's 'not found, then works after clicking back'. lucisv_server/src/caret.rs; nine position-to-symbol requests use it through Server::symbol_offset; the References view opens a row with the caret at the name's start. - process_batch held requests during a scan but applied notifications at once, so a didChange could overtake an older one still queued; with incremental sync that corrupted the server's copy of the buffer until the file was reopened. Rule now: no notification overtakes an older unapplied notification (Server::must_hold, the 'behind' count for the burst a scan is nested in). Cancels wait whenever anything is held; responses never. - answer_what_is_waiting acted on a didOpen it had queued but not applied, re-holding the same batch at every file of the startup scan (~300-400 s on OpenTitan). Scan state now changes only where a message is applied; a held batch is handed back at most once per applied didOpen. An attempt to also drop excluded buffers from the index on close cost 190 ms per open of an outside file and was reverted after QA measured it. Dev (two rounds), QA (two rounds, own ordering driver: 9 118 bursts, no wrong text) and lead review: docs/features/scan-ordering/README.md. TODO.md files what stays open. Version bump (Cargo.toml, Cargo.lock, editors/vscode/package.json and both package-lock.json spots); notes in CHANGELOG.md (## 0.65.8) and editors/vscode/CHANGELOG.md. Gates: tools/gates.sh PASS on this tree including cross; lucisv_server 2030/0. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
162 lines
6.8 KiB
JavaScript
162 lines
6.8 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 puts the caret on 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}`;
|
|
// The caret goes to the identifier's start, as VS Code's own references
|
|
// view and go to definition put it. `vscode.open` makes a range's *end* the
|
|
// caret (a selection's direction does not survive it), and a Find All
|
|
// References from just past a name used to ask about the next character.
|
|
const { line, character } = range.start;
|
|
item.command = {
|
|
command: 'vscode.open',
|
|
title: 'Open Reference',
|
|
arguments: [uri, { selection: new vscode.Range(line, character, line, character), 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 };
|