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