From d818fd0624205b34e14888358037fb6f5dc51234 Mon Sep 17 00:00:00 2001 From: NAKAI Tsuyoshi <82267684+uga-rosa@users.noreply.github.com> Date: Tue, 16 Jul 2024 10:33:54 +0900 Subject: [PATCH] fix(view): check in get_selected_index (#1993) --- lua/cmp/view/custom_entries_view.lua | 2 +- lua/cmp/view/native_entries_view.lua | 15 ++++++++------- lua/cmp/view/wildmenu_entries_view.lua | 5 +++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lua/cmp/view/custom_entries_view.lua b/lua/cmp/view/custom_entries_view.lua index a12d95fc5..500fe2bbd 100644 --- a/lua/cmp/view/custom_entries_view.lua +++ b/lua/cmp/view/custom_entries_view.lua @@ -307,7 +307,7 @@ custom_entries_view.info = function(self) end custom_entries_view.get_selected_index = function(self) - if self:visible() and self.active then + if self:visible() and self.entries_win:option('cursorline') then return vim.api.nvim_win_get_cursor(self.entries_win.win)[1] end end diff --git a/lua/cmp/view/native_entries_view.lua b/lua/cmp/view/native_entries_view.lua index caa986cee..e6f90d057 100644 --- a/lua/cmp/view/native_entries_view.lua +++ b/lua/cmp/view/native_entries_view.lua @@ -117,8 +117,11 @@ native_entries_view.preselect = function(self, index) end native_entries_view.get_selected_index = function(self) - if self:visible() and (vim.v.completed_item or {}).word then - return vim.fn.complete_info({ 'selected' }).selected + if self:visible() then + local idx = vim.fn.complete_info({ 'selected' }).selected + if idx > -1 then + return math.max(0, idx) + 1 + end end end @@ -169,11 +172,9 @@ native_entries_view.get_first_entry = function(self) end native_entries_view.get_selected_entry = function(self) - if self:visible() then - local idx = self:get_selected_index() - if idx > -1 then - return self.entries[math.max(0, idx) + 1] - end + local idx = self:get_selected_index() + if idx then + return self.entries[idx] end end diff --git a/lua/cmp/view/wildmenu_entries_view.lua b/lua/cmp/view/wildmenu_entries_view.lua index d0b1b977b..f93f2fd32 100644 --- a/lua/cmp/view/wildmenu_entries_view.lua +++ b/lua/cmp/view/wildmenu_entries_view.lua @@ -229,8 +229,9 @@ wildmenu_entries_view.get_first_entry = function(self) end wildmenu_entries_view.get_selected_entry = function(self) - if self:visible() and self.active then - return self.entries[self:get_selected_index()] + local idx = self:get_selected_index() + if idx then + return self.entries[idx] end end