aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-30 18:58:44 +0000
committerAleksey Kladov <[email protected]>2019-12-30 18:58:44 +0000
commit6b118c9b8de3c3ed84a16aba2e71fa5a9ada6e74 (patch)
treec3c537d5b953374e1108edbfb2b4fcda2cc96b91 /editors/code
parent237abb85c40672e8cdafa423db6187c107369a09 (diff)
Refactor runables
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/commands/index.ts5
-rw-r--r--editors/code/src/commands/runnables.ts122
-rw-r--r--editors/code/src/main.ts6
3 files changed, 66 insertions, 67 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 325ae3da8..9f4636e52 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -8,7 +8,7 @@ import { parentModule } from './parent_module';
8import { syntaxTree } from './syntax_tree'; 8import { syntaxTree } from './syntax_tree';
9import { 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 { run, runSingle } from './runnables';
12 12
13function collectGarbage(ctx: Ctx): Cmd { 13function collectGarbage(ctx: Ctx): Cmd {
14 return async () => { 14 return async () => {
@@ -22,9 +22,10 @@ export {
22 joinLines, 22 joinLines,
23 matchingBrace, 23 matchingBrace,
24 parentModule, 24 parentModule,
25 runnables,
26 syntaxTree, 25 syntaxTree,
27 onEnter, 26 onEnter,
28 inlayHints, 27 inlayHints,
29 collectGarbage, 28 collectGarbage,
29 run,
30 runSingle
30}; 31};
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts
index 7728541de..c4be21a0c 100644
--- a/editors/code/src/commands/runnables.ts
+++ b/editors/code/src/commands/runnables.ts
@@ -1,7 +1,67 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3 3
4import { Server } from '../server'; 4import { Ctx, Cmd } from '../ctx';
5
6export function run(ctx: Ctx): Cmd {
7 let prevRunnable: RunnableQuickPick | undefined;
8
9 return async () => {
10 const editor = ctx.activeRustEditor;
11 if (!editor) return
12
13 const textDocument: lc.TextDocumentIdentifier = {
14 uri: editor.document.uri.toString(),
15 };
16 const params: RunnablesParams = {
17 textDocument,
18 position: ctx.client.code2ProtocolConverter.asPosition(
19 editor.selection.active,
20 ),
21 };
22 const runnables = await ctx.client.sendRequest<Runnable[]>(
23 'rust-analyzer/runnables',
24 params,
25 );
26 const items: RunnableQuickPick[] = [];
27 if (prevRunnable) {
28 items.push(prevRunnable);
29 }
30 for (const r of runnables) {
31 if (
32 prevRunnable &&
33 JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)
34 ) {
35 continue;
36 }
37 items.push(new RunnableQuickPick(r));
38 }
39 const item = await vscode.window.showQuickPick(items);
40 if (!item) return;
41
42 item.detail = 'rerun';
43 prevRunnable = item;
44 const task = createTask(item.runnable);
45 return await vscode.tasks.executeTask(task);
46 }
47}
48
49export function runSingle(ctx: Ctx): Cmd {
50 return async (runnable: Runnable) => {
51 const editor = ctx.activeRustEditor;
52 if (!editor) return
53
54 const task = createTask(runnable);
55 task.group = vscode.TaskGroup.Build;
56 task.presentationOptions = {
57 reveal: vscode.TaskRevealKind.Always,
58 panel: vscode.TaskPanelKind.Dedicated,
59 clear: true,
60 };
61
62 return vscode.tasks.executeTask(task);
63 }
64}
5 65
6interface RunnablesParams { 66interface RunnablesParams {
7 textDocument: lc.TextDocumentIdentifier; 67 textDocument: lc.TextDocumentIdentifier;
@@ -67,63 +127,3 @@ function createTask(spec: Runnable): vscode.Task {
67 t.presentationOptions.clear = true; 127 t.presentationOptions.clear = true;
68 return t; 128 return t;
69} 129}
70
71let prevRunnable: RunnableQuickPick | undefined;
72export async function handle(): Promise<vscode.TaskExecution | undefined> {
73 const editor = vscode.window.activeTextEditor;
74 if (editor == null || editor.document.languageId !== 'rust') {
75 return;
76 }
77 const textDocument: lc.TextDocumentIdentifier = {
78 uri: editor.document.uri.toString(),
79 };
80 const params: RunnablesParams = {
81 textDocument,
82 position: Server.client.code2ProtocolConverter.asPosition(
83 editor.selection.active,
84 ),
85 };
86 const runnables = await Server.client.sendRequest<Runnable[]>(
87 'rust-analyzer/runnables',
88 params,
89 );
90 const items: RunnableQuickPick[] = [];
91 if (prevRunnable) {
92 items.push(prevRunnable);
93 }
94 for (const r of runnables) {
95 if (
96 prevRunnable &&
97 JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)
98 ) {
99 continue;
100 }
101 items.push(new RunnableQuickPick(r));
102 }
103 const item = await vscode.window.showQuickPick(items);
104 if (!item) {
105 return;
106 }
107
108 item.detail = 'rerun';
109 prevRunnable = item;
110 const task = createTask(item.runnable);
111 return await vscode.tasks.executeTask(task);
112}
113
114export async function handleSingle(runnable: Runnable) {
115 const editor = vscode.window.activeTextEditor;
116 if (editor == null || editor.document.languageId !== 'rust') {
117 return;
118 }
119
120 const task = createTask(runnable);
121 task.group = vscode.TaskGroup.Build;
122 task.presentationOptions = {
123 reveal: vscode.TaskRevealKind.Always,
124 panel: vscode.TaskPanelKind.Dedicated,
125 clear: true,
126 };
127
128 return vscode.tasks.executeTask(task);
129}
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index b8e3396a6..7ad5e6934 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -20,6 +20,8 @@ export async function activate(context: vscode.ExtensionContext) {
20 ctx.registerCommand('parentModule', commands.parentModule); 20 ctx.registerCommand('parentModule', commands.parentModule);
21 ctx.registerCommand('syntaxTree', commands.syntaxTree); 21 ctx.registerCommand('syntaxTree', commands.syntaxTree);
22 ctx.registerCommand('expandMacro', commands.expandMacro); 22 ctx.registerCommand('expandMacro', commands.expandMacro);
23 ctx.registerCommand('run', commands.run);
24 ctx.registerCommand('runSingle', commands.runSingle); // Internal action for lenses
23 25
24 function disposeOnDeactivation(disposable: vscode.Disposable) { 26 function disposeOnDeactivation(disposable: vscode.Disposable) {
25 context.subscriptions.push(disposable); 27 context.subscriptions.push(disposable);
@@ -29,10 +31,6 @@ export async function activate(context: vscode.ExtensionContext) {
29 disposeOnDeactivation(vscode.commands.registerCommand(name, f)); 31 disposeOnDeactivation(vscode.commands.registerCommand(name, f));
30 } 32 }
31 33
32 // Commands are requests from vscode to the language server
33 registerCommand('rust-analyzer.run', commands.runnables.handle);
34 // Unlike the above this does not send requests to the language server
35 registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle);
36 registerCommand( 34 registerCommand(
37 'rust-analyzer.showReferences', 35 'rust-analyzer.showReferences',
38 (uri: string, position: lc.Position, locations: lc.Location[]) => { 36 (uri: string, position: lc.Position, locations: lc.Location[]) => {