apprt/gtk: support new config diagnostics API

This commit is contained in:
Mitchell Hashimoto
2024-10-17 08:13:35 -07:00
parent 70c175e2a6
commit 4e25840e08
3 changed files with 25 additions and 8 deletions

View File

@ -123,9 +123,13 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
errdefer config.deinit();
// If we had configuration errors, then log them.
if (!config._errors.empty()) {
for (config._errors.list.items) |err| {
log.warn("configuration error: {s}", .{err.message});
if (!config._diagnostics.empty()) {
var buf = std.ArrayList(u8).init(core_app.alloc);
defer buf.deinit();
for (config._diagnostics.items()) |diag| {
try diag.write(buf.writer());
log.warn("configuration error: {s}", .{buf.items});
buf.clearRetainingCapacity();
}
}
@ -815,7 +819,7 @@ fn syncConfigChanges(self: *App) !void {
/// there are new configuration errors and hide the window if the errors
/// are resolved.
fn updateConfigErrors(self: *App) !void {
if (!self.config._errors.empty()) {
if (!self.config._diagnostics.empty()) {
if (self.config_errors_window == null) {
try ConfigErrorsWindow.create(self);
assert(self.config_errors_window != null);

View File

@ -28,7 +28,7 @@ pub fn create(app: *App) !void {
}
pub fn update(self: *ConfigErrors) void {
if (self.app.config._errors.empty()) {
if (self.app.config._diagnostics.empty()) {
c.gtk_window_destroy(@ptrCast(self.window));
return;
}
@ -130,8 +130,21 @@ const PrimaryView = struct {
const buf = c.gtk_text_buffer_new(null);
errdefer c.g_object_unref(buf);
for (config._errors.list.items) |err| {
c.gtk_text_buffer_insert_at_cursor(buf, err.message, @intCast(err.message.len));
var msg_buf: [4096]u8 = undefined;
var fbs = std.io.fixedBufferStream(&msg_buf);
for (config._diagnostics.items()) |diag| {
fbs.reset();
diag.write(fbs.writer()) catch |err| {
log.warn(
"error writing diagnostic to buffer err={}",
.{err},
);
continue;
};
const msg = fbs.getWritten();
c.gtk_text_buffer_insert_at_cursor(buf, msg.ptr, @intCast(msg.len));
c.gtk_text_buffer_insert_at_cursor(buf, "\n", -1);
}

View File

@ -118,7 +118,7 @@ pub const DiagnosticList = struct {
return self.list.items.len == 0;
}
pub fn items(self: *DiagnosticList) []Diagnostic {
pub fn items(self: *const DiagnosticList) []const Diagnostic {
return self.list.items;
}
};