-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeLens.ts
More file actions
92 lines (78 loc) · 3.2 KB
/
Copy pathcodeLens.ts
File metadata and controls
92 lines (78 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { CodeLens, Range, Command } from 'vscode-languageserver';
import { DocumentModel, BisonDocument, FlexDocument, isBisonDocument } from '../parser/types';
/**
* Code Lenses:
* Bison rules → "N references" + "⬤ entry point" (start symbol only)
* Flex SC decls → "N references"
* Flex abbreviations → "N references"
*
* The "N references" lens triggers `bisonFlex.showReferences` (registered
* client-side) which calls `editor.action.showReferences` with pre-built args.
*/
export function getCodeLenses(doc: DocumentModel, uri: string): CodeLens[] {
if (isBisonDocument(doc)) {
return getBisonCodeLenses(doc, uri);
}
return getFlexCodeLenses(doc as FlexDocument, uri);
}
// ── Bison ────────────────────────────────────────────────────────────────────
function getBisonCodeLenses(doc: BisonDocument, uri: string): CodeLens[] {
const lenses: CodeLens[] = [];
const startSymbol = doc.startSymbol ?? [...doc.rules.keys()][0];
for (const [name, rule] of doc.rules) {
const line = rule.location.start.line;
const lensRange = Range.create(line, 0, line, 0);
const refCount = doc.ruleReferences.get(name)?.length ?? 0;
// "N references" — clickable, triggers showReferences on client side
lenses.push({
range: lensRange,
command: Command.create(
`$(references) ${refCount} reference${refCount !== 1 ? 's' : ''}`,
'bisonFlex.showReferences',
uri,
{ line, character: rule.location.start.character },
),
});
// "entry point" indicator — informational, no-op command
if (name === startSymbol) {
lenses.push({
range: lensRange,
command: Command.create('$(play-circle) entry point', 'bisonFlex.noOp'),
});
}
}
return lenses;
}
// ── Flex ─────────────────────────────────────────────────────────────────────
function getFlexCodeLenses(doc: FlexDocument, uri: string): CodeLens[] {
const lenses: CodeLens[] = [];
for (const [name, sc] of doc.startConditions) {
const line = sc.location.start.line;
const lensRange = Range.create(line, 0, line, 0);
const refCount = doc.startConditionRefs.get(name)?.length ?? 0;
lenses.push({
range: lensRange,
command: Command.create(
`$(references) ${refCount} reference${refCount !== 1 ? 's' : ''}`,
'bisonFlex.showReferences',
uri,
{ line, character: sc.location.start.character },
),
});
}
for (const [name, abbr] of doc.abbreviations) {
const line = abbr.location.start.line;
const lensRange = Range.create(line, 0, line, 0);
const refCount = doc.abbreviationRefs.get(name)?.length ?? 0;
lenses.push({
range: lensRange,
command: Command.create(
`$(references) ${refCount} reference${refCount !== 1 ? 's' : ''}`,
'bisonFlex.showReferences',
uri,
{ line, character: abbr.location.start.character },
),
});
}
return lenses;
}