dotfiles/modules/editor/neovim/lua/packer/completion.lua

122 lines
4.4 KiB
Lua
Raw Normal View History

2022-07-09 21:27:04 +00:00
-- =======================================================================
-- Completion System
-- =======================================================================
local M = {}
M.packer = function(use)
-- Completion sources
use("hrsh7th/cmp-nvim-lsp") --- Language server completion plugin
use("hrsh7th/cmp-buffer") --- Generic text completion
use("hrsh7th/cmp-path") --- Local file completion
use("hrsh7th/cmp-cmdline") --- Command line completion
use("hrsh7th/cmp-nvim-lua") --- Nvim lua api completion
use("saadparwaiz1/cmp_luasnip") --- Luasnip completion
use("lukas-reineke/cmp-rg") --- Ripgrep completion
-- Completion engine
use({
"hrsh7th/nvim-cmp",
requires = { "L3MON4D3/LuaSnip" },
config = function()
local cmp = require("cmp")
cmp.setup({
2022-07-11 02:47:54 +00:00
-- Setup snippet completion
2022-07-09 21:27:04 +00:00
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
2022-07-11 02:47:54 +00:00
-- Setup completion keybinds
2022-07-09 21:27:04 +00:00
mapping = {
["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
["<Esc>"] = function(fallback)
cmp.mapping({
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
})
vim.cmd("stopinsert") --- Abort and leave insert mode
end,
["<CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Insert,
select = true,
}),
["<C-r>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}),
["<C-l>"] = cmp.mapping(function(fallback)
if require("luasnip").expand_or_jumpable() then
require("luasnip").expand_or_jump()
end
end, { "i", "s" }),
},
2022-07-11 02:47:54 +00:00
-- Setup completion engines
2022-07-09 21:27:04 +00:00
sources = {
{ name = "nvim_lua" },
{ name = "nvim_lsp" },
{ name = "luasnip" },
2022-07-11 02:47:54 +00:00
{ name = "path" },
2022-07-09 21:27:04 +00:00
{ name = "buffer", keyword_length = 3, max_item_count = 10 },
{
name = "rg",
keyword_length = 6,
max_item_count = 10,
option = { additional_arguments = "--ignore-case" },
},
},
2022-07-11 02:47:54 +00:00
-- Visual presentation
formatting = {
fields = { "abbr", "menu" },
format = function(entry, vim_item)
vim_item.menu = ({
luasnip = "[Snippet]",
buffer = "[Buffer]",
path = "[Path]",
rg = "[Grep]",
nvim_lsp = "[LSP]",
nvim_lua = "[Lua]",
})[entry.source.name]
return vim_item
end,
},
-- Docs
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
-- Extra features
2022-07-09 21:27:04 +00:00
experimental = {
native_menu = false, --- Use cmp menu instead of Vim menu
ghost_text = true, --- Show preview auto-completion
},
})
-- Use buffer source for `/`
cmp.setup.cmdline("/", {
sources = {
{ name = "buffer", keyword_length = 5 },
},
})
-- Use cmdline & path source for ':'
cmp.setup.cmdline(":", {
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
})
end,
})
end
return M