Fixed code completion and added tab to select completion

This commit is contained in:
Warwick 2023-08-06 20:50:34 +01:00
parent e8483645af
commit fdc9e0c32d
2 changed files with 40 additions and 0 deletions

View file

@ -1,4 +1,11 @@
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
local cmp = require("cmp")
local luasnip = require("luasnip")
cmp.setup({
mapping = cmp.mapping.preset.insert({
@ -7,11 +14,38 @@ cmp.setup({
['<C-o>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- they way you will only jump inside the snippet region
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
}, {
{ name = 'buffer' },
}),

View file

@ -37,6 +37,12 @@ local plugins = {
-- completion
'hrsh7th/nvim-cmp',
'hrsh7th/cmp-nvim-lsp',
{
"L3MON4D3/LuaSnip",
version = "2.*", -- Replace <CurrentMajor> by the latest released major
-- install jsregexp (optional!).
build = "make install_jsregexp"
}
}
local opts = {}