|
|
|
local present, cmp = pcall(require, "cmp")
|
|
|
|
|
|
|
|
if not present then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
require("base46").load_highlight "cmp"
|
|
|
|
|
|
|
|
vim.opt.completeopt = "menuone,noselect"
|
|
|
|
|
|
|
|
local function border(hl_name)
|
|
|
|
return {
|
|
|
|
{ "╭", hl_name },
|
|
|
|
{ "─", hl_name },
|
|
|
|
{ "╮", hl_name },
|
|
|
|
{ "│", hl_name },
|
|
|
|
{ "╯", hl_name },
|
|
|
|
{ "─", hl_name },
|
|
|
|
{ "╰", hl_name },
|
|
|
|
{ "│", hl_name },
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local cmp_window = require "cmp.utils.window"
|
|
|
|
|
|
|
|
local has_words_before = function()
|
|
|
|
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
|
|
|
|
|
|
|
|
cmp_window.info_ = cmp_window.info
|
|
|
|
cmp_window.info = function(self)
|
|
|
|
local info = self:info_()
|
|
|
|
info.scrollable = false
|
|
|
|
return info
|
|
|
|
end
|
|
|
|
|
|
|
|
local options = {
|
|
|
|
-- preselect = cmp.PreselectMode.None,
|
|
|
|
window = {
|
|
|
|
completion = {
|
|
|
|
border = border "CmpBorder",
|
|
|
|
winhighlight = "Normal:CmpPmenu,CursorLine:PmenuSel,Search:None",
|
|
|
|
},
|
|
|
|
documentation = {
|
|
|
|
border = border "CmpDocBorder",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
snippet = {
|
|
|
|
expand = function(args)
|
|
|
|
require("luasnip").lsp_expand(args.body)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
formatting = {
|
|
|
|
format = function(entry, vim_item)
|
|
|
|
local icons = require("nvchad_ui.icons").lspkind
|
|
|
|
vim_item.kind = string.format("%s %s", icons[vim_item.kind], vim_item.kind)
|
|
|
|
-- show source
|
|
|
|
vim_item.menu = ({
|
|
|
|
buffer = ":buf",
|
|
|
|
nvim_lsp = ":lsp",
|
|
|
|
luasnip = ":LuaS",
|
|
|
|
nvim_lua = ":lua",
|
|
|
|
latex_symbols = ":ltx",
|
|
|
|
})[entry.source.name]
|
|
|
|
return vim_item
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
mapping = {
|
|
|
|
["<C-p>"] = cmp.mapping.select_prev_item(),
|
|
|
|
["<C-n>"] = cmp.mapping.select_next_item(),
|
|
|
|
["<C-d>"] = cmp.mapping.scroll_docs(-4),
|
|
|
|
["<C-f>"] = cmp.mapping.scroll_docs(4),
|
|
|
|
-- Try to emulate native neovim C-x completion style behavior
|
|
|
|
["<C-e><C-e>"] = cmp.mapping.complete(),
|
|
|
|
["<C-c>"] = cmp.mapping.close(),
|
|
|
|
["<C-e>"] = cmp.mapping.abort(),
|
|
|
|
["<C-y>"] = cmp.mapping.confirm {
|
|
|
|
behavior = cmp.ConfirmBehavior.Replace,
|
|
|
|
select = false,
|
|
|
|
},
|
|
|
|
["<CR>"] = cmp.mapping.confirm {
|
|
|
|
behavior = cmp.ConfirmBehavior.Replace,
|
|
|
|
select = false,
|
|
|
|
},
|
|
|
|
|
|
|
|
-- luasnip forward jump
|
|
|
|
["<C-k>"] = cmp.mapping(function(fallback)
|
|
|
|
local luasnip = require("luasnip")
|
|
|
|
if luasnip.jumpable(1) then
|
|
|
|
luasnip.jump(1)
|
|
|
|
elseif luasnip.expandable() then
|
|
|
|
luasnip.expand_or_jump()
|
|
|
|
elseif has_words_before() then
|
|
|
|
cmp.confirm()
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end, {
|
|
|
|
"i",
|
|
|
|
"s",
|
|
|
|
}),
|
|
|
|
-- luasnip reverse jump
|
|
|
|
["<C-j>"] = cmp.mapping(function(fallback)
|
|
|
|
local luasnip = require("luasnip")
|
|
|
|
if luasnip.jumpable(-1) then
|
|
|
|
luasnip.jump(-1)
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end, {
|
|
|
|
"i",
|
|
|
|
"s",
|
|
|
|
}),
|
|
|
|
["<Tab>"] = cmp.mapping(function(fallback)
|
|
|
|
local luasnip = require("luasnip")
|
|
|
|
if cmp.visible() then
|
|
|
|
cmp.select_next_item()
|
|
|
|
elseif luasnip.expand_or_jumpable() then
|
|
|
|
luasnip.expand_or_jump()
|
|
|
|
elseif has_words_before() then
|
|
|
|
cmp.confirm()
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end, {
|
|
|
|
"i",
|
|
|
|
"s",
|
|
|
|
}),
|
|
|
|
["<S-Tab>"] = cmp.mapping(function(fallback)
|
|
|
|
local luasnip = require("luasnip")
|
|
|
|
if cmp.visible() then
|
|
|
|
cmp.select_prev_item()
|
|
|
|
elseif luasnip.jumpable(-1) then
|
|
|
|
luasnip.jump(-1)
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end, {
|
|
|
|
"i",
|
|
|
|
"s",
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
sources = {
|
|
|
|
{ name = "luasnip", priority = 1000 },
|
|
|
|
{ name = "nvim_lsp", priority = 800 },
|
|
|
|
{
|
|
|
|
name = "buffer",
|
|
|
|
option = {
|
|
|
|
get_bufnrs = function()
|
|
|
|
local bufs = {}
|
|
|
|
for _, win in ipairs(vim.api.nvim_list_wins()) do
|
|
|
|
bufs[vim.api.nvim_win_get_buf(win)] = true
|
|
|
|
end
|
|
|
|
return vim.tbl_keys(bufs)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ name = "nvim_lua", priority = 900 },
|
|
|
|
{ name = "path" },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
-- check for any override
|
|
|
|
options = require("core.utils").load_override(options, "hrsh7th/nvim-cmp")
|
|
|
|
|
|
|
|
cmp.setup(options)
|