aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r--editors/code/src/commands/syntaxTree.ts46
1 files changed, 36 insertions, 10 deletions
diff --git a/editors/code/src/commands/syntaxTree.ts b/editors/code/src/commands/syntaxTree.ts
index c0baf08c5..2f50fe14b 100644
--- a/editors/code/src/commands/syntaxTree.ts
+++ b/editors/code/src/commands/syntaxTree.ts
@@ -1,11 +1,11 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import { TextDocumentIdentifier } from 'vscode-languageclient'; 2import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
3 3
4import { Server } from '../server'; 4import { Server } from '../server';
5 5
6export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree'); 6export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree');
7 7
8export class TextDocumentContentProvider 8export class SyntaxTreeContentProvider
9 implements vscode.TextDocumentContentProvider { 9 implements vscode.TextDocumentContentProvider {
10 public eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 10 public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
11 public syntaxTree: string = 'Not available'; 11 public syntaxTree: string = 'Not available';
@@ -17,8 +17,21 @@ export class TextDocumentContentProvider
17 if (editor == null) { 17 if (editor == null) {
18 return ''; 18 return '';
19 } 19 }
20
21 let range: Range | undefined;
22
23 // When the range based query is enabled we take the range of the selection
24 if (uri.query === 'range=true') {
25 range = editor.selection.isEmpty
26 ? undefined
27 : Server.client.code2ProtocolConverter.asRange(
28 editor.selection
29 );
30 }
31
20 const request: SyntaxTreeParams = { 32 const request: SyntaxTreeParams = {
21 textDocument: { uri: editor.document.uri.toString() } 33 textDocument: { uri: editor.document.uri.toString() },
34 range
22 }; 35 };
23 return Server.client.sendRequest<SyntaxTreeResult>( 36 return Server.client.sendRequest<SyntaxTreeResult>(
24 'rust-analyzer/syntaxTree', 37 'rust-analyzer/syntaxTree',
@@ -33,6 +46,7 @@ export class TextDocumentContentProvider
33 46
34interface SyntaxTreeParams { 47interface SyntaxTreeParams {
35 textDocument: TextDocumentIdentifier; 48 textDocument: TextDocumentIdentifier;
49 range?: Range;
36} 50}
37 51
38type SyntaxTreeResult = string; 52type SyntaxTreeResult = string;
@@ -40,11 +54,23 @@ type SyntaxTreeResult = string;
40// Opens the virtual file that will show the syntax tree 54// Opens the virtual file that will show the syntax tree
41// 55//
42// The contents of the file come from the `TextDocumentContentProvider` 56// The contents of the file come from the `TextDocumentContentProvider`
43export async function handle() { 57export function createHandle(provider: SyntaxTreeContentProvider) {
44 const document = await vscode.workspace.openTextDocument(syntaxTreeUri); 58 return async () => {
45 return vscode.window.showTextDocument( 59 const editor = vscode.window.activeTextEditor;
46 document, 60 const rangeEnabled = !!(editor && !editor.selection.isEmpty);
47 vscode.ViewColumn.Two, 61
48 true 62 const uri = rangeEnabled
49 ); 63 ? vscode.Uri.parse(`${syntaxTreeUri.toString()}?range=true`)
64 : syntaxTreeUri;
65
66 const document = await vscode.workspace.openTextDocument(uri);
67
68 provider.eventEmitter.fire(uri);
69
70 return vscode.window.showTextDocument(
71 document,
72 vscode.ViewColumn.Two,
73 true
74 );
75 };
50} 76}