From dcddd373b564c486c8ef1338c09f16441453dd46 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 15 Jan 2025 14:23:30 +0100 Subject: [PATCH] feat(find_in_files): clear file list and log if no matches are found --- src/ripgrep.zig | 2 ++ src/tui/mainview.zig | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/ripgrep.zig b/src/ripgrep.zig index 54c3aba..3b86f2a 100644 --- a/src/ripgrep.zig +++ b/src/ripgrep.zig @@ -179,6 +179,8 @@ const Process = struct { var exit_code: i64 = undefined; if (try m.match(.{ tp.any, tp.any, "exited", 0 })) { self.logger.print("found {d} matches", .{self.match_count}); + } else if (try m.match(.{ tp.any, tp.any, "exited", 1 })) { + self.logger.print("no matches found", .{}); } else if (try m.match(.{ tp.any, tp.any, "error.FileNotFound", 1 })) { self.logger.print_err(ripgrep_binary, "'{s}' executable not found", .{ripgrep_binary}); } else if (try m.match(.{ tp.any, tp.any, tp.extract(&err_msg), tp.extract(&exit_code) })) { diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index e253008..7510578 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -48,7 +48,7 @@ panels: ?*WidgetList = null, last_match_text: ?[]const u8 = null, location_history: location_history, file_stack: std.ArrayList([]const u8), -find_in_files_done: bool = false, +find_in_files_state: enum { init, adding, done } = .done, file_list_type: FileListType = .find_in_files, panel_height: ?usize = null, @@ -121,10 +121,14 @@ pub fn receive(self: *Self, from_: tp.pid_ref, m: tp.message) error{Exit}!bool { try self.add_find_in_files_result(.find_in_files, path, begin_line, begin_pos, end_line, end_pos, lines, .Information); return true; } else if (try m.match(.{ "REF", "done" })) { - self.find_in_files_done = true; + self.find_in_files_state = .done; return true; } else if (try m.match(.{ "FIF", "done" })) { - self.find_in_files_done = true; + switch (self.find_in_files_state) { + .init => self.clear_find_in_files_results(self.file_list_type), + else => {}, + } + self.find_in_files_state = .done; return true; } else if (try m.match(.{ "hover", tp.extract(&path), tp.string, tp.extract(&lines), tp.extract(&begin_line), tp.extract(&begin_pos), tp.extract(&end_line), tp.extract(&end_pos) })) { try self.add_info_content(lines); @@ -607,6 +611,7 @@ const cmds = struct { if (std.mem.indexOfScalar(u8, query, '\n')) |_| return; var rg = try find_f(self.allocator, query, "FIF"); defer rg.deinit(); + self.find_in_files_state = .init; } pub const find_in_files_query_meta = .{ .arguments = &.{.string} }; @@ -916,9 +921,16 @@ fn add_find_in_files_result( if (!self.is_panel_view_showing(filelist_view)) _ = self.toggle_panel_view(filelist_view, false) catch |e| return tp.exit_error(e, @errorReturnTrace()); const fl = self.get_panel_view(filelist_view) orelse @panic("filelist_view missing"); - if (self.find_in_files_done or self.file_list_type != file_list_type) { + if (self.file_list_type != file_list_type) { self.clear_find_in_files_results(self.file_list_type); self.file_list_type = file_list_type; + } else switch (self.find_in_files_state) { + .init, .done => { + self.clear_find_in_files_results(self.file_list_type); + self.file_list_type = file_list_type; + self.find_in_files_state = .adding; + }, + .adding => {}, } fl.add_item(.{ .path = path, @@ -935,7 +947,7 @@ fn clear_find_in_files_results(self: *Self, file_list_type: FileListType) void { if (self.file_list_type != file_list_type) return; if (!self.is_panel_view_showing(filelist_view)) return; const fl = self.get_panel_view(filelist_view) orelse @panic("filelist_view missing"); - self.find_in_files_done = false; + self.find_in_files_state = .done; self.file_list_type = file_list_type; fl.reset(); }