nvim/lua/plugins/cmp.lua

117 lines
3.0 KiB
Lua
Raw Normal View History

2023-02-19 23:01:47 +01:00
function leave_snippet()
2023-03-27 14:42:59 +02:00
if
((vim.v.event.old_mode == 's' and vim.v.event.new_mode == 'n') or vim.v.event.old_mode == 'i')
2023-02-19 23:01:47 +01:00
and require('luasnip').session.current_nodes[vim.api.nvim_get_current_buf()]
and not require('luasnip').session.jump_active
then
require('luasnip').unlink_current()
end
end
2023-03-27 14:42:20 +02:00
local border = {
{ '', 'CmpBorder' },
{ '', 'CmpBorder' },
{ '', 'CmpBorder' },
{ '', 'CmpBorder' },
{ '', 'CmpBorder' },
{ '', 'CmpBorder' },
{ '', 'CmpBorder' },
{ '', 'CmpBorder' },
}
vim.api.nvim_command [[
2023-02-19 23:01:47 +01:00
autocmd ModeChanged * lua leave_snippet()
2023-03-27 14:42:59 +02:00
]]
2023-02-19 23:01:47 +01:00
return {
{
'hrsh7th/nvim-cmp',
dependencies = {
'hrsh7th/cmp-nvim-lsp',
'L3MON4D3/LuaSnip',
2023-03-23 23:07:09 +01:00
'saadparwaiz1/cmp_luasnip',
2023-03-27 14:42:59 +02:00
{
-- copilot
2023-03-23 23:07:09 +01:00
'zbirenbaum/copilot.lua',
opts = {
suggestion = { enabled = false },
panel = { enabled = false },
filetypes = {
sh = function()
if string.match(vim.fs.basename(vim.api.nvim_buf_get_name(0)), '^%.env.*') then
return false
end
return true
end,
},
},
2023-03-23 23:07:09 +01:00
},
2023-03-27 14:42:59 +02:00
{
-- copilot-cmp source
2023-03-23 23:07:09 +01:00
'zbirenbaum/copilot-cmp',
config = function()
require('copilot_cmp').setup { clear_after_cursor = true }
vim.keymap.set('n', '<leader>cd', ":Copilot disable<CR>", { desc = '[C]opilot [d]isable' })
vim.keymap.set('n', '<leader>ce', ":Copilot enable<CR>", { desc = '[C]opilot [e]nable' })
end,
2023-03-27 14:42:59 +02:00
},
2023-02-19 23:01:47 +01:00
},
2023-03-23 23:07:09 +01:00
config = function()
2023-02-19 23:01:47 +01:00
-- nvim-cmp setup
local cmp = require 'cmp'
local luasnip = require 'luasnip'
luasnip.config.setup {}
cmp.setup {
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert {
2023-03-27 14:42:59 +02:00
['<C-d>'] = cmp.mapping.scroll_docs(-4),
2023-02-19 23:01:47 +01:00
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete {},
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
2023-03-27 14:42:59 +02:00
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
2023-02-19 23:01:47 +01:00
else
fallback()
end
end, { 'i', 's' }),
},
sources = {
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
2023-03-23 23:07:09 +01:00
{ name = 'copilot' },
{ name = 'crates' },
2023-02-19 23:01:47 +01:00
},
2023-03-27 14:42:20 +02:00
window = {
completion = {
border = border,
},
},
2023-02-19 23:01:47 +01:00
}
2023-03-27 14:42:59 +02:00
end,
2023-02-19 23:01:47 +01:00
},
}