Skip to content

feat: add sort by arbitrary key order#2218

Open
komar007 wants to merge 1 commit intohrsh7th:mainfrom
komar007:enforce_order
Open

feat: add sort by arbitrary key order#2218
komar007 wants to merge 1 commit intohrsh7th:mainfrom
komar007:enforce_order

Conversation

@komar007
Copy link
Copy Markdown

A generic comparator that helps ensure that certain classes of entries are sorted in the order of a given list.

For example, with a function that returns the name of the source LSP:

---@param entry cmp.Entry
---@return string|nil
local function lsp_client_name(entry)
  local source = entry.source and entry.source.source
  local client = source and source.client
  return client and client.config and client.config.name or nil
end

you can ensure that for buffers with multiple LSP connected, the entries from one LSP will always be sorted before entries from another one, using this comparator before others:

local cmp = require('cmp')

cmp.setup({
  sorting = {
    comparators = {
      cmp.config.compare.key_order(lsp_client_name, { 'crates.nvim', 'taplo' }),
      ...
    }
  }
})

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

Successfully merging this pull request may close these issues.

1 participant