Repository navigation
Expand file tree
/
Copy patheditor-intelligence.js
More file actions
391 lines (328 loc) · 12.4 KB
/
Copy patheditor-intelligence.js
File metadata and controls
391 lines (328 loc) · 12.4 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
'use strict';
const vscode = require('vscode');
const {
LAYOUT_SCHEME,
LAYOUT_UNAVAILABLE_TEXT,
REFERENCE_PROVIDER_COMMAND,
SHOW_REFERENCES_COMMAND,
SHOW_REFERENCES_EDITOR_COMMAND,
SHOW_TYPE_LAYOUT_COMMAND,
TYPE_LAYOUT_REQUEST,
formatByteCount,
layoutDocumentDescriptor,
normalizeTypeLayout,
parseSourceTarget,
renderLayout,
typeLayoutRequestParams
} = require('./editor-intelligence-runtime');
function formatError(error) {
return error?.message ?? String(error);
}
function activeSourceTarget() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return null;
}
return {
uri: editor.document.uri.toString(),
position: {
line: editor.selection.active.line,
character: editor.selection.active.character
}
};
}
function resolveSourceTarget(args) {
return parseSourceTarget(args) ?? activeSourceTarget();
}
async function showReferences(args, log) {
const target = resolveSourceTarget(args);
if (!target) {
void vscode.window.showWarningMessage('Cvolo: open a Cvolo file and place the cursor on a declaration.');
return;
}
const uri = vscode.Uri.parse(target.uri);
const position = new vscode.Position(target.position.line, target.position.character);
const locations = await vscode.commands.executeCommand(REFERENCE_PROVIDER_COMMAND, uri, position);
const results = Array.isArray(locations) ? locations : [];
log(`show references: ${results.length} location(s) at ${target.uri}:${target.position.line + 1}`);
await vscode.commands.executeCommand(SHOW_REFERENCES_EDITOR_COMMAND, uri, position, results);
}
// The two viewer settings, read at the moment the document is written. The notation and the padding
// share are presentation choices about numbers the compiler already reported, so they are read here
// and never carried through the language server request.
function layoutViewOptions() {
const layout = vscode.workspace.getConfiguration('cvolo').get('layout', {});
return {
offsetFormat: layout.offsetFormat,
showPaddingPercentage: layout.showPaddingPercentage
};
}
async function showTypeLayout(args, getClient, views, log) {
const target = resolveSourceTarget(args);
if (!target) {
void vscode.window.showWarningMessage('Cvolo: open a Cvolo file and place the cursor on a type.');
return;
}
const client = getClient();
if (!client) {
void vscode.window.showErrorMessage('Cvolo Language Server is not running.');
return;
}
let response;
try {
response = await client.sendRequest(TYPE_LAYOUT_REQUEST, typeLayoutRequestParams(target));
} catch (error) {
void vscode.window.showErrorMessage(`Cvolo: type layout request failed. ${formatError(error)}`);
return;
}
const layout = normalizeTypeLayout(response);
if (!layout) {
void vscode.window.showInformationMessage('Cvolo: no type layout is available at this position.');
return;
}
const options = layoutViewOptions();
const view = renderLayout(layout, options);
const uri = vscode.Uri.from(layoutDocumentDescriptor(layout));
// The registry entry is presentation state: the latest text, the spans to navigate by, the
// compiler's subject so the view can be re-asked, the source document that anchors it, and a
// generation so a slow answer cannot replace a newer one (§45).
views.set(uri.toString(), {
text: view.text,
tokens: view.tokens,
options,
subject: layout.subject,
anchor: { uri: target.uri },
generation: 0
});
log(`show type layout: ${layout.typeDisplay} (${layout.targetDisplay}) for ${target.uri}`);
const document = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(document, {
preview: true,
viewColumn: vscode.ViewColumn.Beside
});
}
// A view re-renders from the same registry entry, so a refresh only replaces the text and the spans.
// A null answer means the type the view was about is gone; the view says so instead of keeping the
// numbers it was opened with (§30).
function applyLayoutResponse(entry, response) {
const layout = normalizeTypeLayout(response);
if (!layout) {
entry.text = LAYOUT_UNAVAILABLE_TEXT;
entry.tokens = [];
return;
}
const view = renderLayout(layout, entry.options);
entry.text = view.text;
entry.tokens = view.tokens;
}
// Edits arrive in bursts, so a refresh waits for a short quiet period before it runs. The refresh
// walks every open view and re-asks by the compiler's subject; the server re-resolves it against the
// snapshot that is current, so the numbers always describe the program as it is now, not as it was
// when the view opened (§27-§29).
const REFRESH_DEBOUNCE_MS = 200;
function createLayoutRefresher(getClient, views, layoutChanged, log) {
let timer = null;
function schedule() {
if (timer !== null) {
clearTimeout(timer);
}
timer = setTimeout(async () => {
timer = null;
const client = getClient();
if (!client) {
return;
}
for (const [key, entry] of views) {
if (!entry.subject) {
continue;
}
const generation = ++entry.generation;
let response;
try {
response = await client.sendRequest(
TYPE_LAYOUT_REQUEST,
typeLayoutRequestParams({ uri: entry.anchor.uri, subject: entry.subject })
);
} catch (error) {
log(`layout refresh failed: ${formatError(error)}`);
continue;
}
// A slower answer must not replace a newer one (§55).
if (entry.generation !== generation) {
continue;
}
entry.options = layoutViewOptions();
applyLayoutResponse(entry, response);
layoutChanged.fire(vscode.Uri.parse(key));
}
}, REFRESH_DEBOUNCE_MS);
}
return { schedule };
}
// The token whose span contains the cursor, if any. The server decided what each span means; the
// extension only asks "is the cursor inside it", so no displayed text is ever parsed (§19).
function tokenAt(tokens, position) {
for (const token of tokens) {
if (token.line === position.line && position.character >= token.start && position.character <= token.end) {
return token;
}
}
return null;
}
function toLocation(target) {
if (!target) {
return null;
}
return new vscode.Location(
vscode.Uri.parse(target.uri),
new vscode.Position(target.position.line, target.position.character)
);
}
function factsBlock(facts) {
const width = facts.reduce((longest, [label]) => Math.max(longest, label.length + 1), 0);
const body = facts.map(([label, text]) => `${label}:`.padEnd(width) + text).join('\n');
return ['```text', body, '```'].join('\n');
}
function fieldHover(token, options) {
const facts = [
['Offset', formatByteCount(token.offset, options, true)],
['Size', formatByteCount(token.size, options, true)],
['Alignment', formatByteCount(token.alignment, options, true)]
];
const blocks = [`\`\`\`cvolo\n${token.signature}\n\`\`\``, factsBlock(facts)];
if (token.documentation) {
blocks.push(token.documentation);
}
return blocks.join('\n\n');
}
function typeHover(token, options) {
if (!token.facts) {
return `\`\`\`cvolo\n${token.display}\n\`\`\``;
}
const facts = token.facts;
const lines = [];
if (facts.elementCount !== null && facts.elementSize !== null) {
lines.push(['Length', String(facts.elementCount)]);
lines.push(['Element size', formatByteCount(facts.elementSize, options, true)]);
if (facts.stride !== null) {
lines.push(['Stride', formatByteCount(facts.stride, options, true)]);
}
lines.push(['Total size', formatByteCount(facts.size, options, true)]);
lines.push(['Alignment', formatByteCount(facts.alignment, options, true)]);
} else {
lines.push(['Size', formatByteCount(facts.size, options, true)]);
lines.push(['Alignment', formatByteCount(facts.alignment, options, true)]);
}
return [`\`\`\`cvolo\n${token.display}\n\`\`\``, factsBlock(lines)].join('\n\n');
}
function paddingHover(token, options) {
if (token.paddingKind === 'tail') {
return `${formatByteCount(token.size, options, true)} of tail padding required to preserve the type's ${token.alignment}-byte alignment.`;
}
const before = token.before ? ` before field '${token.before}'` : '';
return `${formatByteCount(token.size, options, true)} of alignment padding${before}.`;
}
function hoverMarkdown(token, options) {
switch (token.kind) {
case 'field':
return fieldHover(token, options);
case 'padding':
return paddingHover(token, options);
default:
return typeHover(token, options);
}
}
function registerLayoutProviders(context, views) {
context.subscriptions.push(vscode.languages.registerDefinitionProvider({ scheme: LAYOUT_SCHEME }, {
provideDefinition: (document, position) => {
const view = views.get(document.uri.toString());
if (!view) {
return null;
}
const token = tokenAt(view.tokens, position);
// A padding row describes a gap in the layout; there is nothing in the source for it to open.
if (!token || token.kind === 'padding') {
return null;
}
return toLocation(token.definition);
}
}));
context.subscriptions.push(vscode.languages.registerHoverProvider({ scheme: LAYOUT_SCHEME }, {
provideHover: (document, position) => {
const view = views.get(document.uri.toString());
if (!view) {
return null;
}
const token = tokenAt(view.tokens, position);
return token ? new vscode.Hover(hoverMarkdown(token, view.options)) : null;
}
}));
// A concrete nested aggregate type can open its own layout. The target is the compiler's, so the
// lens reuses the same command with the server-resolved location; no name is resolved here (§24).
context.subscriptions.push(vscode.languages.registerCodeLensProvider({ scheme: LAYOUT_SCHEME }, {
provideCodeLenses: document => {
const view = views.get(document.uri.toString());
if (!view) {
return [];
}
const lenses = [];
for (const token of view.tokens) {
if (token.kind === 'type' && token.nestedLayout) {
lenses.push(new vscode.CodeLens(
new vscode.Range(token.line, token.start, token.line, token.end),
{
title: 'Show Layout',
command: SHOW_TYPE_LAYOUT_COMMAND,
arguments: [token.nestedLayout.uri, token.nestedLayout.position]
}
));
}
}
return lenses;
}
}));
}
function registerEditorIntelligence(context, { getClient, log = () => {} }) {
const views = new Map();
// An open view is text the extension owns, so a refresh has to tell the editor to ask for it again.
const layoutChanged = new vscode.EventEmitter();
context.subscriptions.push(layoutChanged);
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(LAYOUT_SCHEME, {
onDidChange: layoutChanged.event,
provideTextDocumentContent: uri => views.get(uri.toString())?.text ?? ''
}));
registerLayoutProviders(context, views);
// Every open view is re-asked when a Cvolo document changes or a viewer setting changes. The
// project's own semantic generation and target live on the server side of the snapshot, so the
// client only has to know that something relevant moved (§29).
const refresher = createLayoutRefresher(getClient, views, layoutChanged, log);
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => {
if (event.document.languageId === 'cvolo') {
refresher.schedule();
}
}));
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration('cvolo')) {
refresher.schedule();
}
}));
context.subscriptions.push(vscode.commands.registerCommand(SHOW_REFERENCES_COMMAND, async (...args) => {
try {
await showReferences(args, log);
} catch (error) {
log(`show references failed: ${formatError(error)}`);
void vscode.window.showErrorMessage(`Cvolo: showing references failed. ${formatError(error)}`);
}
}));
context.subscriptions.push(vscode.commands.registerCommand(SHOW_TYPE_LAYOUT_COMMAND, async (...args) => {
try {
await showTypeLayout(args, getClient, views, log);
} catch (error) {
log(`show type layout failed: ${formatError(error)}`);
void vscode.window.showErrorMessage(`Cvolo: showing the type layout failed. ${formatError(error)}`);
}
}));
}
module.exports = {
registerEditorIntelligence
};