8995: fix: Create tasks for all workspaces r=matklad a=SomeoneToIgnore

Follow-up of https://github.com/rust-analyzer/rust-analyzer/pull/8955#discussion_r637897170

Before: 
<img width="593" alt="image" src="https://user-images.githubusercontent.com/2690773/119575267-712b5300-bdbf-11eb-833c-f688f7a7dd0f.png">

After: 
<img width="643" alt="image" src="https://user-images.githubusercontent.com/2690773/119575273-74264380-bdbf-11eb-8283-a78bbcb7346e.png">

This is the first time I've used multiple workspaces feature in VSCode, but so far looks like
* opening detached files works
* run and debug lens work
* Rust Analyzer: Run action works
* run task works and now shows tasks for all workspaces
* there are no platform-specific changes involved

Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
bors[bot] 2021-05-26 20:58:07 +00:00 committed by GitHub
commit b4015b6aaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 22 deletions

View File

@ -32,14 +32,9 @@ export function createClient(serverPath: string, workspace: Workspace, extraEnv:
const newEnv = Object.assign({}, process.env);
Object.assign(newEnv, extraEnv);
let cwd = undefined;
if (workspace.kind === "Workspace Folder") {
cwd = workspace.folder.fsPath;
};
const run: lc.Executable = {
command: serverPath,
options: { cwd, env: newEnv },
options: { env: newEnv },
};
const serverOptions: lc.ServerOptions = {
run,

View File

@ -10,7 +10,6 @@ import { ServerStatusParams } from './lsp_ext';
export type Workspace =
{
kind: 'Workspace Folder';
folder: vscode.Uri;
}
| {
kind: 'Detached Files';

View File

@ -45,8 +45,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
throw new Error(message);
});
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
if (workspaceFolder === undefined) {
if (vscode.workspace.workspaceFolders?.length === 0) {
const rustDocuments = vscode.workspace.textDocuments.filter(document => isRustDocument(document));
if (rustDocuments.length > 0) {
ctx = await Ctx.create(config, context, serverPath, { kind: 'Detached Files', files: rustDocuments });
@ -58,8 +57,8 @@ async function tryActivate(context: vscode.ExtensionContext) {
// registers its `onDidChangeDocument` handler before us.
//
// This a horribly, horribly wrong way to deal with this problem.
ctx = await Ctx.create(config, context, serverPath, { kind: "Workspace Folder", folder: workspaceFolder.uri });
ctx.pushCleanup(activateTaskProvider(workspaceFolder, ctx.config));
ctx = await Ctx.create(config, context, serverPath, { kind: "Workspace Folder" });
ctx.pushCleanup(activateTaskProvider(ctx.config));
}
await initCommonContext(context, ctx);

View File

@ -17,11 +17,9 @@ export interface CargoTaskDefinition extends vscode.TaskDefinition {
}
class CargoTaskProvider implements vscode.TaskProvider {
private readonly target: vscode.WorkspaceFolder;
private readonly config: Config;
constructor(target: vscode.WorkspaceFolder, config: Config) {
this.target = target;
constructor(config: Config) {
this.config = config;
}
@ -40,10 +38,12 @@ class CargoTaskProvider implements vscode.TaskProvider {
];
const tasks: vscode.Task[] = [];
for (const def of defs) {
const vscodeTask = await buildCargoTask(this.target, { type: TASK_TYPE, command: def.command }, `cargo ${def.command}`, [def.command], this.config.cargoRunner);
vscodeTask.group = def.group;
tasks.push(vscodeTask);
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
for (const def of defs) {
const vscodeTask = await buildCargoTask(workspaceTarget, { type: TASK_TYPE, command: def.command }, `cargo ${def.command}`, [def.command], this.config.cargoRunner);
vscodeTask.group = def.group;
tasks.push(vscodeTask);
}
}
return tasks;
@ -58,14 +58,19 @@ class CargoTaskProvider implements vscode.TaskProvider {
if (definition.type === TASK_TYPE && definition.command) {
const args = [definition.command].concat(definition.args ?? []);
return await buildCargoTask(this.target, definition, task.name, args, this.config.cargoRunner);
if (isWorkspaceFolder(task.scope)) {
return await buildCargoTask(task.scope, definition, task.name, args, this.config.cargoRunner);
}
}
return undefined;
}
}
function isWorkspaceFolder(scope?: any): scope is vscode.WorkspaceFolder {
return (scope as vscode.WorkspaceFolder).name !== undefined;
}
export async function buildCargoTask(
target: vscode.WorkspaceFolder,
definition: CargoTaskDefinition,
@ -119,7 +124,7 @@ export async function buildCargoTask(
);
}
export function activateTaskProvider(target: vscode.WorkspaceFolder, config: Config): vscode.Disposable {
const provider = new CargoTaskProvider(target, config);
export function activateTaskProvider(config: Config): vscode.Disposable {
const provider = new CargoTaskProvider(config);
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
}