aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/code/src/commands/analyzer_status.ts4
-rw-r--r--editors/code/src/commands/expand_macro.ts102
-rw-r--r--editors/code/src/commands/index.ts2
-rw-r--r--editors/code/src/commands/syntax_tree.ts19
-rw-r--r--editors/code/src/main.ts15
5 files changed, 60 insertions, 82 deletions
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts
index b2b624b75..2c8362286 100644
--- a/editors/code/src/commands/analyzer_status.ts
+++ b/editors/code/src/commands/analyzer_status.ts
@@ -1,8 +1,8 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2 2
3import { Ctx, Cmd } from '../ctx'; 3import { Ctx, Cmd } from '../ctx';
4// Shows status of rust-analyzer (for debugging)
5 4
5// Shows status of rust-analyzer (for debugging)
6export function analyzerStatus(ctx: Ctx): Cmd { 6export function analyzerStatus(ctx: Ctx): Cmd {
7 let poller: NodeJS.Timer | null = null; 7 let poller: NodeJS.Timer | null = null;
8 const tdcp = new TextDocumentContentProvider(ctx); 8 const tdcp = new TextDocumentContentProvider(ctx);
@@ -37,7 +37,7 @@ export function analyzerStatus(ctx: Ctx): Cmd {
37 37
38class TextDocumentContentProvider 38class TextDocumentContentProvider
39 implements vscode.TextDocumentContentProvider { 39 implements vscode.TextDocumentContentProvider {
40 ctx: Ctx; 40 private ctx: Ctx;
41 uri = vscode.Uri.parse('rust-analyzer-status://status'); 41 uri = vscode.Uri.parse('rust-analyzer-status://status');
42 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 42 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
43 43
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts
index 17c78280a..da208257a 100644
--- a/editors/code/src/commands/expand_macro.ts
+++ b/editors/code/src/commands/expand_macro.ts
@@ -1,60 +1,23 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3import { Server } from '../server';
4 3
5export const expandMacroUri = vscode.Uri.parse( 4import { Ctx, Cmd } from '../ctx';
6 'rust-analyzer://expandMacro/[EXPANSION].rs',
7);
8
9export class ExpandMacroContentProvider
10 implements vscode.TextDocumentContentProvider {
11 public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
12
13 public provideTextDocumentContent(
14 _uri: vscode.Uri,
15 ): vscode.ProviderResult<string> {
16 async function handle() {
17 const editor = vscode.window.activeTextEditor;
18 if (editor == null) {
19 return '';
20 }
21
22 const position = editor.selection.active;
23 const request: MacroExpandParams = {
24 textDocument: { uri: editor.document.uri.toString() },
25 position,
26 };
27 const expanded = await Server.client.sendRequest<ExpandedMacro>(
28 'rust-analyzer/expandMacro',
29 request,
30 );
31
32 if (expanded == null) {
33 return 'Not available';
34 }
35
36 return code_format(expanded);
37 }
38
39 return handle();
40 }
41
42 get onDidChange(): vscode.Event<vscode.Uri> {
43 return this.eventEmitter.event;
44 }
45}
46 5
47// Opens the virtual file that will show the syntax tree 6// Opens the virtual file that will show the syntax tree
48// 7//
49// The contents of the file come from the `TextDocumentContentProvider` 8// The contents of the file come from the `TextDocumentContentProvider`
50export function createHandle(provider: ExpandMacroContentProvider) { 9export function expandMacro(ctx: Ctx): Cmd {
51 return async () => { 10 const tdcp = new TextDocumentContentProvider(ctx);
52 const uri = expandMacroUri; 11 ctx.pushCleanup(
53 12 vscode.workspace.registerTextDocumentContentProvider(
54 const document = await vscode.workspace.openTextDocument(uri); 13 'rust-analyzer',
55 14 tdcp,
56 provider.eventEmitter.fire(uri); 15 ),
16 );
57 17
18 return async () => {
19 const document = await vscode.workspace.openTextDocument(tdcp.uri);
20 tdcp.eventEmitter.fire(tdcp.uri);
58 return vscode.window.showTextDocument( 21 return vscode.window.showTextDocument(
59 document, 22 document,
60 vscode.ViewColumn.Two, 23 vscode.ViewColumn.Two,
@@ -63,11 +26,6 @@ export function createHandle(provider: ExpandMacroContentProvider) {
63 }; 26 };
64} 27}
65 28
66interface MacroExpandParams {
67 textDocument: TextDocumentIdentifier;
68 position: Position;
69}
70
71interface ExpandedMacro { 29interface ExpandedMacro {
72 name: string; 30 name: string;
73 expansion: string; 31 expansion: string;
@@ -81,3 +39,37 @@ function code_format(expanded: ExpandedMacro): string {
81 39
82 return result; 40 return result;
83} 41}
42
43class TextDocumentContentProvider
44 implements vscode.TextDocumentContentProvider {
45 private ctx: Ctx;
46 uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs');
47 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
48
49 constructor(ctx: Ctx) {
50 this.ctx = ctx;
51 }
52
53 async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> {
54 const editor = vscode.window.activeTextEditor;
55 if (editor == null) return '';
56
57 const position = editor.selection.active;
58 const request: lc.TextDocumentPositionParams = {
59 textDocument: { uri: editor.document.uri.toString() },
60 position,
61 };
62 const expanded = await this.ctx.client.sendRequest<ExpandedMacro>(
63 'rust-analyzer/expandMacro',
64 request,
65 );
66
67 if (expanded == null) return 'Not available';
68
69 return code_format(expanded);
70 }
71
72 get onDidChange(): vscode.Event<vscode.Uri> {
73 return this.eventEmitter.event;
74 }
75}
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 8f91b3b7d..325ae3da8 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -6,7 +6,7 @@ import { 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 { syntaxTree } from './syntax_tree';
9import * as expandMacro from './expand_macro'; 9import { expandMacro } from './expand_macro';
10import * as inlayHints from './inlay_hints'; 10import * as inlayHints from './inlay_hints';
11import * as runnables from './runnables'; 11import * as runnables from './runnables';
12 12
diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts
index e61fb36df..20ff7e5ca 100644
--- a/editors/code/src/commands/syntax_tree.ts
+++ b/editors/code/src/commands/syntax_tree.ts
@@ -7,12 +7,12 @@ import { Ctx, Cmd } from '../ctx';
7// 7//
8// The contents of the file come from the `TextDocumentContentProvider` 8// The contents of the file come from the `TextDocumentContentProvider`
9export function syntaxTree(ctx: Ctx): Cmd { 9export function syntaxTree(ctx: Ctx): Cmd {
10 const stcp = new SyntaxTreeContentProvider(ctx); 10 const tdcp = new TextDocumentContentProvider(ctx);
11 11
12 ctx.pushCleanup( 12 ctx.pushCleanup(
13 vscode.workspace.registerTextDocumentContentProvider( 13 vscode.workspace.registerTextDocumentContentProvider(
14 'rust-analyzer', 14 'rust-analyzer',
15 stcp, 15 tdcp,
16 ), 16 ),
17 ); 17 );
18 18
@@ -20,7 +20,7 @@ export function syntaxTree(ctx: Ctx): Cmd {
20 (event: vscode.TextDocumentChangeEvent) => { 20 (event: vscode.TextDocumentChangeEvent) => {
21 const doc = event.document; 21 const doc = event.document;
22 if (doc.languageId !== 'rust') return; 22 if (doc.languageId !== 'rust') return;
23 afterLs(() => stcp.eventEmitter.fire(stcp.uri)); 23 afterLs(() => tdcp.eventEmitter.fire(tdcp.uri));
24 }, 24 },
25 ctx.subscriptions, 25 ctx.subscriptions,
26 ); 26 );
@@ -28,7 +28,7 @@ export function syntaxTree(ctx: Ctx): Cmd {
28 vscode.window.onDidChangeActiveTextEditor( 28 vscode.window.onDidChangeActiveTextEditor(
29 (editor: vscode.TextEditor | undefined) => { 29 (editor: vscode.TextEditor | undefined) => {
30 if (!editor || editor.document.languageId !== 'rust') return; 30 if (!editor || editor.document.languageId !== 'rust') return;
31 stcp.eventEmitter.fire(stcp.uri); 31 tdcp.eventEmitter.fire(tdcp.uri);
32 }, 32 },
33 ctx.subscriptions, 33 ctx.subscriptions,
34 ); 34 );
@@ -38,12 +38,12 @@ export function syntaxTree(ctx: Ctx): Cmd {
38 const rangeEnabled = !!(editor && !editor.selection.isEmpty); 38 const rangeEnabled = !!(editor && !editor.selection.isEmpty);
39 39
40 const uri = rangeEnabled 40 const uri = rangeEnabled
41 ? vscode.Uri.parse(`${stcp.uri.toString()}?range=true`) 41 ? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`)
42 : stcp.uri; 42 : tdcp.uri;
43 43
44 const document = await vscode.workspace.openTextDocument(uri); 44 const document = await vscode.workspace.openTextDocument(uri);
45 45
46 stcp.eventEmitter.fire(uri); 46 tdcp.eventEmitter.fire(uri);
47 47
48 return vscode.window.showTextDocument( 48 return vscode.window.showTextDocument(
49 document, 49 document,
@@ -64,12 +64,11 @@ interface SyntaxTreeParams {
64 range?: lc.Range; 64 range?: lc.Range;
65} 65}
66 66
67export class SyntaxTreeContentProvider 67class TextDocumentContentProvider
68 implements vscode.TextDocumentContentProvider { 68 implements vscode.TextDocumentContentProvider {
69 ctx: Ctx; 69 private ctx: Ctx;
70 uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); 70 uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
71 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 71 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
72 syntaxTree: string = 'Not available';
73 72
74 constructor(ctx: Ctx) { 73 constructor(ctx: Ctx) {
75 this.ctx = ctx; 74 this.ctx = ctx;
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index d92cd164f..b8e3396a6 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -2,7 +2,6 @@ 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 { ExpandMacroContentProvider } from './commands/expand_macro';
6import { HintsUpdater } from './commands/inlay_hints'; 5import { HintsUpdater } from './commands/inlay_hints';
7import { StatusDisplay } from './commands/watch_status'; 6import { StatusDisplay } from './commands/watch_status';
8import * as events from './events'; 7import * as events from './events';
@@ -20,6 +19,7 @@ export async function activate(context: vscode.ExtensionContext) {
20 ctx.registerCommand('joinLines', commands.joinLines); 19 ctx.registerCommand('joinLines', commands.joinLines);
21 ctx.registerCommand('parentModule', commands.parentModule); 20 ctx.registerCommand('parentModule', commands.parentModule);
22 ctx.registerCommand('syntaxTree', commands.syntaxTree); 21 ctx.registerCommand('syntaxTree', commands.syntaxTree);
22 ctx.registerCommand('expandMacro', commands.expandMacro);
23 23
24 function disposeOnDeactivation(disposable: vscode.Disposable) { 24 function disposeOnDeactivation(disposable: vscode.Disposable) {
25 context.subscriptions.push(disposable); 25 context.subscriptions.push(disposable);
@@ -65,25 +65,12 @@ export async function activate(context: vscode.ExtensionContext) {
65 params => watchStatus.handleProgressNotification(params), 65 params => watchStatus.handleProgressNotification(params),
66 ], 66 ],
67 ]; 67 ];
68 const expandMacroContentProvider = new ExpandMacroContentProvider();
69 68
70 // The events below are plain old javascript events, triggered and handled by vscode 69 // The events below are plain old javascript events, triggered and handled by vscode
71 vscode.window.onDidChangeActiveTextEditor( 70 vscode.window.onDidChangeActiveTextEditor(
72 events.changeActiveTextEditor.makeHandler(), 71 events.changeActiveTextEditor.makeHandler(),
73 ); 72 );
74 73
75 disposeOnDeactivation(
76 vscode.workspace.registerTextDocumentContentProvider(
77 'rust-analyzer',
78 expandMacroContentProvider,
79 ),
80 );
81
82 registerCommand(
83 'rust-analyzer.expandMacro',
84 commands.expandMacro.createHandle(expandMacroContentProvider),
85 );
86
87 const startServer = () => Server.start(allNotifications); 74 const startServer = () => Server.start(allNotifications);
88 const reloadCommand = () => reloadServer(startServer); 75 const reloadCommand = () => reloadServer(startServer);
89 76