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

89 lines
2.6 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';
2018-10-07 20:44:25 +00:00
import { Server } from '../server';
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 {
range: lc.Range;
label: string;
bin: string;
args: string[];
2018-10-07 20:59:02 +00:00
env: { [index: string]: 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 {
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: 'cargo',
command: spec.bin,
args: spec.args,
2018-10-07 20:59:02 +00:00
env: spec.env,
};
2018-10-07 20:44:25 +00:00
2018-10-07 20:59:02 +00:00
const execCmd = `${definition.command} ${definition.args.join(' ')}`;
const execOption: vscode.ShellExecutionOptions = {
2018-10-07 20:44:25 +00:00
cwd: '.',
env: definition.env,
};
2018-10-07 20:59:02 +00:00
const exec = new vscode.ShellExecution(`clear; ${execCmd}`, execOption);
2018-10-07 20:44:25 +00:00
2018-10-07 20:59:02 +00:00
const f = vscode.workspace.workspaceFolders![0];
const t = new vscode.Task(definition, f, definition.label, TASK_SOURCE, exec, ['$rustc']);
2018-10-07 20:44:25 +00:00
return t;
}
2018-10-07 20:59:02 +00:00
let prevRunnable: RunnableQuickPick | undefined;
2018-10-07 20:44:25 +00:00
export async function handle() {
2018-10-07 20:59:02 +00:00
const editor = vscode.window.activeTextEditor;
2018-10-08 18:18:55 +00:00
if (editor == null || editor.document.languageId !== 'rust') { return; }
2018-10-07 20:59:02 +00:00
const textDocument: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString(),
};
const params: RunnablesParams = {
2018-10-07 20:44:25 +00:00
textDocument,
2018-10-07 20:59:02 +00:00
position: Server.client.code2ProtocolConverter.asPosition(editor.selection.active),
};
const runnables = await Server.client.sendRequest<Runnable[]>('m/runnables', params);
const items: RunnableQuickPick[] = [];
2018-10-07 20:44:25 +00:00
if (prevRunnable) {
2018-10-07 20:59:02 +00:00
items.push(prevRunnable);
2018-10-07 20:44:25 +00:00
}
2018-10-07 20:59:02 +00:00
for (const r of runnables) {
2018-10-08 18:18:55 +00:00
if (prevRunnable && JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)) {
2018-10-07 20:59:02 +00:00
continue;
2018-10-07 20:44:25 +00:00
}
2018-10-07 20:59:02 +00:00
items.push(new RunnableQuickPick(r));
2018-10-07 20:44:25 +00:00
}
2018-10-07 20:59:02 +00:00
const item = await vscode.window.showQuickPick(items);
2018-10-07 20:44:25 +00:00
if (item) {
2018-10-07 20:59:02 +00:00
item.detail = 'rerun';
prevRunnable = item;
const task = createTask(item.runnable);
return await vscode.tasks.executeTask(task);
2018-10-07 20:44:25 +00:00
}
}