2021-09-16 20:54:40 +00:00
|
|
|
local has_words_before = function()
|
2022-10-10 12:16:20 +00:00
|
|
|
if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
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
|
2021-09-16 20:54:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local feedkey = function(key, mode)
|
2022-10-10 12:16:20 +00:00
|
|
|
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
|
2021-09-16 20:54:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local lsp_symbols = {
|
|
|
|
Text = " (Text) ",
|
|
|
|
Method = " (Method)",
|
|
|
|
Function = " (Function)",
|
|
|
|
Constructor = " (Constructor)",
|
|
|
|
Field = " ﴲ (Field)",
|
|
|
|
Variable = "[] (Variable)",
|
|
|
|
Class = " (Class)",
|
|
|
|
Interface = " ﰮ (Interface)",
|
|
|
|
Module = " (Module)",
|
|
|
|
Property = " 襁 (Property)",
|
|
|
|
Unit = " (Unit)",
|
|
|
|
Value = " (Value)",
|
|
|
|
Enum = " 練 (Enum)",
|
|
|
|
Keyword = " (Keyword)",
|
|
|
|
Snippet = " (Snippet)",
|
|
|
|
Color = " (Color)",
|
|
|
|
File = " (File)",
|
|
|
|
Reference = " (Reference)",
|
|
|
|
Folder = " (Folder)",
|
|
|
|
EnumMember = " (EnumMember)",
|
|
|
|
Constant = " ﲀ (Constant)",
|
|
|
|
Struct = " ﳤ (Struct)",
|
|
|
|
Event = " (Event)",
|
|
|
|
Operator = " (Operator)",
|
|
|
|
TypeParameter = " (TypeParameter)",
|
|
|
|
}
|
|
|
|
|
2022-08-20 03:59:04 +00:00
|
|
|
local status, cmp = pcall(require, "cmp")
|
|
|
|
if (not status) then return end
|
|
|
|
|
2022-10-10 12:16:20 +00:00
|
|
|
cmp.setup {
|
2021-09-16 20:54:40 +00:00
|
|
|
completion = {
|
2022-10-10 12:16:20 +00:00
|
|
|
completeopt = 'menuone,noinsert,noselect',
|
2021-09-16 20:54:40 +00:00
|
|
|
},
|
|
|
|
formatting = {
|
|
|
|
format = function(entry, vim_item)
|
|
|
|
-- fancy icons and a name of kind
|
|
|
|
vim_item.kind = lsp_symbols[vim_item.kind]
|
|
|
|
-- set a name for each source
|
|
|
|
vim_item.menu = ({
|
|
|
|
buffer = "[Buffer]",
|
|
|
|
nvim_lsp = "[LSP]",
|
|
|
|
vsnip = "[vSnip]",
|
|
|
|
path = "[Path]",
|
|
|
|
spell = "[Spell]",
|
|
|
|
})[entry.source.name]
|
|
|
|
return vim_item
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
snippet = {
|
|
|
|
expand = function(args)
|
|
|
|
vim.fn["vsnip#anonymous"](args.body)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
mapping = {
|
2022-10-10 12:16:20 +00:00
|
|
|
["<cr>"] = cmp.mapping.confirm({ select = false, behavior = cmp.ConfirmBehavior.Replace }),
|
2022-05-02 05:27:33 +00:00
|
|
|
-- ["<Tab>"] = cmp.mapping.confirm({select = true, behavior = cmp.ConfirmBehavior.Insert}),
|
|
|
|
["<Tab>"] = cmp.mapping(function(fallback)
|
|
|
|
if cmp.visible() then
|
|
|
|
cmp.select_next_item()
|
|
|
|
elseif vim.fn["vsnip#available"](1) == 1 then
|
|
|
|
feedkey("<Plug>(vsnip-expand-or-jump)", "")
|
|
|
|
elseif has_words_before() then
|
|
|
|
cmp.complete()
|
|
|
|
else
|
|
|
|
fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
|
|
|
|
end
|
|
|
|
end, { "i", "s" }),
|
|
|
|
|
|
|
|
["<S-Tab>"] = cmp.mapping(function()
|
|
|
|
if cmp.visible() then
|
|
|
|
cmp.select_prev_item()
|
|
|
|
elseif vim.fn["vsnip#jumpable"](-1) == 1 then
|
|
|
|
feedkey("<Plug>(vsnip-jump-prev)", "")
|
|
|
|
end
|
|
|
|
end, { "i", "s" }),
|
2021-09-16 20:54:40 +00:00
|
|
|
},
|
2021-09-19 12:32:12 +00:00
|
|
|
sources = {
|
2021-09-16 20:54:40 +00:00
|
|
|
{ name = 'vsnip' },
|
2021-09-25 19:11:18 +00:00
|
|
|
{ name = 'nvim_lsp' },
|
2021-09-16 20:54:40 +00:00
|
|
|
{ name = 'path' },
|
2022-10-10 12:16:20 +00:00
|
|
|
{
|
|
|
|
name = 'buffer',
|
|
|
|
keyword_length = 4,
|
2022-02-08 11:55:24 +00:00
|
|
|
keyword_pattern = [[\d\@!\k\k*]],
|
|
|
|
options = {
|
|
|
|
-- keyword_pattern = [[\k\+]]-- for non ascii caracters
|
|
|
|
keyword_pattern = [[\d\@!\k\k*]],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
-- { name = 'spell', keyword_length = 4 },
|
2021-11-01 10:46:37 +00:00
|
|
|
},
|
|
|
|
experimental = {
|
|
|
|
-- I like the new menu better! Nice work hrsh7th
|
|
|
|
native_menu = false,
|
|
|
|
|
|
|
|
-- Let's play with this for a day or two
|
|
|
|
ghost_text = true,
|
|
|
|
},
|
2021-09-16 20:54:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 05:27:33 +00:00
|
|
|
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
|
|
|
|
cmp.setup.cmdline('/', {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = {
|
|
|
|
{ name = 'buffer' }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-05-05 12:15:30 +00:00
|
|
|
cmp.setup.cmdline(':', {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = cmp.config.sources({
|
2022-10-10 12:16:20 +00:00
|
|
|
{ name = 'path' },
|
2022-05-05 12:15:30 +00:00
|
|
|
{ name = 'cmdline' }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-05-02 05:27:33 +00:00
|
|
|
|
2021-09-16 20:54:40 +00:00
|
|
|
vim.cmd [[autocmd FileType TelescopePrompt lua require('cmp').setup.buffer { enabled = false }]]
|
|
|
|
vim.g.vsnip_snippet_dir = '~/.config/nvim/vsnips'
|