feat(autocomplete): better luasnip support
This commit is contained in:
parent
4aa419b23d
commit
6ff029a0c8
1 changed files with 24 additions and 6 deletions
|
@ -1,13 +1,19 @@
|
||||||
local cmp = require('cmp')
|
local cmp = require('cmp')
|
||||||
|
local luasnip = require('luasnip')
|
||||||
|
|
||||||
vim.cmd([[
|
vim.cmd([[
|
||||||
autocmd FileType TelescopePrompt lua require('cmp').setup.buffer { enabled = false }
|
autocmd FileType TelescopePrompt lua require('cmp').setup.buffer { enabled = false }
|
||||||
]])
|
]])
|
||||||
|
|
||||||
|
local has_words_before = function()
|
||||||
|
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({
|
cmp.setup({
|
||||||
snippet = {
|
snippet = {
|
||||||
expand = function(args)
|
expand = function(args)
|
||||||
require'luasnip'.lsp_expand(args.body)
|
luasnip.lsp_expand(args.body)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
mapping = {
|
mapping = {
|
||||||
|
@ -19,20 +25,32 @@ cmp.setup({
|
||||||
behavior = cmp.ConfirmBehavior.Replace,
|
behavior = cmp.ConfirmBehavior.Replace,
|
||||||
select = true,
|
select = true,
|
||||||
}),
|
}),
|
||||||
['<Tab>'] = function(fallback)
|
['<Tab>'] = cmp.mapping(function(fallback)
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
cmp.select_next_item()
|
cmp.select_next_item()
|
||||||
|
elseif luasnip.expand_or_jumpable() then
|
||||||
|
luasnip.expand_or_jump()
|
||||||
|
elseif has_words_before() then
|
||||||
|
cmp.complete()
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end,
|
end, {
|
||||||
['<S-Tab>'] = function(fallback)
|
'i',
|
||||||
|
's',
|
||||||
|
}),
|
||||||
|
['<S-Tab>'] = cmp.mapping(function(fallback)
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
cmp.select_prev_item()
|
cmp.select_prev_item()
|
||||||
|
elseif luasnip.jumpable(-1) then
|
||||||
|
luasnip.jump(-1)
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end,
|
end, {
|
||||||
|
'i',
|
||||||
|
's',
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
documentation = {
|
documentation = {
|
||||||
border = 'single',
|
border = 'single',
|
||||||
|
@ -43,10 +61,10 @@ cmp.setup({
|
||||||
},
|
},
|
||||||
sources = {
|
sources = {
|
||||||
{ name = 'nvim_lsp' },
|
{ name = 'nvim_lsp' },
|
||||||
|
{ name = 'luasnip' },
|
||||||
{ name = 'buffer' },
|
{ name = 'buffer' },
|
||||||
{ name = 'nvim_lua' },
|
{ name = 'nvim_lua' },
|
||||||
{ name = 'path' },
|
{ name = 'path' },
|
||||||
{ name = 'luasnip' },
|
|
||||||
},
|
},
|
||||||
formatting = {
|
formatting = {
|
||||||
format = require('lspkind').cmp_format({
|
format = require('lspkind').cmp_format({
|
||||||
|
|
Loading…
Add table
Reference in a new issue