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

106 lines
2.7 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 {
range: lc.Range;
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 };
}
function createTask(spec: Runnable): vscode.Task {
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: 'cargo',
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
};
2018-12-15 13:33:09 -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']
);
2018-12-15 13:33:09 -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[]>(
'm/runnables',
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
}
}