rust/editors/code/src/commands/runnables.ts

146 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-10-07 15:44:25 -05:00
import * as vscode from 'vscode';
2018-10-07 15:59:02 -05:00
import * as lc from 'vscode-languageclient';
2018-10-07 15:44:25 -05:00
import { Server } from '../server';
interface RunnablesParams {
2018-10-07 15:59:02 -05:00
textDocument: lc.TextDocumentIdentifier;
position?: lc.Position;
2018-10-07 15:44:25 -05:00
}
interface Runnable {
label: string;
bin: string;
args: string[];
2018-10-07 15:59:02 -05:00
env: { [index: string]: string };
2018-10-07 15:44:25 -05:00
}
class RunnableQuickPick implements vscode.QuickPickItem {
2018-10-07 15:59:02 -05:00
public label: string;
public description?: string | undefined;
public detail?: string | undefined;
public picked?: boolean | undefined;
2018-10-07 15:44:25 -05:00
constructor(public runnable: Runnable) {
2018-10-07 15:59:02 -05:00
this.label = runnable.label;
2018-10-07 15:44:25 -05:00
}
}
interface CargoTaskDefinition extends vscode.TaskDefinition {
type: 'cargo';
label: string;
command: string;
2018-10-07 15:59:02 -05:00
args: string[];
2018-10-07 15:44:25 -05:00
env?: { [key: string]: string };
}
2019-03-18 14:47:52 -05:00
export function createTask(spec: Runnable): vscode.Task {
2018-10-07 15:44:25 -05:00
const TASK_SOURCE = 'Rust';
2018-10-07 15:59:02 -05:00
const definition: CargoTaskDefinition = {
2018-10-07 15:44:25 -05:00
type: 'cargo',
label: spec.label,
2018-10-07 15:44:25 -05:00
command: spec.bin,
args: spec.args,
2018-10-08 16:38:33 -05:00
env: spec.env
2018-10-07 15:59:02 -05:00
};
2018-10-07 15:44:25 -05:00
2018-10-07 15:59:02 -05:00
const execOption: vscode.ShellExecutionOptions = {
2018-10-07 15:44:25 -05:00
cwd: '.',
2018-10-08 16:38:33 -05:00
env: definition.env
2018-10-07 15:44:25 -05:00
};
2019-01-12 17:49:07 -06:00
const exec = new vscode.ShellExecution(
definition.command,
definition.args,
execOption
);
2018-10-07 15:44:25 -05:00
2018-10-07 15:59:02 -05:00
const f = vscode.workspace.workspaceFolders![0];
2018-10-08 16:38:33 -05:00
const t = new vscode.Task(
definition,
f,
definition.label,
TASK_SOURCE,
exec,
['$rustc']
);
2019-01-12 17:49:07 -06:00
t.presentationOptions.clear = true;
2018-10-07 15:44:25 -05:00
return t;
}
2018-10-07 15:59:02 -05:00
let prevRunnable: RunnableQuickPick | undefined;
2018-10-07 15:44:25 -05:00
export async function handle() {
2018-10-07 15:59:02 -05:00
const editor = vscode.window.activeTextEditor;
2018-10-08 16:38:33 -05:00
if (editor == null || editor.document.languageId !== 'rust') {
return;
}
2018-10-07 15:59:02 -05:00
const textDocument: lc.TextDocumentIdentifier = {
2018-10-08 16:38:33 -05:00
uri: editor.document.uri.toString()
2018-10-07 15:59:02 -05:00
};
const params: RunnablesParams = {
2018-10-07 15:44:25 -05:00
textDocument,
2018-10-08 16:38:33 -05:00
position: Server.client.code2ProtocolConverter.asPosition(
editor.selection.active
)
2018-10-07 15:59:02 -05:00
};
2018-10-08 16:38:33 -05:00
const runnables = await Server.client.sendRequest<Runnable[]>(
2019-01-28 05:43:07 -06:00
'rust-analyzer/runnables',
2018-10-08 16:38:33 -05:00
params
);
2018-10-07 15:59:02 -05:00
const items: RunnableQuickPick[] = [];
2018-10-07 15:44:25 -05:00
if (prevRunnable) {
2018-10-07 15:59:02 -05:00
items.push(prevRunnable);
2018-10-07 15:44:25 -05:00
}
2018-10-07 15:59:02 -05:00
for (const r of runnables) {
2018-10-08 16:38:33 -05:00
if (
prevRunnable &&
JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)
) {
2018-10-07 15:59:02 -05:00
continue;
2018-10-07 15:44:25 -05:00
}
2018-10-07 15:59:02 -05:00
items.push(new RunnableQuickPick(r));
2018-10-07 15:44:25 -05:00
}
2018-10-07 15:59:02 -05:00
const item = await vscode.window.showQuickPick(items);
2018-10-07 15:44:25 -05:00
if (item) {
2018-10-07 15:59:02 -05:00
item.detail = 'rerun';
prevRunnable = item;
const task = createTask(item.runnable);
return await vscode.tasks.executeTask(task);
2018-10-07 15:44:25 -05:00
}
}
2019-01-12 12:54:08 -06:00
export async function handleSingle(runnable: Runnable) {
const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId !== 'rust') {
return;
}
const task = createTask(runnable);
task.group = vscode.TaskGroup.Build;
task.presentationOptions = {
reveal: vscode.TaskRevealKind.Always,
panel: vscode.TaskPanelKind.Dedicated,
clear: true
2019-01-12 12:54:08 -06:00
};
2019-01-12 17:49:07 -06:00
2019-01-12 12:54:08 -06:00
return vscode.tasks.executeTask(task);
2019-01-12 17:49:07 -06:00
}
2019-03-18 14:47:52 -05:00
export const autoCargoWatchTask: vscode.Task = {
name: 'cargo watch',
source: 'rust-analyzer',
definition: {
2019-03-18 16:15:03 -05:00
type: 'watch'
2019-03-18 14:47:52 -05:00
},
execution: new vscode.ShellExecution('cargo', ['watch'], { cwd: '.' }),
isBackground: true,
problemMatchers: ['$rustc-watch'],
presentationOptions: {
clear: true
},
// Not yet exposed in the vscode.d.ts
2019-03-18 16:15:03 -05:00
// https://github.com/Microsoft/vscode/blob/ea7c31d770e04b51d586b0d3944f3a7feb03afb9/src/vs/workbench/contrib/tasks/common/tasks.ts#L444-L456
runOptions: ({
runOn: 2 // RunOnOptions.folderOpen
} as unknown) as vscode.RunOptions
2019-03-18 14:47:52 -05:00
};