aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-04 10:50:40 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-04 10:50:40 +0000
commit698aa9b3f6420351a41a3fb4819b871fec3c891c (patch)
treec782b2b62dcfaa253b8ed55824772ea7bf8fa16d /editors/code/src
parent17aaece6b39c2fb525be0eccce4626fc622e8236 (diff)
parent1ef2c0613134633ef0fe0d515f7d416e482f07fb (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')
-rw-r--r--editors/code/src/commands/syntaxTree.ts46
-rw-r--r--editors/code/src/events/change_active_text_editor.ts39
-rw-r--r--editors/code/src/events/change_text_document.ts10
-rw-r--r--editors/code/src/extension.ts16
4 files changed, 74 insertions, 37 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}
diff --git a/editors/code/src/events/change_active_text_editor.ts b/editors/code/src/events/change_active_text_editor.ts
index af295b2ec..64be56225 100644
--- a/editors/code/src/events/change_active_text_editor.ts
+++ b/editors/code/src/events/change_active_text_editor.ts
@@ -1,23 +1,32 @@
1import { TextEditor } from 'vscode'; 1import { TextEditor } from 'vscode';
2import { TextDocumentIdentifier } from 'vscode-languageclient'; 2import { TextDocumentIdentifier } from 'vscode-languageclient';
3 3
4import {
5 SyntaxTreeContentProvider,
6 syntaxTreeUri
7} from '../commands/syntaxTree';
4import { Decoration } from '../highlighting'; 8import { Decoration } from '../highlighting';
5import { Server } from '../server'; 9import { Server } from '../server';
6 10
7export async function handle(editor: TextEditor | undefined) { 11export function makeHandler(syntaxTreeProvider: SyntaxTreeContentProvider) {
8 if ( 12 return async function handle(editor: TextEditor | undefined) {
9 !Server.config.highlightingOn || 13 if (!editor || editor.document.languageId !== 'rust') {
10 !editor || 14 return;
11 editor.document.languageId !== 'rust' 15 }
12 ) { 16
13 return; 17 syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri);
14 } 18
15 const params: TextDocumentIdentifier = { 19 if (!Server.config.highlightingOn) {
16 uri: editor.document.uri.toString() 20 return;
21 }
22
23 const params: TextDocumentIdentifier = {
24 uri: editor.document.uri.toString()
25 };
26 const decorations = await Server.client.sendRequest<Decoration[]>(
27 'rust-analyzer/decorationsRequest',
28 params
29 );
30 Server.highlighter.setHighlights(editor, decorations);
17 }; 31 };
18 const decorations = await Server.client.sendRequest<Decoration[]>(
19 'rust-analyzer/decorationsRequest',
20 params
21 );
22 Server.highlighter.setHighlights(editor, decorations);
23} 32}
diff --git a/editors/code/src/events/change_text_document.ts b/editors/code/src/events/change_text_document.ts
index 6be057245..89488bc61 100644
--- a/editors/code/src/events/change_text_document.ts
+++ b/editors/code/src/events/change_text_document.ts
@@ -1,20 +1,18 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2 2
3import { 3import {
4 syntaxTreeUri, 4 SyntaxTreeContentProvider,
5 TextDocumentContentProvider 5 syntaxTreeUri
6} from '../commands/syntaxTree'; 6} from '../commands/syntaxTree';
7 7
8export function createHandler( 8export function createHandler(syntaxTreeProvider: SyntaxTreeContentProvider) {
9 textDocumentContentProvider: TextDocumentContentProvider
10) {
11 return (event: vscode.TextDocumentChangeEvent) => { 9 return (event: vscode.TextDocumentChangeEvent) => {
12 const doc = event.document; 10 const doc = event.document;
13 if (doc.languageId !== 'rust') { 11 if (doc.languageId !== 'rust') {
14 return; 12 return;
15 } 13 }
16 afterLs(() => { 14 afterLs(() => {
17 textDocumentContentProvider.eventEmitter.fire(syntaxTreeUri); 15 syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri);
18 }); 16 });
19 }; 17 };
20} 18}
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index 8b332eeb2..941beba18 100644
--- a/editors/code/src/extension.ts
+++ b/editors/code/src/extension.ts
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3 3
4import * as commands from './commands'; 4import * as commands from './commands';
5import { TextDocumentContentProvider } from './commands/syntaxTree'; 5import { SyntaxTreeContentProvider } from './commands/syntaxTree';
6import * as events from './events'; 6import * as events from './events';
7import * as notifications from './notifications'; 7import * as notifications from './notifications';
8import { Server } from './server'; 8import { Server } from './server';
@@ -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
@@ -95,22 +94,27 @@ export function activate(context: vscode.ExtensionContext) {
95 notifications.publishDecorations.handle 94 notifications.publishDecorations.handle
96 ] 95 ]
97 ]; 96 ];
97 const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
98 98
99 // The events below are plain old javascript events, triggered and handled by vscode 99 // The events below are plain old javascript events, triggered and handled by vscode
100 vscode.window.onDidChangeActiveTextEditor( 100 vscode.window.onDidChangeActiveTextEditor(
101 events.changeActiveTextEditor.handle 101 events.changeActiveTextEditor.makeHandler(syntaxTreeContentProvider)
102 ); 102 );
103 103
104 const textDocumentContentProvider = new TextDocumentContentProvider();
105 disposeOnDeactivation( 104 disposeOnDeactivation(
106 vscode.workspace.registerTextDocumentContentProvider( 105 vscode.workspace.registerTextDocumentContentProvider(
107 'rust-analyzer', 106 'rust-analyzer',
108 textDocumentContentProvider 107 syntaxTreeContentProvider
109 ) 108 )
110 ); 109 );
111 110
111 registerCommand(
112 'rust-analyzer.syntaxTree',
113 commands.syntaxTree.createHandle(syntaxTreeContentProvider)
114 );
115
112 vscode.workspace.onDidChangeTextDocument( 116 vscode.workspace.onDidChangeTextDocument(
113 events.changeTextDocument.createHandler(textDocumentContentProvider), 117 events.changeTextDocument.createHandler(syntaxTreeContentProvider),
114 null, 118 null,
115 context.subscriptions 119 context.subscriptions
116 ); 120 );