Repository navigation
fix: keep the autocompleter suggestions in the backend's ranked order - #139
Open
bluzername wants to merge 1 commit into
Open
bluzername wants to merge 1 commit into
bluzername wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is wrong
`parse_suggestions` in `src/autocomplete.rs` collect the suggestions into a `BTreeSet`. A `BTreeSet` iterate in sorted order, so the final list is alphabetical, not in the order the backend send it. For an autocompleter box this order IS the ranking, this is the whole point of it: DuckDuckGo, Wikipedia and mwmbl all return the suggestion they think is most relevant first.
Example: backend answer `[{"phrase":"zebra"},{"phrase":"apple"}]`, "zebra" is the top ranked one. Current code give the user `["apple", "zebra"]`, so the best match end up last (or dropped off the end once there is more than 10).
The existing tests do not catch this because by coincidence the two fixtures (`"rust"`/`"rust book"` and `"Rust"`/`"Rust (programming language)"`) are already alphabetical (one is a prefix of the other), so the bug stay invisible.
Fix
Swap the `BTreeSet` for a plain `Vec` plus a `HashSet` used only to drop duplicate, insertion order is kept. `suggestions.truncate(10)` replace `.take(10)` on the old sorted iterator, same limit as before.
Added two tests:
The three existing tests keep passing unchanged (their fixtures happen to be alphabetical already, so they do not distinguish the two behaviors, but they still exercise the same code path).
Checks
I could not run `cargo fmt`/`cargo test`/`cargo clippy` here (no execution access to a fresh clone of someone else's repo in this environment), so I am saying it directly instead of claiming a check that did not happen, like CONTRIBUTING.md ask. I read the diff carefully and traced the logic and the new tests by hand against the exact behavior of `BTreeSet` vs `Vec`/`HashSet`. Please run the normal `cargo fmt --all --check`, `cargo test --all-targets`, `cargo clippy --all-targets -- -D warnings` on CI before merge.