|
| 1 | +import vscode from "vscode"; |
| 2 | + |
| 3 | +import { handleConfigOption } from "./zigUtil"; |
| 4 | + |
| 5 | +export function registerBuildOnSaveProvider(): vscode.Disposable { |
| 6 | + return new BuildOnSaveProvider(); |
| 7 | +} |
| 8 | + |
| 9 | +type BuildOnSaveProviderKind = "off" | "auto" | "extension" | "zls"; |
| 10 | + |
| 11 | +class BuildOnSaveProvider implements vscode.Disposable { |
| 12 | + disposables: vscode.Disposable[] = []; |
| 13 | + /** This may be replacable with `vscode.tasks.taskExecutions` */ |
| 14 | + tasks = new Map<string, vscode.TaskExecution | null>(); |
| 15 | + |
| 16 | + constructor() { |
| 17 | + for (const folder of vscode.workspace.workspaceFolders ?? []) { |
| 18 | + void this.addOrRestart(folder); |
| 19 | + } |
| 20 | + |
| 21 | + vscode.workspace.onDidChangeWorkspaceFolders(async (e) => { |
| 22 | + for (const folder of e.added) { |
| 23 | + await this.addOrRestart(folder); |
| 24 | + } |
| 25 | + for (const folder of e.removed) { |
| 26 | + this.stop(folder); |
| 27 | + } |
| 28 | + }, this.disposables); |
| 29 | + |
| 30 | + vscode.workspace.onDidChangeConfiguration(async (e) => { |
| 31 | + if (!e.affectsConfiguration("zig.buildOnSaveProvider")) return; |
| 32 | + |
| 33 | + for (const folder of vscode.workspace.workspaceFolders ?? []) { |
| 34 | + await this.addOrRestart(folder); |
| 35 | + } |
| 36 | + }, this.disposables); |
| 37 | + } |
| 38 | + |
| 39 | + dispose() { |
| 40 | + for (const disposable of this.disposables) { |
| 41 | + disposable.dispose(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + async addOrRestart(folder: vscode.WorkspaceFolder): Promise<void> { |
| 46 | + this.stop(folder); |
| 47 | + |
| 48 | + const zigConfig = vscode.workspace.getConfiguration("zig", folder); |
| 49 | + const buildOnSaveProvider = zigConfig.get<BuildOnSaveProviderKind>("buildOnSaveProvider", "auto"); |
| 50 | + const buildOnSaveArgs = zigConfig |
| 51 | + .get<string[]>("buildOnSaveArgs", []) |
| 52 | + .map((unresolved) => handleConfigOption(unresolved, folder)); |
| 53 | + |
| 54 | + if (buildOnSaveProvider !== "extension") return; |
| 55 | + |
| 56 | + if (buildOnSaveArgs.includes("--build-file")) { |
| 57 | + // The build file has been explicitly provided through a command line argument |
| 58 | + } else { |
| 59 | + const workspaceBuildZigUri = vscode.Uri.joinPath(folder.uri, "build.zig"); |
| 60 | + try { |
| 61 | + await vscode.workspace.fs.stat(workspaceBuildZigUri); |
| 62 | + } catch { |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const task = new vscode.Task( |
| 68 | + { |
| 69 | + type: "zig", |
| 70 | + }, |
| 71 | + folder, |
| 72 | + "Zig Watch", |
| 73 | + "zig", |
| 74 | + new vscode.ShellExecution("zig", ["build", "--watch", ...buildOnSaveArgs], {}), |
| 75 | + "zig", |
| 76 | + ); |
| 77 | + task.isBackground = true; |
| 78 | + task.presentationOptions.reveal = vscode.TaskRevealKind.Never; |
| 79 | + task.presentationOptions.close = true; |
| 80 | + const taskExecutor = await vscode.tasks.executeTask(task); |
| 81 | + this.stop(folder); // Try to stop again just in case a task got started while we were suspended |
| 82 | + this.tasks.set(folder.uri.toString(), taskExecutor); |
| 83 | + |
| 84 | + vscode.workspace.onDidChangeConfiguration(async (e) => { |
| 85 | + if (e.affectsConfiguration("zig.buildOnSaveProvider", folder)) { |
| 86 | + // We previously checked that the build on save provider is "extension" so now it has to be something different |
| 87 | + this.stop(folder); |
| 88 | + return; |
| 89 | + } |
| 90 | + if (e.affectsConfiguration("zig.buildOnSaveArgs", folder)) { |
| 91 | + await this.addOrRestart(folder); |
| 92 | + } |
| 93 | + }, this.disposables); |
| 94 | + } |
| 95 | + |
| 96 | + stop(folder: vscode.WorkspaceFolder): void { |
| 97 | + const oldTask = this.tasks.get(folder.uri.toString()); |
| 98 | + if (oldTask) oldTask.terminate(); |
| 99 | + this.tasks.delete(folder.uri.toString()); |
| 100 | + } |
| 101 | +} |
0 commit comments