feat(view): add api get_selected_index (#1986)
This commit is contained in:
@@ -88,6 +88,12 @@ cmp.visible = cmp.sync(function()
|
|||||||
return cmp.core.view:visible() or vim.fn.pumvisible() == 1
|
return cmp.core.view:visible() or vim.fn.pumvisible() == 1
|
||||||
end)
|
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
|
---Get current selected entry or nil
|
||||||
cmp.get_selected_entry = cmp.sync(function()
|
cmp.get_selected_entry = cmp.sync(function()
|
||||||
return cmp.core.view:get_selected_entry()
|
return cmp.core.view:get_selected_entry()
|
||||||
|
|||||||
@@ -211,6 +211,13 @@ view.scroll_docs = function(self, delta)
|
|||||||
self.docs_view:scroll(delta)
|
self.docs_view:scroll(delta)
|
||||||
end
|
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.
|
---Select prev menu item.
|
||||||
---@param option cmp.SelectOption
|
---@param option cmp.SelectOption
|
||||||
view.select_next_item = function(self, option)
|
view.select_next_item = function(self, option)
|
||||||
|
|||||||
@@ -306,9 +306,15 @@ custom_entries_view.info = function(self)
|
|||||||
return self.entries_win:info()
|
return self.entries_win:info()
|
||||||
end
|
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)
|
custom_entries_view.select_next_item = function(self, option)
|
||||||
if self:visible() then
|
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 is_top_down = self:is_direction_top_down()
|
||||||
local last = #self.entries
|
local last = #self.entries
|
||||||
|
|
||||||
@@ -345,7 +351,7 @@ end
|
|||||||
|
|
||||||
custom_entries_view.select_prev_item = function(self, option)
|
custom_entries_view.select_prev_item = function(self, option)
|
||||||
if self:visible() then
|
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 is_top_down = self:is_direction_top_down()
|
||||||
local last = #self.entries
|
local last = #self.entries
|
||||||
|
|
||||||
@@ -402,7 +408,7 @@ end
|
|||||||
|
|
||||||
custom_entries_view.get_selected_entry = function(self)
|
custom_entries_view.get_selected_entry = function(self)
|
||||||
if self:visible() and self.entries_win:option('cursorline') then
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -116,6 +116,12 @@ native_entries_view.preselect = function(self, index)
|
|||||||
end
|
end
|
||||||
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)
|
native_entries_view.select_next_item = function(self, option)
|
||||||
local callback = function()
|
local callback = function()
|
||||||
self.event:emit('change')
|
self.event:emit('change')
|
||||||
@@ -164,7 +170,7 @@ end
|
|||||||
|
|
||||||
native_entries_view.get_selected_entry = function(self)
|
native_entries_view.get_selected_entry = function(self)
|
||||||
if self:visible() then
|
if self:visible() then
|
||||||
local idx = vim.fn.complete_info({ 'selected' }).selected
|
local idx = self:get_selected_index()
|
||||||
if idx > -1 then
|
if idx > -1 then
|
||||||
return self.entries[math.max(0, idx) + 1]
|
return self.entries[math.max(0, idx) + 1]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -179,6 +179,12 @@ wildmenu_entries_view.info = function(self)
|
|||||||
return self.entries_win:info()
|
return self.entries_win:info()
|
||||||
end
|
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)
|
wildmenu_entries_view.select_next_item = function(self, option)
|
||||||
if self:visible() then
|
if self:visible() then
|
||||||
local cursor
|
local cursor
|
||||||
@@ -224,7 +230,7 @@ end
|
|||||||
|
|
||||||
wildmenu_entries_view.get_selected_entry = function(self)
|
wildmenu_entries_view.get_selected_entry = function(self)
|
||||||
if self:visible() and self.active then
|
if self:visible() and self.active then
|
||||||
return self.entries[self.selected_index]
|
return self.entries[self:get_selected_index()]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user