-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Better sorting in picker in case of ties #5169
Conversation
Previous work for context: #4698 Thanks for taking the time to contribute! I like the general idea of sorting file picker entries alphabetically. I think the same result can be better achieved as follows: When creating the picker we could then sort all items before passing them to Furthermore this keeps the picker order a bit more consistent. |
makes makes sense, ptal! |
b397f18
to
9267e3d
Compare
helix-term/src/ui/picker.rs
Outdated
len: usize, | ||
} | ||
|
||
impl PickerMatch { | ||
fn key(&self) -> impl Ord { | ||
(cmp::Reverse(self.score), self.index, self.len) |
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.
Interestingly, this also fixes the Eq
impl -- previously the derived impl would disagree with the Ord
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.
I suppose that was a small oversight in the previous implementation. We probably should have returned None
in case of equality to be consistent with PartialEq
so this is definitly more consistent 👍
helix-term/src/ui/picker.rs
Outdated
len: usize, | ||
} | ||
|
||
impl PickerMatch { | ||
fn key(&self) -> impl Ord { | ||
(cmp::Reverse(self.score), self.index, self.len) |
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.
(cmp::Reverse(self.score), self.index, self.len) | |
(cmp::Reverse(self.score), self.len, self.index) |
I believe len
and index
must be swapped here.
Otherwise the length tiebreaker would have no effect (as the index is always unique)
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.
🤔 do we need a len at all then? as in, I'd expect
aaaaac
bc
order perhaps?
Or maybe not -- I can see the appeal of starting with shorter entitiees... Let's go for a minimal diff for the start
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.
Fixed (good thing that rust-analyzer has "flip comma" code action, hehe)
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.
Sorting by length is useful because it usually shows you what you want first.
For example foo/bar
matches foo/bar.rs
before foo/bar/baz.rs
this way.
See #4698 and #4688 for context. Fzf
shares the same behavior by default and I tested this a bit and it's quite useful in practice.
Note that we only apply this sorting once something is typed into the query.
I think the following example illustrates the intended behaviour.
Consider the initial list of files (sorted alphabetically):
aaaaac
ac
bc
Once you type ac
the following is shown:
ac
aaaaac
Should be good now! I briefly entertained thee idea of adding |
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.
LGTM, I think this is pretty uncontroversial because it only acts as a tiebreaker (for what was previously an unstable sort).
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.
Thanks!
TIL about "flip comma", that's handy 😄
users not knowing what assists are available is a hard problem. I myself only remeber things I've added personally :D Some thoughts on how to address that: #2011 (comment) |
Before:
After:
Wouldn't be opposed to writing a test here, but, given that this doesn't fail any existing test, also happy to let it pass :P