mirror of
https://github.com/hrsh7th/nvim-cmp
synced 2024-09-16 20:54:03 +02:00
feat(view): add api get_selected_index (#1986)
This commit is contained in:
parent
7e348da6e5
commit
e1757aea61
5 changed files with 36 additions and 5 deletions
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue