Skip to content

Commit

Permalink
feat(cmp): reimplement comparators for completion entries (ayamir#746)
Browse files Browse the repository at this point in the history
* feat(cmp): update sort logic.

* fixup! feat(cmp): update sort logic

* fixup commit

---------

Co-authored-by: Jint-lzxy <[email protected]>
  • Loading branch information
2 people authored and singlemancombat committed May 16, 2023
1 parent 63ae1b3 commit 4262b3b
Showing 1 changed file with 127 additions and 128 deletions.
255 changes: 127 additions & 128 deletions lua/modules/configs/completion/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ return function()
}
end

local cmp_window = require("cmp.utils.window")

cmp_window.info_ = cmp_window.info
cmp_window.info = function(self)
local info = self:info_()
info.scrollable = false
return info
end

local compare = require("cmp.config.compare")
compare.lsp_scores = function(entry1, entry2)
local diff
Expand All @@ -41,78 +32,86 @@ return function()
return (diff < 0)
end

local function cmp_format(opts)
opts = opts or {}

return function(entry, vim_item)
if opts.before then
vim_item = opts.before(entry, vim_item)
end
local function cmp_format(opts)
opts = opts or {}

local kind_symbol = opts.symbol_map[vim_item.kind] or icons.kind.Undefined
local source_symbol = opts.symbol_map[entry.source.name] or icons.cmp.undefined
return function(entry, vim_item)
if opts.before then
vim_item = opts.before(entry, vim_item)
end

vim_item.menu = " " .. source_symbol .. " |"
vim_item.kind = string.format(" 〔 %s %s 〕", kind_symbol, vim_item.kind)
local kind_symbol = opts.symbol_map[vim_item.kind] or icons.kind.Undefined
local source_symbol = opts.symbol_map[entry.source.name] or icons.cmp.undefined

if opts.maxwidth ~= nil then
if opts.ellipsis_char == nil then
vim_item.abbr = string.sub(vim_item.abbr, 1, opts.maxwidth)
else
local label = vim_item.abbr
local truncated_label = vim.fn.strcharpart(label, 0, opts.maxwidth)
if truncated_label ~= label then
vim_item.abbr = truncated_label .. opts.ellipsis_char
end
end
end
return vim_item
end
end
vim_item.menu = " " .. source_symbol .. " |"
vim_item.kind = string.format(" 〔 %s %s 〕", kind_symbol, vim_item.kind)

local cmp = require("cmp")
if opts.maxwidth ~= nil then
if opts.ellipsis_char == nil then
vim_item.abbr = string.sub(vim_item.abbr, 1, opts.maxwidth)
else
local label = vim_item.abbr
local truncated_label = vim.fn.strcharpart(label, 0, opts.maxwidth)
if truncated_label ~= label then
vim_item.abbr = truncated_label .. opts.ellipsis_char
end
end
end
return vim_item
end
end

