rust-analyzer/editors/code/src/run.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

252 lines
8.2 KiB
TypeScript
Raw Normal View History

2018-10-07 20:44:25 +00:00
import * as vscode from "vscode";
import type * as lc from "vscode-languageclient";
import * as ra from "./lsp_ext";
2020-06-18 19:20:13 +00:00
import * as tasks from "./tasks";
2022-05-17 17:15:06 +00:00
import type { CtxInit } from "./ctx";
2020-06-02 12:33:47 +00:00
import { makeDebugConfig } from "./debug";
2023-07-18 10:51:57 +00:00
import type { Config, RunnableEnvCfg, RunnableEnvCfgItem } from "./config";
import { unwrapUndefinable } from "./undefinable";
import type { LanguageClient } from "vscode-languageclient/node";
import type { RustEditor } from "./util";
import * as toolchain from "./toolchain";
2022-05-17 17:15:06 +00:00
2020-05-14 10:22:52 +00:00
const quickPickButtons = [
2022-05-26 17:12:49 +00:00
{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configuration." },
2020-05-14 10:22:52 +00:00
];
2022-05-17 17:15:06 +00:00
2020-05-25 10:02:30 +00:00
export async function selectRunnable(
2022-10-28 22:44:37 +00:00
ctx: CtxInit,
2020-05-25 10:02:30 +00:00
prevRunnable?: RunnableQuickPick,
debuggeeOnly = false,
2023-07-11 13:35:10 +00:00
showButtons: boolean = true,
2020-05-25 10:02:30 +00:00
): Promise<RunnableQuickPick | undefined> {
2020-05-11 13:06:57 +00:00
const editor = ctx.activeRustEditor;
2022-10-17 13:05:20 +00:00
if (!editor) return;
2020-05-11 13:06:57 +00:00
// show a placeholder while we get the runnables from the server
const quickPick = vscode.window.createQuickPick();
quickPick.title = "Select Runnable";
if (showButtons) {
quickPick.buttons = quickPickButtons;
2020-05-11 13:06:57 +00:00
}
quickPick.items = [{ label: "Looking for runnables..." }];
quickPick.activeItems = [];
quickPick.show();
2020-05-17 17:29:59 +00:00
const runnables = await getRunnables(ctx.client, editor, prevRunnable, debuggeeOnly);
2020-05-14 10:22:52 +00:00
if (runnables.length === 0) {
2020-05-17 17:29:59 +00:00
// it is the debug case, run always has at least 'cargo check ...'
// see crates\rust-analyzer\src\main_loop\handlers.rs, handle_runnables
2021-02-09 14:12:46 +00:00
await vscode.window.showErrorMessage("There's no debug target!");
quickPick.dispose();
2020-05-17 17:29:59 +00:00
return;
}
// clear the list before we hook up listeners to avoid invoking them
// if the user happens to accept the placeholder item
quickPick.items = [];
2020-05-14 10:22:52 +00:00
return await populateAndGetSelection(
quickPick as vscode.QuickPick<RunnableQuickPick>,
runnables,
ctx,
showButtons,
);
2020-05-11 13:06:57 +00:00
}
2019-12-30 18:58:44 +00:00
2020-05-25 10:02:30 +00:00
export 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: ra.Runnable) {
2018-10-07 20:59:02 +00:00
this.label = runnable.label;
2018-10-07 20:44:25 +00:00
}
}
export function prepareBaseEnv(): Record<string, string> {
const env: Record<string, string> = { RUST_BACKTRACE: "short" };
Object.assign(env, process.env as { [key: string]: string });
return env;
}
2020-07-02 18:33:26 +00:00
export function prepareEnv(
label: string,
runnableArgs: ra.CargoRunnableArgs,
2023-07-11 13:35:10 +00:00
runnableEnvCfg: RunnableEnvCfg,
2020-07-02 18:33:26 +00:00
): Record<string, string> {
const env = prepareBaseEnv();
2020-07-02 16:47:40 +00:00
if (runnableArgs.expectTest) {
2020-07-02 16:47:40 +00:00
env["UPDATE_EXPECT"] = "1";
}
2023-07-18 10:51:57 +00:00
const platform = process.platform;
const checkPlatform = (it: RunnableEnvCfgItem) => {
if (it.platform) {
const platforms = Array.isArray(it.platform) ? it.platform : [it.platform];
return platforms.indexOf(platform) >= 0;
}
return true;
};
2020-07-03 11:56:30 +00:00
2020-07-02 18:33:26 +00:00
if (runnableEnvCfg) {
if (Array.isArray(runnableEnvCfg)) {
for (const it of runnableEnvCfg) {
const masked = !it.mask || new RegExp(it.mask).test(label);
2023-07-18 10:51:57 +00:00
if (masked && checkPlatform(it)) {
2020-07-02 16:47:40 +00:00
Object.assign(env, it.env);
}
}
} else {
2020-07-02 19:08:33 +00:00
Object.assign(env, runnableEnvCfg);
2020-07-02 16:47:40 +00:00
}
}
return env;
}
2024-06-17 12:06:01 +00:00
export async function createTaskFromRunnable(
runnable: ra.Runnable,
config: Config,
): Promise<vscode.Task> {
let definition: tasks.RustTargetDefinition;
if (runnable.kind === "cargo") {
2024-06-17 12:06:01 +00:00
const runnableArgs = runnable.args;
let args = createCargoArgs(runnableArgs);
let program: string;
if (runnableArgs.overrideCargo) {
// Split on spaces to allow overrides like "wrapper cargo".
const cargoParts = runnableArgs.overrideCargo.split(" ");
program = unwrapUndefinable(cargoParts[0]);
args = [...cargoParts.slice(1), ...args];
} else {
program = await toolchain.cargoPath();
}
2020-06-02 15:22:23 +00:00
definition = {
2024-06-17 12:06:01 +00:00
type: tasks.CARGO_TASK_TYPE,
command: program,
args,
cwd: runnableArgs.workspaceRoot || ".",
env: prepareEnv(runnable.label, runnableArgs, config.runnablesExtraEnv),
};
} else {
2024-06-17 12:06:01 +00:00
const runnableArgs = runnable.args;
definition = {
2024-06-17 12:06:01 +00:00
type: tasks.SHELL_TASK_TYPE,
command: runnableArgs.program,
args: runnableArgs.args,
cwd: runnableArgs.cwd,
env: prepareBaseEnv(),
};
2020-06-02 15:22:23 +00:00
}
2020-06-18 19:20:13 +00:00
2021-02-07 18:36:16 +00:00
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
2024-06-17 12:06:01 +00:00
const exec = await tasks.targetToExecution(definition, config.cargoRunner, true);
const task = await tasks.buildRustTask(
target,
definition,
runnable.label,
config.problemMatcher,
2024-06-17 12:06:01 +00:00
exec,
);
task.presentationOptions.clear = true;
// Sadly, this doesn't prevent focus stealing if the terminal is currently
// hidden, and will become revealed due to task execution.
task.presentationOptions.focus = false;
2020-06-18 19:20:13 +00:00
return task;
2018-10-07 20:44:25 +00:00
}
export function createCargoArgs(runnableArgs: ra.CargoRunnableArgs): string[] {
const args = [...runnableArgs.cargoArgs]; // should be a copy!
if (runnableArgs.cargoExtraArgs) {
args.push(...runnableArgs.cargoExtraArgs); // Append user-specified cargo options.
}
if (runnableArgs.executableArgs.length > 0) {
args.push("--", ...runnableArgs.executableArgs);
}
return args;
}
async function getRunnables(
client: LanguageClient,
editor: RustEditor,
prevRunnable?: RunnableQuickPick,
debuggeeOnly = false,
): Promise<RunnableQuickPick[]> {
const textDocument: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString(),
};
const runnables = await client.sendRequest(ra.runnables, {
textDocument,
position: client.code2ProtocolConverter.asPosition(editor.selection.active),
});
const items: RunnableQuickPick[] = [];
if (prevRunnable) {
items.push(prevRunnable);
}
for (const r of runnables) {
if (prevRunnable && JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)) {
continue;
}
if (debuggeeOnly && (r.label.startsWith("doctest") || r.label.startsWith("cargo"))) {
continue;
}
items.push(new RunnableQuickPick(r));
}
return items;
}
async function populateAndGetSelection(
quickPick: vscode.QuickPick<RunnableQuickPick>,
runnables: RunnableQuickPick[],
ctx: CtxInit,
showButtons: boolean,
): Promise<RunnableQuickPick | undefined> {
return new Promise((resolve) => {
const disposables: vscode.Disposable[] = [];
const close = (result?: RunnableQuickPick) => {
resolve(result);
disposables.forEach((d) => d.dispose());
};
disposables.push(
quickPick.onDidHide(() => close()),
quickPick.onDidAccept(() => close(quickPick.selectedItems[0] as RunnableQuickPick)),
quickPick.onDidTriggerButton(async (_button) => {
const runnable = unwrapUndefinable(
quickPick.activeItems[0] as RunnableQuickPick,
).runnable;
await makeDebugConfig(ctx, runnable);
close();
}),
quickPick.onDidChangeActive((activeList) => {
if (showButtons && activeList.length > 0) {
const active = unwrapUndefinable(activeList[0]);
if (active.label.startsWith("cargo")) {
// save button makes no sense for `cargo test` or `cargo check`
quickPick.buttons = [];
} else if (quickPick.buttons.length === 0) {
quickPick.buttons = quickPickButtons;
}
}
}),
quickPick,
);
// populate the list with the actual runnables
quickPick.items = runnables;
});
}