stevearc / dressing.nvim

Neovim plugin to improve the default vim.ui interfaces
MIT License
1.73k stars 32 forks source link

Custom sorter based on `kind` for `vim.ui.select()`? #22

Closed mrjones2014 closed 8 months ago

mrjones2014 commented 2 years ago

Is it possible to use a custom sorter if the kind option is a certain value, otherwise use the default sorter?

For example, I want to be able to sort user created items to be before builtin items in my plugin legendary.nvim.

I think this could probably be done via the get_config option of dressing.nvim?

mrjones2014 commented 2 years ago

I can work on a PR for this.

mrjones2014 commented 2 years ago

Hmm, actually I'm not sure this gets me what I want still. I want to be able to use the table properties for sorting, but it seems like telescope only sorts on the strings.

mrjones2014 commented 2 years ago

Maybe we can have items specify their own "ordinal" values, but keeping what you have there as the default.

mrjones2014 commented 2 years ago

I don't think that works either :/

stevearc commented 2 years ago

So you're trying to specifically force a sort ordering for the telescope selector backend? In my tests the items (at least initially) are sorted by the order they're passed in. This is using https://github.com/stevearc/dressing.nvim/blob/master/tests/manual/select.lua

Are you seeing a different initial order? Or are you trying to sort it differently after the user starts typing?

If you do need a different sort function, it should be straightforward to add that to the config options.

mrjones2014 commented 2 years ago

I'm trying to sort after the user starts typing. For example here, I'd like the 2nd option to appear first in the list:

CleanShot-2022-03-14-at-15 19 35

The 2nd item is one I've added custom. The other 2 items are "builtins" that are added by my plugin by default. I was users' custom entries to appear before the "builtin" entries, does that make sense?

So basically I want default fuzzy sort (based on closeness to search term), then by a custom sort, if that makes sense?

stevearc commented 2 years ago

Yep, that makes sense! The first step would be to figure out how you would even do this within Telescope. I think that the way to do that would be to override the sorter?

If you get a custom sorter that works, we can make a change to the telescope backend that allows the user to pass in a custom sorter. This line https://github.com/stevearc/dressing.nvim/blob/b36b69c6a5d6b30b16782be22674d6d037dc87e3/lua/dressing/select/telescope.lua#L36 would change to something like

sorter = config.sorter or conf.generic_sorter(opts),

Note that this would still require the user to provide this custom sorter in their config

require('dressing').setup({
  select = {
    get_config = function(opts)
      if opts.kind == 'legendary' then
        return {
          telescope = {
            sorter = require('legendary').custom_telescope_sorter(),
          }
        }
      end
    end,
  }
})
mrjones2014 commented 2 years ago

Yup I'm definitely aware that pretty much no matter way, it would require some user config. I'm going to document it in my README.md with an example config for dressing.nvim (since that's what I use, and from what I can tell, what most people who use my plugin use).

As far as creating a custom sorter, I tried starting down that route, but I couldn't figure out how to get the actual item reference in the sorter (I want to sort by one of the properties of the table, not the resulting formatted string that gets shown in the actual Telescope UI). Do you have any insight or tips there? ๐Ÿ˜…

stevearc commented 2 years ago

That's more of a question for the telescope repo. I don't know off the top of my head, and while I could probably figure it out given enough time, it's probably faster to just ask the people that actually built it ๐Ÿ˜›

If you can get a minimal telescope example working, I'll definitely help figure out how to translate that to dressing

mrjones2014 commented 2 years ago

Cool, I'll spend some more time digging into it and come back here, thanks!

stevearc commented 8 months ago

Based on https://github.com/mrjones2014/legendary.nvim/pull/219, it looks like this was resolved

mrjones2014 commented 8 months ago

Yup I think weโ€™re all good here, thanks