-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
edit/completion: add sorter #642
Conversation
Signed-off-by: Tw <tw19881113@gmail.com>
} | ||
|
||
func sortRawCandidates(ev *eval.Evaler, customSorter eval.Callable, seed string, rs []rawCandidate) ([]rawCandidate, error) { | ||
// default sorter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I prefer to remove this special case, but seems it is extremely fast compared to the following.
I have concerns over this approach. The proposed directory directory sorting involves calling In addition, the |
why cant implicit sorting be disabled and sorting routine just would be explicitly implemented in each completer? is there really a need for another layer? |
@occivink Thanks for your concerns. The proposed directory sorting just a example, i think stat() calls is necessary. For |
@SolitudeSF , For now we just use the default sorter to sort the candidates, anyway we could add a option to disable sorting entirely. What do you think @xiaq ? And the sorting routine could be set per completer like the matcher. |
Thank you, @tw4452852, but I share @occivink's concern. @SolitudeSF, decoupling the sorter from the completer is useful. For instance, you can customize the ordering of filename completions without touching the filename completer. The example you have given is an extreme case, but in general, if you can make do with calling an Elvish function O(1) times you should not call it O(n) times. You can usually rely on the pipeline for communication to reduce the number of calls; the matcher API is an example of how this principle is applied. Instead of calling the matcher for each candidate, it calls the matcher once and feeds the text of all candidates using the pipeline. Granted, to implement such a matcher in Elvish, it's likely that you still need to use In our earlier discussion in the user group we have outlined two possible approaches to the sorter API:
I have just thought of a variant of the second approach that can work, though, and it seems to be the best so far. The sorter still outputs a "score" for each candidate, but it doesn't have to be a numeric score. It can be anything, and the candidates are sorted according to the "score". For instance, if you want candidates to be sorted first by whether they are directories and then by the name, you can use this sorter:
There is one problem with this approach: Elvish doesn't support the notion of comparing, nor sorting anything other than strings and numbers. You can implement something specifically for the sorter of course. For instance, instead of relying on the default list type, you can implement a special
Another open question is how to distinguish numerical ordering and lexicographical ordering (Elvish doesn't have separate number types; #456), but that is also relatively easy if you have a dedicated All these said, the completion pipeline still lacks something to make the sorter API more useful. In particular, you almost always want a specific sorter to only apply to a certain "type" of candidates. We do have a notion of "completion type" now, which includes But the
This can be solved by making the "completion type" part of the candidate structure. Right now it is a property of the whole completion. I hope I have made my vision clear. If you (still) want to tackle it, I recommend that you start with the last alternative of the sorter API I suggested and implement some version of |
@xiaq Ping? This is now almost two years old and seems unlikely to be merged. If it is still an interesting idea perhaps it should be converted into an enhancement issue. Should this PR be closed as rejected? |
Thank you, @krader1961. I have filed #915 for the feature and am closing this PR. |
Just got inspiration from sort package, user can setup a comparion function (like the
Less
method in sort),and its definition is:
func isBefore (seed, a, b string) bool
, its result determines whether candidatea
should be placed before candidateb
.For issue #580 , a simple directory first sorter would be:
[seed a b]{ put ?(test -d $a) }
, you can also create any sophisticated sorter for sure.BTW, now custom sort is a little slow, I will try to figure out the potention reason and improve it.
Thanks.