fix: correct mapping for snippet insertion in completion

This commit is contained in:
2024-12-18 14:24:34 -05:00
parent d2bda27817
commit 00a78e028b
+190 -179
View File
@@ -22,6 +22,7 @@ local function cmp_provider(performance)
} }
end end
end end
local cmp_repo = cmp_provider(perf) local cmp_repo = cmp_provider(perf)
if vim.g.useblinkcmp then if vim.g.useblinkcmp then
@@ -40,8 +41,13 @@ if vim.g.useblinkcmp then
['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' }, ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
['<C-e>'] = { 'hide', 'fallback' }, ['<C-e>'] = { 'hide', 'fallback' },
['<C-y>'] = {
LazyVim.cmp.map({ 'select_and_accept', 'ai_accept' }),
'fallback',
},
['<Tab>'] = { ['<Tab>'] = {
LazyVim.cmp.map({ 'snippet_forward', 'ai_accept' }), 'snippet_forward',
'fallback', 'fallback',
}, },
['<S-Tab>'] = { 'snippet_backward', 'fallback' }, ['<S-Tab>'] = { 'snippet_backward', 'fallback' },
@@ -76,186 +82,191 @@ if vim.g.useblinkcmp then
end end
-- Else use classic cmp -- Else use classic cmp
return { -- cmp return {
url = cmp_repo.url, { import = 'lazyvim.plugins.extras.coding.nvim-cmp' },
build = cmp_repo.build, { -- cmp
branch = cmp_repo.branch, url = cmp_repo.url,
dev = cmp_repo.dev, build = cmp_repo.build,
enabled = not vim.g.useblinkcmp, branch = cmp_repo.branch,
event = 'VeryLazy', dev = cmp_repo.dev,
dependencies = require('data.deps').cmp, enabled = not vim.g.useblinkcmp,
config = function() event = 'VeryLazy',
local cmp = require('cmp') dependencies = require('data.deps').cmp,
local luasnip = require('luasnip') config = function()
cmp.event:on('menu_opened', function() local cmp = require('cmp')
if require('data.cond').neocodeium() == true then local luasnip = require('luasnip')
require('neocodeium').clear() cmp.event:on('menu_opened', function()
if require('data.cond').neocodeium() == true then
require('neocodeium').clear()
end
end)
cmp.event:on('menu_closed', function()
if require('data.cond').neocodeium() == true then
require('neocodeium.commands').enable()
require('neocodeium').cycle_or_complete()
end
end)
local border_opts = {
border = vim.g.completion_borders or 'rounded',
}
local window_opts = {
completion = cmp.config.window.bordered(border_opts),
documentation = cmp.config.window.bordered(border_opts),
}
luasnip.config.setup({})
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0
and vim.api
.nvim_buf_get_lines(0, line - 1, line, true)[1]
:sub(col, col)
:match('%s')
== nil
end end
end) cmp.setup({
cmp.event:on('menu_closed', function() snippet = {
if require('data.cond').neocodeium() == true then expand = function(args)
require('neocodeium.commands').enable() luasnip.lsp_expand(args.body)
require('neocodeium').cycle_or_complete() end,
end
end)
local border_opts = {
border = vim.g.completion_borders or 'rounded',
}
local window_opts = {
completion = cmp.config.window.bordered(border_opts),
documentation = cmp.config.window.bordered(border_opts),
}
luasnip.config.setup({})
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0
and vim.api
.nvim_buf_get_lines(0, line - 1, line, true)[1]
:sub(col, col)
:match('%s')
== nil
end
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
completion = { completeopt = 'menu,menuone,noinsert' },
window = vim.g.completion_borders == 'rounded' and window_opts or {},
mapping = cmp.mapping.preset.insert({
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<Up>'] = cmp.mapping.select_prev_item({
behavior = cmp.SelectBehavior.Select,
}),
['<Down>'] = cmp.mapping.select_next_item({
behavior = cmp.SelectBehavior.Select,
}),
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-y>'] = cmp.mapping.confirm({ select = true }),
['<C-Space>'] = cmp.mapping.complete({}),
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
-- If the completion popup is visible, select the next item
cmp.select_next_item()
elseif luasnip.expand_or_locally_jumpable() then
-- If a snippet can be expanded or jump to the next location, do so
luasnip.expand_or_jump()
elseif vim.snippet.active({ direction = 1 }) then
-- If a snippet is active, jump to the next location
vim.schedule(function()
vim.snippet.jump(1)
end)
elseif has_words_before() then
-- If there are words before the cursor, complete them
cmp.complete()
else
-- Otherwise, fallback to the default behavior of the Tab key
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
elseif vim.snippet.active({ direction = -1 }) then
vim.schedule(function()
vim.snippet.jump(-1)
end)
else
fallback()
end
end, { 'i', 's' }),
}),
sources = {
{ name = 'ecolog' },
{ name = 'nvim_lsp', priority = 1000 },
{ name = 'luasnip', priority = 750 },
{ name = 'path', priority = 250 },
{ name = 'buffer', priority = 250 },
{ name = 'cmp_yanky' },
{ name = 'dotenv' },
{ name = 'calc' },
{ name = 'conventionalcommits' },
},
formatting = {
fields = { 'abbr', 'kind', 'menu' },
expandable_indicator = true,
format = function(entry, item)
local custom_menu_icon = {
calc = '󰃬 Calculator',
math = ' Math',
nerd = ' Glyphs',
gitmoji = ' Gitmoji',
emoji = '󰞅 Emoji',
conventionalcommits = ' Commit Message',
path = ' Path',
buffer = '󰓩 Buffer',
dotenv = ' Dotenv',
cmp_yanky = '󰅍 Yanked',
}
local color_item =
require('nvim-highlight-colors').format(entry, { kind = item.kind })
item = require('lspkind').cmp_format({
require('tailwind-tools.cmp').lspkind_format,
})(entry, item)
if color_item.abbr_hl_group then
item.kind_hl_group = color_item.abbr_hl_group
item.kind = color_item.abbr
end
-- if entry.source.name is in the custom_menu_icon table, match its icon
if custom_menu_icon[entry.source.name] then
item.kind = custom_menu_icon[entry.source.name]
end
return item
end,
},
})
-- `:` cmdline setup.
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' },
}, {
{
name = 'cmdline',
option = {
ignore_cmds = { 'Man', '!' },
},
}, },
}, { name = 'cmp-cmdline-history' }, { completion = { completeopt = 'menu,menuone,noinsert' },
name = 'cmp-cmdline-prompt', window = vim.g.completion_borders == 'rounded' and window_opts or {},
}), mapping = cmp.mapping.preset.insert({
}) ['<C-n>'] = cmp.mapping.select_next_item(),
-- Additional setup for cmdline filetype ['<C-p>'] = cmp.mapping.select_prev_item(),
cmp.setup.filetype('cmdline', { ['<Up>'] = cmp.mapping.select_prev_item({
sources = { behavior = cmp.SelectBehavior.Select,
{ name = 'cmdline' }, -- Ensure 'cmdline' source is available }),
{ name = 'path' }, ['<Down>'] = cmp.mapping.select_next_item({
{ name = 'cmp-cmdline-history' }, behavior = cmp.SelectBehavior.Select,
{ name = 'cmp-cmdline-prompt' }, }),
}, ['<C-b>'] = cmp.mapping.scroll_docs(-4),
}) ['<C-f>'] = cmp.mapping.scroll_docs(4),
-- Input filetype ['<C-y>'] = cmp.mapping.confirm({ select = true }),
cmp.setup.filetype('input', { ['<C-Space>'] = cmp.mapping.complete({}),
sources = {}, ['<Tab>'] = cmp.mapping(function(fallback)
}) if cmp.visible() then
-- Custom filetype configuration -- If the completion popup is visible, select the next item
cmp.setup.filetype('config', { cmp.select_next_item()
sources = vim.tbl_filter(function(source) elseif luasnip.expand_or_locally_jumpable() then
return source.name ~= 'emoji' and source.name ~= 'gitmoji' -- If a snippet can be expanded or jump to the next location, do so
end, cmp.get_config().sources), luasnip.expand_or_jump()
}) elseif vim.snippet.active({ direction = 1 }) then
-- List of filetypes to disable completion -- If a snippet is active, jump to the next location
local disabled_filetypes = require('data.types').cmp vim.schedule(function()
for _, filetype in ipairs(disabled_filetypes) do vim.snippet.jump(1)
cmp.setup.filetype(filetype, { end)
elseif has_words_before() then
-- If there are words before the cursor, complete them
cmp.complete()
else
-- Otherwise, fallback to the default behavior of the Tab key
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
elseif vim.snippet.active({ direction = -1 }) then
vim.schedule(function()
vim.snippet.jump(-1)
end)
else
fallback()
end
end, { 'i', 's' }),
}),
sources = {
{ name = 'ecolog' },
{ name = 'nvim_lsp', priority = 1000 },
{ name = 'luasnip', priority = 750 },
{ name = 'path', priority = 250 },
{ name = 'buffer', priority = 250 },
{ name = 'cmp_yanky' },
{ name = 'dotenv' },
{ name = 'calc' },
{ name = 'conventionalcommits' },
},
formatting = {
fields = { 'abbr', 'kind', 'menu' },
expandable_indicator = true,
format = function(entry, item)
local custom_menu_icon = {
calc = '󰃬 Calculator',
math = ' Math',
nerd = ' Glyphs',
gitmoji = ' Gitmoji',
emoji = '󰞅 Emoji',
conventionalcommits = ' Commit Message',
path = ' Path',
buffer = '󰓩 Buffer',
dotenv = ' Dotenv',
cmp_yanky = '󰅍 Yanked',
}
local color_item = require('nvim-highlight-colors').format(
entry,
{ kind = item.kind }
)
item = require('lspkind').cmp_format({
require('tailwind-tools.cmp').lspkind_format,
})(entry, item)
if color_item.abbr_hl_group then
item.kind_hl_group = color_item.abbr_hl_group
item.kind = color_item.abbr
end
-- if entry.source.name is in the custom_menu_icon table, match its icon
if custom_menu_icon[entry.source.name] then
item.kind = custom_menu_icon[entry.source.name]
end
return item
end,
},
})
-- `:` cmdline setup.
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' },
}, {
{
name = 'cmdline',
option = {
ignore_cmds = { 'Man', '!' },
},
},
}, { name = 'cmp-cmdline-history' }, {
name = 'cmp-cmdline-prompt',
}),
})
-- Additional setup for cmdline filetype
cmp.setup.filetype('cmdline', {
sources = {
{ name = 'cmdline' }, -- Ensure 'cmdline' source is available
{ name = 'path' },
{ name = 'cmp-cmdline-history' },
{ name = 'cmp-cmdline-prompt' },
},
})
-- Input filetype
cmp.setup.filetype('input', {
sources = {}, sources = {},
}) })
end -- Custom filetype configuration
end, cmp.setup.filetype('config', {
sources = vim.tbl_filter(function(source)
return source.name ~= 'emoji' and source.name ~= 'gitmoji'
end, cmp.get_config().sources),
})
-- List of filetypes to disable completion
local disabled_filetypes = require('data.types').cmp
for _, filetype in ipairs(disabled_filetypes) do
cmp.setup.filetype(filetype, {
sources = {},
})
end
end,
},
} }