Skip to content

Commit bf4f653

Browse files
committed
Better zig build config
1 parent 67b5adc commit bf4f653

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ Then, you can use the `zls` executable in an editor of your choice that has a Zi
4545

4646
You can configure zls by providing a zls.json file.
4747
zls will look for a zls.json configuration file in multiple locations with the following priority:
48-
- In the folders open in your workspace (this applies for files in those folders)
4948
- In the local configuration folder of your OS (as provided by [known-folders](https://github.com/ziglibs/known-folders#folder-list))
5049
- In the same directory as the executable
5150

@@ -58,7 +57,7 @@ The following options are currently available.
5857
| `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. |
5958
| `warn_style` | `bool` | `false` | Enables warnings for style *guideline* mismatches |
6059
| `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` |
61-
| `enable_semantic_tokens` | `bool` | `false` | Enables semantic token support when the client also supports it. |
60+
| `enable_semantic_tokens` | `bool` | `true` | Enables semantic token support when the client also supports it. |
6261
| `operator_completions` | `bool` | `true` | Enables `*` and `?` operators in completion lists. |
6362

6463
## Features

build.zig

+39-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,46 @@ pub fn config(step: *std.build.Step) anyerror!void {
1010
@setEvalBranchQuota(2500);
1111
std.debug.warn("Welcome to the ZLS configuration wizard! (insert mage emoji here)\n", .{});
1212

13-
const lib_path = try zinput.askDirPath(builder.allocator, "What is your Zig lib path (path that contains the 'std' folder)?", 512);
13+
var zig_exe_path: ?[]const u8 = null;
14+
std.debug.print("Looking for 'zig' in PATH...\n", .{});
15+
find_zig: {
16+
const allocator = builder.allocator;
17+
const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
18+
error.EnvironmentVariableNotFound => {
19+
break :find_zig;
20+
},
21+
else => return err,
22+
};
23+
defer allocator.free(env_path);
24+
25+
const exe_extension = @as(std.zig.CrossTarget, .{}).exeFileExt();
26+
const zig_exe = try std.fmt.allocPrint(allocator, "zig{}", .{exe_extension});
27+
defer allocator.free(zig_exe);
28+
29+
var it = std.mem.tokenize(env_path, &[_]u8{std.fs.path.delimiter});
30+
while (it.next()) |path| {
31+
const full_path = try std.fs.path.join(allocator, &[_][]const u8{
32+
path,
33+
zig_exe,
34+
});
35+
defer allocator.free(full_path);
36+
37+
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
38+
zig_exe_path = try std.mem.dupe(allocator, u8, std.os.realpath(full_path, &buf) catch continue);
39+
break :find_zig;
40+
}
41+
}
42+
43+
if (zig_exe_path == null) {
44+
std.debug.print("Could not find 'zig' in PATH\n", .{});
45+
zig_exe_path = try zinput.askString(builder.allocator, "What is the path to the 'zig' executable you would like to use?", 512);
46+
} else {
47+
std.debug.print("Found zig executable '{}'\n", .{zig_exe_path.?});
48+
}
1449
const snippets = try zinput.askBool("Do you want to enable snippets?");
1550
const style = try zinput.askBool("Do you want to enable style warnings?");
1651
const semantic_tokens = try zinput.askBool("Do you want to enable semantic highlighting?");
52+
const operator_completions = try zinput.askBool("Do you want to enable .* and .? completions");
1753

1854
var dir = try std.fs.cwd().openDir(builder.exe_dir, .{});
1955
defer dir.close();
@@ -26,10 +62,11 @@ pub fn config(step: *std.build.Step) anyerror!void {
2662
std.debug.warn("Writing to config...\n", .{});
2763

2864
const content = std.json.stringify(.{
29-
.zig_lib_path = lib_path,
65+
.zig_exe_path = zig_exe_path,
3066
.enable_snippets = snippets,
3167
.warn_style = style,
3268
.enable_semantic_tokens = semantic_tokens,
69+
.operator_completions = operator_completions,
3370
}, std.json.StringifyOptions{}, out);
3471

3572
std.debug.warn("Successfully saved configuration options!\n", .{});

src/config.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ warn_style: bool = false,
1919
build_runner_path: ?[]const u8 = null,
2020

2121
/// Semantic token support
22-
enable_semantic_tokens: bool = false,
22+
enable_semantic_tokens: bool = true,
2323

2424
/// Whether to enable `*` and `?` operators in completion lists
2525
operator_completions: bool = true,

0 commit comments

Comments
 (0)