Create tasks for all workspaces

This commit is contained in:
Kirill Bulatov 2021-05-26 01:11:52 +03:00
parent 5587d0a3e3
commit a05163db14
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); const newEnv = Object.assign({}, process.env);
Object.assign(newEnv, extraEnv); Object.assign(newEnv, extraEnv);
let cwd = undefined;
if (workspace.kind === "Workspace Folder") {
cwd = workspace.folder.fsPath;
};
const run: lc.Executable = { const run: lc.Executable = {
command: serverPath, command: serverPath,
options: { cwd, env: newEnv }, options: { env: newEnv },
}; };
const serverOptions: lc.ServerOptions = { const serverOptions: lc.ServerOptions = {
run, run,

View file

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

View file

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

View file

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