diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-04 10:50:40 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-04 10:50:40 +0000 |
commit | 698aa9b3f6420351a41a3fb4819b871fec3c891c (patch) | |
tree | c782b2b62dcfaa253b8ed55824772ea7bf8fa16d /editors/code/src/commands | |
parent | 17aaece6b39c2fb525be0eccce4626fc622e8236 (diff) | |
parent | 1ef2c0613134633ef0fe0d515f7d416e482f07fb (diff) |
Merge #924
924: Improve show syntax tree r=matklad a=vipentti
This implements some of the features discussed in #820.
You can now select a range of syntax in a file and then use "Show Syntax Tree" to show its syntax. In addition you can select a range of syntax that is inside a string (typically test cases) and show its syntax as well.
Previous behavior is still available, simply use "Show Syntax Tree" without a selection, and you get the live updating syntax tree. Additionally now the live updating tree will update when the active file is changed. Previously you had to type something in the new file to get the syntax tree to update.
Co-authored-by: Ville Penttinen <[email protected]>
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r-- | editors/code/src/commands/syntaxTree.ts | 46 |
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 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import { TextDocumentIdentifier } from 'vscode-languageclient'; | 2 | import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; |
3 | 3 | ||
4 | import { Server } from '../server'; | 4 | import { Server } from '../server'; |
5 | 5 | ||
6 | export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree'); | 6 | export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree'); |
7 | 7 | ||
8 | export class TextDocumentContentProvider | 8 | export 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 | ||
34 | interface SyntaxTreeParams { | 47 | interface SyntaxTreeParams { |
35 | textDocument: TextDocumentIdentifier; | 48 | textDocument: TextDocumentIdentifier; |
49 | range?: Range; | ||
36 | } | 50 | } |
37 | 51 | ||
38 | type SyntaxTreeResult = string; | 52 | type 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` |
43 | export async function handle() { | 57 | export 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 | } |