Files
lucisv/editors/vscode/referencesModel_test.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

194 lines
9.5 KiB
JavaScript

const assert = require('assert/strict');
const { MAX_PREVIEW, buildReferenceGroups, linesOf, previewLine, referencesTitle } = require('./referencesModel');
function at(uri, line, start, endLine, end) {
return { uri, range: { start: { line, character: start }, end: { line: endLine, character: end } } };
}
function word(uri, line, start, length) {
return at(uri, line, start, line, start + length);
}
/** A reader over in-memory files, keyed by URI string, as the view builds one. */
function reader(files) {
const lines = new Map(Object.entries(files).map(([uri, text]) => [uri, linesOf(text)]));
return (key, line) => (lines.has(key) ? lines.get(key)[line] : undefined);
}
function build(files, locations, options = {}) {
return buildReferenceGroups({
locations,
definitions: options.definitions || [],
currentUri: options.currentUri,
lineText: reader(files),
relativePath: (uri) => String(uri).replace('file:///ws/', ''),
});
}
/** The highlighted text of an item, which is what the user actually sees marked. */
function marked(item) {
return item.highlights.map(([start, end]) => item.text.slice(start, end));
}
function theReportedLineIsShownWhole() {
const files = { 'file:///ws/a.sv': 'module a;\n always_ff @(posedge clk) r <= ~r;\nendmodule\n' };
const [group] = build(files, [word('file:///ws/a.sv', 1, 27, 1)]);
const [item] = group.items;
assert.equal(item.text, 'always_ff @(posedge clk) r <= ~r;', 'the whole line, not "clk) r <= ~r;"');
assert.deepEqual(item.highlights, [[25, 26]]);
assert.deepEqual(marked(item), ['r']);
assert.equal(item.line, 2);
assert.equal(item.character, 28);
}
function groupsAreOneFilePerGroupCurrentFileFirstThenByPath() {
const files = {
'file:///ws/z.sv': 'x\n',
'file:///ws/b/m.sv': 'x\n',
'file:///ws/a.sv': 'x\nx\n',
};
const groups = build(files, [
word('file:///ws/a.sv', 1, 0, 1),
word('file:///ws/z.sv', 0, 0, 1),
word('file:///ws/b/m.sv', 0, 0, 1),
word('file:///ws/a.sv', 0, 0, 1),
], { currentUri: 'file:///ws/z.sv' });
assert.deepEqual(groups.map((group) => group.path), ['z.sv', 'a.sv', 'b/m.sv']);
assert.deepEqual(groups.map((group) => group.count), [1, 2, 1]);
assert.equal(referencesTitle('x', groups), 'References: x (4 in 3 files)');
assert.equal(referencesTitle('x', groups.slice(0, 1)), 'References: x (1 in 1 file)');
}
function itemsAreInLineOrderAndDuplicatesCollapse() {
const files = { 'file:///ws/a.sv': 'a b a\nb\na a\n' };
const [group] = build(files, [
word('file:///ws/a.sv', 2, 2, 1),
word('file:///ws/a.sv', 0, 4, 1),
word('file:///ws/a.sv', 2, 0, 1),
word('file:///ws/a.sv', 0, 0, 1),
word('file:///ws/a.sv', 0, 0, 1),
]);
assert.equal(group.count, 4, 'the repeated location is one reference');
assert.deepEqual(group.items.map((item) => [item.line, item.character]), [[1, 1], [1, 5], [3, 1], [3, 3]]);
}
function trimmingMovesTheHighlightWithTheText() {
const [item] = build({ 'file:///ws/a.sv': ' assign y = sig; \n' }, [word('file:///ws/a.sv', 0, 17, 3)])[0].items;
assert.equal(item.text, 'assign y = sig;');
assert.deepEqual(marked(item), ['sig']);
}
function tabsBecomeSpacesWithoutMovingOffsets() {
const [item] = build({ 'file:///ws/a.sv': '\t\tq\t<= d;\n' }, [word('file:///ws/a.sv', 0, 7, 1)])[0].items;
assert.equal(item.text, 'q <= d;');
assert.deepEqual(marked(item), ['d']);
}
function crlfLinesCarryNoCarriageReturn() {
const files = { 'file:///ws/a.sv': 'module a;\r\n wire w;\r\n assign w = 1;\r\nendmodule\r\n' };
const [group] = build(files, [word('file:///ws/a.sv', 2, 9, 1), word('file:///ws/a.sv', 1, 7, 1)]);
assert.deepEqual(group.items.map((item) => item.text), ['wire w;', 'assign w = 1;']);
assert.deepEqual(group.items.map(marked), [['w'], ['w']]);
assert.deepEqual(linesOf('a\rb\r\nc\nd'), ['a', 'b', 'c', 'd'], 'a lone CR ends a line, as in VS Code');
}
function aMultiLineRangeIsHighlightedToTheEndOfItsFirstLine() {
const files = { 'file:///ws/a.sv': ' foo #(\n .W(1)\n ) u ();\n' };
const [item] = build(files, [at('file:///ws/a.sv', 0, 2, 2, 3)])[0].items;
assert.equal(item.text, 'foo #(');
assert.deepEqual(marked(item), ['foo #(']);
}
function aRangePastTheEndOfTheLineIsClamped() {
const short = previewLine(' ab', { start: { line: 0, character: 3 }, end: { line: 0, character: 99 } });
assert.deepEqual(short, { text: 'ab', highlights: [[1, 2]] });
const beyond = previewLine('ab', { start: { line: 0, character: 50 }, end: { line: 0, character: 60 } });
assert.deepEqual(beyond, { text: 'ab', highlights: [] }, 'nothing to mark, but the line still shows');
const inTrailingSpace = previewLine('ab ', { start: { line: 0, character: 3 }, end: { line: 0, character: 4 } });
assert.deepEqual(inTrailingSpace, { text: 'ab', highlights: [] });
}
function anUnreadableFileStillListsItsReferences() {
const [group] = build({}, [word('file:///ws/gone.sv', 4, 0, 3)]);
assert.equal(group.count, 1);
assert.deepEqual([group.items[0].text, group.items[0].highlights, group.items[0].line], ['', [], 5]);
const pastEnd = build({ 'file:///ws/a.sv': 'x\n' }, [word('file:///ws/a.sv', 9, 0, 1)])[0].items[0];
assert.equal(pastEnd.text, '', 'a line the file no longer has');
}
function anOverlongLineIsWindowedAroundTheMatch() {
const filler = 'a'.repeat(1000);
const line = `${filler} target ${filler}`;
const preview = previewLine(line, { start: { line: 0, character: 1001 }, end: { line: 0, character: 1007 } });
assert(preview.text.length <= MAX_PREVIEW + 2, 'two ellipses at most beyond the window');
assert(preview.text.startsWith('…') && preview.text.endsWith('…'));
const [[start, end]] = preview.highlights;
assert.equal(preview.text.slice(start, end), 'target');
const head = previewLine(`x ${filler}`, { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } });
assert(!head.text.startsWith('…') && head.text.endsWith('…'));
assert.deepEqual(head.highlights, [[0, 1]]);
const tail = previewLine(`${filler} x`, { start: { line: 0, character: 1001 }, end: { line: 0, character: 1002 } });
assert(tail.text.startsWith('…') && !tail.text.endsWith('…'));
assert.equal(tail.text.slice(...tail.highlights[0]), 'x');
}
function theDeclarationIsTheReferenceAtADefinitionsStart() {
const files = { 'file:///ws/a.sv': 'logic v;\nassign v = 1;\n', 'file:///ws/b.sv': 'v\n' };
const locations = [word('file:///ws/a.sv', 1, 7, 1), word('file:///ws/a.sv', 0, 6, 1), word('file:///ws/b.sv', 0, 0, 1)];
const asLocation = build(files, locations, { definitions: [word('file:///ws/a.sv', 0, 6, 1)] });
assert.deepEqual(asLocation.flatMap((group) => group.items.map((item) => item.declaration)), [true, false, false]);
const asLink = build(files, locations, {
definitions: [{ targetUri: 'file:///ws/a.sv', targetRange: at('file:///ws/a.sv', 0, 0, 0, 8).range, targetSelectionRange: word('file:///ws/a.sv', 0, 6, 1).range }],
});
assert.deepEqual(asLink.flatMap((group) => group.items.map((item) => item.declaration)), [true, false, false]);
const wholeDeclaration = build(files, locations, { definitions: [at('file:///ws/a.sv', 0, 0, 0, 8)] });
assert.deepEqual(wholeDeclaration.flatMap((group) => group.items.map((item) => item.declaration)), [true, false, false],
'a definition range spanning the declaration marks the reference inside it');
const elsewhere = build(files, locations, { definitions: [word('file:///ws/c.sv', 0, 6, 1)] });
assert(elsewhere.every((group) => group.items.every((item) => !item.declaration)), 'same position, other file: not it');
const none = build(files, locations, { definitions: undefined });
assert(none.every((group) => group.items.every((item) => !item.declaration)));
}
function anEmptyOrMalformedAnswerIsNoGroups() {
assert.deepEqual(build({}, []), []);
assert.deepEqual(build({}, undefined), []);
assert.deepEqual(build({}, [null, { uri: 'file:///ws/a.sv' }, { uri: 'file:///ws/a.sv', range: { start: {} } }]), []);
}
function fiveHundredReferencesAreCheap() {
const text = Array.from({ length: 200 }, (_, line) => ` assign out_${line} = sig & mask_${line};`).join('\n');
const files = {};
const locations = [];
for (let file = 0; file < 25; file += 1) {
const uri = `file:///ws/f${String(file).padStart(2, '0')}.sv`;
files[uri] = text;
for (let line = 0; line < 20; line += 1) locations.push(word(uri, line * 10, 16 + String(line * 10).length, 3));
}
const started = process.hrtime.bigint();
const groups = build(files, locations, { currentUri: 'file:///ws/f07.sv' });
const elapsedMs = Number(process.hrtime.bigint() - started) / 1e6;
assert.equal(groups.reduce((sum, group) => sum + group.count, 0), 500);
assert.equal(groups[0].path, 'f07.sv');
assert(groups.every((group) => group.items.every((item) => marked(item)[0] === 'sig')));
assert(elapsedMs < 200, `500 references took ${elapsedMs} ms`);
}
for (const test of [
theReportedLineIsShownWhole,
groupsAreOneFilePerGroupCurrentFileFirstThenByPath,
itemsAreInLineOrderAndDuplicatesCollapse,
trimmingMovesTheHighlightWithTheText,
tabsBecomeSpacesWithoutMovingOffsets,
crlfLinesCarryNoCarriageReturn,
aMultiLineRangeIsHighlightedToTheEndOfItsFirstLine,
aRangePastTheEndOfTheLineIsClamped,
anUnreadableFileStillListsItsReferences,
anOverlongLineIsWindowedAroundTheMatch,
theDeclarationIsTheReferenceAtADefinitionsStart,
anEmptyOrMalformedAnswerIsNoGroups,
fiveHundredReferencesAreCheap,
]) test();
console.log('references model: OK — whole lines, grouping and order, trim/tab/CRLF offsets, multi-line and clamped ranges, windows, the declaration mark, empty answers, and 500 references');