aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/syntax_tree.ts
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-30 18:05:41 +0000
committerAleksey Kladov <[email protected]>2019-12-30 18:07:59 +0000
commitca5c59507f76b8e30658d6c815b823c9636d786a (patch)
treec953cf7eaaf7f6892e7cff63b5b96ff3060826a8 /editors/code/src/commands/syntax_tree.ts
parentac3d0e83403be22ec31d62a1501d726f6e6f81e1 (diff)
Refactor show syntax tree action
Diffstat (limited to 'editors/code/src/commands/syntax_tree.ts')
-rw-r--r--editors/code/src/commands/syntax_tree.ts106
1 files changed, 106 insertions, 0 deletions
diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts
new file mode 100644
index 000000000..e61fb36df
--- /dev/null
+++ b/editors/code/src/commands/syntax_tree.ts
@@ -0,0 +1,106 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3
4import { Ctx, Cmd } from '../ctx';
5
6// Opens the virtual file that will show the syntax tree
7//
8// The contents of the file come from the `TextDocumentContentProvider`
9export function syntaxTree(ctx: Ctx): Cmd {
10 const stcp = new SyntaxTreeContentProvider(ctx);
11
12 ctx.pushCleanup(
13 vscode.workspace.registerTextDocumentContentProvider(
14 'rust-analyzer',
15 stcp,
16 ),
17 );
18
19 vscode.workspace.onDidChangeTextDocument(
20 (event: vscode.TextDocumentChangeEvent) => {
21 const doc = event.document;
22 if (doc.languageId !== 'rust') return;
23 afterLs(() => stcp.eventEmitter.fire(stcp.uri));
24 },
25 ctx.subscriptions,
26 );
27
28 vscode.window.onDidChangeActiveTextEditor(
29 (editor: vscode.TextEditor | undefined) => {
30 if (!editor || editor.document.languageId !== 'rust') return;
31 stcp.eventEmitter.fire(stcp.uri);
32 },
33 ctx.subscriptions,
34 );
35
36 return async () => {
37 const editor = vscode.window.activeTextEditor;
38 const rangeEnabled = !!(editor && !editor.selection.isEmpty);
39
40 const uri = rangeEnabled
41 ? vscode.Uri.parse(`${stcp.uri.toString()}?range=true`)
42 : stcp.uri;
43
44 const document = await vscode.workspace.openTextDocument(uri);
45
46 stcp.eventEmitter.fire(uri);
47
48 return vscode.window.showTextDocument(
49 document,
50 vscode.ViewColumn.Two,
51 true,
52 );
53 };
54}
55
56// We need to order this after LS updates, but there's no API for that.
57// Hence, good old setTimeout.
58function afterLs(f: () => any) {
59 setTimeout(f, 10);
60}
61
62interface SyntaxTreeParams {
63 textDocument: lc.TextDocumentIdentifier;
64 range?: lc.Range;
65}
66
67export class SyntaxTreeContentProvider
68 implements vscode.TextDocumentContentProvider {
69 ctx: Ctx;
70 uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
71 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
72 syntaxTree: string = 'Not available';
73
74 constructor(ctx: Ctx) {
75 this.ctx = ctx;
76 }
77
78 provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {
79 const editor = vscode.window.activeTextEditor;
80 if (editor == null) return '';
81
82 let range: lc.Range | undefined;
83
84 // When the range based query is enabled we take the range of the selection
85 if (uri.query === 'range=true') {
86 range = editor.selection.isEmpty
87 ? undefined
88 : this.ctx.client.code2ProtocolConverter.asRange(
89 editor.selection,
90 );
91 }
92
93 const request: SyntaxTreeParams = {
94 textDocument: { uri: editor.document.uri.toString() },
95 range,
96 };
97 return this.ctx.client.sendRequest<string>(
98 'rust-analyzer/syntaxTree',
99 request,
100 );
101 }
102
103 get onDidChange(): vscode.Event<vscode.Uri> {
104 return this.eventEmitter.event;
105 }
106}