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

install Zig if the zig.path config option has been unset #400

Merged
merged 2 commits into from
Feb 22, 2025
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
29 changes: 7 additions & 22 deletions src/zigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,12 @@ interface ExeWithVersion {
version: semver.SemVer;
}

export class ZigProvider implements vscode.Disposable {
export class ZigProvider {
onChange: vscode.EventEmitter<ExeWithVersion | null> = new vscode.EventEmitter();
private value: ExeWithVersion | null;
private disposables: vscode.Disposable[];

constructor() {
this.value = this.resolveZigPathConfigOption();
this.disposables = [
vscode.workspace.onDidChangeConfiguration((change) => {
if (change.affectsConfiguration("zig.path")) {
const newValue = this.resolveZigPathConfigOption();
if (newValue) {
this.set(newValue);
}
}
}),
];
this.value = this.resolveZigPathConfigOption() ?? null;
}

/** Returns the version of the Zig executable that is currently being used. */
Expand All @@ -40,6 +29,8 @@ export class ZigProvider implements vscode.Disposable {

/** Set the path the Zig executable. The `zig.path` config option will be ignored */
public set(value: ExeWithVersion | null) {
if (value === null && this.value === null) return;
if (value !== null && this.value !== null && value.version.compare(this.value.version) === 0) return;
this.value = value;
this.onChange.fire(value);
}
Expand All @@ -61,22 +52,16 @@ export class ZigProvider implements vscode.Disposable {
this.set(newValue);
}

/** Resolves the `zig.path` configuration option */
private resolveZigPathConfigOption(zigPath?: string): ExeWithVersion | null {
/** Resolves the `zig.path` configuration option. */
public resolveZigPathConfigOption(zigPath?: string): ExeWithVersion | null | undefined {
zigPath ??= vscode.workspace.getConfiguration("zig").get<string>("path", "");
if (!zigPath) return null;
const exePath = zigPath !== "zig" ? zigPath : null; // the string "zig" means lookup in PATH
const result = resolveExePathAndVersion(exePath, "zig", "zig.path", "version");
if ("message" in result) {
void vscode.window.showErrorMessage(result.message);
return null;
return undefined;
}
return result;
}

dispose() {
for (const disposable of this.disposables) {
disposable.dispose();
}
}
}
10 changes: 8 additions & 2 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,17 +641,23 @@ export async function setupZig(context: vscode.ExtensionContext) {
onDidChangeActiveTextEditor(vscode.window.activeTextEditor);

context.subscriptions.push(
zigProvider,
statusItem,
languageStatusItem,
vscode.commands.registerCommand("zig.install", async () => {
await selectVersionAndInstall(context);
}),
vscode.workspace.onDidChangeConfiguration((change) => {
// The `zig.path` config option is handled by `zigProvider.onChange`.
if (change.affectsConfiguration("zig.version")) {
void refreshZigInstallation();
}
if (change.affectsConfiguration("zig.path")) {
const result = zigProvider.resolveZigPathConfigOption();
if (result === undefined) return; // error message already reported
if (result !== null) {
zigProvider.set(result);
}
void refreshZigInstallation();
}
}),
vscode.window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor),
zigProvider.onChange.event(() => {
Expand Down