From e1757aea6145d14208f3035f6b56c99e36593799 Mon Sep 17 00:00:00 2001 From: NAKAI Tsuyoshi <82267684+uga-rosa@users.noreply.github.com> Date: Tue, 16 Jul 2024 01:29:25 +0900 Subject: [PATCH] feat(view): add api get_selected_index (#1986) --- lua/cmp/init.lua | 6 ++++++ lua/cmp/view.lua | 7 +++++++ lua/cmp/view/custom_entries_view.lua | 12 +++++++++--- lua/cmp/view/native_entries_view.lua | 8 +++++++- lua/cmp/view/wildmenu_entries_view.lua | 8 +++++++- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index e896217..6867679 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -88,6 +88,12 @@ cmp.visible = cmp.sync(function() return cmp.core.view:visible() or vim.fn.pumvisible() == 1 end) +---Get what number candidates are currently selected. +---If not selected, nil is returned. +cmp.get_selected_index = cmp.sync(function() + return cmp.core.view:get_selected_index() +end) + ---Get current selected entry or nil cmp.get_selected_entry = cmp.sync(function() return cmp.core.view:get_selected_entry() diff --git a/lua/cmp/view.lua b/lua/cmp/view.lua index aa184c2..aef688c 100644 --- a/lua/cmp/view.lua +++ b/lua/cmp/view.lua @@ -211,6 +211,13 @@ view.scroll_docs = function(self, delta) self.docs_view:scroll(delta) end +---Get what number candidates are currently selected. +---If not selected, nil is returned. +---@return integer|nil +view.get_selected_index = function(self) + return self:_get_entries_view():get_selected_index() +end + ---Select prev menu item. ---@param option cmp.SelectOption view.select_next_item = function(self, option) diff --git a/lua/cmp/view/custom_entries_view.lua b/lua/cmp/view/custom_entries_view.lua index e889416..a12d95f 100644 --- a/lua/cmp/view/custom_entries_view.lua +++ b/lua/cmp/view/custom_entries_view.lua @@ -306,9 +306,15 @@ custom_entries_view.info = function(self) return self.entries_win:info() end +custom_entries_view.get_selected_index = function(self) + if self:visible() and self.active then + return vim.api.nvim_win_get_cursor(self.entries_win.win)[1] + end +end + custom_entries_view.select_next_item = function(self, option) if self:visible() then - local cursor = vim.api.nvim_win_get_cursor(self.entries_win.win)[1] + local cursor = self:get_selected_index() local is_top_down = self:is_direction_top_down() local last = #self.entries @@ -345,7 +351,7 @@ end custom_entries_view.select_prev_item = function(self, option) if self:visible() then - local cursor = vim.api.nvim_win_get_cursor(self.entries_win.win)[1] + local cursor = self:get_selected_index() local is_top_down = self:is_direction_top_down() local last = #self.entries @@ -402,7 +408,7 @@ end custom_entries_view.get_selected_entry = function(self) if self:visible() and self.entries_win:option('cursorline') then - return self.entries[vim.api.nvim_win_get_cursor(self.entries_win.win)[1]] + return self.entries[self:get_selected_index()] end end diff --git a/lua/cmp/view/native_entries_view.lua b/lua/cmp/view/native_entries_view.lua index 35ad4f2..caa986c 100644 --- a/lua/cmp/view/native_entries_view.lua +++ b/lua/cmp/view/native_entries_view.lua @@ -116,6 +116,12 @@ native_entries_view.preselect = function(self, index) end 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 + end +end + native_entries_view.select_next_item = function(self, option) local callback = function() self.event:emit('change') @@ -164,7 +170,7 @@ end native_entries_view.get_selected_entry = function(self) if self:visible() then - local idx = vim.fn.complete_info({ 'selected' }).selected + local idx = self:get_selected_index() if idx > -1 then return self.entries[math.max(0, idx) + 1] end diff --git a/lua/cmp/view/wildmenu_entries_view.lua b/lua/cmp/view/wildmenu_entries_view.lua index e5608d6..d0b1b97 100644 --- a/lua/cmp/view/wildmenu_entries_view.lua +++ b/lua/cmp/view/wildmenu_entries_view.lua @@ -179,6 +179,12 @@ wildmenu_entries_view.info = function(self) return self.entries_win:info() end +wildmenu_entries_view.get_selected_index = function(self) + if self:visible() and self.active then + return self.selected_index + end +end + wildmenu_entries_view.select_next_item = function(self, option) if self:visible() then local cursor @@ -224,7 +230,7 @@ end wildmenu_entries_view.get_selected_entry = function(self) if self:visible() and self.active then - return self.entries[self.selected_index] + return self.entries[self:get_selected_index()] end end