Skip to content

Commit

Permalink
Merge #3777 #3784
Browse files Browse the repository at this point in the history
3777: Add basic task support r=matklad a=Timmmm

This adds basic support for running `cargo build`, `cargo run`, etc.

Fixes #1935

I have tested this and it seems to work. There are two things I'm not sure about:

1. The workspace folder handling seems wrong - just get the first workspace folder? Is this just a TODO item? I don't know if it is right to lift `workspaceFolder` up to `activate()` but I couldn't see another way.
2. If you manually add an entry to `tasks.json` like this:

```
    {
      "type": "cargo",
      "command": "build",
      "problemMatcher": [
        "$rustc"
      ],
      "group": "build"
    }
```

then VSCode somehow magically knows to run `cargo build`. The documentation for `resolveTask` *sounds* like I should have to implement that for it to work:

```
 * Resolves a task that has no [`execution`](#Task.execution) set. Tasks are
 * often created from information found in the `tasks.json`-file. Such tasks miss
 * the information on how to execute them and a task provider must fill in
 * the missing information in the `resolveTask`-method.
```

But then it also says this:

```
* This method will not be
 * called for tasks returned from the above `provideTasks` method since those
 * tasks are always fully resolved. A valid default implementation for the
 * `resolveTask` method is to return `undefined`.
```

Either way, it works without implementing it so the only thing I can think is that it is doing some kind of crazy pattern matching of the tasks returned by `provideTasks()` and the ones found in `tasks.json`.

3784: Ignore createProgress request in tests r=matklad a=matklad

closes #3783



bors r+
🤖

Co-authored-by: Tim <[email protected]>
Co-authored-by: Aleksey Kladov <[email protected]>
  • Loading branch information
3 people authored Mar 31, 2020
3 parents 6f0d8db + 3eb45b9 + 6b2f02f commit 668980d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
14 changes: 7 additions & 7 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,14 +808,14 @@ fn send_startup_progress(sender: &Sender<Message>, loop_state: &mut LoopState) {
),
_ => {}
}
}

fn send_startup_progress_notif(sender: &Sender<Message>, work_done_progress: WorkDoneProgress) {
let notif = notification_new::<req::Progress>(req::ProgressParams {
token: req::ProgressToken::String("rustAnalyzer/startup".into()),
value: req::ProgressParamsValue::WorkDone(work_done_progress),
});
sender.send(notif.into()).unwrap();
fn send_startup_progress_notif(sender: &Sender<Message>, work_done_progress: WorkDoneProgress) {
let notif = notification_new::<req::Progress>(req::ProgressParams {
token: req::ProgressToken::String("rustAnalyzer/startup".into()),
value: req::ProgressParamsValue::WorkDone(work_done_progress),
});
sender.send(notif.into()).unwrap();
}
}

struct PoolDispatcher<'a> {
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/tests/heavy_tests/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl Server {
self.client.sender.send(r.into()).unwrap();
while let Some(msg) = self.recv() {
match msg {
Message::Request(req) if req.method == "window/workDoneProgress/create" => (),
Message::Request(req) => panic!("unexpected request: {:?}", req),
Message::Notification(_) => (),
Message::Response(res) => {
Expand Down
5 changes: 2 additions & 3 deletions editors/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ export function configToServerOptions(config: Config) {
};
}

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

const run: lc.Executable = {
command: serverPath,
options: { cwd: workspaceFolderPath },
options: { cwd },
};
const serverOptions: lc.ServerOptions = {
run,
Expand Down
9 changes: 7 additions & 2 deletions editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ export class Ctx {

}

static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
const client = await createClient(config, serverPath);
static async create(
config: Config,
extCtx: vscode.ExtensionContext,
serverPath: string,
cwd: string,
): Promise<Ctx> {
const client = await createClient(config, serverPath, cwd);
const res = new Ctx(config, extCtx, client, serverPath);
res.pushCleanup(client.start());
await client.onReady();
Expand Down
12 changes: 11 additions & 1 deletion editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { log, assert } from './util';
import { PersistentState } from './persistent_state';
import { fetchRelease, download } from './net';
import { spawnSync } from 'child_process';
import { activateTaskProvider } from './tasks';

let ctx: Ctx | undefined;

Expand Down Expand Up @@ -41,11 +42,18 @@ export async function activate(context: vscode.ExtensionContext) {
const state = new PersistentState(context.globalState);
const serverPath = await bootstrap(config, state);

const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
if (workspaceFolder === undefined) {
const err = "Cannot activate rust-analyzer when no folder is opened";
void vscode.window.showErrorMessage(err);
throw new Error(err);
}

// Note: we try to start the server before we activate type hints so that it
// registers its `onDidChangeDocument` handler before us.
//
// This a horribly, horribly wrong way to deal with this problem.
ctx = await Ctx.create(config, context, serverPath);
ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath);

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

Expand Down Expand Up @@ -85,6 +93,8 @@ export async function activate(context: vscode.ExtensionContext) {
ctx.registerCommand('applySourceChange', commands.applySourceChange);
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);

ctx.pushCleanup(activateTaskProvider(workspaceFolder));

activateStatusDisplay(ctx);

if (!ctx.config.highlightingSemanticTokens) {
Expand Down
52 changes: 52 additions & 0 deletions editors/code/src/tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as vscode from 'vscode';

// 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 function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
const provider: vscode.TaskProvider = {
// Detect Rust tasks. Currently we do not do any actual detection
// of tasks (e.g. aliases in .cargo/config) and just return a fixed
// set of tasks that always exist. These tasks cannot be removed in
// tasks.json - only tweaked.
provideTasks: () => getStandardCargoTasks(target),

// We don't need to implement this.
resolveTask: () => undefined,
};

return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
}

function getStandardCargoTasks(target: vscode.WorkspaceFolder): vscode.Task[] {
return [
{ command: 'build', group: vscode.TaskGroup.Build },
{ command: 'check', group: vscode.TaskGroup.Build },
{ command: 'test', group: vscode.TaskGroup.Test },
{ command: 'clean', group: vscode.TaskGroup.Clean },
{ command: 'run', group: undefined },
]
.map(({ command, group }) => {
const vscodeTask = new vscode.Task(
// The contents of this object end up in the tasks.json entries.
{
type: TASK_TYPE,
command,
},
// The scope of the task - workspace or specific folder (global
// is not supported).
target,
// The task name, and task source. These are shown in the UI as
// `${source}: ${name}`, e.g. `rust: cargo build`.
`cargo ${command}`,
'rust',
// What to do when this command is executed.
new vscode.ShellExecution('cargo', [command]),
// Problem matchers.
['$rustc'],
);
vscodeTask.group = group;
return vscodeTask;
});
}

0 comments on commit 668980d

Please sign in to comment.