Skip to content

Commit

Permalink
Rollup merge of rust-lang#129430 - lolbinarycat:rustdoc-search-exact-…
Browse files Browse the repository at this point in the history
…case, r=notriddle

rustdoc: show exact case-sensitive matches first

fixes rust-lang#119480
  • Loading branch information
workingjubilee authored Aug 24, 2024
2 parents 15a7043 + 4c5e888 commit 25a3c88
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,7 @@ function initSearch(rawSearchIndex) {
*/
async function sortResults(results, isType, preferredCrate) {
const userQuery = parsedQuery.userQuery;
const casedUserQuery = parsedQuery.original;
const result_list = [];
for (const result of results.values()) {
result.item = searchIndex[result.id];
Expand All @@ -1403,6 +1404,13 @@ function initSearch(rawSearchIndex) {
result_list.sort((aaa, bbb) => {
let a, b;

// sort by exact case-sensitive match
a = (aaa.item.name !== casedUserQuery);
b = (bbb.item.name !== casedUserQuery);
if (a !== b) {
return a - b;
}

// sort by exact match with regard to the last word (mismatch goes later)
a = (aaa.word !== userQuery);
b = (bbb.word !== userQuery);
Expand Down
7 changes: 7 additions & 0 deletions tests/rustdoc-js-std/exact-case.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const EXPECTED = {
'query': 'Copy',
'others': [
{ 'path': 'std::marker', 'name': 'Copy' },
{ 'path': 'std::fs', 'name': 'copy' },
],
}

0 comments on commit 25a3c88

Please sign in to comment.