fix: check for formatter executable before trying to start formatter job

closes #359
This commit is contained in:
CJ van den Berg 2025-11-04 21:32:18 +01:00
parent 9a13191e10
commit 0f7a4f25e4
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 24 additions and 2 deletions

View file

@ -635,6 +635,7 @@ pub fn build_exe(
.{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") }, .{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") },
.{ .name = "zeit", .module = zeit_mod }, .{ .name = "zeit", .module = zeit_mod },
.{ .name = "VcsStatus", .module = VcsStatus_mod }, .{ .name = "VcsStatus", .module = VcsStatus_mod },
.{ .name = "bin_path", .module = bin_path_mod },
}, },
}); });

View file

@ -2,6 +2,7 @@ const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const tp = @import("thespian"); const tp = @import("thespian");
const cbor = @import("cbor"); const cbor = @import("cbor");
const bin_path = @import("bin_path");
const log = @import("log"); const log = @import("log");
const Buffer = @import("Buffer"); const Buffer = @import("Buffer");
const ripgrep = @import("ripgrep"); const ripgrep = @import("ripgrep");
@ -383,6 +384,8 @@ pub const Editor = struct {
completion_col: usize = 0, completion_col: usize = 0,
completion_is_complete: bool = true, completion_is_complete: bool = true,
checked_formatter: bool = false,
formatter: ?[]const []const u8 = null,
enable_format_on_save: bool, enable_format_on_save: bool,
restored_state: bool = false, restored_state: bool = false,
@ -661,6 +664,8 @@ pub const Editor = struct {
else else
file_type_config.guess_file_type(self.file_path, content.written()); file_type_config.guess_file_type(self.file_path, content.written());
}; };
self.checked_formatter = false;
self.formatter = null;
self.maybe_enable_auto_save(); self.maybe_enable_auto_save();
@ -5850,10 +5855,24 @@ pub const Editor = struct {
pub const select_meta: Meta = .{ .arguments = &.{ .integer, .integer, .integer, .integer } }; pub const select_meta: Meta = .{ .arguments = &.{ .integer, .integer, .integer, .integer } };
fn get_formatter(self: *Self) ?[]const []const u8 { fn get_formatter(self: *Self) ?[]const []const u8 {
if (self.file_type) |file_type| if (file_type.formatter) |fmtr| if (fmtr.len > 0) return fmtr; if (self.checked_formatter) return self.formatter;
self.checked_formatter = true;
if (self.file_type) |file_type| if (file_type.formatter) |fmtr| if (fmtr.len > 0) if (can_execute(self.allocator, fmtr[0])) {
self.formatter = fmtr;
return fmtr;
} else {
self.logger.print_err("format", "formatter executable '{s}' not found", .{fmtr[0]});
};
self.formatter = null;
return null; return null;
} }
fn can_execute(allocator: std.mem.Allocator, binary_name: []const u8) bool {
const resolved_binary_path = bin_path.find_binary_in_path(allocator, binary_name) catch return false;
defer if (resolved_binary_path) |path| allocator.free(path);
return resolved_binary_path != null;
}
pub fn format(self: *Self, ctx: Context) Result { pub fn format(self: *Self, ctx: Context) Result {
if (ctx.args.buf.len > 0 and try ctx.args.match(.{ tp.string, tp.more })) { if (ctx.args.buf.len > 0 and try ctx.args.match(.{ tp.string, tp.more })) {
try self.filter_cmd(ctx.args); try self.filter_cmd(ctx.args);
@ -5867,7 +5886,7 @@ pub const Editor = struct {
try self.filter_cmd(.{ .buf = args.written() }); try self.filter_cmd(.{ .buf = args.written() });
return; return;
} }
return tp.exit("no formatter"); self.logger.print("no formatter", .{});
} }
pub const format_meta: Meta = .{ .description = "Language: Format file or selection" }; pub const format_meta: Meta = .{ .description = "Language: Format file or selection" };
@ -6136,6 +6155,8 @@ pub const Editor = struct {
const file_type_config_ = try file_type_config.get(file_type); const file_type_config_ = try file_type_config.get(file_type);
self.file_type = file_type_config_; self.file_type = file_type_config_;
self.checked_formatter = false;
self.formatter = null;
self.syntax = blk: { self.syntax = blk: {
break :blk if (self.file_type) |ft| break :blk if (self.file_type) |ft|