dotfiles/vim/lua/nvim_cmp_config.lua

93 lines
2.5 KiB
Lua
Raw Normal View History

2023-09-19 05:21:43 +00:00
local cmp = require("cmp")
2023-10-04 03:12:14 +00:00
-- https://github.com/zbirenbaum/copilot-cmp#tab-completion-configuration-highly-recommended
2023-10-01 06:10:27 +00:00
local has_words_before = function()
2023-10-04 03:12:14 +00:00
if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then
return false
end
2023-10-01 06:10:27 +00:00
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
2023-10-04 03:12:14 +00:00
return col ~= 0 and vim.api.nvim_buf_get_text(0, line - 1, 0, line - 1, col, {})[1]:match("^%s*$") == nil
2023-10-01 06:10:27 +00:00
end
cmp.setup({
2023-09-19 05:21:43 +00:00
completion = {
2023-10-01 06:10:27 +00:00
completeopt = "menu,menuone,noinsert,noselect",
2023-09-19 05:21:43 +00:00
},
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
2023-09-21 14:00:41 +00:00
preselect = cmp.PreselectMode.None,
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
2023-09-19 05:21:43 +00:00
mapping = cmp.mapping.preset.insert({
["<C-u>"] = cmp.mapping.scroll_docs(-4),
["<C-d>"] = cmp.mapping.scroll_docs(4),
["<C-e>"] = cmp.mapping.close(),
["C-y"] = cmp.mapping.confirm({ select = true, behaviour = cmp.ConfirmBehavior.Replace }),
2023-09-22 12:26:13 +00:00
-- https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#super-tab-like-mapping
["<C-Space>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Insert,
select = true,
}),
2023-10-04 03:12:14 +00:00
["<Tab>"] = vim.schedule_wrap(function(fallback)
if cmp.visible() and has_words_before() then
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
else
fallback()
2023-09-22 12:26:13 +00:00
end
2023-10-04 03:12:14 +00:00
end),
2023-09-22 12:26:13 +00:00
["<S-Tab>"] = function(fallback)
if not cmp.select_prev_item() then
if vim.bo.buftype ~= "prompt" and has_words_before() then
cmp.complete()
else
fallback()
end
end
end,
2023-09-19 05:21:43 +00:00
}),
sources = cmp.config.sources({
2023-10-31 02:18:30 +00:00
{ name = "copilot" },
{ name = "nvim_lsp" },
2023-09-19 05:21:43 +00:00
{
name = "buffer",
option = {
get_bufnrs = function()
return vim.api.nvim_list_bufs()
end,
},
},
2023-10-22 05:55:18 +00:00
-- { name = "tmux", option = { all_panes = true } },
2023-09-19 05:21:43 +00:00
{ name = "path" },
{ name = "calc" },
}),
sorting = {
comparators = {
2023-09-21 14:00:41 +00:00
cmp.config.compare.exact,
2023-10-04 03:27:52 +00:00
require("copilot_cmp.comparators").prioritize,
cmp.config.compare.offset,
2023-09-21 14:00:41 +00:00
cmp.config.compare.score,
cmp.config.compare.recently_used,
2023-10-04 03:27:52 +00:00
cmp.config.compare.locality,
2023-09-21 14:00:41 +00:00
cmp.config.compare.kind,
2023-10-04 03:27:52 +00:00
cmp.config.compare.sort_text,
cmp.config.compare.length,
cmp.config.compare.order,
2023-09-19 05:21:43 +00:00
},
},
2023-10-04 03:27:52 +00:00
-- https://github.com/hrsh7th/nvim-cmp/wiki/Advanced-techniques#disabling-completion-in-certain-contexts-such-as-comments
enabled = function()
return true
2023-10-04 03:27:52 +00:00
end,
2023-09-21 14:00:41 +00:00
formatting = {
fields = { "kind", "abbr", "menu" },
2023-09-19 05:21:43 +00:00
},
experimental = {
ghost_text = true,
},
})