Skip to content

Commit

Permalink
rustdoc-search: case-sensitive only when capitals are used
Browse files Browse the repository at this point in the history
This is the "smartcase" behavior, described by vim and dtolnay.

(cherry picked from commit 32500aa)
  • Loading branch information
notriddle authored and cuviper committed Nov 15, 2024
1 parent 80f109a commit 71ed455
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,7 @@ class DocSearch {
const sortResults = async(results, isType, preferredCrate) => {
const userQuery = parsedQuery.userQuery;
const casedUserQuery = parsedQuery.original;
const isMixedCase = casedUserQuery !== userQuery;
const result_list = [];
for (const result of results.values()) {
result.item = this.searchIndex[result.id];
Expand All @@ -2109,10 +2110,12 @@ class DocSearch {
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;
if (isMixedCase) {
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)
Expand Down
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;

0 comments on commit 71ed455

Please sign in to comment.