cmp.setup({
window = {
completion = {
border = border("Normal"),
max_width = 80,
max_height = 20,
},
documentation = {
border = border("CmpDocBorder"),
},
},
sorting = {
priority_weight = 2,
comparators = {
require("copilot_cmp.comparators").prioritize,
require("copilot_cmp.comparators").score,
-- require("cmp_tabnine.compare"),
compare.offset,
compare.exact,
compare.lsp_scores,
require("cmp-under-comparator").under,
compare.kind,
compare.sort_text,
compare.length,
compare.order,
},
},
formatting = {
fields = { "menu", "abbr", "kind" },
format = function(entry, vim_item)
local kind_map = vim.tbl_deep_extend("force", icons.kind, icons.type, icons.cmp)
local kind = cmp_format({
maxwidth = 50,
symbol_map = kind_map,
})(entry, vim_item)
return kind
end,
},
-- You can set mappings if you want
mapping = cmp.mapping.preset.insert({
["<CR>"] = cmp.mapping.confirm({ select = true, behavior = cmp.ConfirmBehavior.Replace }),
local cmp = require("cmp")
cmp.setup({
preselect = cmp.PreselectMode.Item,
window = {
completion = {
border = border("Normal"),
max_width = 80,
max_height = 20,
scrollbar = false,
},
documentation = {
border = border("CmpDocBorder"),
},
},
sorting = {
priority_weight = 2,
comparators = {
require("copilot_cmp.comparators").prioritize,
require("copilot_cmp.comparators").score,
-- require("cmp_tabnine.compare"),
compare.offset, -- Items closer to cursor will have lower priority
compare.exact,
-- compare.scopes,
compare.lsp_scores,
compare.sort_text,
compare.score,
compare.recently_used,
-- compare.locality, -- Items closer to cursor will have higher priority, conflicts with `offset`
require("cmp-under-comparator").under,
compare.kind,
compare.length,
compare.order,
},
},
formatting = {
fields = { "menu", "abbr", "kind" },
format = function(entry, vim_item)
local kind_map = vim.tbl_deep_extend("force", icons.kind, icons.type, icons.cmp)
local kind = cmp_format({
maxwidth = 50,
symbol_map = kind_map,
})(entry, vim_item)
return kind
end,
},
matching = {
disallow_partial_fuzzy_matching = false,
},
-- You can set mappings if you want
mapping = cmp.mapping.preset.insert({
["<CR>"] = cmp.mapping.confirm({ select = true, behavior = cmp.ConfirmBehavior.Replace }),
["<C-p>"] = cmp.mapping.select_prev_item(),
["<C-n>"] = cmp.mapping.select_next_item(),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
Expand All @@ -122,56 +121,56 @@ return function()
if cmp.visible() then
cmp.select_next_item()
elseif require("luasnip").expand_or_locally_jumpable() then
vim.fn.feedkeys(t("<Plug>luasnip-expand-or-jump"))
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif require("luasnip").jumpable(-1) then
vim.fn.feedkeys(t("<Plug>luasnip-jump-prev"), "")
else
fallback()
end
end, { "i", "s" }),
}),
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
-- You should specify your *installed* sources.
sources = {
{ name = "nvim_lsp" },
{ name = "nvim_lua" },
{ name = "luasnip" },
{ name = "path" },
{
name = "treesitter",
entry_filter = function(entry)
local ignore_list = {
"Error",
"Comment",
}
local kind = entry:get_completion_item().cmp.kind_text
return not vim.tbl_contains(ignore_list, kind)
end,
},
{ name = "spell" },
{ name = "tmux" },
{ name = "orgmode" },
{ name = "buffer" },
{ name = "latex_symbols" },
{ name = "copilot" },
-- { name = "codeium" },
-- { name = "cmp_tabnine" },
},
experimental = {
ghost_text = {
hl_group = "Whitespace",
},
},
})
vim.fn.feedkeys(t("<Plug>luasnip-expand-or-jump"))
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif require("luasnip").jumpable(-1) then
vim.fn.feedkeys(t("<Plug>luasnip-jump-prev"), "")
else
fallback()
end
end, { "i", "s" }),
}),
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
-- You should specify your *installed* sources.
sources = {
{ name = "nvim_lsp", max_item_count = 350 },
{ name = "nvim_lua" },
{ name = "luasnip" },
{ name = "path" },
{
name = "treesitter",
entry_filter = function(entry)
local ignore_list = {
"Error",
"Comment",
}
local kind = entry:get_completion_item().cmp.kind_text
return not vim.tbl_contains(ignore_list, kind)
end,
},
{ name = "spell" },
{ name = "tmux" },
{ name = "orgmode" },
{ name = "buffer" },
{ name = "latex_symbols" },
{ name = "copilot" },
-- { name = "codeium" },
-- { name = "cmp_tabnine" },
},
experimental = {
ghost_text = {
hl_group = "Whitespace",
},
},
})
end

0 comments on commit 4262b3b

Please sign in to comment.