mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Add custom cargo runners
This commit is contained in:
parent
c544f9a137
commit
a43a9103bc
6 changed files with 72 additions and 55 deletions
|
@ -1,11 +1,14 @@
|
|||
import * as vscode from 'vscode';
|
||||
import * as toolchain from "./toolchain";
|
||||
import { Config } from './config';
|
||||
import { log } from './util';
|
||||
|
||||
// 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.
|
||||
const TASK_TYPE = 'cargo';
|
||||
export const TASK_TYPE = 'cargo';
|
||||
export const TASK_SOURCE = 'rust';
|
||||
|
||||
interface CargoTaskDefinition extends vscode.TaskDefinition {
|
||||
export interface CargoTaskDefinition extends vscode.TaskDefinition {
|
||||
command?: string;
|
||||
args?: string[];
|
||||
cwd?: string;
|
||||
|
@ -14,9 +17,11 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
|
|||
|
||||
class CargoTaskProvider implements vscode.TaskProvider {
|
||||
private readonly target: vscode.WorkspaceFolder;
|
||||
private readonly config: Config;
|
||||
|
||||
constructor(target: vscode.WorkspaceFolder) {
|
||||
constructor(target: vscode.WorkspaceFolder, config: Config) {
|
||||
this.target = target;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
provideTasks(): vscode.Task[] {
|
||||
|
@ -58,29 +63,52 @@ class CargoTaskProvider implements vscode.TaskProvider {
|
|||
});
|
||||
}
|
||||
|
||||
resolveTask(task: vscode.Task): vscode.Task | undefined {
|
||||
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
|
||||
// VSCode calls this for every cargo task in the user's tasks.json,
|
||||
// we need to inform VSCode how to execute that command by creating
|
||||
// a ShellExecution for it.
|
||||
|
||||
const definition = task.definition as CargoTaskDefinition;
|
||||
|
||||
if (definition.type === 'cargo' && definition.command) {
|
||||
if (definition.type === TASK_TYPE && definition.command) {
|
||||
const args = [definition.command].concat(definition.args ?? []);
|
||||
|
||||
return new vscode.Task(
|
||||
definition,
|
||||
task.name,
|
||||
'rust',
|
||||
new vscode.ShellExecution('cargo', args, definition),
|
||||
);
|
||||
return await buildCargoTask(definition, task.name, args, this.config.cargoRunner);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
|
||||
const provider = new CargoTaskProvider(target);
|
||||
export async function buildCargoTask(definition: CargoTaskDefinition, name: string, args: string[], customRunner?: string): Promise<vscode.Task> {
|
||||
if (customRunner) {
|
||||
const runnerCommand = `${customRunner}.createCargoTask`;
|
||||
try {
|
||||
const runnerArgs = { name, args, cwd: definition.cwd, env: definition.env, source: TASK_SOURCE };
|
||||
const task = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
||||
|
||||
if (task instanceof vscode.Task) {
|
||||
return task;
|
||||
} else if (task) {
|
||||
log.debug("Invalid cargo task", task);
|
||||
throw `Invalid task!`;
|
||||
}
|
||||
// fallback to default processing
|
||||
|
||||
} catch (e) {
|
||||
throw `Cargo runner '${customRunner}' failed! ${e}`;
|
||||
}
|
||||
}
|
||||
|
||||
return new vscode.Task(
|
||||
definition,
|
||||
name,
|
||||
TASK_SOURCE,
|
||||
new vscode.ShellExecution(toolchain.cargoPath(), args, definition),
|
||||
);
|
||||
}
|
||||
|
||||
export function activateTaskProvider(target: vscode.WorkspaceFolder, config: Config): vscode.Disposable {
|
||||
const provider = new CargoTaskProvider(target, config);
|
||||
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue