aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/code/src/commands/index.ts2
-rw-r--r--editors/code/src/commands/syntaxTree.ts76
-rw-r--r--editors/code/src/commands/syntax_tree.ts106
-rw-r--r--editors/code/src/ctx.ts4
-rw-r--r--editors/code/src/events/change_active_text_editor.ts9
-rw-r--r--editors/code/src/events/change_text_document.ts24
-rw-r--r--editors/code/src/events/index.ts3
-rw-r--r--editors/code/src/main.ts26
8 files changed, 116 insertions, 134 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index a7f3bc4c1..8f91b3b7d 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -5,10 +5,10 @@ import { matchingBrace } from './matching_brace';
5import { joinLines } from './join_lines'; 5import { joinLines } from './join_lines';
6import { onEnter } from './on_enter'; 6import { onEnter } from './on_enter';
7import { parentModule } from './parent_module'; 7import { parentModule } from './parent_module';
8import { syntaxTree } from './syntax_tree';
8import * as expandMacro from './expand_macro'; 9import * as expandMacro from './expand_macro';
9import * as inlayHints from './inlay_hints'; 10import * as inlayHints from './inlay_hints';
10import * as runnables from './runnables'; 11import * as runnables from './runnables';
11import * as syntaxTree from './syntaxTree';
12 12
13function collectGarbage(ctx: Ctx): Cmd { 13function collectGarbage(ctx: Ctx): Cmd {
14 return async () => { 14 return async () => {
diff --git a/editors/code/src/commands/syntaxTree.ts b/editors/code/src/commands/syntaxTree.ts
deleted file mode 100644
index 89a80550c..000000000
--- a/editors/code/src/commands/syntaxTree.ts
+++ /dev/null
@@ -1,76 +0,0 @@
1import * as vscode from 'vscode';
2import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
3
4import { Server } from '../server';
5
6export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree');
7
8export class SyntaxTreeContentProvider
9 implements vscode.TextDocumentContentProvider {
10 public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
11 public syntaxTree: string = 'Not available';
12
13 public provideTextDocumentContent(
14 uri: vscode.Uri,
15 ): vscode.ProviderResult<string> {
16 const editor = vscode.window.activeTextEditor;
17 if (editor == null) {
18 return '';
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
32 const request: SyntaxTreeParams = {
33 textDocument: { uri: editor.document.uri.toString() },
34 range,
35 };
36 return Server.client.sendRequest<SyntaxTreeResult>(
37 'rust-analyzer/syntaxTree',
38 request,
39 );
40 }
41
42 get onDidChange(): vscode.Event<vscode.Uri> {
43 return this.eventEmitter.event;
44 }
45}
46
47interface SyntaxTreeParams {
48 textDocument: TextDocumentIdentifier;
49 range?: Range;
50}
51
52type SyntaxTreeResult = string;
53
54// Opens the virtual file that will show the syntax tree
55//
56// The contents of the file come from the `TextDocumentContentProvider`
57export function createHandle(provider: SyntaxTreeContentProvider) {
58 return async () => {
59 const editor = vscode.window.activeTextEditor;
60 const rangeEnabled = !!(editor && !editor.selection.isEmpty);
61
62 const uri = rangeEnabled
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 };
76}
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}
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index 22af5ef32..c3a3583b5 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -49,6 +49,10 @@ export class Ctx {
49 } 49 }
50 } 50 }
51 51
52 get subscriptions(): { dispose(): any }[] {
53 return this.extCtx.subscriptions;
54 }
55
52 pushCleanup(d: { dispose(): any }) { 56 pushCleanup(d: { dispose(): any }) {
53 this.extCtx.subscriptions.push(d); 57 this.extCtx.subscriptions.push(d);
54 } 58 }
diff --git a/editors/code/src/events/change_active_text_editor.ts b/editors/code/src/events/change_active_text_editor.ts
index 74b91bd48..4384ee567 100644
--- a/editors/code/src/events/change_active_text_editor.ts
+++ b/editors/code/src/events/change_active_text_editor.ts
@@ -1,21 +1,14 @@
1import { TextEditor } from 'vscode'; 1import { TextEditor } from 'vscode';
2import { TextDocumentIdentifier } from 'vscode-languageclient'; 2import { TextDocumentIdentifier } from 'vscode-languageclient';
3
4import {
5 SyntaxTreeContentProvider,
6 syntaxTreeUri,
7} from '../commands/syntaxTree';
8import { Decoration } from '../highlighting'; 3import { Decoration } from '../highlighting';
9import { Server } from '../server'; 4import { Server } from '../server';
10 5
11export function makeHandler(syntaxTreeProvider: SyntaxTreeContentProvider) { 6export function makeHandler() {
12 return async function handle(editor: TextEditor | undefined) { 7 return async function handle(editor: TextEditor | undefined) {
13 if (!editor || editor.document.languageId !== 'rust') { 8 if (!editor || editor.document.languageId !== 'rust') {
14 return; 9 return;
15 } 10 }
16 11
17 syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri);
18
19 if (!Server.config.highlightingOn) { 12 if (!Server.config.highlightingOn) {
20 return; 13 return;
21 } 14 }
diff --git a/editors/code/src/events/change_text_document.ts b/editors/code/src/events/change_text_document.ts
deleted file mode 100644
index 2e998e889..000000000
--- a/editors/code/src/events/change_text_document.ts
+++ /dev/null
@@ -1,24 +0,0 @@
1import * as vscode from 'vscode';
2
3import {
4 SyntaxTreeContentProvider,
5 syntaxTreeUri,
6} from '../commands/syntaxTree';
7
8export function createHandler(syntaxTreeProvider: SyntaxTreeContentProvider) {
9 return (event: vscode.TextDocumentChangeEvent) => {
10 const doc = event.document;
11 if (doc.languageId !== 'rust') {
12 return;
13 }
14 afterLs(() => {
15 syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri);
16 });
17 };
18}
19
20// We need to order this after LS updates, but there's no API for that.
21// Hence, good old setTimeout.
22function afterLs(f: () => any) {
23 setTimeout(f, 10);
24}
diff --git a/editors/code/src/events/index.ts b/editors/code/src/events/index.ts
index 4c154563f..be135474d 100644
--- a/editors/code/src/events/index.ts
+++ b/editors/code/src/events/index.ts
@@ -1,4 +1,3 @@
1import * as changeActiveTextEditor from './change_active_text_editor'; 1import * as changeActiveTextEditor from './change_active_text_editor';
2import * as changeTextDocument from './change_text_document';
3 2
4export { changeActiveTextEditor, changeTextDocument }; 3export { changeActiveTextEditor };
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 55fedd8bb..d92cd164f 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -4,7 +4,6 @@ import * as lc from 'vscode-languageclient';
4import * as commands from './commands'; 4import * as commands from './commands';
5import { ExpandMacroContentProvider } from './commands/expand_macro'; 5import { ExpandMacroContentProvider } from './commands/expand_macro';
6import { HintsUpdater } from './commands/inlay_hints'; 6import { HintsUpdater } from './commands/inlay_hints';
7import { SyntaxTreeContentProvider } from './commands/syntaxTree';
8import { StatusDisplay } from './commands/watch_status'; 7import { StatusDisplay } from './commands/watch_status';
9import * as events from './events'; 8import * as events from './events';
10import * as notifications from './notifications'; 9import * as notifications from './notifications';
@@ -20,6 +19,7 @@ export async function activate(context: vscode.ExtensionContext) {
20 ctx.registerCommand('matchingBrace', commands.matchingBrace); 19 ctx.registerCommand('matchingBrace', commands.matchingBrace);
21 ctx.registerCommand('joinLines', commands.joinLines); 20 ctx.registerCommand('joinLines', commands.joinLines);
22 ctx.registerCommand('parentModule', commands.parentModule); 21 ctx.registerCommand('parentModule', commands.parentModule);
22 ctx.registerCommand('syntaxTree', commands.syntaxTree);
23 23
24 function disposeOnDeactivation(disposable: vscode.Disposable) { 24 function disposeOnDeactivation(disposable: vscode.Disposable) {
25 context.subscriptions.push(disposable); 25 context.subscriptions.push(disposable);
@@ -55,10 +55,7 @@ export async function activate(context: vscode.ExtensionContext) {
55 disposeOnDeactivation(watchStatus); 55 disposeOnDeactivation(watchStatus);
56 56
57 // Notifications are events triggered by the language server 57 // Notifications are events triggered by the language server
58 const allNotifications: Iterable<[ 58 const allNotifications: [string, lc.GenericNotificationHandler][] = [
59 string,
60 lc.GenericNotificationHandler,
61 ]> = [
62 [ 59 [
63 'rust-analyzer/publishDecorations', 60 'rust-analyzer/publishDecorations',
64 notifications.publishDecorations.handle, 61 notifications.publishDecorations.handle,
@@ -68,42 +65,25 @@ export async function activate(context: vscode.ExtensionContext) {
68 params => watchStatus.handleProgressNotification(params), 65 params => watchStatus.handleProgressNotification(params),
69 ], 66 ],
70 ]; 67 ];
71 const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
72 const expandMacroContentProvider = new ExpandMacroContentProvider(); 68 const expandMacroContentProvider = new ExpandMacroContentProvider();
73 69
74 // The events below are plain old javascript events, triggered and handled by vscode 70 // The events below are plain old javascript events, triggered and handled by vscode
75 vscode.window.onDidChangeActiveTextEditor( 71 vscode.window.onDidChangeActiveTextEditor(
76 events.changeActiveTextEditor.makeHandler(syntaxTreeContentProvider), 72 events.changeActiveTextEditor.makeHandler(),
77 ); 73 );
78 74
79 disposeOnDeactivation( 75 disposeOnDeactivation(
80 vscode.workspace.registerTextDocumentContentProvider( 76 vscode.workspace.registerTextDocumentContentProvider(
81 'rust-analyzer', 77 'rust-analyzer',
82 syntaxTreeContentProvider,
83 ),
84 );
85 disposeOnDeactivation(
86 vscode.workspace.registerTextDocumentContentProvider(
87 'rust-analyzer',
88 expandMacroContentProvider, 78 expandMacroContentProvider,
89 ), 79 ),
90 ); 80 );
91 81
92 registerCommand( 82 registerCommand(
93 'rust-analyzer.syntaxTree',
94 commands.syntaxTree.createHandle(syntaxTreeContentProvider),
95 );
96 registerCommand(
97 'rust-analyzer.expandMacro', 83 'rust-analyzer.expandMacro',
98 commands.expandMacro.createHandle(expandMacroContentProvider), 84 commands.expandMacro.createHandle(expandMacroContentProvider),
99 ); 85 );
100 86
101 vscode.workspace.onDidChangeTextDocument(
102 events.changeTextDocument.createHandler(syntaxTreeContentProvider),
103 null,
104 context.subscriptions,
105 );
106
107 const startServer = () => Server.start(allNotifications); 87 const startServer = () => Server.start(allNotifications);
108 const reloadCommand = () => reloadServer(startServer); 88 const reloadCommand = () => reloadServer(startServer);
109 89