mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +00:00
Auto merge of #16839 - Wilfred:extension_refactor_for_shell, r=Veykril
Refactor extension to support arbitrary shell command runnables Currently, the extension assumes that all runnables invoke cargo. Arguments are sometimes full CLI arguments, and sometimes arguments passed to a cargo subcommand. Refactor the extension so that tasks are just a `program` and a list of strings `args`, and rename `CargoTask` to `RustTask` to make it generic. (This was factored out of #16135 and tidied.)
This commit is contained in:
commit
f6e2895ee6
2 changed files with 40 additions and 38 deletions
|
@ -2,6 +2,7 @@ import * as vscode from "vscode";
|
||||||
import type * as lc from "vscode-languageclient";
|
import type * as lc from "vscode-languageclient";
|
||||||
import * as ra from "./lsp_ext";
|
import * as ra from "./lsp_ext";
|
||||||
import * as tasks from "./tasks";
|
import * as tasks from "./tasks";
|
||||||
|
import * as toolchain from "./toolchain";
|
||||||
|
|
||||||
import type { CtxInit } from "./ctx";
|
import type { CtxInit } from "./ctx";
|
||||||
import { makeDebugConfig } from "./debug";
|
import { makeDebugConfig } from "./debug";
|
||||||
|
@ -111,12 +112,22 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
|
||||||
throw `Unexpected runnable kind: ${runnable.kind}`;
|
throw `Unexpected runnable kind: ${runnable.kind}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = createArgs(runnable);
|
let program: string;
|
||||||
|
let args = createArgs(runnable);
|
||||||
|
if (runnable.args.overrideCargo) {
|
||||||
|
// Split on spaces to allow overrides like "wrapper cargo".
|
||||||
|
const cargoParts = runnable.args.overrideCargo.split(" ");
|
||||||
|
|
||||||
const definition: tasks.CargoTaskDefinition = {
|
program = unwrapUndefinable(cargoParts[0]);
|
||||||
|
args = [...cargoParts.slice(1), ...args];
|
||||||
|
} else {
|
||||||
|
program = await toolchain.cargoPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
const definition: tasks.RustTargetDefinition = {
|
||||||
type: tasks.TASK_TYPE,
|
type: tasks.TASK_TYPE,
|
||||||
command: args[0], // run, test, etc...
|
program,
|
||||||
args: args.slice(1),
|
args,
|
||||||
cwd: runnable.args.workspaceRoot || ".",
|
cwd: runnable.args.workspaceRoot || ".",
|
||||||
env: prepareEnv(runnable, config.runnablesExtraEnv),
|
env: prepareEnv(runnable, config.runnablesExtraEnv),
|
||||||
overrideCargo: runnable.args.overrideCargo,
|
overrideCargo: runnable.args.overrideCargo,
|
||||||
|
@ -124,22 +135,21 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||||
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
|
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
|
||||||
const cargoTask = await tasks.buildCargoTask(
|
const task = await tasks.buildRustTask(
|
||||||
target,
|
target,
|
||||||
definition,
|
definition,
|
||||||
runnable.label,
|
runnable.label,
|
||||||
args,
|
|
||||||
config.problemMatcher,
|
config.problemMatcher,
|
||||||
config.cargoRunner,
|
config.cargoRunner,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
cargoTask.presentationOptions.clear = true;
|
task.presentationOptions.clear = true;
|
||||||
// Sadly, this doesn't prevent focus stealing if the terminal is currently
|
// Sadly, this doesn't prevent focus stealing if the terminal is currently
|
||||||
// hidden, and will become revealed due to task execution.
|
// hidden, and will become revealed due to task execution.
|
||||||
cargoTask.presentationOptions.focus = false;
|
task.presentationOptions.focus = false;
|
||||||
|
|
||||||
return cargoTask;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createArgs(runnable: ra.Runnable): string[] {
|
export function createArgs(runnable: ra.Runnable): string[] {
|
||||||
|
|
|
@ -2,22 +2,20 @@ import * as vscode from "vscode";
|
||||||
import * as toolchain from "./toolchain";
|
import * as toolchain from "./toolchain";
|
||||||
import type { Config } from "./config";
|
import type { Config } from "./config";
|
||||||
import { log } from "./util";
|
import { log } from "./util";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
|
|
||||||
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
|
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
|
||||||
// our configuration should be compatible with it so use the same key.
|
// our configuration should be compatible with it so use the same key.
|
||||||
export const TASK_TYPE = "cargo";
|
export const TASK_TYPE = "cargo";
|
||||||
export const TASK_SOURCE = "rust";
|
export const TASK_SOURCE = "rust";
|
||||||
|
|
||||||
export interface CargoTaskDefinition extends vscode.TaskDefinition {
|
export interface RustTargetDefinition extends vscode.TaskDefinition {
|
||||||
command?: string;
|
program: string;
|
||||||
args?: string[];
|
args: string[];
|
||||||
cwd?: string;
|
cwd?: string;
|
||||||
env?: { [key: string]: string };
|
env?: { [key: string]: string };
|
||||||
overrideCargo?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class CargoTaskProvider implements vscode.TaskProvider {
|
class RustTaskProvider implements vscode.TaskProvider {
|
||||||
private readonly config: Config;
|
private readonly config: Config;
|
||||||
|
|
||||||
constructor(config: Config) {
|
constructor(config: Config) {
|
||||||
|
@ -39,14 +37,15 @@ class CargoTaskProvider implements vscode.TaskProvider {
|
||||||
{ command: "run", group: undefined },
|
{ command: "run", group: undefined },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const cargoPath = await toolchain.cargoPath();
|
||||||
|
|
||||||
const tasks: vscode.Task[] = [];
|
const tasks: vscode.Task[] = [];
|
||||||
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
|
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
|
||||||
for (const def of defs) {
|
for (const def of defs) {
|
||||||
const vscodeTask = await buildCargoTask(
|
const vscodeTask = await buildRustTask(
|
||||||
workspaceTarget,
|
workspaceTarget,
|
||||||
{ type: TASK_TYPE, command: def.command },
|
{ type: TASK_TYPE, program: cargoPath, args: [def.command] },
|
||||||
`cargo ${def.command}`,
|
`cargo ${def.command}`,
|
||||||
[def.command],
|
|
||||||
this.config.problemMatcher,
|
this.config.problemMatcher,
|
||||||
this.config.cargoRunner,
|
this.config.cargoRunner,
|
||||||
);
|
);
|
||||||
|
@ -63,15 +62,13 @@ class CargoTaskProvider implements vscode.TaskProvider {
|
||||||
// we need to inform VSCode how to execute that command by creating
|
// we need to inform VSCode how to execute that command by creating
|
||||||
// a ShellExecution for it.
|
// a ShellExecution for it.
|
||||||
|
|
||||||
const definition = task.definition as CargoTaskDefinition;
|
const definition = task.definition as RustTargetDefinition;
|
||||||
|
|
||||||
if (definition.type === TASK_TYPE && definition.command) {
|
if (definition.type === TASK_TYPE) {
|
||||||
const args = [definition.command].concat(definition.args ?? []);
|
return await buildRustTask(
|
||||||
return await buildCargoTask(
|
|
||||||
task.scope,
|
task.scope,
|
||||||
definition,
|
definition,
|
||||||
task.name,
|
task.name,
|
||||||
args,
|
|
||||||
this.config.problemMatcher,
|
this.config.problemMatcher,
|
||||||
this.config.cargoRunner,
|
this.config.cargoRunner,
|
||||||
);
|
);
|
||||||
|
@ -81,11 +78,10 @@ class CargoTaskProvider implements vscode.TaskProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function buildCargoTask(
|
export async function buildRustTask(
|
||||||
scope: vscode.WorkspaceFolder | vscode.TaskScope | undefined,
|
scope: vscode.WorkspaceFolder | vscode.TaskScope | undefined,
|
||||||
definition: CargoTaskDefinition,
|
definition: RustTargetDefinition,
|
||||||
name: string,
|
name: string,
|
||||||
args: string[],
|
|
||||||
problemMatcher: string[],
|
problemMatcher: string[],
|
||||||
customRunner?: string,
|
customRunner?: string,
|
||||||
throwOnError: boolean = false,
|
throwOnError: boolean = false,
|
||||||
|
@ -95,7 +91,12 @@ export async function buildCargoTask(
|
||||||
if (customRunner) {
|
if (customRunner) {
|
||||||
const runnerCommand = `${customRunner}.buildShellExecution`;
|
const runnerCommand = `${customRunner}.buildShellExecution`;
|
||||||
try {
|
try {
|
||||||
const runnerArgs = { kind: TASK_TYPE, args, cwd: definition.cwd, env: definition.env };
|
const runnerArgs = {
|
||||||
|
kind: TASK_TYPE,
|
||||||
|
args: definition.args,
|
||||||
|
cwd: definition.cwd,
|
||||||
|
env: definition.env,
|
||||||
|
};
|
||||||
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
||||||
if (customExec) {
|
if (customExec) {
|
||||||
if (customExec instanceof vscode.ShellExecution) {
|
if (customExec instanceof vscode.ShellExecution) {
|
||||||
|
@ -113,16 +114,7 @@ export async function buildCargoTask(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!exec) {
|
if (!exec) {
|
||||||
// Check whether we must use a user-defined substitute for cargo.
|
exec = new vscode.ProcessExecution(definition.program, definition.args, definition);
|
||||||
// Split on spaces to allow overrides like "wrapper cargo".
|
|
||||||
const overrideCargo = definition.overrideCargo ?? definition.overrideCargo;
|
|
||||||
const cargoPath = await toolchain.cargoPath();
|
|
||||||
const cargoCommand = overrideCargo?.split(" ") ?? [cargoPath];
|
|
||||||
|
|
||||||
const fullCommand = [...cargoCommand, ...args];
|
|
||||||
|
|
||||||
const processName = unwrapUndefinable(fullCommand[0]);
|
|
||||||
exec = new vscode.ProcessExecution(processName, fullCommand.slice(1), definition);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new vscode.Task(
|
return new vscode.Task(
|
||||||
|
@ -138,6 +130,6 @@ export async function buildCargoTask(
|
||||||
}
|
}
|
||||||
|
|
||||||
export function activateTaskProvider(config: Config): vscode.Disposable {
|
export function activateTaskProvider(config: Config): vscode.Disposable {
|
||||||
const provider = new CargoTaskProvider(config);
|
const provider = new RustTaskProvider(config);
|
||||||
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue