Skip to content

Commit

Permalink
Merge pull request #400 from ziglang/techatrix/zig-redundant-set
Browse files Browse the repository at this point in the history
install Zig if the `zig.path` config option has been unset
  • Loading branch information
Vexu authored Feb 22, 2025
2 parents a096305 + bb9e901 commit 97ea927
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
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

0 comments on commit 97ea927

Please sign in to comment.