Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update tasks sidebar on config change notification #964

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,14 @@ export async function activate(
treeDataProvider.refresh();
}
}));
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument((event) => {
if (event.uri.fsPath.match(/\/deno\.jsonc?$/)) {
treeDataProvider.refresh();
}
}));
context.subscriptions.push(
extensionContext.client!.onNotification(
"deno/didChangeDenoConfiguration",
() => {
treeDataProvider.refresh();
},
),
);
context.subscriptions.push(vscode.workspace.onDidRenameFiles((event) => {
if (
event.files.some(({ oldUri: uri }) => uri.fsPath.match(/\/deno\.jsonc?$/))
Expand Down
1 change: 1 addition & 0 deletions client/src/lsp_extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const registryState = new NotificationType<RegistryStateParams>(
export interface TaskRequestResponse {
name: string;
detail: string;
sourceUri: string;
}

/** Requests any tasks from the language server that the language server is
Expand Down
88 changes: 34 additions & 54 deletions client/src/tasks_sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from "path";
import * as fs from "fs";
import {
commands,
EventEmitter,
Expand Down Expand Up @@ -50,20 +49,14 @@ class DenoJSON extends TreeItem {

constructor(
public readonly folder: Folder,
relativePath: string,
fileName: string,
sourceUri: Uri,
) {
const label = relativePath.length > 0
? path.join(relativePath, fileName)
: fileName;
const label = folder.resourceUri
? sourceUri.toString().replace(folder.resourceUri.toString() + "/", "")
: sourceUri.toString();
super(label, TreeItemCollapsibleState.Expanded);

this.contextValue = "denoJSON";

this.resourceUri = Uri.file(
path.join(folder.resourceUri!.fsPath, relativePath, fileName),
);

this.resourceUri = sourceUri;
this.iconPath = ThemeIcon.File;
}

Expand All @@ -85,7 +78,7 @@ class DenoTask extends TreeItem {
workspace.getConfiguration("deno").get<DefaultCommand>(
"defaultTaskCommand",
) ??
"open";
"open";

const commandList = {
"open": {
Expand Down Expand Up @@ -120,13 +113,12 @@ function buildDenoConfigTask(
scope: WorkspaceFolder,
process: string,
name: string,
fileName: string,
command?: string,
command: string,
sourceUri: Uri,
): Task {
const execution = new ProcessExecution(process, ["task", name]);

const task = new Task(
{ type: "deno", name, command, fileName },
{ type: "deno", name, command, sourceUri },
scope,
name,
"deno task",
Expand Down Expand Up @@ -164,28 +156,28 @@ class DenoTaskProvider implements TaskProvider {
if (client && supportsConfigTasks) {
try {
const configTasks = await client.sendRequest(taskReq);
if (configTasks) {
for (const workspaceFolder of workspace.workspaceFolders ?? []) {
// Check if config file is .json or .jsonc
let fileName = "deno.json";
try {
const filePath = path.join(workspaceFolder.uri.fsPath, fileName);
fs.accessSync(filePath, fs.constants.F_OK);
} catch {
fileName = "deno.jsonc";
}
for (const { name, detail: command } of configTasks) {
tasks.push(
buildDenoConfigTask(
workspaceFolder,
process,
name,
fileName,
command,
),
);
}
for (const { name, detail: command, sourceUri } of configTasks ?? []) {
// TODO(nayeemrmn): Deno LSP versions < 1.37.2 doesn't provide the
// `sourceUri`. Remove this eventually.
if (!sourceUri) {
continue;
}
const workspaceFolder = (workspace.workspaceFolders ?? []).find((
f,
) => sourceUri.startsWith(f.uri.toString()));
if (!workspaceFolder) {
continue;
}

tasks.push(
buildDenoConfigTask(
workspaceFolder,
process,
name,
command,
Uri.parse(sourceUri),
),
);
}
} catch (err) {
window.showErrorMessage("Failed to retrieve config tasks.");
Expand Down Expand Up @@ -341,25 +333,13 @@ export class DenoTasksTreeDataProvider implements TreeDataProvider<TreeItem> {
}

const definition = task.definition;
const relativePath = definition.path ? definition.path : "";
const fullPath = path.join(task.scope.name, relativePath);
const sourceUri = definition.sourceUri;

let denoJson = configs.get(fullPath);
let denoJson = configs.get(sourceUri.toString());
if (!denoJson) {
let fileName = "deno.json";
try {
const filePath = path.join(
task.scope.uri.fsPath,
relativePath,
fileName,
);
fs.accessSync(filePath, fs.constants.F_OK);
} catch {
fileName = "deno.jsonc";
}
denoJson = new DenoJSON(folder, relativePath, fileName);
denoJson = new DenoJSON(folder, sourceUri);
folder.addConfig(denoJson);
configs.set(fullPath, denoJson);
configs.set(sourceUri.toString(), denoJson);
}
denoJson.addTask(new DenoTask(denoJson, task));
}
Expand Down