2024-02-21 04:09:18 +00:00
|
|
|
return {
|
|
|
|
"hrsh7th/nvim-cmp",
|
|
|
|
dependencies = {
|
|
|
|
-- Adds LSP completion capabilities
|
|
|
|
"hrsh7th/cmp-buffer",
|
|
|
|
"hrsh7th/cmp-cmdline",
|
|
|
|
"hrsh7th/cmp-nvim-lsp",
|
|
|
|
"hrsh7th/cmp-path",
|
|
|
|
"neovim/nvim-lspconfig",
|
|
|
|
|
|
|
|
-- Snippets
|
|
|
|
"L3MON4D3/LuaSnip", -- snippet engine
|
|
|
|
"rafamadriz/friendly-snippets", -- pre-configured snippet texts
|
|
|
|
"saadparwaiz1/cmp_luasnip", -- nvim-cmp source
|
|
|
|
},
|
2024-02-23 17:24:08 +00:00
|
|
|
event = { "InsertEnter", "CmdlineEnter" },
|
|
|
|
opts = function()
|
2024-02-21 04:09:18 +00:00
|
|
|
local cmp = require("cmp")
|
|
|
|
local luasnip = require("luasnip")
|
|
|
|
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
|
|
|
|
|
|
|
|
require("mason").setup()
|
|
|
|
require("luasnip.loaders.from_vscode").lazy_load()
|
|
|
|
|
2024-02-23 17:24:08 +00:00
|
|
|
cmp.setup({
|
|
|
|
-- keybinds
|
|
|
|
-- see: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings
|
|
|
|
mapping = {
|
2024-02-21 04:09:18 +00:00
|
|
|
["<Tab>"] = cmp.mapping(function(fallback)
|
|
|
|
if cmp.visible() then
|
|
|
|
cmp.select_next_item()
|
|
|
|
elseif luasnip.expand_or_jumpable() then
|
|
|
|
luasnip.expand_or_jump()
|
|
|
|
elseif has_words_before() then
|
|
|
|
cmp.complete()
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end, { "i", "s" }),
|
2024-02-23 17:24:08 +00:00
|
|
|
|
2024-02-21 04:09:18 +00:00
|
|
|
["<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" }),
|
2024-02-24 06:31:50 +00:00
|
|
|
|
2024-02-26 18:42:26 +00:00
|
|
|
["<CR>"] = cmp.mapping.confirm(),
|
|
|
|
["<Up>"] = cmp.mapping.select_prev_item({ behavior = "select" }),
|
|
|
|
["<Down>"] = cmp.mapping.select_next_item({ behavior = "select" }),
|
2024-02-23 17:24:08 +00:00
|
|
|
},
|
2024-02-21 04:09:18 +00:00
|
|
|
|
|
|
|
-- snippet engine
|
|
|
|
snippet = {
|
|
|
|
expand = function(args)
|
|
|
|
require("luasnip").lsp_expand(args.body)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
|
2024-02-23 17:24:08 +00:00
|
|
|
-- completion source
|
2024-02-21 04:09:18 +00:00
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = "nvim_lsp" },
|
|
|
|
{ name = "luasnip" },
|
|
|
|
{ name = "buffer" },
|
|
|
|
{ name = "path" },
|
|
|
|
}),
|
|
|
|
})
|
2024-02-23 17:24:08 +00:00
|
|
|
|
|
|
|
-- Use buffer source for `/` and `?`
|
|
|
|
cmp.setup.cmdline({ "/", "?" }, {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = {
|
|
|
|
{ name = "buffer" },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
-- Use cmdline & path source for ':'
|
|
|
|
cmp.setup.cmdline(":", {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = "path" },
|
|
|
|
{ name = "cmdline" },
|
|
|
|
}),
|
|
|
|
})
|
2024-02-21 04:09:18 +00:00
|
|
|
end,
|
|
|
|
}
|