Better cmp sorting and binds

This commit is contained in:
Natty 2024-02-10 16:32:15 +01:00
parent fec4cc7b9e
commit f4442b6d61
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
1 changed files with 67 additions and 1 deletions

View File

@ -162,7 +162,52 @@ return {{
-- opts parameter is the default options table -- opts parameter is the default options table
-- the function is lazy loaded so cmp is able to be required -- the function is lazy loaded so cmp is able to be required
local lspkind = require("lspkind") local lspkind = require("lspkind")
-- modify the sources part of the options table
local cmp = require("cmp")
-- Completion sources
opts.sources = cmp.config.sources({
{ name = "crates", priority = 1250 },
{ name = "nvim_lsp", priority = 1000 },
{ name = "luasnip", priority = 750 },
{ name = "buffer", priority = 500 },
{ name = "path", priority = 250 },
})
-- Custom comparator to sort LSP text and snippets last
local types = require("cmp.types")
local function compareLspKind(entry1, entry2)
local kind1 = entry1:get_kind() --- @type lsp.CompletionItemKind | number
local kind2 = entry2:get_kind() --- @type lsp.CompletionItemKind | number
kind1 = kind1 == types.lsp.CompletionItemKind.Text and 100 or kind1
kind2 = kind2 == types.lsp.CompletionItemKind.Text and 100 or kind2
kind1 = kind1 == types.lsp.CompletionItemKind.Snippet and 90 or kind1
kind2 = kind2 == types.lsp.CompletionItemKind.Snippet and 90 or kind2
if kind1 ~= kind2 then
local diff = kind1 - kind2
if diff < 0 then
return true
elseif diff > 0 then
return false
end
end
return nil
end
-- Custom comparators for sorting of options
local compare = require('cmp.config.compare')
opts.sorting = {
priority_weight = 2,
comparators = {
compare.offset,
compare.exact,
compare.score,
compare.recently_used,
compareLspKind,
compare.sort_text,
compare.length,
compare.order,
},
}
-- Make the cmp prompt pretty -- Make the cmp prompt pretty
-- How to customize: https://docs.astronvim.com/recipes/cmp/ -- How to customize: https://docs.astronvim.com/recipes/cmp/
@ -182,6 +227,27 @@ return {{
end, end,
} }
-- Use PageUp and PageDown to navigate through completions
local luasnip = require("luasnip")
opts.mapping["<PageDown>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item({ count = 5 })
elseif luasnip.jumpable(5) then
luasnip.jump(5)
else
fallback()
end
end, { "i", "s" })
opts.mapping["<PageUp>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item({ count = 5 })
elseif luasnip.jumpable(-5) then
luasnip.jump(-5)
else
fallback()
end
end, { "i", "s" })
-- return the new table to be used -- return the new table to be used
return opts return opts
end, end,