Skip to content

Set match_indices for fuzzy-matched suggestions - #15887

Merged
ysthakur merged 10 commits into
nushell:mainfrom
ysthakur:fuzzy-underline
Nov 9, 2025
Merged

ysthakur merged 10 commits into
nushell:mainfrom
ysthakur:fuzzy-underline

Conversation

@ysthakur

@ysthakur ysthakur commented Jun 4, 2025 •

Copy link
Copy Markdown
Member

Requires nushell/reedline#798, which adds a match_indices field to Suggestion that 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.

image

Note that file/directory/dotnu completions don't currently make use of the match_indices field. 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.

@ysthakur ysthakur added A:completions Issues related to tab completion quality-of-life labels Jun 4, 2025
@ysthakur
ysthakur marked this pull request as draft June 4, 2025 02:48
@ysthakur ysthakur mentioned this pull request Oct 22, 2025
1 task
sholderbach pushed a commit that referenced this pull request Oct 22, 2025
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
@ysthakur
ysthakur marked this pull request as ready for review October 25, 2025 20:44
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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +245 to +253
pub fn suggestion_results(self) -> Vec<SemanticSuggestion> {
self.results()
.into_iter()
.map(|(mut sugg, indices)| {
sugg.suggestion.match_indices = indices;
sugg
})
.collect()
}

@blindFS blindFS Oct 26, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems counterintuitive to me, why we don't just write the indices to the results during add_aux or something?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

impl NuMatcher<'_, SemanticSuggestion> {
pub fn add_semantic_suggestion(&mut self, sugg: SemanticSuggestion) -> bool {
let value = sugg.suggestion.value.to_string();
self.add(value, sugg)
}
}

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@ysthakur ysthakur Nov 5, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ysthakur ysthakur Nov 5, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

blindFS commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

Any outstanding comments @blindFS ?

I'd like to see this merged in this PR.

But I'm fine to land this first, then let you guys review that separately in another PR. That's a bit heavy after all.

@ysthakur

ysthakur commented Nov 6, 2025

Copy link
Copy Markdown
Member Author

@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:

  • provides match_indices with NuMatcher::check_match (previously named NuMatcher::matches)
  • makes sure that match indices are set even with custom completers that choose not to sort suggestions
  • also sets match indices when using prefix or substring matching

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.

Comment on lines +50 to +54
struct UnscoredMatch<T> {
item: T,
haystack: String,
match_indices: Vec<usize>,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

https://github.com/blindFS/nushell/blob/10f995a942d10fe52ed36f35a15df9027971da6a/crates/nu-cli/tests/completions/mod.rs#L2941-L2990

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ysthakur I see, LGTM now. Thanks!

@ysthakur

ysthakur commented Nov 7, 2025

Copy link
Copy Markdown
Member Author

@sholderbach Let me know if you're cool with merging. @blindFS signed off on it

@ysthakur
ysthakur merged commit 5b4ed82 into nushell:main Nov 9, 2025
@ysthakur
ysthakur deleted the fuzzy-underline branch November 9, 2025 07:16
@github-actions github-actions Bot added this to the v0.109.0 milestone Nov 9, 2025
@WindSoilder WindSoilder added the notes:other Noted in "Other changes" section label Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:completions Issues related to tab completion notes:other Noted in "Other changes" section quality-of-life

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants