Skip to content

Commit

Permalink
feat(view): add api get_selected_index (hrsh7th#1986)
Browse files Browse the repository at this point in the history
  • Loading branch information
uga-rosa authored Jul 15, 2024
1 parent 7e348da commit e1757ae
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lua/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions lua/cmp/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions lua/cmp/view/custom_entries_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion lua/cmp/view/native_entries_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lua/cmp/view/wildmenu_entries_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e1757ae

Please sign in to comment.