Skip to content

Commit f9bf376

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

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
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: 16 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,6 +4332,16 @@ 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 (
4338+
ret.others[exactMatches] !== undefined &&
4339+
ret.others[exactMatches].dist === 0 &&
4340+
ret.others[exactMatches].path_dist === 0
4341+
) {
4342+
exactMatches += 1;
4343+
}
4344+
43314345
// @ts-expect-error
43324346
const pushFunc = alias => {
43334347
alias.alias = query;
@@ -4336,12 +4350,11 @@ class DocSearch {
43364350
alias.fullPath = alias.displayPath + alias.name;
43374351
alias.href = res[1];
43384352

4339-
ret.others.unshift(alias);
4353+
ret.others.splice(exactMatches, 0, alias);
43404354
if (ret.others.length > MAX_RESULTS) {
43414355
ret.others.pop();
43424356
}
43434357
};
4344-
43454358
aliases.forEach((alias, i) => {
43464359
// @ts-expect-error
43474360
alias.desc = descs[i];

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() {}

tests/rustdoc-js/doc-alias.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ const EXPECTED = [
231231
{
232232
'query': 'UnionItem',
233233
'others': [
234+
// normalization actually removes the underscores, so this counts as an exact match.
235+
{
236+
'path': 'doc_alias::Union',
237+
'name': 'union_item',
238+
'desc': 'Doc for <code>Union::union_item</code>',
239+
'href': '../doc_alias/union.Union.html#structfield.union_item'
240+
},
234241
{
235242
'path': 'doc_alias',
236243
'name': 'Union',
@@ -239,13 +246,6 @@ const EXPECTED = [
239246
'href': '../doc_alias/union.Union.html',
240247
'is_alias': true
241248
},
242-
// Not an alias!
243-
{
244-
'path': 'doc_alias::Union',
245-
'name': 'union_item',
246-
'desc': 'Doc for <code>Union::union_item</code>',
247-
'href': '../doc_alias/union.Union.html#structfield.union_item'
248-
},
249249
],
250250
},
251251
{

tests/rustdoc-js/non-english-identifier.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-order
2+
13
const PARSED = [
24
{
35
query: '中文',

0 commit comments

Comments
 (0)