aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands.ts
diff options
context:
space:
mode:
authorPhil Ellison <[email protected]>2020-12-28 18:29:58 +0000
committerPhil Ellison <[email protected]>2020-12-28 18:29:58 +0000
commit077592a12fd982de3e69572a4c738dd4468617f9 (patch)
tree24bc738d02fb8c88ef662f85e4a0c9a4c8ab0fac /editors/code/src/commands.ts
parent1d530756ed7ba175ec32ff71247072798dc9a748 (diff)
Initial implementation of view-hir command
Diffstat (limited to 'editors/code/src/commands.ts')
-rw-r--r--editors/code/src/commands.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 9d4823a34..21b0c27f3 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -340,6 +340,75 @@ export function syntaxTree(ctx: Ctx): Cmd {
340 }; 340 };
341} 341}
342 342
343// Opens the virtual file that will show hir
344//
345// The contents of the file come from the `TextDocumentContentProvider`
346export function viewHir(ctx: Ctx): Cmd {
347 const tdcp = new class implements vscode.TextDocumentContentProvider {
348 readonly uri = vscode.Uri.parse('rust-analyzer://viewHir/hir.txt');
349 readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
350 constructor() {
351 vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
352 vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
353 }
354
355 private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
356 if (isRustDocument(event.document)) {
357 // We need to order this after language server updates, but there's no API for that.
358 // Hence, good old sleep().
359 void sleep(10).then(() => this.eventEmitter.fire(this.uri));
360 }
361 }
362 private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) {
363 if (editor && isRustEditor(editor)) {
364 this.eventEmitter.fire(this.uri);
365 }
366 }
367
368 provideTextDocumentContent(_uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> {
369 const rustEditor = ctx.activeRustEditor;
370 const client = ctx.client;
371 if (!rustEditor || !client) return '';
372
373 const params = {
374 textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(rustEditor.document),
375 position: client.code2ProtocolConverter.asPosition(
376 rustEditor.selection.active,
377 ),
378 };
379 return client.sendRequest(ra.viewHir, params, ct);
380 }
381
382 get onDidChange(): vscode.Event<vscode.Uri> {
383 return this.eventEmitter.event;
384 }
385 };
386
387 void new AstInspector(ctx);
388
389 ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider('rust-analyzer', tdcp));
390 ctx.pushCleanup(vscode.languages.setLanguageConfiguration("ra_syntax_tree", {
391 brackets: [["[", ")"]],
392 }));
393
394 return async () => {
395 const editor = vscode.window.activeTextEditor;
396 const rangeEnabled = !!editor && !editor.selection.isEmpty;
397
398 const uri = rangeEnabled
399 ? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`)
400 : tdcp.uri;
401
402 const document = await vscode.workspace.openTextDocument(uri);
403
404 tdcp.eventEmitter.fire(uri);
405
406 void await vscode.window.showTextDocument(document, {
407 viewColumn: vscode.ViewColumn.Two,
408 preserveFocus: true
409 });
410 };
411}
343 412
344// Opens the virtual file that will show the syntax tree 413// Opens the virtual file that will show the syntax tree
345// 414//