Skip to content

Commit 7960a64

Browse files
notriddlecuviper
authored andcommitted
rustdoc-search: case-sensitive only when capitals are used
This is the "smartcase" behavior, described by vim and dtolnay. (cherry picked from commit 32500aa)
1 parent 80f109a commit 7960a64

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

src/librustdoc/html/static/js/search.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,8 @@ class DocSearch {
20972097
*/
20982098
const sortResults = async(results, isType, preferredCrate) => {
20992099
const userQuery = parsedQuery.userQuery;
2100-
const casedUserQuery = parsedQuery.original;
2100+
const normalizedUserQuery = parsedQuery.userQuery.toLowerCase();
2101+
const isMixedCase = normalizedUserQuery !== userQuery;
21012102
const result_list = [];
21022103
for (const result of results.values()) {
21032104
result.item = this.searchIndex[result.id];
@@ -2109,15 +2110,17 @@ class DocSearch {
21092110
let a, b;
21102111

21112112
// sort by exact case-sensitive match
2112-
a = (aaa.item.name !== casedUserQuery);
2113-
b = (bbb.item.name !== casedUserQuery);
2114-
if (a !== b) {
2115-
return a - b;
2113+
if (isMixedCase) {
2114+
a = (aaa.item.name !== userQuery);
2115+
b = (bbb.item.name !== userQuery);
2116+
if (a !== b) {
2117+
return a - b;
2118+
}
21162119
}
21172120

21182121
// sort by exact match with regard to the last word (mismatch goes later)
2119-
a = (aaa.word !== userQuery);
2120-
b = (bbb.word !== userQuery);
2122+
a = (aaa.word !== normalizedUserQuery);
2123+
b = (bbb.word !== normalizedUserQuery);
21212124
if (a !== b) {
21222125
return a - b;
21232126
}

tests/rustdoc-js-std/write.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const EXPECTED = [
2+
{
3+
'query': 'write',
4+
'others': [
5+
{ 'path': 'std::fmt', 'name': 'write' },
6+
{ 'path': 'std::fs', 'name': 'write' },
7+
{ 'path': 'std::ptr', 'name': 'write' },
8+
{ 'path': 'std::fmt', 'name': 'Write' },
9+
{ 'path': 'std::io', 'name': 'Write' },
10+
{ 'path': 'std::hash::Hasher', 'name': 'write' },
11+
],
12+
},
13+
{
14+
'query': 'Write',
15+
'others': [
16+
{ 'path': 'std::fmt', 'name': 'Write' },
17+
{ 'path': 'std::io', 'name': 'Write' },
18+
{ 'path': 'std::fmt', 'name': 'write' },
19+
{ 'path': 'std::fs', 'name': 'write' },
20+
{ 'path': 'std::ptr', 'name': 'write' },
21+
{ 'path': 'std::hash::Hasher', 'name': 'write' },
22+
],
23+
},
24+
];

tests/rustdoc-js/case.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const EXPECTED = [
2+
{
3+
'query': 'Foo',
4+
'others': [
5+
{ 'path': 'case', 'name': 'Foo', 'desc': 'Docs for Foo' },
6+
{ 'path': 'case', 'name': 'foo', 'desc': 'Docs for foo' },
7+
],
8+
},
9+
{
10+
'query': 'foo',
11+
'others': [
12+
// https://github.com/rust-lang/rust/issues/133017
13+
{ 'path': 'case', 'name': 'Foo', 'desc': 'Docs for Foo' },
14+
{ 'path': 'case', 'name': 'foo', 'desc': 'Docs for foo' },
15+
],
16+
},
17+
];

tests/rustdoc-js/case.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![allow(nonstandard_style)]
2+
3+
/// Docs for Foo
4+
pub struct Foo;
5+
6+
/// Docs for foo
7+
pub struct foo;

0 commit comments

Comments
 (0)