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

202 lines
5.6 KiB
TypeScript
Raw Normal View History

2019-03-18 16:51:01 -05:00
import * as child_process from 'child_process';
2019-03-31 07:00:50 -05:00
import * as util from 'util';
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';
2019-03-31 07:00:50 -05:00
import { CargoWatchProvider } from './cargo_watch';
2018-10-07 15:44:25 -05:00
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 };
}
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
/**
* Interactively asks the user whether we should run `cargo check` in order to
* provide inline diagnostics; the user is met with a series of dialog boxes
* that, when accepted, allow us to `cargo install cargo-watch` and then run it.
*/
2019-03-31 08:21:14 -05:00
export async function interactivelyStartCargoWatch(
context: vscode.ExtensionContext
) {
2019-04-02 00:07:40 -05:00
if (Server.config.cargoWatchOptions.enableOnStartup === 'disabled') {
return;
}
2019-04-02 00:07:40 -05:00
if (Server.config.cargoWatchOptions.enableOnStartup === 'ask') {
const watch = await vscode.window.showInformationMessage(
'Start watching changes with cargo? (Executes `cargo watch`, provides inline diagnostics)',
'yes',
'no'
);
if (watch !== 'yes') {
return;
}
}
const execPromise = util.promisify(child_process.exec);
2019-03-18 16:51:01 -05:00
const { stderr } = await execPromise('cargo watch --version').catch(e => e);
if (stderr.includes('no such subcommand: `watch`')) {
const msg =
'The `cargo-watch` subcommand is not installed. Install? (takes ~1-2 minutes)';
const install = await vscode.window.showInformationMessage(
msg,
'yes',
'no'
);
if (install !== 'yes') {
return;
}
const label = 'install-cargo-watch';
const taskFinished = new Promise((resolve, reject) => {
2019-03-18 16:51:01 -05:00
const disposable = vscode.tasks.onDidEndTask(({ execution }) => {
if (execution.task.name === label) {
disposable.dispose();
resolve();
}
});
});
vscode.tasks.executeTask(
createTask({
label,
bin: 'cargo',
args: ['install', 'cargo-watch'],
env: {}
})
);
await taskFinished;
2019-03-18 16:51:01 -05:00
const output = await execPromise('cargo watch --version').catch(e => e);
if (output.stderr !== '') {
vscode.window.showErrorMessage(
2019-03-18 16:51:01 -05:00
`Couldn't install \`cargo-\`watch: ${output.stderr}`
);
return;
}
}
2019-03-31 08:01:51 -05:00
const validater = new CargoWatchProvider();
2019-03-31 08:21:14 -05:00
validater.activate(context.subscriptions);
}