2023-03-09 16:26:45 +08:00
|
|
|
require("luasnip.loaders.from_vscode").lazy_load()
|
2022-08-07 21:00:11 +08:00
|
|
|
|
|
|
|
local has_words_before = function()
|
2023-03-09 16:26:45 +08:00
|
|
|
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
|
2022-08-07 21:00:11 +08:00
|
|
|
end
|
|
|
|
|
2023-03-09 16:26:45 +08:00
|
|
|
local luasnip = require("luasnip")
|
|
|
|
local cmp = require("cmp")
|
2022-08-07 21:00:11 +08:00
|
|
|
|
2023-03-09 16:26:45 +08:00
|
|
|
cmp.setup({
|
|
|
|
-- ... Your other configuration ...
|
|
|
|
snippet = {
|
|
|
|
-- REQUIRED - you must specify a snippet engine
|
|
|
|
expand = function(args)
|
|
|
|
-- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
|
|
|
|
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
|
|
|
|
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
|
|
|
|
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
mapping = {
|
2023-03-09 17:25:09 +08:00
|
|
|
["<Tab>"] = cmp.mapping(function(fallback)
|
2023-03-09 16:26:45 +08:00
|
|
|
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" }),
|
2023-03-09 17:25:09 +08:00
|
|
|
["<S-Tab>"] = cmp.mapping(function(fallback)
|
2023-03-09 16:26:45 +08:00
|
|
|
if cmp.visible() then
|
|
|
|
cmp.select_prev_item()
|
|
|
|
elseif luasnip.jumpable(-1) then
|
|
|
|
luasnip.jump(-1)
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end, { "i", "s" }),
|
2023-03-09 17:25:09 +08:00
|
|
|
['<C-b>'] = cmp.mapping.scroll_docs(-4),
|
|
|
|
['<C-f>'] = cmp.mapping.scroll_docs(4),
|
|
|
|
['<C-Space>'] = cmp.mapping.complete(),
|
|
|
|
['<C-e>'] = cmp.mapping.abort(),
|
|
|
|
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items
|
2023-03-09 16:26:45 +08:00
|
|
|
-- ... Your other mappings ...
|
|
|
|
},
|
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = "nvim_lsp" },
|
|
|
|
{ name = "treesitter" },
|
|
|
|
{ name = "buffer" },
|
|
|
|
{ name = "luasnip" }, -- For luasnip users.
|
|
|
|
{ name = "path" },
|
|
|
|
{ name = "git" },
|
|
|
|
{ name = "ctags" },
|
|
|
|
})
|
|
|
|
-- ... Your other configuration ...
|
2022-08-07 21:00:11 +08:00
|
|
|
})
|
|
|
|
|
2023-03-09 16:26:45 +08:00
|
|
|
cmp.setup.cmdline({ '/', '?' }, {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = {
|
|
|
|
{ name = 'buffer' }
|
|
|
|
}
|
2022-08-07 21:00:11 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
-- Set configuration for specific filetype.
|
|
|
|
cmp.setup.filetype('gitcommit', {
|
2023-03-09 16:26:45 +08:00
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it.
|
|
|
|
}, {
|
|
|
|
{ name = 'buffer' },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
-- `:` cmdline setup.
|
|
|
|
cmp.setup.cmdline(':', {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = 'path' }
|
|
|
|
}, {
|
|
|
|
{
|
|
|
|
name = 'cmdline',
|
|
|
|
option = {
|
|
|
|
ignore_cmds = { 'Man', '!' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-08-07 21:00:11 +08:00
|
|
|
})
|