aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/commands.ts')
-rw-r--r--editors/code/src/commands.ts155
1 files changed, 134 insertions, 21 deletions
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 86302db37..1f3a7cf7e 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -8,6 +8,7 @@ import { spawnSync } from 'child_process';
8import { RunnableQuickPick, selectRunnable, createTask } from './run'; 8import { RunnableQuickPick, selectRunnable, createTask } from './run';
9import { AstInspector } from './ast_inspector'; 9import { AstInspector } from './ast_inspector';
10import { isRustDocument, sleep, isRustEditor } from './util'; 10import { isRustDocument, sleep, isRustEditor } from './util';
11import { startDebugSession, makeDebugConfig } from './debug';
11 12
12export * from './ast_inspector'; 13export * from './ast_inspector';
13export * from './run'; 14export * from './run';
@@ -54,6 +55,38 @@ export function analyzerStatus(ctx: Ctx): Cmd {
54 }; 55 };
55} 56}
56 57
58export function memoryUsage(ctx: Ctx): Cmd {
59 const tdcp = new class implements vscode.TextDocumentContentProvider {
60 readonly uri = vscode.Uri.parse('rust-analyzer-memory://memory');
61 readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
62
63 provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult<string> {
64 if (!vscode.window.activeTextEditor) return '';
65
66 return ctx.client.sendRequest(ra.memoryUsage, null).then((mem) => {
67 return 'Per-query memory usage:\n' + mem + '\n(note: database has been cleared)';
68 });
69 }
70
71 get onDidChange(): vscode.Event<vscode.Uri> {
72 return this.eventEmitter.event;
73 }
74 }();
75
76 ctx.pushCleanup(
77 vscode.workspace.registerTextDocumentContentProvider(
78 'rust-analyzer-memory',
79 tdcp,
80 ),
81 );
82
83 return async () => {
84 tdcp.eventEmitter.fire(tdcp.uri);
85 const document = await vscode.workspace.openTextDocument(tdcp.uri);
86 return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true);
87 };
88}
89
57export function matchingBrace(ctx: Ctx): Cmd { 90export function matchingBrace(ctx: Ctx): Cmd {
58 return async () => { 91 return async () => {
59 const editor = ctx.activeRustEditor; 92 const editor = ctx.activeRustEditor;
@@ -157,7 +190,7 @@ export function ssr(ctx: Ctx): Cmd {
157 190
158 const options: vscode.InputBoxOptions = { 191 const options: vscode.InputBoxOptions = {
159 value: "() ==>> ()", 192 value: "() ==>> ()",
160 prompt: "Enter request, for example 'Foo($a:expr) ==> Foo::new($a)' ", 193 prompt: "Enter request, for example 'Foo($a) ==> Foo::new($a)' ",
161 validateInput: async (x: string) => { 194 validateInput: async (x: string) => {
162 try { 195 try {
163 await client.sendRequest(ra.ssr, { query: x, parseOnly: true }); 196 await client.sendRequest(ra.ssr, { query: x, parseOnly: true });
@@ -170,9 +203,15 @@ export function ssr(ctx: Ctx): Cmd {
170 const request = await vscode.window.showInputBox(options); 203 const request = await vscode.window.showInputBox(options);
171 if (!request) return; 204 if (!request) return;
172 205
173 const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); 206 vscode.window.withProgress({
207 location: vscode.ProgressLocation.Notification,
208 title: "Structured search replace in progress...",
209 cancellable: false,
210 }, async (_progress, _token) => {
211 const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false });
174 212
175 await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit)); 213 await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit));
214 });
176 }; 215 };
177} 216}
178 217
@@ -197,20 +236,6 @@ export function toggleInlayHints(ctx: Ctx): Cmd {
197 }; 236 };
198} 237}
199 238
200export function run(ctx: Ctx): Cmd {
201 let prevRunnable: RunnableQuickPick | undefined;
202
203 return async () => {
204 const item = await selectRunnable(ctx, prevRunnable);
205 if (!item) return;
206
207 item.detail = 'rerun';
208 prevRunnable = item;
209 const task = createTask(item.runnable);
210 return await vscode.tasks.executeTask(task);
211 };
212}
213
214// Opens the virtual file that will show the syntax tree 239// Opens the virtual file that will show the syntax tree
215// 240//
216// The contents of the file come from the `TextDocumentContentProvider` 241// The contents of the file come from the `TextDocumentContentProvider`
@@ -337,8 +362,8 @@ export function expandMacro(ctx: Ctx): Cmd {
337 }; 362 };
338} 363}
339 364
340export function collectGarbage(ctx: Ctx): Cmd { 365export function reloadWorkspace(ctx: Ctx): Cmd {
341 return async () => ctx.client.sendRequest(ra.collectGarbage, null); 366 return async () => ctx.client.sendRequest(ra.reloadWorkspace, null);
342} 367}
343 368
344export function showReferences(ctx: Ctx): Cmd { 369export function showReferences(ctx: Ctx): Cmd {
@@ -356,10 +381,39 @@ export function showReferences(ctx: Ctx): Cmd {
356} 381}
357 382
358export function applyActionGroup(_ctx: Ctx): Cmd { 383export function applyActionGroup(_ctx: Ctx): Cmd {
359 return async (actions: { label: string; edit: vscode.WorkspaceEdit }[]) => { 384 return async (actions: { label: string; arguments: ra.ResolveCodeActionParams }[]) => {
360 const selectedAction = await vscode.window.showQuickPick(actions); 385 const selectedAction = await vscode.window.showQuickPick(actions);
361 if (!selectedAction) return; 386 if (!selectedAction) return;
362 await applySnippetWorkspaceEdit(selectedAction.edit); 387 vscode.commands.executeCommand(
388 'rust-analyzer.resolveCodeAction',
389 selectedAction.arguments,
390 );
391 };
392}
393
394export function gotoLocation(ctx: Ctx): Cmd {
395 return async (locationLink: lc.LocationLink) => {
396 const client = ctx.client;
397 if (client) {
398 const uri = client.protocol2CodeConverter.asUri(locationLink.targetUri);
399 let range = client.protocol2CodeConverter.asRange(locationLink.targetSelectionRange);
400 // collapse the range to a cursor position
401 range = range.with({ end: range.start });
402
403 await vscode.window.showTextDocument(uri, { selection: range });
404 }
405 };
406}
407
408export function resolveCodeAction(ctx: Ctx): Cmd {
409 const client = ctx.client;
410 return async (params: ra.ResolveCodeActionParams) => {
411 const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, params);
412 if (!item) {
413 return;
414 }
415 const edit = client.protocol2CodeConverter.asWorkspaceEdit(item);
416 await applySnippetWorkspaceEdit(edit);
363 }; 417 };
364} 418}
365 419
@@ -368,3 +422,62 @@ export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd {
368 await applySnippetWorkspaceEdit(edit); 422 await applySnippetWorkspaceEdit(edit);
369 }; 423 };
370} 424}
425
426export function run(ctx: Ctx): Cmd {
427 let prevRunnable: RunnableQuickPick | undefined;
428
429 return async () => {
430 const item = await selectRunnable(ctx, prevRunnable);
431 if (!item) return;
432
433 item.detail = 'rerun';
434 prevRunnable = item;
435 const task = await createTask(item.runnable, ctx.config);
436 return await vscode.tasks.executeTask(task);
437 };
438}
439
440export function runSingle(ctx: Ctx): Cmd {
441 return async (runnable: ra.Runnable) => {
442 const editor = ctx.activeRustEditor;
443 if (!editor) return;
444
445 const task = await createTask(runnable, ctx.config);
446 task.group = vscode.TaskGroup.Build;
447 task.presentationOptions = {
448 reveal: vscode.TaskRevealKind.Always,
449 panel: vscode.TaskPanelKind.Dedicated,
450 clear: true,
451 };
452
453 return vscode.tasks.executeTask(task);
454 };
455}
456
457export function debug(ctx: Ctx): Cmd {
458 let prevDebuggee: RunnableQuickPick | undefined;
459
460 return async () => {
461 const item = await selectRunnable(ctx, prevDebuggee, true);
462 if (!item) return;
463
464 item.detail = 'restart';
465 prevDebuggee = item;
466 return await startDebugSession(ctx, item.runnable);
467 };
468}
469
470export function debugSingle(ctx: Ctx): Cmd {
471 return async (config: ra.Runnable) => {
472 await startDebugSession(ctx, config);
473 };
474}
475
476export function newDebugConfig(ctx: Ctx): Cmd {
477 return async () => {
478 const item = await selectRunnable(ctx, undefined, true, false);
479 if (!item) return;
480
481 await makeDebugConfig(ctx, item.runnable);
482 };
483}