Skip to content

Commit 82e03b1

Browse files
Jezda1337Saghen
andauthored
feat: auto_show on autocomplete window (#98)
* Implement new behavior for autocompletion menu, toggle option to require keypress for showing suggestions. * docs: auto_show in readme and explanation * feat: handle auto_show at the window level --------- Co-authored-by: Liam Dyer <[email protected]>
1 parent ff1f5fa commit 82e03b1

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ MiniDeps.add({
286286
-- which directions to show the window,
287287
-- falling back to the next direction when there's not enough space
288288
direction_priority = { 's', 'n' },
289+
-- Controls whether the completion window will automatically show when typing
290+
auto_show = true,
289291
-- Controls how the completion items are selected
290292
-- 'preselect' will automatically select the first item in the completion list
291293
-- 'manual' will not select any item by default
@@ -318,6 +320,7 @@ MiniDeps.add({
318320
autocomplete_north = { 'e', 'w', 'n', 's' },
319321
autocomplete_south = { 'e', 'w', 's', 'n' },
320322
},
323+
-- Controls whether the documentation window will automatically show when selecting a completion item
321324
auto_show = false,
322325
auto_show_delay_ms = 500,
323326
update_delay_ms = 50,

lua/blink/cmp/config.lua

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
--- @field border? blink.cmp.WindowBorder
9090
--- @field order? "top_down" | "bottom_up"
9191
--- @field direction_priority? ("n" | "s")[]
92+
--- @field auto_show? boolean
9293
--- @field selection? "preselect" | "manual" | "auto_insert"
9394
--- @field winhighlight? string
9495
--- @field scrolloff? number
@@ -250,6 +251,8 @@ local config = {
250251
-- which directions to show the window,
251252
-- falling back to the next direction when there's not enough space
252253
direction_priority = { 's', 'n' },
254+
-- Controls whether the completion window will automatically show when typing
255+
auto_show = true,
253256
-- Controls how the completion items are selected
254257
-- 'preselect' will automatically select the first item in the completion list
255258
-- 'manual' will not select any item by default
@@ -281,6 +284,7 @@ local config = {
281284
autocomplete_north = { 'e', 'w', 'n', 's' },
282285
autocomplete_south = { 'e', 'w', 's', 'n' },
283286
},
287+
-- Controls whether the documentation window will automatically show when selecting a completion item
284288
auto_show = false,
285289
auto_show_delay_ms = 500,
286290
update_delay_ms = 50,

lua/blink/cmp/init.lua

+12-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ end
119119

120120
cmp.show = function()
121121
if cmp.windows.autocomplete.win:is_open() then return end
122-
vim.schedule(cmp.trigger.show)
122+
vim.schedule(function()
123+
cmp.windows.autocomplete.auto_show = true
124+
cmp.trigger.show({ force = true })
125+
end)
123126
return true
124127
end
125128

@@ -149,13 +152,19 @@ cmp.accept = function()
149152
end
150153

151154
cmp.select_prev = function()
152-
if not cmp.windows.autocomplete.win:is_open() then return end
155+
if not cmp.windows.autocomplete.win:is_open() then
156+
cmp.show()
157+
return true
158+
end
153159
vim.schedule(cmp.windows.autocomplete.select_prev)
154160
return true
155161
end
156162

157163
cmp.select_next = function()
158-
if not cmp.windows.autocomplete.win:is_open() then return end
164+
if not cmp.windows.autocomplete.win:is_open() then
165+
cmp.show()
166+
return true
167+
end
159168
vim.schedule(cmp.windows.autocomplete.select_next)
160169
return true
161170
end

lua/blink/cmp/trigger/completion.lua

+7-3
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,18 @@ function trigger.suppress_events_for_callback(cb)
144144
and is_insert_mode
145145
end
146146

147-
--- @param opts { trigger_character?: string, send_upstream?: boolean } | nil
147+
--- @param opts { trigger_character?: string, send_upstream?: boolean, force?: boolean } | nil
148148
function trigger.show(opts)
149149
opts = opts or {}
150150

151151
local cursor = vim.api.nvim_win_get_cursor(0)
152152
-- already triggered at this position, ignore
153-
if trigger.context ~= nil and cursor[1] == trigger.context.cursor[1] and cursor[2] == trigger.context.cursor[2] then
153+
if
154+
not opts.force
155+
and trigger.context ~= nil
156+
and cursor[1] == trigger.context.cursor[1]
157+
and cursor[2] == trigger.context.cursor[2]
158+
then
154159
return
155160
end
156161

@@ -177,7 +182,6 @@ function trigger.listen_on_show(callback) trigger.event_targets.on_show = callba
177182

178183
function trigger.hide()
179184
if not trigger.context then return end
180-
181185
trigger.context = nil
182186
trigger.event_targets.on_hide()
183187
end

lua/blink/cmp/windows/autocomplete.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ local autocomplete = {
1414
---@type blink.cmp.CompletionItem[]
1515
items = {},
1616
has_selected = nil,
17+
-- hack: ideally this doesn't get mutated by the public API
18+
auto_show = autocmp_config.auto_show,
1719
---@type blink.cmp.Context?
1820
context = nil,
1921
event_targets = {
@@ -72,9 +74,9 @@ function autocomplete.open_with_items(context, items)
7274

7375
vim.iter(autocomplete.event_targets.on_open):each(function(callback) callback() end)
7476

77+
if not autocomplete.auto_show then return end
7578
autocomplete.win:open()
7679

77-
autocomplete.context = context
7880
autocomplete.update_position(context)
7981
autocomplete.set_has_selected(autocmp_config.selection == 'preselect')
8082

@@ -92,6 +94,7 @@ end
9294

9395
function autocomplete.close()
9496
if not autocomplete.win:is_open() then return end
97+
autocomplete.auto_show = autocmp_config.auto_show
9598
autocomplete.win:close()
9699
autocomplete.set_has_selected(autocmp_config.selection == 'preselect')
97100

0 commit comments

Comments
 (0)