dotfiles/nvim/.config/nvim/lua/completion.lua

99 lines
3.2 KiB
Lua

local has_words_before = function()
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
end
local feedkey = function(key, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
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)",
}
local cmp = require'cmp'
cmp.setup{
completion = {
completeopt = "menuone,noinsert,noselect",
},
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 = {
-- ["<cr>"] = cmp.mapping.confirm({select = true, behavior = cmp.ConfirmBehavior.Replace}),
["<cr>"] = cmp.mapping.confirm({select = true, behavior = cmp.ConfirmBehavior.Insert}),
["<Tab>"] = cmp.mapping(function(fallback)
if vim.fn.pumvisible() == 1 then
feedkey("<C-n>", "n")
elseif vim.fn["vsnip#available"]() == 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 vim.fn.pumvisible() == 1 then
feedkey("<C-p>", "n")
elseif vim.fn["vsnip#jumpable"](-1) == 1 then
feedkey("<Plug>(vsnip-jump-prev)", "")
end
end, { "i", "s" }),
},
source = {
{ name = 'buffer' },
{ name = 'vsnip' },
{ name = 'path' },
{ name = 'spell' },
}
}
vim.cmd [[autocmd FileType TelescopePrompt lua require('cmp').setup.buffer { enabled = false }]]
vim.g.vsnip_snippet_dir = '~/.config/nvim/vsnips'