From 453fc0f22c8443c7c540d9d2abedeca5f679378c Mon Sep 17 00:00:00 2001 From: Eyal Chojnowski Date: Thu, 10 Oct 2024 16:48:39 +0200 Subject: [PATCH] feat!: change config of auto_insert,preselect to a single 'selection' --- README.md | 9 +++++---- lua/blink/cmp/config.lua | 12 ++++++------ lua/blink/cmp/trigger/completion.lua | 2 +- lua/blink/cmp/windows/autocomplete.lua | 8 ++++---- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 664d79b0..a9ebba7c 100644 --- a/README.md +++ b/README.md @@ -242,10 +242,11 @@ For LazyVim/distro users, you can disable nvim-cmp via: -- which directions to show the window, -- falling back to the next direction when there's not enough space direction_priority = { 's', 'n' }, - -- whether to preselect the first item in the completion list - preselect = true, - -- Whether to insert the completion items automatically when selecting them. Requires `preselect = false` - auto_insert = 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 + -- 'auto_insert' will not select any item by default, and insert the completion items automatically when selecting them + selection = 'preselect', -- Controls how the completion items are rendered on the popup window -- 'simple' will render the item's kind icon the left alongside the label -- 'reversed' will render the label on the left and the kind icon + name on the right diff --git a/lua/blink/cmp/config.lua b/lua/blink/cmp/config.lua index a4ac461c..e6e70a98 100644 --- a/lua/blink/cmp/config.lua +++ b/lua/blink/cmp/config.lua @@ -88,8 +88,7 @@ --- @field border? blink.cmp.WindowBorder --- @field order? "top_down" | "bottom_up" --- @field direction_priority? ("n" | "s")[] ---- @field preselect? boolean ---- @field auto_insert? boolean +--- @field selection? "preselect" | "manual" | "auto_insert" --- @field winhighlight? string --- @field scrolloff? number --- @field draw? 'simple' | 'reversed' | function(blink.cmp.CompletionRenderContext): blink.cmp.Component[] @@ -247,10 +246,11 @@ local config = { -- which directions to show the window, -- falling back to the next direction when there's not enough space direction_priority = { 's', 'n' }, - -- whether to preselect the first item in the completion list - preselect = true, - -- Whether to insert the completion items automatically when selecting them. Requires `preselect = false` - auto_insert = 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 + -- 'auto_insert' will not select any item by default, and insert the completion items automatically when selecting them + selection = 'preselect', -- Controls how the completion items are rendered on the popup window -- 'simple' will render the item's kind icon the left alongside the label -- 'reversed' will render the label on the left and the kind icon + name on the right diff --git a/lua/blink/cmp/trigger/completion.lua b/lua/blink/cmp/trigger/completion.lua index 521f3560..fa13efd6 100644 --- a/lua/blink/cmp/trigger/completion.lua +++ b/lua/blink/cmp/trigger/completion.lua @@ -67,7 +67,7 @@ function trigger.activate_autocmds() and not vim.tbl_contains(config_trigger.show_on_insert_blocked_trigger_characters, char_under_cursor) local is_on_context_char = char_under_cursor:match(config_trigger.keyword_regex) ~= nil - if config.windows.autocomplete.auto_insert and trigger.triggered_by ~= 'select' then + if config.windows.autocomplete.selection == 'auto_insert' and trigger.triggered_by ~= 'select' then if is_within_bounds then trigger.show() elseif diff --git a/lua/blink/cmp/windows/autocomplete.lua b/lua/blink/cmp/windows/autocomplete.lua index 48261402..a4a9e704 100644 --- a/lua/blink/cmp/windows/autocomplete.lua +++ b/lua/blink/cmp/windows/autocomplete.lua @@ -66,7 +66,7 @@ function autocomplete.open_with_items(context, items) autocomplete.context = context autocomplete.update_position(context) - autocomplete.set_has_selected(autocmp_config.preselect) + autocomplete.set_has_selected(autocmp_config.selection == 'preselect') -- todo: some logic to maintain the selection if the user moved the cursor? vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { 1, 0 }) @@ -76,13 +76,13 @@ end function autocomplete.open() if autocomplete.win:is_open() then return end autocomplete.win:open() - autocomplete.set_has_selected(autocmp_config.preselect) + autocomplete.set_has_selected(autocmp_config.selection == 'preselect') end function autocomplete.close() if not autocomplete.win:is_open() then return end autocomplete.win:close() - autocomplete.has_selected = autocmp_config.preselect + autocomplete.has_selected = autocmp_config.selection == 'preselect' autocomplete.event_targets.on_close() end function autocomplete.listen_on_close(callback) autocomplete.event_targets.on_close = callback end @@ -140,7 +140,7 @@ end --- @param line number local function select(line) - local auto_insert = config.windows.autocomplete.auto_insert + local auto_insert = config.windows.autocomplete.selection == 'auto_insert' local prev_selected_item = autocomplete.get_selected_item()