Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting of output #11

Open
hinell opened this issue Aug 17, 2023 · 2 comments
Open

Sorting of output #11

hinell opened this issue Aug 17, 2023 · 2 comments

Comments

@hinell
Copy link

hinell commented Aug 17, 2023

There is a way to write sorting functions for nvim-cmp.

Checkout examples:

Please, add info on caching. Thanks!

@David-Kunz
Copy link
Owner

Hi @hinell ,

Thanks, would you be willing to open a PR?

@RayGuo-ergou
Copy link

I have tried to implement this with cmp.comparators, toke some code from #6 (comment)

And because it filters by filename so should have minimal impact to cmp overall.

  ---@param entry1 cmp.Entry
  ---@param entry2 cmp.Entry
  local function package_json_npm(entry1, entry2)
    local filename = vim.fn.expand('%:t')
    if filename == 'package.json' then
      local source1 = entry1.source.name
      local source2 = entry2.source.name

      -- make source npm has higher priority
      if source1 == 'npm' and source2 ~= 'npm' then
        return true
      end

      if source1 ~= 'npm' and source2 == 'npm' then
        return false
      end

      -- if both source are npm, sort by version
      if source1 == 'npm' and source2 == 'npm' then
        local label1 = entry1.completion_item.label
        local label2 = entry2.completion_item.label
        local major1, minor1, patch1 = string.match(label1, '(%d+)%.(%d+)%.(%d+)')
        local major2, minor2, patch2 = string.match(label2, '(%d+)%.(%d+)%.(%d+)')
        if major1 ~= major2 then
          return tonumber(major1) > tonumber(major2)
        end
        if minor1 ~= minor2 then
          return tonumber(minor1) > tonumber(minor2)
        end
        if patch1 ~= patch2 then
          return tonumber(patch1) > tonumber(patch2)
        end
      end
    end

    return false
  end

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants