Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(view): add api get_selected_index #1986

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading