aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorVille Penttinen <[email protected]>2019-03-03 19:21:40 +0000
committerVille Penttinen <[email protected]>2019-03-03 19:43:40 +0000
commitc2d3203d0c708dc2ccb7c0d017ae876180c0e5a8 (patch)
tree694ca7020bfdc8403e45060e10a590c2514df978 /editors/code/src
parentac52d9a1f1a94e2c836c8a04a316f6454936a79a (diff)
Add vscode support for range in SyntaxTreeParams
This enables the client to use a command to either show the live-updating version of the syntax tree for the current file. Or optionally when a selected range is provided, we then provide a snapshot of the syntax tree for the range.
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/commands/syntaxTree.ts44
-rw-r--r--editors/code/src/extension.ts6
2 files changed, 40 insertions, 10 deletions
diff --git a/editors/code/src/commands/syntaxTree.ts b/editors/code/src/commands/syntaxTree.ts
index c0baf08c5..7200ae823 100644
--- a/editors/code/src/commands/syntaxTree.ts
+++ b/editors/code/src/commands/syntaxTree.ts
@@ -1,5 +1,5 @@
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
@@ -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: TextDocumentContentProvider) {
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}
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index 8b332eeb2..5134bb258 100644
--- a/editors/code/src/extension.ts
+++ b/editors/code/src/extension.ts
@@ -52,7 +52,6 @@ export function activate(context: vscode.ExtensionContext) {
52 registerCommand('rust-analyzer.collectGarbage', () => 52 registerCommand('rust-analyzer.collectGarbage', () =>
53 Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null) 53 Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null)
54 ); 54 );
55 registerCommand('rust-analyzer.syntaxTree', commands.syntaxTree.handle);
56 registerCommand( 55 registerCommand(
57 'rust-analyzer.extendSelection', 56 'rust-analyzer.extendSelection',
58 commands.extendSelection.handle 57 commands.extendSelection.handle
@@ -109,6 +108,11 @@ export function activate(context: vscode.ExtensionContext) {
109 ) 108 )
110 ); 109 );
111 110
111 registerCommand(
112 'rust-analyzer.syntaxTree',
113 commands.syntaxTree.createHandle(textDocumentContentProvider)
114 );
115
112 vscode.workspace.onDidChangeTextDocument( 116 vscode.workspace.onDidChangeTextDocument(
113 events.changeTextDocument.createHandler(textDocumentContentProvider), 117 events.changeTextDocument.createHandler(textDocumentContentProvider),
114 null, 118 null,