feat: select_next_item and select_prev_item support "count" option. deafults to 1 (#1262)
User can increase count value to select item with pagedown/pageup.
- cmp.select_prev_item({ behavior = behavior, count = 8 })
- cmp.select_next_item({ behavior = behavior, count = 8 })
This commit is contained in:
12
doc/cmp.txt
12
doc/cmp.txt
@@ -169,11 +169,11 @@ NOTE: `<Cmd>lua require('cmp').complete()<CR>` can be used to call these functio
|
||||
*cmp.abort* ()
|
||||
Closes the completion menu and restore the current line to the state before the current completion was started.
|
||||
|
||||
*cmp.select_next_item* (option: { behavior = cmp.SelectBehavior })
|
||||
Select the next item.
|
||||
*cmp.select_next_item* (option: { behavior = cmp.SelectBehavior, count = 1 })
|
||||
Select the next item. Set count with large number to select pagedown.
|
||||
|
||||
*cmp.select_prev_item* (option: { behavior = cmp.SelectBehavior })*
|
||||
Select the previous item.
|
||||
*cmp.select_prev_item* (option: { behavior = cmp.SelectBehavior, count = 1 })
|
||||
Select the previous item. Set count with large number to select pageup.
|
||||
|
||||
*cmp.scroll_docs* (delta: number)
|
||||
Scroll the documentation window if visible.
|
||||
@@ -292,10 +292,10 @@ There are also builtin mapping helper functions you can use:
|
||||
*cmp.mapping.abort* ()
|
||||
Same as |cmp.abort|.
|
||||
|
||||
*cmp.mapping.select_next_item* (option: { behavior = cmp.SelectBehavior })
|
||||
*cmp.mapping.select_next_item* (option: { behavior = cmp.SelectBehavior, count = 1 })
|
||||
Same as |cmp.select_next_item|.
|
||||
|
||||
*cmp.mapping.select_prev_item* (option: { behavior = cmp.SelectBehavior })
|
||||
*cmp.mapping.select_prev_item* (option: { behavior = cmp.SelectBehavior, count = 1 })
|
||||
Same as |cmp.select_prev_item|.
|
||||
|
||||
*cmp.mapping.scroll_docs* (delta: number)
|
||||
|
||||
@@ -131,6 +131,7 @@ end)
|
||||
cmp.select_next_item = cmp.sync(function(option)
|
||||
option = option or {}
|
||||
option.behavior = option.behavior or cmp.SelectBehavior.Insert
|
||||
option.count = option.count or 1
|
||||
|
||||
if cmp.core.view:visible() then
|
||||
local release = cmp.core:suspend()
|
||||
@@ -139,9 +140,9 @@ cmp.select_next_item = cmp.sync(function(option)
|
||||
return true
|
||||
elseif vim.fn.pumvisible() == 1 then
|
||||
if option.behavior == cmp.SelectBehavior.Insert then
|
||||
feedkeys.call(keymap.t('<C-n>'), 'in')
|
||||
feedkeys.call(keymap.t(string.format('%s<C-n>', option.count)), 'in')
|
||||
else
|
||||
feedkeys.call(keymap.t('<Down>'), 'in')
|
||||
feedkeys.call(keymap.t(string.format('%s<Down>', option.count)), 'in')
|
||||
end
|
||||
return true
|
||||
end
|
||||
@@ -152,6 +153,7 @@ end)
|
||||
cmp.select_prev_item = cmp.sync(function(option)
|
||||
option = option or {}
|
||||
option.behavior = option.behavior or cmp.SelectBehavior.Insert
|
||||
option.count = option.count or 1
|
||||
|
||||
if cmp.core.view:visible() then
|
||||
local release = cmp.core:suspend()
|
||||
@@ -160,9 +162,9 @@ cmp.select_prev_item = cmp.sync(function(option)
|
||||
return true
|
||||
elseif vim.fn.pumvisible() == 1 then
|
||||
if option.behavior == cmp.SelectBehavior.Insert then
|
||||
feedkeys.call(keymap.t('<C-p>'), 'in')
|
||||
feedkeys.call(keymap.t(string.format('%s<C-p>', option.count)), 'in')
|
||||
else
|
||||
feedkeys.call(keymap.t('<Up>'), 'in')
|
||||
feedkeys.call(keymap.t(string.format('%s<Up>', option.count)), 'in')
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -285,16 +285,33 @@ 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]
|
||||
if self:is_direction_top_down() then
|
||||
cursor = cursor + 1
|
||||
else
|
||||
cursor = cursor - 1
|
||||
end
|
||||
local is_top_down = self:is_direction_top_down()
|
||||
local last = #self.entries
|
||||
|
||||
if not self.entries_win:option('cursorline') then
|
||||
cursor = (self:is_direction_top_down() and 1) or #self.entries
|
||||
elseif #self.entries < cursor then
|
||||
cursor = (not self:is_direction_top_down() and #self.entries + 1) or 0
|
||||
cursor = (is_top_down and 1) or last
|
||||
else
|
||||
if is_top_down then
|
||||
if cursor == last then
|
||||
cursor = 0
|
||||
else
|
||||
cursor = cursor + option.count
|
||||
if last < cursor then
|
||||
cursor = last
|
||||
end
|
||||
end
|
||||
else
|
||||
if cursor == 0 then
|
||||
cursor = last
|
||||
else
|
||||
cursor = cursor - option.count
|
||||
if cursor < 0 then
|
||||
cursor = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self:_select(cursor, option)
|
||||
end
|
||||
end
|
||||
@@ -302,16 +319,33 @@ 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]
|
||||
if self:is_direction_top_down() then
|
||||
cursor = cursor - 1
|
||||
else
|
||||
cursor = cursor + 1
|
||||
end
|
||||
local is_top_down = self:is_direction_top_down()
|
||||
local last = #self.entries
|
||||
|
||||
if not self.entries_win:option('cursorline') then
|
||||
cursor = (self:is_direction_top_down() and #self.entries) or 1
|
||||
elseif #self.entries < cursor then
|
||||
cursor = (not self:is_direction_top_down() and 0) or #self.entries + 1
|
||||
cursor = (is_top_down and last) or 1
|
||||
else
|
||||
if is_top_down then
|
||||
if cursor == 1 then
|
||||
cursor = 0
|
||||
else
|
||||
cursor = cursor - option.count
|
||||
if cursor < 0 then
|
||||
cursor = 1
|
||||
end
|
||||
end
|
||||
else
|
||||
if cursor == last then
|
||||
cursor = 0
|
||||
else
|
||||
cursor = cursor + option.count
|
||||
if last < cursor then
|
||||
cursor = last
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self:_select(cursor, option)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -122,9 +122,9 @@ native_entries_view.select_next_item = function(self, option)
|
||||
end
|
||||
if self:visible() then
|
||||
if (option.behavior or types.cmp.SelectBehavior.Insert) == types.cmp.SelectBehavior.Insert then
|
||||
feedkeys.call(keymap.t('<C-n>'), 'n', callback)
|
||||
feedkeys.call(keymap.t(string.format('%s<C-n>', option.count), 'n', callback))
|
||||
else
|
||||
feedkeys.call(keymap.t('<Down>'), 'n', callback)
|
||||
feedkeys.call(keymap.t(string.format('%s<Down>', option.count)), 'n', callback)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -135,9 +135,9 @@ native_entries_view.select_prev_item = function(self, option)
|
||||
end
|
||||
if self:visible() then
|
||||
if (option.behavior or types.cmp.SelectBehavior.Insert) == types.cmp.SelectBehavior.Insert then
|
||||
feedkeys.call(keymap.t('<C-p>'), 'n', callback)
|
||||
feedkeys.call(keymap.t(string.format('%s<C-p>', option.count), 'n', callback))
|
||||
else
|
||||
feedkeys.call(keymap.t('<Up>'), 'n', callback)
|
||||
feedkeys.call(keymap.t(string.format('%s<Up>', option.count)), 'n', callback)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -181,11 +181,14 @@ end
|
||||
|
||||
wildmenu_entries_view.select_next_item = function(self, option)
|
||||
if self:visible() then
|
||||
local cursor
|
||||
if self.selected_index == 0 or self.selected_index == #self.entries then
|
||||
self:_select(1, option)
|
||||
cursor = option.count
|
||||
else
|
||||
self:_select(self.selected_index + 1, option)
|
||||
cursor = self.selected_index + option.count
|
||||
end
|
||||
cursor = math.max(math.min(cursor, #self.entries), 0)
|
||||
self:_select(cursor, option)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -194,7 +197,7 @@ wildmenu_entries_view.select_prev_item = function(self, option)
|
||||
if self.selected_index == 0 or self.selected_index <= 1 then
|
||||
self:_select(#self.entries, option)
|
||||
else
|
||||
self:_select(self.selected_index - 1, option)
|
||||
self:_select(math.max(self.selected_index - option.count, 0), option)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user