Skip to content

implement new behavior for autocompletion menu, toggle option to require keypress for showing suggestions. #98

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

Merged
merged 3 commits into from
Oct 15, 2024
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -286,6 +286,8 @@ MiniDeps.add({
-- which directions to show the window,
-- falling back to the next direction when there's not enough space
direction_priority = { 's', 'n' },
-- Controls whether the completion window will automatically show when typing
auto_show = true,
-- Controls how the completion items are selected
-- 'preselect' will automatically select the first item in the completion list
-- 'manual' will not select any item by default
@@ -318,6 +320,7 @@ MiniDeps.add({
autocomplete_north = { 'e', 'w', 'n', 's' },
autocomplete_south = { 'e', 'w', 's', 'n' },
},
-- Controls whether the documentation window will automatically show when selecting a completion item
auto_show = false,
auto_show_delay_ms = 500,
update_delay_ms = 50,
4 changes: 4 additions & 0 deletions lua/blink/cmp/config.lua
Original file line number Diff line number Diff line change
@@ -89,6 +89,7 @@
--- @field border? blink.cmp.WindowBorder
--- @field order? "top_down" | "bottom_up"
--- @field direction_priority? ("n" | "s")[]
--- @field auto_show? boolean
--- @field selection? "preselect" | "manual" | "auto_insert"
--- @field winhighlight? string
--- @field scrolloff? number
@@ -250,6 +251,8 @@ local config = {
-- which directions to show the window,
-- falling back to the next direction when there's not enough space
direction_priority = { 's', 'n' },
-- Controls whether the completion window will automatically show when typing
auto_show = true,
-- Controls how the completion items are selected
-- 'preselect' will automatically select the first item in the completion list
-- 'manual' will not select any item by default
@@ -281,6 +284,7 @@ local config = {
autocomplete_north = { 'e', 'w', 'n', 's' },
autocomplete_south = { 'e', 'w', 's', 'n' },
},
-- Controls whether the documentation window will automatically show when selecting a completion item
auto_show = false,
auto_show_delay_ms = 500,
update_delay_ms = 50,
15 changes: 12 additions & 3 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
@@ -119,7 +119,10 @@ end

cmp.show = function()
if cmp.windows.autocomplete.win:is_open() then return end
vim.schedule(cmp.trigger.show)
vim.schedule(function()
cmp.windows.autocomplete.auto_show = true
cmp.trigger.show({ force = true })
end)
return true
end

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

cmp.select_prev = function()
if not cmp.windows.autocomplete.win:is_open() then return end
if not cmp.windows.autocomplete.win:is_open() then
cmp.show()
return true
end
vim.schedule(cmp.windows.autocomplete.select_prev)
return true
end

cmp.select_next = function()
if not cmp.windows.autocomplete.win:is_open() then return end
if not cmp.windows.autocomplete.win:is_open() then
cmp.show()
return true
end
vim.schedule(cmp.windows.autocomplete.select_next)
return true
end
10 changes: 7 additions & 3 deletions lua/blink/cmp/trigger/completion.lua
Original file line number Diff line number Diff line change
@@ -142,13 +142,18 @@ function trigger.suppress_events_for_callback(cb)
and is_insert_mode
end

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

local cursor = vim.api.nvim_win_get_cursor(0)
-- already triggered at this position, ignore
if trigger.context ~= nil and cursor[1] == trigger.context.cursor[1] and cursor[2] == trigger.context.cursor[2] then
if
not opts.force
and trigger.context ~= nil
and cursor[1] == trigger.context.cursor[1]
and cursor[2] == trigger.context.cursor[2]
then
return
end

@@ -175,7 +180,6 @@ function trigger.listen_on_show(callback) trigger.event_targets.on_show = callba

function trigger.hide()
if not trigger.context then return end

trigger.context = nil
trigger.event_targets.on_hide()
end
5 changes: 4 additions & 1 deletion lua/blink/cmp/windows/autocomplete.lua
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ local autocomplete = {
---@type blink.cmp.CompletionItem[]
items = {},
has_selected = nil,
-- hack: ideally this doesn't get mutated by the public API
auto_show = autocmp_config.auto_show,
---@type blink.cmp.Context?
context = nil,
event_targets = {
@@ -71,9 +73,9 @@ function autocomplete.open_with_items(context, items)

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

if not autocomplete.auto_show then return end
autocomplete.win:open()

autocomplete.context = context
autocomplete.update_position(context)
autocomplete.set_has_selected(autocmp_config.selection == 'preselect')

@@ -91,6 +93,7 @@ end

function autocomplete.close()
if not autocomplete.win:is_open() then return end
autocomplete.auto_show = autocmp_config.auto_show
autocomplete.win:close()
autocomplete.set_has_selected(autocmp_config.selection == 'preselect')