From bf4f653bf985dba3e3849ba133848c37721d0445 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Mon, 16 Nov 2020 00:07:35 +0200 Subject: [PATCH] Better `zig build config` --- README.md | 3 +-- build.zig | 41 +++++++++++++++++++++++++++++++++++++++-- src/config.zig | 2 +- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6207d74..005b4dd 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ Then, you can use the `zls` executable in an editor of your choice that has a Zi You can configure zls by providing a zls.json file. zls will look for a zls.json configuration file in multiple locations with the following priority: -- In the folders open in your workspace (this applies for files in those folders) - In the local configuration folder of your OS (as provided by [known-folders](https://github.com/ziglibs/known-folders#folder-list)) - In the same directory as the executable @@ -58,7 +57,7 @@ The following options are currently available. | `zig_exe_path` | `?[]const u8` | `null` | zig executable path, e.g. `/path/to/zig/zig`, used to run the custom build runner. If `null`, zig is looked up in `PATH`. Will be used to infer the zig standard library path if none is provided. | | `warn_style` | `bool` | `false` | Enables warnings for style *guideline* mismatches | | `build_runner_path` | `?[]const u8` | `null` | Path to the build_runner.zig file provided by zls. This option must be present in one of the global configuration files to have any effect. `null` is equivalent to `${executable_directory}/build_runner.zig` | -| `enable_semantic_tokens` | `bool` | `false` | Enables semantic token support when the client also supports it. | +| `enable_semantic_tokens` | `bool` | `true` | Enables semantic token support when the client also supports it. | | `operator_completions` | `bool` | `true` | Enables `*` and `?` operators in completion lists. | ## Features diff --git a/build.zig b/build.zig index f078fb7..fd928df 100644 --- a/build.zig +++ b/build.zig @@ -10,10 +10,46 @@ pub fn config(step: *std.build.Step) anyerror!void { @setEvalBranchQuota(2500); std.debug.warn("Welcome to the ZLS configuration wizard! (insert mage emoji here)\n", .{}); - const lib_path = try zinput.askDirPath(builder.allocator, "What is your Zig lib path (path that contains the 'std' folder)?", 512); + var zig_exe_path: ?[]const u8 = null; + std.debug.print("Looking for 'zig' in PATH...\n", .{}); + find_zig: { + const allocator = builder.allocator; + const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) { + error.EnvironmentVariableNotFound => { + break :find_zig; + }, + else => return err, + }; + defer allocator.free(env_path); + + const exe_extension = @as(std.zig.CrossTarget, .{}).exeFileExt(); + const zig_exe = try std.fmt.allocPrint(allocator, "zig{}", .{exe_extension}); + defer allocator.free(zig_exe); + + var it = std.mem.tokenize(env_path, &[_]u8{std.fs.path.delimiter}); + while (it.next()) |path| { + const full_path = try std.fs.path.join(allocator, &[_][]const u8{ + path, + zig_exe, + }); + defer allocator.free(full_path); + + var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + zig_exe_path = try std.mem.dupe(allocator, u8, std.os.realpath(full_path, &buf) catch continue); + break :find_zig; + } + } + + if (zig_exe_path == null) { + std.debug.print("Could not find 'zig' in PATH\n", .{}); + zig_exe_path = try zinput.askString(builder.allocator, "What is the path to the 'zig' executable you would like to use?", 512); + } else { + std.debug.print("Found zig executable '{}'\n", .{zig_exe_path.?}); + } const snippets = try zinput.askBool("Do you want to enable snippets?"); const style = try zinput.askBool("Do you want to enable style warnings?"); const semantic_tokens = try zinput.askBool("Do you want to enable semantic highlighting?"); + const operator_completions = try zinput.askBool("Do you want to enable .* and .? completions"); var dir = try std.fs.cwd().openDir(builder.exe_dir, .{}); defer dir.close(); @@ -26,10 +62,11 @@ pub fn config(step: *std.build.Step) anyerror!void { std.debug.warn("Writing to config...\n", .{}); const content = std.json.stringify(.{ - .zig_lib_path = lib_path, + .zig_exe_path = zig_exe_path, .enable_snippets = snippets, .warn_style = style, .enable_semantic_tokens = semantic_tokens, + .operator_completions = operator_completions, }, std.json.StringifyOptions{}, out); std.debug.warn("Successfully saved configuration options!\n", .{}); diff --git a/src/config.zig b/src/config.zig index be85f99..bd61523 100644 --- a/src/config.zig +++ b/src/config.zig @@ -19,7 +19,7 @@ warn_style: bool = false, build_runner_path: ?[]const u8 = null, /// Semantic token support -enable_semantic_tokens: bool = false, +enable_semantic_tokens: bool = true, /// Whether to enable `*` and `?` operators in completion lists operator_completions: bool = true,