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

134 lines
4.1 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 status, cmp = pcall(require, "cmp")
if (not status) then return end
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 = ({
vsnip = "[vSnip]",
nvim_lsp = "[LSP]",
buffer = "[Buffer]",
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 = false, behavior = cmp.ConfirmBehavior.Replace }),
-- ["<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" }),
},
sources = {
{ name = 'vsnip' },
{ name = 'nvim_lsp' },
{ name = 'path' },
{
name = 'buffer',
keyword_length = 4,
keyword_pattern = [[\d\@!\k\k*]],
options = {
-- keyword_pattern = [[\k\+]]-- for non ascii caracters
keyword_pattern = [[\d\@!\k\k*]],
}
},
-- { name = 'spell', keyword_length = 4 },
},
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,
},
}
-- 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' }
}
})
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' },
{ name = 'cmdline' }
})
})
vim.cmd [[autocmd FileType TelescopePrompt lua require('cmp').setup.buffer { enabled = false }]]
vim.g.vsnip_snippet_dir = '~/.config/nvim/vsnips'