Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit d6b0e54

Browse files
committed
Add custom cargo runners
1 parent b265eca commit d6b0e54

File tree

6 files changed

+73
-56
lines changed

6 files changed

+73
-56
lines changed

rust-analyzer/editors/code/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@
336336
"default": null,
337337
"description": "List of features to activate. Defaults to `rust-analyzer.cargo.features`."
338338
},
339+
"rust-analyzer.cargoRunner": {
340+
"type": [
341+
"null",
342+
"string"
343+
],
344+
"default": null,
345+
"description": "Custom cargo runner extension ID."
346+
},
339347
"rust-analyzer.inlayHints.enable": {
340348
"type": "boolean",
341349
"default": true,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export function run(ctx: Ctx): Cmd {
394394

395395
item.detail = 'rerun';
396396
prevRunnable = item;
397-
const task = createTask(item.runnable);
397+
const task = await createTask(item.runnable, ctx.config);
398398
return await vscode.tasks.executeTask(task);
399399
};
400400
}
@@ -404,7 +404,7 @@ export function runSingle(ctx: Ctx): Cmd {
404404
const editor = ctx.activeRustEditor;
405405
if (!editor) return;
406406

407-
const task = createTask(runnable);
407+
const task = await createTask(runnable, ctx.config);
408408
task.group = vscode.TaskGroup.Build;
409409
task.presentationOptions = {
410410
reveal: vscode.TaskRevealKind.Always,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export class Config {
110110
};
111111
}
112112

113+
get cargoRunner() {
114+
return this.get<string | undefined>("cargoRunner");
115+
}
116+
113117
get debug() {
114118
// "/rustc/<id>" used by suggestions only.
115119
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export async function activate(context: vscode.ExtensionContext) {
115115
ctx.registerCommand('applyActionGroup', commands.applyActionGroup);
116116
ctx.registerCommand('gotoLocation', commands.gotoLocation);
117117

118-
ctx.pushCleanup(activateTaskProvider(workspaceFolder));
118+
ctx.pushCleanup(activateTaskProvider(workspaceFolder, ctx.config));
119119

120120
activateStatusDisplay(ctx);
121121

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

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
33
import * as ra from './lsp_ext';
4-
import * as toolchain from "./toolchain";
4+
import * as tasks from './tasks';
55

66
import { Ctx } from './ctx';
77
import { makeDebugConfig } from './debug';
8+
import { Config } from './config';
89

910
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
1011

@@ -95,52 +96,28 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
9596
}
9697
}
9798

98-
interface CargoTaskDefinition extends vscode.TaskDefinition {
99-
type: 'cargo';
100-
label: string;
101-
command: string;
102-
args: string[];
103-
env?: { [key: string]: string };
104-
}
105-
106-
export function createTask(runnable: ra.Runnable): vscode.Task {
107-
const TASK_SOURCE = 'Rust';
99+
export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> {
100+
if (runnable.kind !== "cargo") {
101+
// rust-analyzer supports only one kind, "cargo"
102+
// do not use tasks.TASK_TYPE here, these are completely different meanings.
108103

109-
let command;
110-
switch (runnable.kind) {
111-
case "cargo": command = toolchain.getPathForExecutable("cargo");
104+
throw `Unexpected runnable kind: ${runnable.kind}`;
112105
}
106+
113107
const args = [...runnable.args.cargoArgs]; // should be a copy!
114108
if (runnable.args.executableArgs.length > 0) {
115109
args.push('--', ...runnable.args.executableArgs);
116110
}
117-
const definition: CargoTaskDefinition = {
118-
type: 'cargo',
119-
label: runnable.label,
120-
command,
121-
args,
111+
const definition: tasks.CargoTaskDefinition = {
112+
type: tasks.TASK_TYPE,
113+
command: args[0], // run, test, etc...
114+
args: args.slice(1),
115+
cwd: runnable.args.workspaceRoot,
122116
env: Object.assign({}, process.env as { [key: string]: string }, { "RUST_BACKTRACE": "short" }),
123117
};
124118

125-
const execOption: vscode.ShellExecutionOptions = {
126-
cwd: runnable.args.workspaceRoot || '.',
127-
env: definition.env,
128-
};
129-
const exec = new vscode.ShellExecution(
130-
definition.command,
131-
definition.args,
132-
execOption,
133-
);
134-
135-
const f = vscode.workspace.workspaceFolders![0];
136-
const t = new vscode.Task(
137-
definition,
138-
f,
139-
definition.label,
140-
TASK_SOURCE,
141-
exec,
142-
['$rustc'],
143-
);
144-
t.presentationOptions.clear = true;
145-
return t;
119+
const cargoTask = await tasks.buildCargoTask(definition, runnable.label, args, config.cargoRunner);
120+
cargoTask.presentationOptions.clear = true;
121+
122+
return cargoTask;
146123
}

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

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as vscode from 'vscode';
22
import * as toolchain from "./toolchain";
3+
import { Config } from './config';
4+
import { log } from './util';
35

46
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
57
// our configuration should be compatible with it so use the same key.
6-
const TASK_TYPE = 'cargo';
8+
export const TASK_TYPE = 'cargo';
9+
export const TASK_SOURCE = 'rust';
710

8-
interface CargoTaskDefinition extends vscode.TaskDefinition {
11+
export interface CargoTaskDefinition extends vscode.TaskDefinition {
912
command?: string;
1013
args?: string[];
1114
cwd?: string;
@@ -14,9 +17,11 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
1417

1518
class CargoTaskProvider implements vscode.TaskProvider {
1619
private readonly target: vscode.WorkspaceFolder;
20+
private readonly config: Config;
1721

18-
constructor(target: vscode.WorkspaceFolder) {
22+
constructor(target: vscode.WorkspaceFolder, config: Config) {
1923
this.target = target;
24+
this.config = config;
2025
}
2126

2227
provideTasks(): vscode.Task[] {
@@ -58,29 +63,52 @@ class CargoTaskProvider implements vscode.TaskProvider {
5863
});
5964
}
6065

61-
resolveTask(task: vscode.Task): vscode.Task | undefined {
66+
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
6267
// VSCode calls this for every cargo task in the user's tasks.json,
6368
// we need to inform VSCode how to execute that command by creating
6469
// a ShellExecution for it.
6570

6671
const definition = task.definition as CargoTaskDefinition;
6772

68-
if (definition.type === 'cargo' && definition.command) {
73+
if (definition.type === TASK_TYPE && definition.command) {
6974
const args = [definition.command].concat(definition.args ?? []);
7075

71-
return new vscode.Task(
72-
definition,
73-
task.name,
74-
'rust',
75-
new vscode.ShellExecution('cargo', args, definition),
76-
);
76+
return await buildCargoTask(definition, task.name, args, this.config.cargoRunner);
7777
}
7878

7979
return undefined;
8080
}
8181
}
8282

83-
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
84-
const provider = new CargoTaskProvider(target);
83+
export async function buildCargoTask(definition: CargoTaskDefinition, name: string, args: string[], customRunner?: string): Promise<vscode.Task> {
84+
if (customRunner) {
85+
const runnerCommand = `${customRunner}.createCargoTask`;
86+
try {
87+
const runnerArgs = { name, args, cwd: definition.cwd, env: definition.env, source: TASK_SOURCE };
88+
const task = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
89+
90+
if (task instanceof vscode.Task) {
91+
return task;
92+
} else if (task) {
93+
log.debug("Invalid cargo task", task);
94+
throw `Invalid task!`;
95+
}
96+
// fallback to default processing
97+
98+
} catch (e) {
99+
throw `Cargo runner '${customRunner}' failed! ${e}`;
100+
}
101+
}
102+
103+
return new vscode.Task(
104+
definition,
105+
name,
106+
TASK_SOURCE,
107+
new vscode.ShellExecution(toolchain.cargoPath(), args, definition),
108+
);
109+
}
110+
111+
export function activateTaskProvider(target: vscode.WorkspaceFolder, config: Config): vscode.Disposable {
112+
const provider = new CargoTaskProvider(target, config);
85113
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
86114
}

0 commit comments

Comments
 (0)