cmp-npm
cmp-npm copied to clipboard
two digit minor version shown after one digit, sorting wrong
When I try to complete a package that has a two-digit version number (higher than 9), it shows me the single digit minor versions first and then the higher ones after them. This happens multiple times for every two-digit version in major, minor and patch.
See:
https://user-images.githubusercontent.com/8818340/190223103-add4e3e0-5027-4b6a-8609-6264fd3de99a.mov
I found your comment here, which probably references this? https://github.com/David-Kunz/cmp-npm/blob/4b6166c3feeaf8dae162e33ee319dc5880e44a29/lua/cmp-npm/init.lua#L65
Can't we have a custom sorter for different files / sources? Can I configure something like this in cmp directly maybe?
Hi @uloco ,
Thanks for reporting this issue.
Unfortunately, nvim-cmp doesn't allow custom sorting functions for the source, hence two-digit versions appear last.
On a given result set, I try to properly sort by semantic versions, but that's already too late.
I'm afraid there's nothing I can do here, but you might open a feature request on nvim-cmp.
Best regards, David
Looks like there is a way to have custom comparators, see: https://github.com/hrsh7th/nvim-cmp/blob/913eb8599816b0b71fe959693080917d8063b26a/doc/cmp.txt#L491
and looks like this plugin adds a new comparator for underline prefixes https://github.com/lukas-reineke/cmp-under-comparator
Hi @uloco ,
Thanks for the pointer. But this will only set the sorting globally, no? That would be too much of a change for this tiny plugin.
But you can use my sorting function and add it (with slight adjustments) as a global sorter:
table.sort(items, function(a,b)
local a_major,a_minor,a_patch = string.match(a.label, '(%d+)%.(%d+)%.(%d+)')
local b_major,b_minor,b_patch = string.match(b.label, '(%d+)%.(%d+)%.(%d+)')
if a_major ~= b_major then return tonumber(a_major) > tonumber(b_major) end
if a_minor ~= b_minor then return tonumber(a_minor) > tonumber(b_minor) end
if a_patch ~= b_patch then return tonumber(a_patch) > tonumber(b_patch) end
end)
Thanks, I'll give it a shot when I come back to it :)
@David-Kunz did this get resolved or did you just close it?
Hi @uloco ,
Sorry, I should've written a comment. Since there's no way to solve this issue on cmp-npm side, I closed this issue.
Best regards, David