Skip to content

Commit 768aa42

Browse files
TimmmmTim Hutt
authored andcommitted
Add basic task support
This adds basic support for running `cargo build`, `cargo run`, etc.
1 parent 671926a commit 768aa42

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

editors/code/src/client.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ export function configToServerOptions(config: Config) {
3030
};
3131
}
3232

33-
export async function createClient(config: Config, serverPath: string): Promise<lc.LanguageClient> {
33+
export async function createClient(config: Config, serverPath: string, workspaceFolder: vscode.WorkspaceFolder | null): Promise<lc.LanguageClient> {
3434
// '.' Is the fallback if no folder is open
3535
// TODO?: Workspace folders support Uri's (eg: file://test.txt).
3636
// It might be a good idea to test if the uri points to a file.
37-
const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.';
3837

3938
const run: lc.Executable = {
4039
command: serverPath,
41-
options: { cwd: workspaceFolderPath },
40+
options: { cwd: workspaceFolder?.uri.fsPath ?? '.' },
4241
};
4342
const serverOptions: lc.ServerOptions = {
4443
run,

editors/code/src/ctx.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ export class Ctx {
1515

1616
}
1717

18-
static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
19-
const client = await createClient(config, serverPath);
18+
static async create(
19+
config: Config,
20+
extCtx: vscode.ExtensionContext,
21+
serverPath: string,
22+
workspaceFolder: vscode.WorkspaceFolder | null,
23+
): Promise<Ctx> {
24+
const client = await createClient(config, serverPath, workspaceFolder);
2025
const res = new Ctx(config, extCtx, client, serverPath);
2126
res.pushCleanup(client.start());
2227
await client.onReady();

editors/code/src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { log, assert } from './util';
1313
import { PersistentState } from './persistent_state';
1414
import { fetchRelease, download } from './net';
1515
import { spawnSync } from 'child_process';
16+
import { activateTaskProvider } from './tasks';
1617

1718
let ctx: Ctx | undefined;
1819

@@ -41,11 +42,13 @@ export async function activate(context: vscode.ExtensionContext) {
4142
const state = new PersistentState(context.globalState);
4243
const serverPath = await bootstrap(config, state);
4344

45+
const workspaceFolder = vscode.workspace.workspaceFolders?.[0] ?? null;
46+
4447
// Note: we try to start the server before we activate type hints so that it
4548
// registers its `onDidChangeDocument` handler before us.
4649
//
4750
// This a horribly, horribly wrong way to deal with this problem.
48-
ctx = await Ctx.create(config, context, serverPath);
51+
ctx = await Ctx.create(config, context, serverPath, workspaceFolder);
4952

5053
// Commands which invokes manually via command palette, shortcut, etc.
5154

@@ -85,6 +88,10 @@ export async function activate(context: vscode.ExtensionContext) {
8588
ctx.registerCommand('applySourceChange', commands.applySourceChange);
8689
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
8790

91+
if (workspaceFolder !== null) {
92+
ctx.pushCleanup(activateTaskProvider(workspaceFolder));
93+
}
94+
8895
activateStatusDisplay(ctx);
8996

9097
if (!ctx.config.highlightingSemanticTokens) {

editors/code/src/tasks.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
Disposable,
3+
ShellExecution,
4+
Task,
5+
TaskGroup,
6+
TaskProvider,
7+
tasks,
8+
WorkspaceFolder,
9+
} from 'vscode';
10+
11+
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
12+
// our configuration should be compatible with it so use the same key.
13+
const TASK_TYPE = 'cargo';
14+
15+
export function activateTaskProvider(target: WorkspaceFolder): Disposable {
16+
const provider: TaskProvider = {
17+
// Detect Rust tasks. Currently we do not do any actual detection
18+
// of tasks (e.g. aliases in .cargo/config) and just return a fixed
19+
// set of tasks that always exist. These tasks cannot be removed in
20+
// tasks.json - only tweaked.
21+
provideTasks: () => getStandardCargoTasks(target),
22+
23+
// We don't need to implement this.
24+
resolveTask: () => undefined,
25+
};
26+
27+
return tasks.registerTaskProvider(TASK_TYPE, provider);
28+
}
29+
30+
function getStandardCargoTasks(target: WorkspaceFolder): Task[] {
31+
return [
32+
{ command: 'build', group: TaskGroup.Build },
33+
{ command: 'check', group: TaskGroup.Build },
34+
{ command: 'test', group: TaskGroup.Test },
35+
{ command: 'clean', group: TaskGroup.Clean },
36+
{ command: 'run', group: undefined },
37+
]
38+
.map(({ command, group }) => {
39+
const vscodeTask = new Task(
40+
// The contents of this object end up in the tasks.json entries.
41+
{
42+
type: TASK_TYPE,
43+
command,
44+
},
45+
// The scope of the task - workspace or specific folder (global
46+
// is not supported).
47+
target,
48+
// The task name, and task source. These are shown in the UI as
49+
// `${source}: ${name}`, e.g. `rust: cargo build`.
50+
`cargo ${command}`,
51+
'rust',
52+
// What to do when this command is executed.
53+
new ShellExecution('cargo', [command]),
54+
// Problem matchers.
55+
['$rustc'],
56+
);
57+
vscodeTask.group = group;
58+
return vscodeTask;
59+
});
60+
}

0 commit comments

Comments
 (0)