From a5849a7dab578795d2f222c17d4acace2015c5a1 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 12 Dec 2024 17:02:11 +0100 Subject: [PATCH] refactor: eliminate generic InvalidArgument errors InvalidArgument is too generic and makes tracking the source of the error potentially difficult. --- src/tui/editor.zig | 24 ++++++++++++------------ src/tui/mainview.zig | 18 +++++++++--------- src/tui/mode/mini/file_browser.zig | 4 ++-- src/tui/mode/mini/find.zig | 4 ++-- src/tui/mode/mini/find_in_files.zig | 4 ++-- src/tui/mode/mini/goto.zig | 4 ++-- src/tui/mode/mini/move_to_char.zig | 8 ++++---- src/tui/mode/overlay/open_recent.zig | 4 ++-- src/tui/mode/overlay/palette.zig | 4 ++-- src/tui/tui.zig | 10 +++++----- 10 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 92ee1b5..589e68e 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1843,7 +1843,7 @@ pub const Editor = struct { var y: i32 = 0; var x: i32 = 0; if (!try ctx.args.match(.{ tp.extract(&y), tp.extract(&x) })) - return error.InvalidArgument; + return error.InvalidDragToArgument; return self.primary_drag(y, x); } pub const drag_to_meta = .{ .arguments = &.{ .integer, .integer } }; @@ -3114,7 +3114,7 @@ pub const Editor = struct { pub fn insert_chars(self: *Self, ctx: Context) Result { var chars: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&chars)})) - return error.InvalidArgument; + return error.InvalidInsertCharsArgument; const b = try self.buf_for_update(); var root = b.root; for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { @@ -3410,7 +3410,7 @@ pub const Editor = struct { if (ctx.args.match(.{tp.extract(&file_path)}) catch false) { try self.open(file_path); self.clamp(); - } else return error.InvalidArgument; + } else return error.InvalidOpenBufferFromFileArgument; } pub const open_buffer_from_file_meta = .{ .arguments = &.{.string} }; @@ -3420,7 +3420,7 @@ pub const Editor = struct { if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content) }) catch false) { try self.open_scratch(file_path, content); self.clamp(); - } else return error.InvalidArgument; + } else return error.InvalidOpenScratchBufferArgument; } pub const open_scratch_buffer_meta = .{ .arguments = &.{ .string, .string } }; @@ -3450,7 +3450,7 @@ pub const Editor = struct { var file_path: []const u8 = undefined; if (ctx.args.match(.{tp.extract(&file_path)}) catch false) { try self.save_as(file_path); - } else return error.InvalidArgument; + } else return error.InvalidSafeFileAsArgument; } pub const save_file_as_meta = .{ .arguments = &.{.string} }; @@ -3471,7 +3471,7 @@ pub const Editor = struct { if (ctx.args.match(.{tp.extract(&query)}) catch false) { try self.find_in_buffer(query); self.clamp(); - } else return error.InvalidArgument; + } else return error.InvalidFindQueryArgument; } pub const find_query_meta = .{ .arguments = &.{.string} }; @@ -3837,7 +3837,7 @@ pub const Editor = struct { try self.send_editor_jump_source(); var line: usize = 0; if (!try ctx.args.match(.{tp.extract(&line)})) - return error.InvalidArgument; + return error.InvalidGotoLineArgument; const root = self.buf_root() catch return; self.cancel_all_selections(); const primary = self.get_primary(); @@ -3850,7 +3850,7 @@ pub const Editor = struct { pub fn goto_column(self: *Self, ctx: Context) Result { var column: usize = 0; if (!try ctx.args.match(.{tp.extract(&column)})) - return error.InvalidArgument; + return error.InvalidGotoColumnArgument; const root = self.buf_root() catch return; const primary = self.get_primary(); try primary.cursor.move_to(root, primary.cursor.row, @intCast(if (column < 1) 0 else column - 1), self.metrics); @@ -3879,7 +3879,7 @@ pub const Editor = struct { })) { // self.logger.print("goto: l:{d} c:{d} {any}", .{ line, column, sel }); have_sel = true; - } else return error.InvalidArgument; + } else return error.InvalidGotoLineAndColumnArgument; self.cancel_all_selections(); const root = self.buf_root() catch return; const primary = self.get_primary(); @@ -4012,7 +4012,7 @@ pub const Editor = struct { pub fn select(self: *Self, ctx: Context) Result { var sel: Selection = .{}; if (!try ctx.args.match(.{ tp.extract(&sel.begin.row), tp.extract(&sel.begin.col), tp.extract(&sel.end.row), tp.extract(&sel.end.col) })) - return error.InvalidArgument; + return error.InvalidSelectArgument; self.get_primary().selection = sel; } pub const select_meta = .{ .arguments = &.{ .integer, .integer, .integer, .integer } }; @@ -4041,7 +4041,7 @@ pub const Editor = struct { pub fn filter(self: *Self, ctx: Context) Result { if (!try ctx.args.match(.{ tp.string, tp.more })) - return error.InvalidArgument; + return error.InvalidFilterArgument; try self.filter_cmd(ctx.args); } pub const filter_meta = .{ .arguments = &.{.string} }; @@ -4286,7 +4286,7 @@ pub const Editor = struct { pub fn set_file_type(self: *Self, ctx: Context) Result { var file_type: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&file_type)})) - return error.InvalidArgument; + return error.InvalidSetFileTypeArgument; if (self.syntax) |syn| syn.destroy(); self.syntax_last_rendered_root = null; diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index b6ffce6..a9e8b67 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -311,26 +311,26 @@ const cmds = struct { while (len > 0) : (len -= 1) { var field_name: []const u8 = undefined; if (!try cbor.matchString(&iter, &field_name)) - return error.InvalidArgument; + return error.InvalidNavigateArgumentFieldName; if (std.mem.eql(u8, field_name, "line")) { if (!try cbor.matchValue(&iter, cbor.extract(&line))) - return error.InvalidArgument; + return error.InvalidNavigateLineArgument; } else if (std.mem.eql(u8, field_name, "column")) { if (!try cbor.matchValue(&iter, cbor.extract(&column))) - return error.InvalidArgument; + return error.InvalidNavigateColumnArgument; } else if (std.mem.eql(u8, field_name, "file")) { if (!try cbor.matchValue(&iter, cbor.extract(&file))) - return error.InvalidArgument; + return error.InvalidNavigateFileArgument; } else if (std.mem.eql(u8, field_name, "goto")) { if (!try cbor.matchValue(&iter, cbor.extract_cbor(&goto_args))) - return error.InvalidArgument; + return error.InvalidNavigateGotoArgument; } else { try cbor.skipValue(&iter); } } } else |_| if (ctx.args.match(tp.extract(&file_name)) catch false) { file = file_name; - } else return error.InvalidArgument; + } else return error.InvalidNavigateArgument; if (tp.env.get().str("project").len == 0) { try open_project_cwd(self, .{}); @@ -510,7 +510,7 @@ const cmds = struct { tp.extract(&sel.begin.col), tp.extract(&sel.end.row), tp.extract(&sel.end.col), - })) return error.InvalidArgument; + })) return error.InvalidAddDiagnosticArgument; file_path = project_manager.normalize_file_path(file_path); if (self.get_active_editor()) |editor| if (std.mem.eql(u8, file_path, editor.file_path orelse "")) try editor.add_diagnostic(file_path, source, code, message, severity, sel) @@ -530,7 +530,7 @@ const cmds = struct { pub fn clear_diagnostics(self: *Self, ctx: Ctx) Result { var file_path: []const u8 = undefined; - if (!try ctx.args.match(.{tp.extract(&file_path)})) return error.InvalidArgument; + if (!try ctx.args.match(.{tp.extract(&file_path)})) return error.InvalidClearDiagnosticsArgument; file_path = project_manager.normalize_file_path(file_path); if (self.get_active_editor()) |editor| if (std.mem.eql(u8, file_path, editor.file_path orelse "")) editor.clear_diagnostics(); @@ -574,7 +574,7 @@ const cmds = struct { pub fn find_in_files_query(self: *Self, ctx: Ctx) Result { var query: []const u8 = undefined; - if (!try ctx.args.match(.{tp.extract(&query)})) return error.InvalidArgument; + if (!try ctx.args.match(.{tp.extract(&query)})) return error.InvalidFindInFilesQueryArgument; log.logger("find").print("finding files...", .{}); const find_f = ripgrep.find_in_files; if (std.mem.indexOfScalar(u8, query, '\n')) |_| return; diff --git a/src/tui/mode/mini/file_browser.zig b/src/tui/mode/mini/file_browser.zig index b91739e..ed79aaf 100644 --- a/src/tui/mode/mini/file_browser.zig +++ b/src/tui/mode/mini/file_browser.zig @@ -300,7 +300,7 @@ pub fn Create(options: type) type { pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result { var egc: u32 = 0; if (!try ctx.args.match(.{tp.extract(&egc)})) - return error.InvalidArgument; + return error.InvalidFileBrowserInsertCodePointArgument; self.complete_trigger_count = 0; var buf: [32]u8 = undefined; const bytes = try input.ucs32_to_utf8(&[_]u32{egc}, &buf); @@ -312,7 +312,7 @@ pub fn Create(options: type) type { pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result { var bytes: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&bytes)})) - return error.InvalidArgument; + return error.InvalidFileBrowserInsertBytesArgument; self.complete_trigger_count = 0; try self.file_path.appendSlice(bytes); self.update_mini_mode_text(); diff --git a/src/tui/mode/mini/find.zig b/src/tui/mode/mini/find.zig index 09fe83b..117f1b1 100644 --- a/src/tui/mode/mini/find.zig +++ b/src/tui/mode/mini/find.zig @@ -171,7 +171,7 @@ const cmds = struct { pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result { var egc: u32 = 0; if (!try ctx.args.match(.{tp.extract(&egc)})) - return error.InvalidArgument; + return error.InvalidFindInsertCodePointArgument; self.insert_code_point(egc) catch |e| return tp.exit_error(e, @errorReturnTrace()); self.update_mini_mode_text(); } @@ -180,7 +180,7 @@ const cmds = struct { pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result { var bytes: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&bytes)})) - return error.InvalidArgument; + return error.InvalidFindInsertBytesArgument; self.insert_bytes(bytes) catch |e| return tp.exit_error(e, @errorReturnTrace()); self.update_mini_mode_text(); } diff --git a/src/tui/mode/mini/find_in_files.zig b/src/tui/mode/mini/find_in_files.zig index 28e01a4..29bddf4 100644 --- a/src/tui/mode/mini/find_in_files.zig +++ b/src/tui/mode/mini/find_in_files.zig @@ -114,7 +114,7 @@ const cmds = struct { pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result { var egc: u32 = 0; if (!try ctx.args.match(.{tp.extract(&egc)})) - return error.InvalidArgument; + return error.InvalidFindInFilesInsertCodePointArgument; self.insert_code_point(egc) catch |e| return tp.exit_error(e, @errorReturnTrace()); self.update_mini_mode_text(); } @@ -123,7 +123,7 @@ const cmds = struct { pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result { var bytes: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&bytes)})) - return error.InvalidArgument; + return error.InvalidFindInFilesInsertBytesArgument; self.insert_bytes(bytes) catch |e| return tp.exit_error(e, @errorReturnTrace()); self.update_mini_mode_text(); } diff --git a/src/tui/mode/mini/goto.zig b/src/tui/mode/mini/goto.zig index 56257e1..4923876 100644 --- a/src/tui/mode/mini/goto.zig +++ b/src/tui/mode/mini/goto.zig @@ -111,7 +111,7 @@ const cmds = struct { pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result { var keypress: usize = 0; if (!try ctx.args.match(.{tp.extract(&keypress)})) - return error.InvalidArgument; + return error.InvalidGotoInsertCodePointArgument; switch (keypress) { '0'...'9' => self.insert_char(@intCast(keypress)), else => {}, @@ -124,7 +124,7 @@ const cmds = struct { pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result { var bytes: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&bytes)})) - return error.InvalidArgument; + return error.InvalidGotoInsertBytesArgument; self.insert_bytes(bytes); self.update_mini_mode_text(); self.goto(); diff --git a/src/tui/mode/mini/move_to_char.zig b/src/tui/mode/mini/move_to_char.zig index c725e07..ad90339 100644 --- a/src/tui/mode/mini/move_to_char.zig +++ b/src/tui/mode/mini/move_to_char.zig @@ -32,7 +32,7 @@ const Operation = enum { pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } { var right: bool = true; const select = if (tui.get_active_editor()) |editor| if (editor.get_primary().selection) |_| true else false else false; - _ = ctx.args.match(.{tp.extract(&right)}) catch return error.InvalidArgument; + _ = ctx.args.match(.{tp.extract(&right)}) catch return error.InvalidMoveToCharArgument; const self: *Self = try allocator.create(Self); self.* = .{ .allocator = allocator, @@ -92,9 +92,9 @@ const cmds = struct { pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result { var code_point: u32 = 0; if (!try ctx.args.match(.{tp.extract(&code_point)})) - return error.InvalidArgument; + return error.InvalidMoveToCharInsertCodePointArgument; var buf: [6]u8 = undefined; - const bytes = input.ucs32_to_utf8(&[_]u32{code_point}, &buf) catch return error.InvalidArgument; + const bytes = input.ucs32_to_utf8(&[_]u32{code_point}, &buf) catch return error.InvalidMoveToCharCodePoint; return self.execute_operation(command.fmt(.{buf[0..bytes]})); } pub const mini_mode_insert_code_point_meta = .{ .arguments = &.{.integer} }; @@ -102,7 +102,7 @@ const cmds = struct { pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result { var bytes: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&bytes)})) - return error.InvalidArgument; + return error.InvalidMoveToCharInsertBytesArgument; return self.execute_operation(ctx); } pub const mini_mode_insert_bytes_meta = .{ .arguments = &.{.string} }; diff --git a/src/tui/mode/overlay/open_recent.zig b/src/tui/mode/overlay/open_recent.zig index f0edf27..08817fb 100644 --- a/src/tui/mode/overlay/open_recent.zig +++ b/src/tui/mode/overlay/open_recent.zig @@ -309,7 +309,7 @@ const cmds = struct { pub fn overlay_insert_code_point(self: *Self, ctx: Ctx) Result { var egc: u32 = 0; if (!try ctx.args.match(.{tp.extract(&egc)})) - return error.InvalidArgument; + return error.InvalidOpenRecentInsertCodePointArgument; self.insert_code_point(egc) catch |e| return tp.exit_error(e, @errorReturnTrace()); } pub const overlay_insert_code_point_meta = .{ .arguments = &.{.integer} }; @@ -317,7 +317,7 @@ const cmds = struct { pub fn overlay_insert_bytes(self: *Self, ctx: Ctx) Result { var bytes: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&bytes)})) - return error.InvalidArgument; + return error.InvalidOpenRecentInsertBytesArgument; self.insert_bytes(bytes) catch |e| return tp.exit_error(e, @errorReturnTrace()); } pub const overlay_insert_bytes_meta = .{ .arguments = &.{.string} }; diff --git a/src/tui/mode/overlay/palette.zig b/src/tui/mode/overlay/palette.zig index 0934e0b..a9dd0d9 100644 --- a/src/tui/mode/overlay/palette.zig +++ b/src/tui/mode/overlay/palette.zig @@ -446,7 +446,7 @@ pub fn Create(options: type) type { pub fn overlay_insert_code_point(self: *Self, ctx: Ctx) Result { var egc: u32 = 0; if (!try ctx.args.match(.{tp.extract(&egc)})) - return error.InvalidArgument; + return error.InvalidPaletteInsertCodePointArgument; self.insert_code_point(egc) catch |e| return tp.exit_error(e, @errorReturnTrace()); } pub const overlay_insert_code_point_meta = .{ .arguments = &.{.integer} }; @@ -454,7 +454,7 @@ pub fn Create(options: type) type { pub fn overlay_insert_bytes(self: *Self, ctx: Ctx) Result { var bytes: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&bytes)})) - return error.InvalidArgument; + return error.InvalidPaletteInsertBytesArgument; self.insert_bytes(bytes) catch |e| return tp.exit_error(e, @errorReturnTrace()); } pub const overlay_insert_bytes_meta = .{ .arguments = &.{.string} }; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index a62c098..24e052f 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -642,7 +642,7 @@ const cmds = struct { pub fn set_theme(self: *Self, ctx: Ctx) Result { var name: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&name)})) - return tp.exit_error(error.InvalidArgument, null); + return tp.exit_error(error.InvalidSetThemeArgument, null); self.theme = get_theme_by_name(name) orelse { self.logger.print("theme not found: {s}", .{name}); return; @@ -713,7 +713,7 @@ const cmds = struct { pub fn enter_mode(self: *Self, ctx: Ctx) Result { var mode: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&mode)})) - return tp.exit_error(error.InvalidArgument, null); + return tp.exit_error(error.InvalidEnterModeArgument, null); var new_mode = self.get_input_mode(mode) catch ret: { self.logger.print("unknown mode {s}", .{mode}); @@ -835,11 +835,11 @@ const cmds = struct { var iter = ctx.args.buf; var len = try cbor.decodeArrayHeader(&iter); if (len < 1) - return tp.exit_error(error.InvalidArgument, null); + return tp.exit_error(error.InvalidRunAsyncArgument, null); var cmd: []const u8 = undefined; if (!try cbor.matchValue(&iter, cbor.extract(&cmd))) - return tp.exit_error(error.InvalidArgument, null); + return tp.exit_error(error.InvalidRunAsyncArgument, null); len -= 1; var args = std.ArrayList([]const u8).init(self.allocator); @@ -848,7 +848,7 @@ const cmds = struct { var arg: []const u8 = undefined; if (try cbor.matchValue(&iter, cbor.extract_cbor(&arg))) { try args.append(arg); - } else return tp.exit_error(error.InvalidArgument, null); + } else return tp.exit_error(error.InvalidRunAsyncArgument, null); } var args_cb = std.ArrayList(u8).init(self.allocator);