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]>
189 lines
7.6 KiB
JavaScript
189 lines
7.6 KiB
JavaScript
// What the lucisv References view shows, as plain data: the server's reference
|
|
// locations grouped by file, each with its whole source line and the
|
|
// identifier's offsets inside it (docs/features/vscode-references-view).
|
|
//
|
|
// Nothing here touches `vscode`. Locations are read only through `.uri`
|
|
// (stringified) and `.range.{start,end}.{line,character}`, so both VS Code's
|
|
// own objects and plain test fixtures fit.
|
|
|
|
/**
|
|
* The longest preview line kept whole. A longer one — a generated netlist, a
|
|
* minified table — is cut to a window this wide around the match, so that one
|
|
* pathological line cannot make a label of 100 000 characters.
|
|
*/
|
|
const MAX_PREVIEW = 240;
|
|
|
|
/** How much of the line before the match a cut window keeps. */
|
|
const LEAD_CONTEXT = 60;
|
|
|
|
/** A document's lines the way VS Code counts them: `\r\n`, `\r` and `\n` all end one. */
|
|
function linesOf(text) {
|
|
return text.split(/\r\n|\r|\n/);
|
|
}
|
|
|
|
/**
|
|
* Group `locations` by file for the view.
|
|
*
|
|
* - `definitions`: what the definition provider answered — `Location`s or
|
|
* `LocationLink`s — used only to mark which reference is the declaration.
|
|
* - `currentUri`: the file the search started from; its group comes first.
|
|
* - `lineText(uriKey, line)`: the 0-based line's text, or `undefined` when the
|
|
* file could not be read.
|
|
* - `relativePath(uri)`: the label for a file's group.
|
|
*/
|
|
function buildReferenceGroups({ locations, definitions = [], currentUri, lineText, relativePath }) {
|
|
const unique = dedupe(Array.isArray(locations) ? locations.filter(isLocation) : []);
|
|
const declarations = declarationsAmong(unique, definitionTargets(definitions));
|
|
const byFile = new Map();
|
|
for (const location of unique) {
|
|
const key = String(location.uri);
|
|
if (!byFile.has(key)) byFile.set(key, []);
|
|
byFile.get(key).push(location);
|
|
}
|
|
const current = currentUri === undefined ? undefined : String(currentUri);
|
|
const groups = [...byFile].map(([key, members]) => {
|
|
members.sort((a, b) => comparePositions(a.range.start, b.range.start));
|
|
return {
|
|
uri: members[0].uri,
|
|
key,
|
|
path: relativePath(members[0].uri),
|
|
count: members.length,
|
|
items: members.map((location) => referenceItem(location, lineText(key, location.range.start.line), declarations.has(location))),
|
|
};
|
|
});
|
|
return groups.sort((a, b) => compareGroups(a, b, current));
|
|
}
|
|
|
|
/** One reference row: its trimmed line, the highlight inside it, and where it is. */
|
|
function referenceItem(location, rawLine, declaration) {
|
|
const preview = previewLine(rawLine, location.range);
|
|
return {
|
|
location,
|
|
line: location.range.start.line + 1,
|
|
character: location.range.start.character + 1,
|
|
text: preview.text,
|
|
highlights: preview.highlights,
|
|
declaration,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* The line as the view shows it — tabs as single spaces (one for one, so no
|
|
* offset moves), surrounding whitespace trimmed — and the identifier's span in
|
|
* that text. A range running onto later lines is highlighted to the end of its
|
|
* first one; a range past the end of the line is clamped to it.
|
|
*/
|
|
function previewLine(rawLine, range) {
|
|
if (typeof rawLine !== 'string') return { text: '', highlights: [] };
|
|
const line = rawLine.replace(/\t/g, ' ');
|
|
const start = clamp(range.start.character, 0, line.length);
|
|
const end = range.end.line > range.start.line ? line.length : clamp(range.end.character, start, line.length);
|
|
const trimmed = line.trim();
|
|
const lead = line.length - line.trimStart().length;
|
|
const from = clamp(start - lead, 0, trimmed.length);
|
|
const to = clamp(end - lead, from, trimmed.length);
|
|
return windowed(trimmed, from, to);
|
|
}
|
|
|
|
/** Cut an over-long preview to `MAX_PREVIEW` characters around `[from, to)`, marking the cuts with `…`. */
|
|
function windowed(text, from, to) {
|
|
if (text.length <= MAX_PREVIEW) return { text, highlights: to > from ? [[from, to]] : [] };
|
|
const begin = clamp(from - LEAD_CONTEXT, 0, text.length - MAX_PREVIEW);
|
|
const finish = begin + MAX_PREVIEW;
|
|
const prefix = begin > 0 ? '…' : '';
|
|
const body = `${prefix}${text.slice(begin, finish)}${finish < text.length ? '…' : ''}`;
|
|
const shift = prefix.length - begin;
|
|
const start = from + shift;
|
|
const end = Math.min(to, finish) + shift;
|
|
return { text: body, highlights: end > start ? [[start, end]] : [] };
|
|
}
|
|
|
|
/** `References: name (N in K files)` — the view's title for a filled answer. */
|
|
function referencesTitle(name, groups) {
|
|
const total = groups.reduce((sum, group) => sum + group.count, 0);
|
|
const files = groups.length === 1 ? '1 file' : `${groups.length} files`;
|
|
return `References: ${name} (${total} in ${files})`;
|
|
}
|
|
|
|
/** The definition answer as `{ key, range }` targets; a `LocationLink` names its identifier by `targetSelectionRange`. */
|
|
function definitionTargets(definitions) {
|
|
const list = Array.isArray(definitions) ? definitions : definitions ? [definitions] : [];
|
|
const targets = [];
|
|
for (const definition of list) {
|
|
if (!definition) continue;
|
|
if (definition.targetUri !== undefined) {
|
|
const range = definition.targetSelectionRange || definition.targetRange;
|
|
if (isRange(range)) targets.push({ key: String(definition.targetUri), range });
|
|
} else if (isLocation(definition)) {
|
|
targets.push({ key: String(definition.uri), range: definition.range });
|
|
}
|
|
}
|
|
return targets;
|
|
}
|
|
|
|
/**
|
|
* The references that are the declaration. A reference starting exactly where
|
|
* a definition does is one; only if none does, a reference inside a
|
|
* definition's range is — for a provider whose range is the whole declaration
|
|
* rather than its name.
|
|
*/
|
|
function declarationsAmong(locations, targets) {
|
|
const starts = new Set(targets.map((target) => positionKey(target.key, target.range.start)));
|
|
const exact = locations.filter((location) => starts.has(positionKey(String(location.uri), location.range.start)));
|
|
if (exact.length > 0 || targets.length === 0) return new Set(exact);
|
|
return new Set(locations.filter((location) => targets.some((target) =>
|
|
target.key === String(location.uri) && contains(target.range, location.range.start))));
|
|
}
|
|
|
|
/** The same location twice is one reference. */
|
|
function dedupe(locations) {
|
|
const seen = new Set();
|
|
return locations.filter((location) => {
|
|
const { start, end } = location.range;
|
|
const key = `${positionKey(String(location.uri), start)}-${end.line}:${end.character}`;
|
|
if (seen.has(key)) return false;
|
|
seen.add(key);
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/** The current file first, then by path, then by URI for a deterministic tie-break. */
|
|
function compareGroups(a, b, current) {
|
|
if ((a.key === current) !== (b.key === current)) return a.key === current ? -1 : 1;
|
|
return compareStrings(a.path, b.path) || compareStrings(a.key, b.key);
|
|
}
|
|
|
|
function compareStrings(a, b) {
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
}
|
|
|
|
function comparePositions(a, b) {
|
|
return a.line - b.line || a.character - b.character;
|
|
}
|
|
|
|
function contains(range, position) {
|
|
return comparePositions(range.start, position) <= 0 && comparePositions(position, range.end) <= 0;
|
|
}
|
|
|
|
function positionKey(key, position) {
|
|
return `${key}#${position.line}:${position.character}`;
|
|
}
|
|
|
|
function clamp(value, low, high) {
|
|
return Math.max(low, Math.min(high, Number.isFinite(value) ? value : low));
|
|
}
|
|
|
|
function isPosition(value) {
|
|
return Boolean(value) && Number.isInteger(value.line) && Number.isInteger(value.character);
|
|
}
|
|
|
|
function isRange(value) {
|
|
return Boolean(value) && isPosition(value.start) && isPosition(value.end);
|
|
}
|
|
|
|
function isLocation(value) {
|
|
return Boolean(value) && value.uri !== undefined && isRange(value.range);
|
|
}
|
|
|
|
module.exports = { MAX_PREVIEW, buildReferenceGroups, linesOf, previewLine, referencesTitle };
|