aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/runnables.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/commands/runnables.ts')
-rw-r--r--editors/code/src/commands/runnables.ts88
1 files changed, 88 insertions, 0 deletions
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts
new file mode 100644
index 000000000..45c16497d
--- /dev/null
+++ b/editors/code/src/commands/runnables.ts
@@ -0,0 +1,88 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'
3import { Server } from '../server';
4
5interface RunnablesParams {
6 textDocument: lc.TextDocumentIdentifier,
7 position?: lc.Position,
8}
9
10interface Runnable {
11 range: lc.Range;
12 label: string;
13 bin: string;
14 args: string[];
15 env: { [index: string]: string },
16}
17
18class RunnableQuickPick implements vscode.QuickPickItem {
19 label: string;
20 description?: string | undefined;
21 detail?: string | undefined;
22 picked?: boolean | undefined;
23
24 constructor(public runnable: Runnable) {
25 this.label = runnable.label
26 }
27}
28
29interface CargoTaskDefinition extends vscode.TaskDefinition {
30 type: 'cargo';
31 label: string;
32 command: string;
33 args: Array<string>;
34 env?: { [key: string]: string };
35}
36
37function createTask(spec: Runnable): vscode.Task {
38 const TASK_SOURCE = 'Rust';
39 let definition: CargoTaskDefinition = {
40 type: 'cargo',
41 label: 'cargo',
42 command: spec.bin,
43 args: spec.args,
44 env: spec.env
45 }
46
47 let execCmd = `${definition.command} ${definition.args.join(' ')}`;
48 let execOption: vscode.ShellExecutionOptions = {
49 cwd: '.',
50 env: definition.env,
51 };
52 let exec = new vscode.ShellExecution(`clear; ${execCmd}`, execOption);
53
54 let f = vscode.workspace.workspaceFolders![0]
55 let t = new vscode.Task(definition, f, definition.label, TASK_SOURCE, exec, ['$rustc']);
56 return t;
57}
58
59let prevRunnable: RunnableQuickPick | undefined = undefined
60export async function handle() {
61 let editor = vscode.window.activeTextEditor
62 if (editor == null || editor.document.languageId != "rust") return
63 let textDocument: lc.TextDocumentIdentifier = {
64 uri: editor.document.uri.toString()
65 }
66 let params: RunnablesParams = {
67 textDocument,
68 position: Server.client.code2ProtocolConverter.asPosition(editor.selection.active)
69 }
70 let runnables = await Server.client.sendRequest<Runnable[]>('m/runnables', params)
71 let items: RunnableQuickPick[] = []
72 if (prevRunnable) {
73 items.push(prevRunnable)
74 }
75 for (let r of runnables) {
76 if (prevRunnable && JSON.stringify(prevRunnable.runnable) == JSON.stringify(r)) {
77 continue
78 }
79 items.push(new RunnableQuickPick(r))
80 }
81 let item = await vscode.window.showQuickPick(items)
82 if (item) {
83 item.detail = "rerun"
84 prevRunnable = item
85 let task = createTask(item.runnable)
86 return await vscode.tasks.executeTask(task)
87 }
88}