Skip to content

Commit

Permalink
enable build-on-save when a 'check' step is found
Browse files Browse the repository at this point in the history
suggested in #1918

Note that this doesn't work in VS Code until the config option are synced again.
The `enable_build_on_save` can be explicitly set to `false` to disable this behavior.
  • Loading branch information
Techatrix committed Aug 31, 2024
1 parent 1439f19 commit cbe8088
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"default": true
},
"enable_build_on_save": {
"description": "Whether to enable build-on-save diagnostics",
"description": "Whether to enable build-on-save diagnostics. Will be automatically enabled if the `build.zig` has declared a 'check' step.",
"type": "boolean",
"default": false
"default": null
},
"build_on_save_args": {
"description": "Specify which arguments should be passed to Zig when running build-on-save.\n\nIf the `build.zig` has declared a 'check' step, it will be preferred over the default 'install' step.",
Expand Down
4 changes: 2 additions & 2 deletions src/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ enable_snippets: bool = true,
/// Whether to enable function argument placeholder completions
enable_argument_placeholders: bool = true,

/// Whether to enable build-on-save diagnostics
enable_build_on_save: bool = false,
/// Whether to enable build-on-save diagnostics. Will be automatically enabled if the `build.zig` has declared a 'check' step.
enable_build_on_save: ?bool = null,

/// Specify which arguments should be passed to Zig when running build-on-save.
///
Expand Down
6 changes: 3 additions & 3 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ pub fn updateConfiguration(
server.document_store.cimports.clearAndFree(server.document_store.allocator);

if (std.process.can_spawn and
server.config.enable_build_on_save and
server.config.enable_build_on_save != false and
server.client_capabilities.supports_publish_diagnostics)
{
try server.pushJob(.run_build_on_save);
Expand Down Expand Up @@ -1018,7 +1018,7 @@ pub fn updateConfiguration(
}
}

if (server.config.enable_build_on_save) {
if (server.config.enable_build_on_save orelse false) {
if (!std.process.can_spawn) {
log.info("'enable_build_on_save' is ignored because your OS can't spawn a child process", .{});
} else if (server.status == .initialized and server.config.zig_exe_path == null) {
Expand Down Expand Up @@ -1354,7 +1354,7 @@ fn saveDocumentHandler(server: *Server, arena: std.mem.Allocator, notification:
}

if (std.process.can_spawn and
server.config.enable_build_on_save and
server.config.enable_build_on_save != false and
server.client_capabilities.supports_publish_diagnostics)
{
try server.pushJob(.run_build_on_save);
Expand Down
4 changes: 4 additions & 0 deletions src/features/diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ pub fn generateBuildOnSaveDiagnostics(
}
}

if (!(server.config.enable_build_on_save orelse has_check_step)) {
return;
}

if (has_check_step) {
std.debug.assert(!has_explicit_steps);
try argv.append(arena, "check");
Expand Down
6 changes: 3 additions & 3 deletions src/tools/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
},
{
"name": "enable_build_on_save",
"description": "Whether to enable build-on-save diagnostics",
"type": "bool",
"default": false
"description": "Whether to enable build-on-save diagnostics. Will be automatically enabled if the `build.zig` has declared a 'check' step.",
"type": "?bool",
"default": null
},
{
"name": "build_on_save_args",
Expand Down
17 changes: 9 additions & 8 deletions src/tools/config_gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ const ConfigOption = struct {
default: std.json.Value,

fn getTypescriptType(self: ConfigOption) error{UnsupportedType}![]const u8 {
return if (std.mem.eql(u8, self.type, "[]const []const u8"))
std.debug.assert(self.type.len != 0);
const ty = self.type[@intFromBool(self.type[0] == '?')..];
return if (std.mem.eql(u8, ty, "[]const []const u8"))
"array"
else if (std.mem.eql(u8, self.type, "[]const u8"))
else if (std.mem.eql(u8, ty, "[]const u8"))
"string"
else if (std.mem.eql(u8, self.type, "?[]const u8"))
"string"
else if (std.mem.eql(u8, self.type, "bool"))
else if (std.mem.eql(u8, ty, "bool"))
"boolean"
else if (std.mem.eql(u8, self.type, "usize"))
else if (std.mem.eql(u8, ty, "usize"))
"integer"
else if (std.mem.eql(u8, self.type, "enum"))
else if (std.mem.eql(u8, ty, "enum"))
"string"
else
error.UnsupportedType;
Expand Down Expand Up @@ -276,7 +276,8 @@ fn generateVSCodeConfigFile(allocator: std.mem.Allocator, config: Config, path:
.description = option.description,
.@"enum" = option.@"enum",
.format = if (std.mem.indexOf(u8, option.name, "path") != null) "path" else null,
.default = default,
// "enable_build_on_save" need to be explicitly set to 'null' so that it doesn't default to 'false'
.default = default orelse .null,
});
}

Expand Down

0 comments on commit cbe8088

Please sign in to comment.