Fixed code completion and added tab to select completion
This commit is contained in:
parent
e8483645af
commit
fdc9e0c32d
2 changed files with 40 additions and 0 deletions
|
|
@ -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 cmp = require("cmp")
|
||||||
|
local luasnip = require("luasnip")
|
||||||
|
|
||||||
cmp.setup({
|
cmp.setup({
|
||||||
mapping = cmp.mapping.preset.insert({
|
mapping = cmp.mapping.preset.insert({
|
||||||
|
|
@ -7,11 +14,38 @@ cmp.setup({
|
||||||
['<C-o>'] = cmp.mapping.complete(),
|
['<C-o>'] = cmp.mapping.complete(),
|
||||||
['<C-e>'] = cmp.mapping.abort(),
|
['<C-e>'] = cmp.mapping.abort(),
|
||||||
['<CR>'] = cmp.mapping.confirm({ select = true }),
|
['<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 = {
|
snippet = {
|
||||||
|
expand = function(args)
|
||||||
|
require('luasnip').lsp_expand(args.body)
|
||||||
|
end,
|
||||||
},
|
},
|
||||||
sources = cmp.config.sources({
|
sources = cmp.config.sources({
|
||||||
{ name = 'nvim_lsp' },
|
{ name = 'nvim_lsp' },
|
||||||
|
{ name = 'luasnip' },
|
||||||
}, {
|
}, {
|
||||||
{ name = 'buffer' },
|
{ name = 'buffer' },
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,12 @@ local plugins = {
|
||||||
-- completion
|
-- completion
|
||||||
'hrsh7th/nvim-cmp',
|
'hrsh7th/nvim-cmp',
|
||||||
'hrsh7th/cmp-nvim-lsp',
|
'hrsh7th/cmp-nvim-lsp',
|
||||||
|
{
|
||||||
|
"L3MON4D3/LuaSnip",
|
||||||
|
version = "2.*", -- Replace <CurrentMajor> by the latest released major
|
||||||
|
-- install jsregexp (optional!).
|
||||||
|
build = "make install_jsregexp"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local opts = {}
|
local opts = {}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue