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:
ADoyle
2022-11-09 18:26:20 +08:00
committed by GitHub
parent c8d4afdad6
commit bdfadc1238
5 changed files with 72 additions and 33 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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