From f4442b6d61d38fa045d409ad8a3b6e2c64186193 Mon Sep 17 00:00:00 2001 From: Natty Date: Sat, 10 Feb 2024 16:32:15 +0100 Subject: [PATCH] Better cmp sorting and binds --- plugins.lua | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/plugins.lua b/plugins.lua index 31d586a..c976f58 100644 --- a/plugins.lua +++ b/plugins.lua @@ -162,7 +162,52 @@ return {{ -- opts parameter is the default options table -- the function is lazy loaded so cmp is able to be required 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 -- How to customize: https://docs.astronvim.com/recipes/cmp/ @@ -182,6 +227,27 @@ return {{ end, } + -- Use PageUp and PageDown to navigate through completions + local luasnip = require("luasnip") + opts.mapping[""] = 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[""] = 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 opts end,