Skip to content

Commit

Permalink
[vim] refine ai completion keymaps and setup
Browse files Browse the repository at this point in the history
  • Loading branch information
antonk52 committed Oct 12, 2023
1 parent 1296722 commit 2b5b192
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
8 changes: 8 additions & 0 deletions nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ local plugins = {
auto_trigger = true,
},
})
local c = require('copilot.suggestion')
require('antonk52.completion').update_ai_completion({
is_visible = c.is_visible,
accept = c.accept,
accept_word = c.accept_word,
accept_line = c.accept_line,
dismiss = c.dismiss,
})
else
return print('no copilot at work')
end
Expand Down
65 changes: 45 additions & 20 deletions nvim/lua/antonk52/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,33 @@ local snippet_sources = {
{ name = 'buffer', keyword_length = 3 },
}

local noop = function() end

---@class AI_completion
local AI = {
is_visible = function()
return false
end,
accept = noop,
accept_word = noop,
accept_line = noop,
dismiss = noop,
}

---@param opts AI_completion
function M.update_ai_completion(opts)
AI.is_visible = opts.is_visible
AI.accept = opts.accept
AI.accept_word = opts.accept_word
AI.accept_line = opts.accept_line
AI.dismiss = opts.dismiss
end

function M.setup()
local mapping = {
['<Tab>'] = function(fallback)
if vim.env.WORK == nil and require('copilot.suggestion').is_visible() then
require('copilot.suggestion').accept()
if AI.is_visible() then
AI.accept()
elseif cmp.visible() then
cmp.select_next_item()
else
Expand All @@ -33,6 +55,27 @@ function M.setup()
fallback()
end
end,
['<C-d>'] = function(fallback)
if AI.is_visible() then
AI.dismiss()
else
fallback()
end
end,
['<C-e>'] = function(fallback)
if AI.is_visible() then
AI.accept_word()
else
fallback()
end
end,
['<C-r>'] = function(fallback)
if AI.is_visible() then
AI.accept_line()
else
fallback()
end
end,
['<C-y>'] = cmp.mapping.confirm(),
['<CR>'] = cmp.mapping.confirm(),
['<C-j>'] = cmp.mapping(function(fallback)
Expand All @@ -51,24 +94,6 @@ function M.setup()
end),
}

if vim.env.WORK == nil then
mapping['<C-w>'] = function()
if require('copilot.suggestion').is_visible() then
require('copilot.suggestion').accept_word()
end
end
mapping['<C-e>'] = function()
if require('copilot.suggestion').is_visible() then
require('copilot.suggestion').accept_line()
end
end
mapping['<C-q>'] = function()
if require('copilot.suggestion').is_visible() then
require('copilot.suggestion').dismiss()
end
end
end

cmp.setup({
snippet = {
expand = function(arg)
Expand Down

0 comments on commit 2b5b192

Please sign in to comment.