minad / corfu

:desert_island: corfu.el - COmpletion in Region FUnction
GNU General Public License v3.0
1.15k stars 43 forks source link

Possible to sort completion candidates by string length? #280

Closed kings2u closed 1 year ago

kings2u commented 1 year ago

I’m sorry to put this as an issue, it’s really a question. I’m wanting to sort corfu candidates by string length, from shortest to longest. By default, I know corfu does sort by string length, but it groups the candidates and restarts the sort several times in a long list of candidates (e.g., the cape-symbol capf) depending on several factors. If I type "corf", Corfu first groups and sorts the symbol candidates that begin with "corfu" from shortest to longest string, and then towards the end of the list Corfu begins to sort a different group of symbols: those containing "corfu" later in the string, not the beginning. I was hoping to override this so that all candidates are sorted from shortest to longest, no matter where "corfu" appears in the symbol.

Thank you for all you do. I just sponsored :)

minad commented 1 year ago

Thanks! As you figured out, Corfu additionally groups by prefix. The candidates which start with the input as prefix are moved at the beginning. I think this feels somewhat natural since completion is usually also triggered starting with a prefix. Some backends may even return only candidates starting with the prefix. Not every backend supports the full flexibility of Orderless. You can disable this by overriding corfu--move-prefix-candidates-to-front.

(advice-add #'corfu--move-prefix-candidates-to-front :override (lambda (_ x) x))

I think that's good enough. But there has been a proposal before to make this configurable. See also https://github.com/minad/corfu/pull/83. Please let me know what you think.

kings2u commented 1 year ago

That did the trick! Thank you so much :)