fix: cancel completion on update in automatic completion mode

If completion is automatically triggered (the default), then it also makes
sense to automatically cancel completions in situations where a completion
update would be required and we are *not* expected to automatically
re-trigger.
This commit is contained in:
CJ van den Berg 2026-02-04 16:43:43 +01:00
parent 4a97bd8774
commit e26d86eca3
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 17 additions and 0 deletions

View file

@ -4995,6 +4995,15 @@ pub const Editor = struct {
_ = self.pop_tabstop();
}
pub fn get_last_trigger_char(self: *Self) ?u8 {
const root = self.buf_root() catch return null;
var cursor = self.get_primary().cursor;
cursor.move_left(root, self.metrics) catch return null;
const egc, _, _ = cursor.egc_at(root, self.metrics) catch return null;
const char = if (egc.len == 1) egc[0] else return null;
return if (self.is_event_trigger(char, .insert)) char else null;
}
fn is_completion_boundary_left(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics, triggers: []const TriggerSymbol) bool {
if (cursor.col == 0) return true;
var next = cursor.*;
@ -6576,6 +6585,11 @@ pub const Editor = struct {
};
}
pub fn is_event_trigger(self: *Self, char: u8, event: TriggerEvent) bool {
for (self.get_event_triggers(event).items) |item| if (item.char == char) return true;
return false;
}
fn clear_event_triggers(self: *Self) void {
self.insert_triggers.deinit(self.allocator);
self.delete_triggers.deinit(self.allocator);

View file

@ -381,6 +381,9 @@ const cmds = struct {
const Result = command.Result;
pub fn update_completion(self: *Type, _: Ctx) Result {
if (tui.config().completion_trigger == .automatic and self.value.editor.get_last_trigger_char() == null)
return tp.self_pid().send(.{ "cmd", "palette_menu_cancel" }) catch |e| self.logger.err(module_name, e);
clear_entries(self);
self.longest_hint = try load_entries(self);
try update_query_text(self, self.value.editor.get_primary().cursor);