diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 4af99ac..411d80f 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -6552,15 +6552,7 @@ pub const Editor = struct { } pub fn run_triggers(self: *Self, cursel: *const CurSel, char: u8, event: TriggerEvent) void { - var mode = tui.config().completion_trigger; - - if (mode == .every_keystroke) { - const update_completion = "update_completion"; - const in_completion = command.get_id(update_completion) != null; - mode = if (in_completion) .automatic else .every_keystroke; - } - - switch (mode) { + switch (tui.config().completion_trigger) { .manual => return, .every_keystroke => return self.run_triggers_every_keystroke(cursel, char, event), .automatic => {}, @@ -6609,14 +6601,11 @@ pub const Editor = struct { pub fn add_completion_done(self: *Self) anyerror!bool { self.completions.deinit(self.allocator); - self.completions = if (self.completions_request) |*request| request.* else .empty; - self.completions_request = .done; - - if (command.log_execute) { - var iter: []const u8 = self.completions.data.items; - var count: usize = 0; - while (iter.len > 0) : (count += 1) try cbor.skipValue(&iter); - self.logger.print("completions: {d}", .{count}); + self.completions = .empty; + if (self.completions_request) |*request| { + self.completions.deinit(self.allocator); + self.completions = request.*; + self.completions_request = .done; } var open_completions = self.completions.data.items.len > 0; diff --git a/src/tui/mode/overlay/completion_dropdown.zig b/src/tui/mode/overlay/completion_dropdown.zig index 5708532..a035dbb 100644 --- a/src/tui/mode/overlay/completion_dropdown.zig +++ b/src/tui/mode/overlay/completion_dropdown.zig @@ -44,6 +44,7 @@ var max_description: usize = 0; pub fn init(self: *Type) error{ Stop, OutOfMemory }!void { try self.value.commands.init(self); self.value.editor = tui.get_active_editor() orelse return error.Stop; + self.value.cursor = self.value.editor.get_primary().cursor; self.value.view = self.value.editor.view; } @@ -51,7 +52,6 @@ pub fn load_entries(self: *Type) !usize { max_description = 0; var max_label_len: usize = 0; - self.value.cursor = self.value.editor.get_primary().cursor; self.value.query = null; var iter: []const u8 = self.value.editor.completions.data.items; while (iter.len > 0) { @@ -102,8 +102,25 @@ pub fn handle_event(self: *Type, _: tp.pid_ref, m: tp.message) tp.result { try m.match(.{ "E", "close" })) { const cursor = self.value.editor.get_primary().cursor; - if (!maybe_cancel(self, cursor)) - maybe_update_query(self, cursor) catch |e| self.logger.err(module_name, e); + if (self.value.cursor.row != cursor.row or + self.value.cursor.col > cursor.col or + !self.value.view.eql(self.value.editor.view)) + { + tp.self_pid().send(.{ "cmd", "palette_menu_cancel" }) catch |e| self.logger.err(module_name, e); + } else { + const query = get_query_text_nostore(self, cursor, self.allocator) catch |e| switch (e) { + error.Stop => return, + else => |e_| { + self.logger.err(module_name, e_); + return; + }, + }; + defer self.allocator.free(query); + if (self.value.last_query) |last| { + if (!std.mem.eql(u8, query, last)) + update_query(self, cursor) catch |e| self.logger.err(module_name, e); + } else update_query(self, cursor) catch |e| self.logger.err(module_name, e); + } } } @@ -126,35 +143,13 @@ fn get_query_text(self: *Type, cursor: ed.Cursor, allocator: std.mem.Allocator) return query; } -fn maybe_cancel(self: *Type, cursor: Buffer.Cursor) bool { - if (self.value.cursor.row != cursor.row or - self.value.cursor.col > cursor.col or - !self.value.view.eql(self.value.editor.view)) - { - tp.self_pid().send(.{ "cmd", "palette_menu_cancel" }) catch |e| self.logger.err(module_name, e); - return true; - } - return false; -} - -fn maybe_update_query(self: *Type, cursor: Buffer.Cursor) error{OutOfMemory}!void { - const query = get_query_text_nostore(self, cursor, self.allocator) catch |e| switch (e) { - error.Stop => return, - else => |e_| return e_, - }; - defer self.allocator.free(query); - if (self.value.last_query) |last| { - if (!std.mem.eql(u8, query, last)) - try update_query_text(self, cursor); - } else try update_query_text(self, cursor); -} - -fn update_query_text(self: *Type, cursor: ed.Cursor) error{OutOfMemory}!void { +fn update_query(self: *Type, cursor: ed.Cursor) error{OutOfMemory}!void { const query = get_query_text(self, cursor, self.allocator) catch |e| switch (e) { error.Stop => return, else => |e_| return e_, }; - Type.update_query(self, query) catch return; + self.update_query(query) catch return; + tp.self_pid().send(.{ "cmd", "completion" }) catch |e| self.logger.err(module_name, e); return; } @@ -374,8 +369,7 @@ const cmds = struct { } clear_entries(self); - self.longest_hint = try load_entries(self); - try update_query_text(self, self.value.editor.get_primary().cursor); + _ = try load_entries(self); } pub const update_completion_meta: Meta = .{}; };