From 83b5662b78cbd7d47e86898479c53e9b89120e3e Mon Sep 17 00:00:00 2001 From: Marco Caceffo Date: Wed, 8 Jan 2025 21:36:04 +0100 Subject: [PATCH] select_and_enter --- lua/blink/cmp/config/keymap.lua | 2 ++ lua/blink/cmp/init.lua | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lua/blink/cmp/config/keymap.lua b/lua/blink/cmp/config/keymap.lua index 86d74ebb..0dbea6e1 100644 --- a/lua/blink/cmp/config/keymap.lua +++ b/lua/blink/cmp/config/keymap.lua @@ -5,6 +5,7 @@ --- | 'cancel' Cancel the current completion, undoing the preview from auto_insert --- | 'accept' Accept the current completion item --- | 'select_and_accept' Select the first completion item, if there's no selection, and accept +--- | 'select_and_enter' Select the first completion item, if there's no selection, and enter --- | 'select_prev' Select the previous completion item --- | 'select_next' Select the next completion item --- | 'show_documentation' Show the documentation window @@ -129,6 +130,7 @@ function keymap.validate(config) 'cancel', 'accept', 'select_and_accept', + 'select_and_enter', 'select_prev', 'select_next', 'show_documentation', diff --git a/lua/blink/cmp/init.lua b/lua/blink/cmp/init.lua index deb91160..e76f2550 100644 --- a/lua/blink/cmp/init.lua +++ b/lua/blink/cmp/init.lua @@ -129,6 +129,24 @@ function cmp.select_and_accept(opts) return true end +--- Select the first completion item, if there's no selection, and enter +--- @param opts? blink.cmp.CompletionListSelectAndAcceptOpts +function cmp.select_and_enter(opts) + if not cmp.is_visible() then return end + + local completion_list = require('blink.cmp.completion.list') + vim.schedule(function() + completion_list.accept({ + index = completion_list.selected_item_idx or 1, + callback = function() + if opts and opts.callback then opts.callback() end + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', false) + end, + }) + end) + return true +end + --- Select the previous completion item --- @param opts? blink.cmp.CompletionListSelectOpts function cmp.select_prev(opts)