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

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-10-07 20:44:25 +00:00
import * as vscode from 'vscode';
2018-10-07 20:59:02 +00:00
import * as lc from 'vscode-languageclient';
2019-12-30 18:58:44 +00:00
import { Ctx, Cmd } from '../ctx';
export function run(ctx: Ctx): Cmd {
let prevRunnable: RunnableQuickPick | undefined;
return async () => {
const editor = ctx.activeRustEditor;
2019-12-31 17:50:32 +00:00
const client = ctx.client;
if (!editor || !client) return;
2019-12-30 18:58:44 +00:00
const textDocument: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString(),
};
const params: RunnablesParams = {
textDocument,
2019-12-31 17:50:32 +00:00
position: client.code2ProtocolConverter.asPosition(
2019-12-30 18:58:44 +00:00
editor.selection.active,
),
};
2019-12-31 17:50:32 +00:00
const runnables = await client.sendRequest<Runnable[]>(
2019-12-30 18:58:44 +00:00
'rust-analyzer/runnables',
params,
);
const items: RunnableQuickPick[] = [];
if (prevRunnable) {
items.push(prevRunnable);
}
for (const r of runnables) {
if (
prevRunnable &&
JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)
) {
continue;
}
items.push(new RunnableQuickPick(r));
}
const item = await vscode.window.showQuickPick(items);
if (!item) return;
item.detail = 'rerun';
prevRunnable = item;
const task = createTask(item.runnable);
return await vscode.tasks.executeTask(task);
2019-12-30 19:07:04 +00:00
};
2019-12-30 18:58:44 +00:00
}
export function runSingle(ctx: Ctx): Cmd {
return async (runnable: Runnable) => {
const editor = ctx.activeRustEditor;
2019-12-30 19:07:04 +00:00
if (!editor) return;
2019-12-30 18:58:44 +00:00
const task = createTask(runnable);
task.group = vscode.TaskGroup.Build;
task.presentationOptions = {
reveal: vscode.TaskRevealKind.Always,
panel: vscode.TaskPanelKind.Dedicated,
clear: true,
};
return vscode.tasks.executeTask(task);
2019-12-30 19:07:04 +00:00
};
2019-12-30 18:58:44 +00:00
}
2018-10-07 20:44:25 +00:00
interface RunnablesParams {
2018-10-07 20:59:02 +00:00
textDocument: lc.TextDocumentIdentifier;
position?: lc.Position;
2018-10-07 20:44:25 +00:00
}
interface Runnable {
label: string;
bin: string;
args: string[];
2018-10-07 20:59:02 +00:00
env: { [index: string]: string };
cwd?: string;
2018-10-07 20:44:25 +00:00
}
class RunnableQuickPick implements vscode.QuickPickItem {
2018-10-07 20:59:02 +00:00
public label: string;
public description?: string | undefined;
public detail?: string | undefined;
public picked?: boolean | undefined;
2018-10-07 20:44:25 +00:00
constructor(public runnable: Runnable) {
2018-10-07 20:59:02 +00:00
this.label = runnable.label;
2018-10-07 20:44:25 +00:00
}
}
interface CargoTaskDefinition extends vscode.TaskDefinition {
type: 'cargo';
label: string;
command: string;
2018-10-07 20:59:02 +00:00
args: string[];
2018-10-07 20:44:25 +00:00
env?: { [key: string]: string };
}
function createTask(spec: Runnable): vscode.Task {
2018-10-07 20:44:25 +00:00
const TASK_SOURCE = 'Rust';
2018-10-07 20:59:02 +00:00
const definition: CargoTaskDefinition = {
2018-10-07 20:44:25 +00:00
type: 'cargo',
label: spec.label,
2018-10-07 20:44:25 +00:00
command: spec.bin,
args: spec.args,
2019-12-09 18:57:55 +00:00
env: spec.env,
2018-10-07 20:59:02 +00:00
};
2018-10-07 20:44:25 +00:00
2018-10-07 20:59:02 +00:00
const execOption: vscode.ShellExecutionOptions = {
cwd: spec.cwd || '.',
2019-12-09 18:57:55 +00:00
env: definition.env,
2018-10-07 20:44:25 +00:00
};
2019-01-12 23:49:07 +00:00
const exec = new vscode.ShellExecution(
definition.command,
definition.args,
2019-12-09 18:57:55 +00:00
execOption,
2019-01-12 23:49:07 +00:00
);
2018-10-07 20:44:25 +00:00
2018-10-07 20:59:02 +00:00
const f = vscode.workspace.workspaceFolders![0];
2018-10-08 21:38:33 +00:00
const t = new vscode.Task(
definition,
f,
definition.label,
TASK_SOURCE,
exec,
2019-12-09 18:57:55 +00:00
['$rustc'],
2018-10-08 21:38:33 +00:00
);
2019-01-12 23:49:07 +00:00
t.presentationOptions.clear = true;
2018-10-07 20:44:25 +00:00
return t;
}