Skip to content
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

Use vim.on_key for keymap #487

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
81 changes: 0 additions & 81 deletions lua/blink/cmp/keymap/apply.lua

This file was deleted.

51 changes: 0 additions & 51 deletions lua/blink/cmp/keymap/fallback.lua

This file was deleted.

52 changes: 37 additions & 15 deletions lua/blink/cmp/keymap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,43 @@ function keymap.setup()
mappings = vim.tbl_extend('force', preset_keymap, mappings)
end

-- We set on the buffer directly to avoid buffer-local keymaps (such as from autopairs)
-- from overriding our mappings. We also use InsertEnter to avoid conflicts with keymaps
-- applied on other autocmds, such as LspAttach used by nvim-lspconfig and most configs
vim.api.nvim_create_autocmd('InsertEnter', {
callback = function()
if not require('blink.cmp.config').enabled() then return end
require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings)
end,
})

-- This is not called when the plugin loads since it first checks if the binary is
-- installed. As a result, when lazy-loaded on InsertEnter, the event may be missed
if vim.api.nvim_get_mode().mode == 'i' and require('blink.cmp.config').enabled() then
require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings)
end
local snippet_commands = { 'snippet_forward', 'snippet_backward' }

-- We listen to every key and return an empty string when blink handles the key,
-- to tell neovim not to run the default keymaps
-- TODO: handle multiple keys like <C-g><C-o>
vim.on_key(function(original_key, key)
if not require('blink.cmp.config').enabled() then return original_key end

local mode = vim.api.nvim_get_mode().mode
if mode ~= 'i' and mode ~= 's' then return original_key end

for command_key, commands in pairs(mappings) do
if vim.api.nvim_replace_termcodes(command_key, true, true, true) == key then
for _, command in ipairs(commands) do
-- ignore snippet commands for insert mode
if vim.tbl_contains(snippet_commands, command) and mode == 'i' then goto continue end

-- special case for fallback, return the key so that neovim continues like normal
if command == 'fallback' then
return original_key

-- run user defined functions
elseif type(command) == 'function' then
if command(require('blink.cmp')) then return '' end

-- otherwise, run the built-in command
elseif require('blink.cmp')[command]() then
return ''
end

::continue::
end
end
end

return original_key
end)
end

return keymap
Loading