neovimconfig/lua/plugin/nvim-cmp.lua
2023-12-09 15:46:50 +01:00

196 lines
8.2 KiB
Lua

return {
-- snippets
{
"L3MON4D3/LuaSnip",
lazy = true,
dependencies = {
"rafamadriz/friendly-snippets",
config = function()
require("luasnip.loaders.from_vscode").lazy_load()
vim.api.nvim_set_hl(0, "PmenuSel", { fg = 'NONE', bg = '#141617' })
vim.api.nvim_set_hl(0, "Pmenu", { fg = 'NONE', bg = '#1d2021' })
vim.api.nvim_set_hl(0, "CmpItemMenu", { fg = 'NONE', bg = '#141617' })
vim.api.nvim_set_hl(0, "CmpItemMenuDefault", { fg = 'NONE', bg = '#141617' })
vim.api.nvim_set_hl(0, "CmpItemKindFunction", { fg = '#d3869b', bg = 'NONE', italic = true })
vim.api.nvim_set_hl(0, "CmpItemKindSnippet", { fg = '#d8a657', bg = 'NONE', italic = true })
vim.api.nvim_set_hl(0, "CmpItemKindText", { fg = '#ddc7a1', bg = 'NONE', italic = true })
vim.api.nvim_set_hl(0, "CmpItemKindVariable", { fg = '#7daea3', bg = 'NONE', italic = true })
end,
},
},
-- completion
{
"hrsh7th/nvim-cmp",
version = false,
event = {
-- "InsertEnter",
-- "CmdlineEnter",
"BufWinEnter",
},
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-cmdline",
"saadparwaiz1/cmp_luasnip",
},
opts = function()
-- Set up nvim-cmp with luasnip
local kind_icons = {
Text = "",
Method = "",
Function = "",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = ""
}
local has_words_before = function()
unpack = unpack or table.unpack
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 luasnip = require("luasnip")
local cmp = require("cmp")
return {
formatting = {
format = function(entry, vim_item)
-- Kind icons
vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind) -- This concatonates the icons with the name of the item kind
-- Source
vim_item.menu = ({
-- buffer = "[Buffer]",
-- nvim_lsp = "[LSP]",
-- luasnip = "[LuaSnip]",
-- nvim_lua = "[Lua]",
-- latex_symbols = "[LaTeX]",
buffer = "",
nvim_lsp = "",
luasnip = "",
nvim_lua = "",
latex_symbols = "",
})[entry.source.name]
return vim_item
end
},
snippet = {
-- REQUIRED - you must specify a snippet engine
expand = function(args)
-- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
end,
},
-- completion = {
-- autocomplete = false,
-- },
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
-- ['<C-Space>'] = cmp.mapping.complete(),
-- ['<C-e>'] = cmp.mapping.abort(),
['<C-e>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.abort()
else
cmp.complete()
end
end),
['<CR>'] = cmp.mapping.confirm({ select = false }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- they way you will only jump inside the snippet region
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
-- { name = 'vsnip' }, -- For vsnip users.
{ name = 'luasnip' }, -- For luasnip users.
-- { name = 'ultisnips' }, -- For ultisnips users.
-- { name = 'snippy' }, -- For snippy users.
{
name = 'buffer',
option = {
get_bufnrs = function()
return vim.api.nvim_list_bufs()
end
},
},
}, {
}),
-- Set configuration for specific filetype.
cmp.setup.filetype('gitcommit', {
sources = cmp.config.sources({
{ name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it.
}, {
{ name = 'buffer' },
})
}),
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' }
}
}),
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
})
}),
}
end,
}
}