Repository navigation
Set match_indices for fuzzy-matched suggestions - #15887
Conversation
796975c to
1a090d1
Compare
Use the latest commit of Reedline on the `main` branch so we can use [#798](nushell/reedline#798) (Add match_indices field to Suggestion) <!-- Thank you for improving Nushell! Please, read our contributing guide: https://github.com/nushell/nushell/blob/main/CONTRIBUTING.md --> ## Release notes summary - What our users need to know <!-- This section will be included as part of our release notes. See the contributing guide for more details. Please include only details relevant for users here. Motivation and technical details can be added above or below this section. You may leave this section blank until your PR is finalized. Ask a core team member if you need help filling this section. --> N/A ## Tasks after submitting <!-- Remove any tasks which aren't relevant for your PR, or add your own --> - [ ] Make use of the new Suggestion field in #15887
| let haystack = haystack.trim_matches(QUOTES); | ||
| fn matches_aux(&mut self, orig_haystack: &str, item: Option<T>) -> bool { | ||
| let haystack = orig_haystack.trim_start_matches(QUOTES); | ||
| let offset = orig_haystack.len() - haystack.len(); |
There was a problem hiding this comment.
Since we get offset here, maybe we should use it for non-fuzzy situations as well?
It may require reverting the quote trimming on the reedline side. If so, maybe we can mark some TODOs somewhere.
There was a problem hiding this comment.
Hmm, how would we use it for non-fuzzy situations? Right now, I'm only using it for changing the offset of match indices (which we only provide for fuzzy matching)
Related: I just realized this uses .len(), which looks at bytes, whereas the match indices use graphemes. I'll change this.
Mildly related: for case-insensitive completions, we lowercase the needle and haystack. On the Reedline side, we then use the number of graphemes in the needle/typed text to figure out the match indices, but this could cause problems if the needle is ss and the haystack/value is the character ß. We should handle this in the future
There was a problem hiding this comment.
Hmm, how would we use it for non-fuzzy situations? Right now, I'm only using it for changing the offset of match indices (which we only provide for fuzzy matching)
I mean when items are quoted, we could use indices to specify the actual location of matched text.
I assume it's handled by some kind of substring matching in reedline currently?
Related: I just realized this uses
.len(), which looks at bytes, whereas the match indices use graphemes. I'll change this.
len() should be fine here, the delta is only meant for quotes, all ascii here.
There was a problem hiding this comment.
I mean when items are quoted, we could use indices to specify the actual location of matched text.
I assume it's handled by some kind of substring matching in reedline currently?
It is indeed handled in Reedline, but I guess we could specify indices right here, like you said, to avoid recomputing the match location on the Reedline side.
len()should be fine here, the delta is only meant for quotes, all ascii here.
Ooh yeah, missed that. That's good.
There was a problem hiding this comment.
It is indeed handled in Reedline, but I guess we could specify indices right here, like you said, to avoid recomputing the match location on the Reedline side.
Maybe there's some legacy code to cleanup for that, You can leave that for future PRs with a TODO note somewhere.
| pub fn suggestion_results(self) -> Vec<SemanticSuggestion> { | ||
| self.results() | ||
| .into_iter() | ||
| .map(|(mut sugg, indices)| { | ||
| sugg.suggestion.match_indices = indices; | ||
| sugg | ||
| }) | ||
| .collect() | ||
| } |
There was a problem hiding this comment.
This seems counterintuitive to me, why we don't just write the indices to the results during add_aux or something?
There was a problem hiding this comment.
Oh the matcher is meant to be generic.
I think we should solve this using something like trait specialization (maybe the wrong terminology of rust, basically the idea of polymorphism in OOP)
There was a problem hiding this comment.
I don't know, that feels like overkill to me. I was originally thinking about having the results method accept a function that took an item and added the match indices to it, but it was just easier to use tuples. matcher.results() is only used in a handful of places.
I may be misunderstanding what exactly you mean by trait specialization though, could you give me an example of what you mean?
There was a problem hiding this comment.
I may be misunderstanding what exactly you mean by trait specialization though, could you give me an example of what you mean?
I mean here:
nushell/crates/nu-cli/src/completions/completion_options.rs
Lines 222 to 227 in 08f6b6a
We override the generic matches_aux for specialized type SemanticSuggestion, which in turn calls the generic matches_aux then sets indices in-place for each item.
There was a problem hiding this comment.
I've made a commit on my forked branch to move in that direction.
You can cherry-pick it if it makes sense to you.
There was a problem hiding this comment.
One major benefit is that we don't need to store the indices outside of SemanticSuggestion, so we can do the same for prefix/substring algos as I commented above.
My target is to remove the weird quote trimming (nushell syntax specific) on the reedline side.
BTW, match indices are uncompressed (to ranges), will it cause extra ansi rendering in reedline?
There was a problem hiding this comment.
Sorry I didn't get around to this, been busy with other stuff.
One major benefit is that we don't need to store the indices outside of SemanticSuggestion, so we can do the same for prefix/substring algos as I commented above.
Yeah that is nice.
My target is to remove the weird quote trimming (nushell syntax specific) on the reedline side.
We should be able to do that once we add a display_value field to Suggestion, since that field wouldn't need to have quotes/escaping
BTW, match indices are uncompressed (to ranges), will it cause extra ansi rendering in reedline?
No that's handled on the Reedline side.
There was a problem hiding this comment.
No that's handled on the Reedline side.
I mean, will it add an ansi reset for each matching character? It could be shared within a range.
There was a problem hiding this comment.
Yes, that's what I meant. Consecutive highlighted graphemes get treated as one group. There's no resets, and there's only one escape at the start
There was a problem hiding this comment.
Yes, that's what I meant. Consecutive highlighted graphemes get treated as one group. There's no resets, and there's only one escape at the start
Cool, thanks for the explanation.
sholderbach
left a comment
There was a problem hiding this comment.
Any outstanding comments @blindFS ?
From my browsing this looks great, and will get us one step closer to the fuzzy goal: "Be as good as fish with completions" :)
|
@blindFS That commit of yours makes a lot of changes, so I didn't cherry-pick it, but I stole heavily from it and just pushed a commit that:
I don't want to widen the scope of this PR, so I'd rather merge it and then work on figuring out the less necessary stuff later. |
| struct UnscoredMatch<T> { | ||
| item: T, | ||
| haystack: String, | ||
| match_indices: Vec<usize>, | ||
| } |
There was a problem hiding this comment.
I still don't get the point of storing the indices outside of SemanticSuggestion. It brings extra overhead of vector collection during suggestion_results.
But maybe you find it more future proof for the display_value thing?
BTW, do my tests pass with this change? Should we include them?
There was a problem hiding this comment.
I still don't get the point of storing the indices outside of SemanticSuggestion.
I'd rather not, but I wanted this to also work when the matcher holds Strings or FileSuggestions or whatever, because those types don't have their own match_indices fields. We can redesign it right after this PR. I think we'll have to (directly or indirectly) make NuMatcher parameterized on two types: an input item type (like String/SemanticSuggestion) and an output item type that contains match indices (like (String, Vec<usize>)/SemanticSuggestion).
It's not for display_value, that's unrelated.
It brings extra overhead of vector collection during suggestion_results.
True, but iterating over the items once and just setting a field for each suggestion shouldn't be that slow. The matching itself probably takes much longer.
BTW, do my tests pass with this change? Should we include them?
Just pushed a commit with them, thanks!
|
@sholderbach Let me know if you're cool with merging. @blindFS signed off on it |
Requires nushell/reedline#798, which adds a
match_indicesfield toSuggestionthat allows completers to specify exactly which graphemes in the suggestion matched the typed text. This PR takes advantage of this field to accurately underline fuzzily-matched suggestions.Note that file/directory/dotnu completions don't currently make use of the
match_indicesfield. Once we have a way to have separate display values, this will be much easier to implement.Release notes summary - What our users need to know
Users will see sensible highlighting for suggestions when using fuzzy matching, nothing else.