Skip to content

Commit d20e34d

Browse files
authored
feat: cycle completions (#12)
1 parent 88ac59a commit d20e34d

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ For LazyVim/distro users, you can disable nvim-cmp via:
234234
-- 'reversed' will render the label on the left and the kind icon + name on the right
235235
-- 'function(blink.cmp.CompletionRenderContext): blink.cmp.Component[]' for custom rendering
236236
draw = 'simple',
237+
-- Controls the cycling behavior when reaching the beginning or end of the completion list.
238+
cycle = {
239+
-- When `true`, calling `select_next` at the *bottom* of the completion list will select the *first* completion item.
240+
from_bottom = true,
241+
-- When `true`, calling `select_prev` at the *top* of the completion list will select the *last* completion item.
242+
from_top = true
243+
},
237244
},
238245
documentation = {
239246
min_width = 10,

lua/blink/cmp/config.lua

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@
8787
--- @field winhighlight string
8888
--- @field scrolloff number
8989
--- @field draw 'simple' | 'reversed' | function(blink.cmp.CompletionRenderContext): blink.cmp.Component[]
90+
--- @field cycle blink.cmp.AutocompleteConfig.CycleConfig
91+
92+
--- @class blink.cmp.AutocompleteConfig.CycleConfig
93+
--- @field from_bottom boolean When `true`, calling `select_next` at the *bottom* of the completion list will select the *first* completion item.
94+
--- @field from_top boolean When `true`, calling `select_prev` at the *top* of the completion list will select the *last* completion item.
9095

9196
--- @class blink.cmp.DocumentationDirectionPriorityConfig
9297
--- @field autocomplete_north ("n" | "s" | "e" | "w")[]
@@ -233,6 +238,10 @@ local config = {
233238
-- 'reversed' will render the label on the left and the kind icon + name on the right
234239
-- 'function(blink.cmp.CompletionRenderContext): blink.cmp.Component[]' for custom rendering
235240
draw = 'simple',
241+
cycle = {
242+
from_bottom = true,
243+
from_top = true,
244+
},
236245
},
237246
documentation = {
238247
min_width = 10,

lua/blink/cmp/windows/autocomplete.lua

+29-8
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,41 @@ function autocomplete.select_next()
138138

139139
local current_line = vim.api.nvim_win_get_cursor(autocomplete.win:get_win())[1]
140140
local line_count = vim.api.nvim_buf_line_count(autocomplete.win:get_buf())
141-
if current_line == line_count then return end
142-
143-
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { current_line + 1, 0 })
144-
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
141+
local cycle_from_bottom = config.windows.autocomplete.cycle.from_bottom
142+
local is_last_completion = current_line == line_count
143+
144+
-- at the end of completion list and the config is not enabled: do nothing
145+
if is_last_completion and not cycle_from_bottom then return end
146+
if is_last_completion then
147+
-- at the end of completion list and the config is enabled: cycle back to first completion
148+
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { 1, 0 })
149+
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
150+
else
151+
-- select next completion
152+
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { current_line + 1, 0 })
153+
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
154+
end
145155
end
146156

147157
function autocomplete.select_prev()
148158
if not autocomplete.win:is_open() then return end
149159

150160
local current_line = vim.api.nvim_win_get_cursor(autocomplete.win:get_win())[1]
151-
if current_line == 1 then return end
152-
153-
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { math.max(current_line - 1, 1), 0 })
154-
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
161+
local line_count = vim.api.nvim_buf_line_count(autocomplete.win:get_buf())
162+
local cycle_from_top = config.windows.autocomplete.cycle.from_top
163+
local is_first_completion = current_line == 1
164+
165+
-- at the beginning of completion list and the config is not enabled: do nothing
166+
if is_first_completion and not cycle_from_top then return end
167+
if is_first_completion then
168+
-- at the beginning of completion list and the config is enabled: cycle back to last completion
169+
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { line_count, 0 })
170+
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
171+
else
172+
-- select previous completion
173+
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { math.max(current_line - 1, 1), 0 })
174+
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
175+
end
155176
end
156177

157178
function autocomplete.listen_on_select(callback) autocomplete.event_targets.on_select = callback end

0 commit comments

Comments
 (0)