Skip to content

Commit cbf25eb

Browse files
committed
rustdoc search: rank aliases lower than exact matches
1 parent 3bc767e commit cbf25eb

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ declare namespace rustdoc {
129129

130130
/**
131131
* A single parsed "atom" in a search query. For example,
132-
*
132+
*
133133
* std::fmt::Formatter, Write -> Result<()>
134134
* ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
135135
* ┃ │ ┗ QueryElement { ┊
@@ -264,6 +264,7 @@ declare namespace rustdoc {
264264
displayTypeSignature: Promise<rustdoc.DisplayTypeSignature> | null,
265265
item: Row,
266266
dontValidate?: boolean,
267+
alias?: string,
267268
}
268269

269270
/**

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,7 +4273,11 @@ class DocSearch {
42734273
};
42744274
}
42754275

4276-
// @ts-expect-error
4276+
/**
4277+
* Handle searching by doc aliases
4278+
*
4279+
* @type {function(rustdoc.ResultsTable, string, Object=, Object=): Promise<void>}
4280+
*/
42774281
const handleAliases = async(ret, query, filterCrates, currentCrate) => {
42784282
const lowerQuery = query.toLowerCase();
42794283
// We separate aliases and crate aliases because we want to have current crate
@@ -4328,15 +4332,20 @@ class DocSearch {
43284332
Promise.all(aliases.map(fetchDesc)),
43294333
]);
43304334

4335+
// if there are any existing results that match exactly, those go before aliases.
4336+
let exactMatches = 0;
4337+
while (ret.others[exactMatches] !== undefined && ret.others[exactMatches].dist === 0) {
4338+
exactMatches += 1;
4339+
}
4340+
43314341
// @ts-expect-error
43324342
const pushFunc = alias => {
43334343
alias.alias = query;
43344344
const res = buildHrefAndPath(alias);
43354345
alias.displayPath = pathSplitter(res[0]);
43364346
alias.fullPath = alias.displayPath + alias.name;
43374347
alias.href = res[1];
4338-
4339-
ret.others.unshift(alias);
4348+
ret.others.splice(exactMatches, 0, alias);
43404349
if (ret.others.length > MAX_RESULTS) {
43414350
ret.others.pop();
43424351
}

tests/rustdoc-js/alias-sort.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rank doc aliases lower than exact matches
2+
// regression test for <https://github.com/rust-lang/rust/issues/140968>
3+
4+
const EXPECTED = {
5+
'query': 'foobazbar',
6+
'others': [
7+
{ 'name': 'foobazbar' },
8+
{ 'name': 'foo' },
9+
],
10+
};

tests/rustdoc-js/alias-sort.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// asdf
2+
pub fn foobazbar() {}
3+
4+
#[doc(alias("foobazbar"))]
5+
pub fn foo() {}

0 commit comments

Comments
 (0)