Skip to content
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

rustdoc-search: case-sensitive only when capitals are used #133043

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
@@ -2500,6 +2500,7 @@ class DocSearch {
const sortResults = async(results, typeInfo, preferredCrate) => {
const userQuery = parsedQuery.userQuery;
const normalizedUserQuery = parsedQuery.userQuery.toLowerCase();
const isMixedCase = normalizedUserQuery !== userQuery;
const result_list = [];
for (const result of results.values()) {
result.item = this.searchIndex[result.id];
@@ -2511,10 +2512,12 @@ class DocSearch {
let a, b;

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

// sort by exact match with regard to the last word (mismatch goes later)
24 changes: 24 additions & 0 deletions tests/rustdoc-js-std/write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const EXPECTED = [
{
'query': 'write',
'others': [
{ 'path': 'std::fmt', 'name': 'write' },
{ 'path': 'std::fs', 'name': 'write' },
{ 'path': 'std::ptr', 'name': 'write' },
{ 'path': 'std::fmt', 'name': 'Write' },
{ 'path': 'std::io', 'name': 'Write' },
{ 'path': 'std::hash::Hasher', 'name': 'write' },
],
},
{
'query': 'Write',
'others': [
{ 'path': 'std::fmt', 'name': 'Write' },
{ 'path': 'std::io', 'name': 'Write' },
{ 'path': 'std::fmt', 'name': 'write' },
{ 'path': 'std::fs', 'name': 'write' },
{ 'path': 'std::ptr', 'name': 'write' },
{ 'path': 'std::hash::Hasher', 'name': 'write' },
],
},
];
17 changes: 17 additions & 0 deletions tests/rustdoc-js/case.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const EXPECTED = [
{
'query': 'Foo',
'others': [
{ 'path': 'case', 'name': 'Foo', 'desc': 'Docs for Foo' },
{ 'path': 'case', 'name': 'foo', 'desc': 'Docs for foo' },
],
},
{
'query': 'foo',
'others': [
// https://github.com/rust-lang/rust/issues/133017
{ 'path': 'case', 'name': 'Foo', 'desc': 'Docs for Foo' },
{ 'path': 'case', 'name': 'foo', 'desc': 'Docs for foo' },
],
},
];
7 changes: 7 additions & 0 deletions tests/rustdoc-js/case.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![allow(nonstandard_style)]

/// Docs for Foo
pub struct Foo;

/// Docs for foo
pub struct foo;
Loading