nixos-configuration/users/jrpotter/neovim/config/utils/cmp.lua

94 lines
2.3 KiB
Lua
Raw Normal View History

2023-11-18 23:08:50 +00:00
local M = {}
2024-03-05 03:10:57 +00:00
local cmp = require("cmp")
local cmp_buffer = require("cmp_buffer")
local luasnip = require("luasnip")
local types = require("cmp.types")
2023-11-18 23:08:50 +00:00
function M.setup()
cmp.setup {
2023-11-18 23:41:39 +00:00
snippet = {
expand = function(args)
2024-03-05 03:10:57 +00:00
require("luasnip").lsp_expand(args.body)
2023-11-18 23:41:39 +00:00
end,
},
2023-11-18 23:08:50 +00:00
sources = {
2024-03-05 03:10:57 +00:00
{ name = "luasnip", option = { show_autosnippets = true } },
{ name = "nvim_lsp" },
2023-11-18 23:08:50 +00:00
{
2024-03-05 03:10:57 +00:00
name = "buffer",
2023-11-18 23:08:50 +00:00
option = {
-- Complete only on visible buffers.
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
},
},
},
sorting = {
comparators = {
-- Prioritize snippets over other sources.
function(entry1, entry2)
local kind1 = entry1:get_kind()
local kind2 = entry2:get_kind()
if kind1 ~= kind2 then
if kind1 == types.lsp.CompletionItemKind.Snippet then
return true
end
if kind2 == types.lsp.CompletionItemKind.Snippet then
return false
end
end
end,
2024-01-04 19:43:56 +00:00
function(...)
-- This also sorts completion results coming from other sources.
2023-11-18 23:08:50 +00:00
return cmp_buffer:compare_locality(...)
end,
},
},
mapping = {
2024-03-05 03:10:57 +00:00
["<tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.confirm({ select = true })
else
fallback()
end
2024-03-05 03:10:57 +00:00
end, { "i", "s" }),
2023-11-18 23:08:50 +00:00
2024-03-05 03:10:57 +00:00
["<c-l>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.abort()
else
fallback()
end
2024-03-05 03:10:57 +00:00
end, { "i", "s" }),
2023-11-18 23:08:50 +00:00
2024-03-05 03:10:57 +00:00
["<c-n>"] = cmp.mapping(function(fallback)
2024-01-04 19:43:56 +00:00
if cmp.visible() then
cmp.select_next_item()
2023-11-18 23:41:39 +00:00
elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
2024-03-05 03:10:57 +00:00
end, { "i", "s" }),
2023-11-18 23:41:39 +00:00
2024-03-05 03:10:57 +00:00
["<c-p>"] = cmp.mapping(function(fallback)
2024-01-04 19:43:56 +00:00
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
2023-11-18 23:41:39 +00:00
luasnip.jump(-1)
else
fallback()
end
2024-03-05 03:10:57 +00:00
end, { "i", "s" }),
2023-11-18 23:08:50 +00:00
},
}
end
return M