diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 0d2e19e019f34..a889a85f4f052 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -129,7 +129,7 @@ declare namespace rustdoc { /** * A single parsed "atom" in a search query. For example, - * + * * std::fmt::Formatter, Write -> Result<()> * ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐ * ┃ │ ┗ QueryElement { ┊ @@ -264,6 +264,7 @@ declare namespace rustdoc { displayTypeSignature: Promise | null, item: Row, dontValidate?: boolean, + alias?: string, } /** diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index dce5fddb3177e..a013136076ff8 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -4273,7 +4273,11 @@ class DocSearch { }; } - // @ts-expect-error + /** + * Handle searching by doc aliases + * + * @type {function(rustdoc.ResultsTable, string, Object=, Object=): Promise} + */ const handleAliases = async(ret, query, filterCrates, currentCrate) => { const lowerQuery = query.toLowerCase(); // We separate aliases and crate aliases because we want to have current crate @@ -4328,6 +4332,16 @@ class DocSearch { Promise.all(aliases.map(fetchDesc)), ]); + // if there are any existing results that match exactly, those go before aliases. + let exactMatches = 0; + while ( + ret.others[exactMatches] !== undefined && + ret.others[exactMatches].dist === 0 && + ret.others[exactMatches].path_dist === 0 + ) { + exactMatches += 1; + } + // @ts-expect-error const pushFunc = alias => { alias.alias = query; @@ -4336,12 +4350,11 @@ class DocSearch { alias.fullPath = alias.displayPath + alias.name; alias.href = res[1]; - ret.others.unshift(alias); + ret.others.splice(exactMatches, 0, alias); if (ret.others.length > MAX_RESULTS) { ret.others.pop(); } }; - aliases.forEach((alias, i) => { // @ts-expect-error alias.desc = descs[i]; diff --git a/tests/rustdoc-js/alias-sort.js b/tests/rustdoc-js/alias-sort.js new file mode 100644 index 0000000000000..245ff416643c7 --- /dev/null +++ b/tests/rustdoc-js/alias-sort.js @@ -0,0 +1,10 @@ +// rank doc aliases lower than exact matches +// regression test for + +const EXPECTED = { + 'query': 'foobazbar', + 'others': [ + { 'name': 'foobazbar' }, + { 'name': 'foo' }, + ], +}; diff --git a/tests/rustdoc-js/alias-sort.rs b/tests/rustdoc-js/alias-sort.rs new file mode 100644 index 0000000000000..542bb4be4387e --- /dev/null +++ b/tests/rustdoc-js/alias-sort.rs @@ -0,0 +1,5 @@ +/// asdf +pub fn foobazbar() {} + +#[doc(alias("foobazbar"))] +pub fn foo() {} diff --git a/tests/rustdoc-js/doc-alias.js b/tests/rustdoc-js/doc-alias.js index e57bd71419dcd..ddac7bb682814 100644 --- a/tests/rustdoc-js/doc-alias.js +++ b/tests/rustdoc-js/doc-alias.js @@ -231,6 +231,13 @@ const EXPECTED = [ { 'query': 'UnionItem', 'others': [ + // normalization actually removes the underscores, so this counts as an exact match. + { + 'path': 'doc_alias::Union', + 'name': 'union_item', + 'desc': 'Doc for Union::union_item', + 'href': '../doc_alias/union.Union.html#structfield.union_item' + }, { 'path': 'doc_alias', 'name': 'Union', @@ -239,13 +246,6 @@ const EXPECTED = [ 'href': '../doc_alias/union.Union.html', 'is_alias': true }, - // Not an alias! - { - 'path': 'doc_alias::Union', - 'name': 'union_item', - 'desc': 'Doc for Union::union_item', - 'href': '../doc_alias/union.Union.html#structfield.union_item' - }, ], }, { diff --git a/tests/rustdoc-js/non-english-identifier.js b/tests/rustdoc-js/non-english-identifier.js index f2180b4c75530..1de273d6ae67f 100644 --- a/tests/rustdoc-js/non-english-identifier.js +++ b/tests/rustdoc-js/non-english-identifier.js @@ -1,3 +1,5 @@ +// ignore-order + const PARSED = [ { query: '中文',