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

165 lines
5.9 KiB
Lua
Raw Permalink 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
2022-07-13 02:15:00 +00:00
use("rafamadriz/friendly-snippets") -- Lots of pre-generated snippets
2022-07-09 21:27:04 +00:00
-- Completion engine
use({
"hrsh7th/nvim-cmp",
requires = { "L3MON4D3/LuaSnip" },
config = function()
local cmp = require("cmp")
2022-07-13 02:15:00 +00:00
local kind_icons = {
Text = "",
Method = "m",
Function = "",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = "",
}
2022-07-09 21:27:04 +00:00
cmp.setup({
2022-07-11 02:47:54 +00:00
2022-07-13 04:23:38 +00:00
-- Only enable on non-prompt buffers
-- So don't use in telescope
enabled = function()
if vim.bo.buftype == "prompt" then
return false
end
return true
end,
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 = {
2022-07-13 02:15:00 +00:00
["<C-n>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }),
["<C-p>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }),
2022-07-09 21:27:04 +00:00
["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
2022-07-13 04:23:38 +00:00
["<C-e>"] = cmp.mapping(cmp.mapping.abort(), { "i", "c" }),
2022-07-13 02:15:00 +00:00
["<Esc>"] = function(_)
2022-07-09 21:27:04 +00:00
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,
}),
2022-07-13 02:15:00 +00:00
["<C-l>"] = cmp.mapping(function(_)
2022-07-09 21:27:04 +00:00
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 = {
2022-07-13 02:15:00 +00:00
fields = { "kind", "abbr", "menu" },
2022-07-11 02:47:54 +00:00
format = function(entry, vim_item)
2022-07-13 02:15:00 +00:00
vim_item.kind = string.format("%s", kind_icons[vim_item.kind])
2022-07-11 02:47:54 +00:00
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
2022-07-13 02:15:00 +00:00
-- window = {
-- completion = cmp.config.window.bordered(),
-- documentation = cmp.config.window.bordered(),
-- },
2022-07-11 02:47:54 +00:00
-- 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