Skip to content

Commit c3171d1

Browse files
authored
Rollup merge of #147701 - lolbinarycat:rustdoc-search-alias-fix, r=GuillaumeGomez
rustdoc: don't ignore path distance for doc aliases Ran into a bit of an issue due to the overloading of space (it needs to be a metachar for most searches, but not for doc aliases that have space in them). Not sure if I need to also need to account for other whitespace chars. <img width="1778" height="494" alt="screenshot" src="https://github.com/user-attachments/assets/041e76f1-3b29-4de5-a72b-1431021fb676" /> fixes #146214 r? `@GuillaumeGomez`
2 parents 0186755 + 3ba87c4 commit c3171d1

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3926,16 +3926,25 @@ class DocSearch {
39263926
* @returns {Promise<rustdoc.PlainResultObject?>}
39273927
*/
39283928
const handleAlias = async(name, alias, dist, index) => {
3929+
const item = nonnull(await this.getRow(alias, false));
3930+
// space both is an alias for ::,
3931+
// and is also allowed to appear in doc alias names
3932+
const path_dist = name.includes(" ") || parsedQuery.elems.length === 0 ?
3933+
0 : checkRowPath(parsedQuery.elems[0].pathWithoutLast, item);
3934+
// path distance exceeds max, omit alias from results
3935+
if (path_dist === null) {
3936+
return null;
3937+
}
39293938
return {
39303939
id: alias,
39313940
dist,
3932-
path_dist: 0,
3941+
path_dist,
39333942
index,
39343943
alias: name,
39353944
is_alias: true,
39363945
elems: [], // only used in type-based queries
39373946
returned: [], // only used in type-based queries
3938-
item: nonnull(await this.getRow(alias, false)),
3947+
item,
39393948
};
39403949
};
39413950
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// exact-check
2+
3+
// consider path distance for doc aliases
4+
// regression test for <https://github.com/rust-lang/rust/issues/146214>
5+
6+
const EXPECTED = [
7+
{
8+
'query': 'Foo::zzz',
9+
'others': [
10+
{ 'path': 'alias_path_distance::Foo', 'name': 'baz' },
11+
],
12+
},
13+
{
14+
'query': '"Foo::zzz"',
15+
'others': [
16+
{ 'path': 'alias_path_distance::Foo', 'name': 'baz' },
17+
],
18+
},
19+
{
20+
'query': 'Foo::zzzz',
21+
'others': [
22+
{ 'path': 'alias_path_distance::Foo', 'name': 'baz' },
23+
],
24+
},
25+
{
26+
'query': 'zzzz',
27+
'others': [
28+
{ 'path': 'alias_path_distance::Foo', 'name': 'baz' },
29+
{ 'path': 'alias_path_distance::Bar', 'name': 'baz' },
30+
],
31+
},
32+
];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![crate_name = "alias_path_distance"]
2+
3+
pub struct Foo;
4+
pub struct Bar;
5+
6+
impl Foo {
7+
#[doc(alias = "zzz")]
8+
pub fn baz() {}
9+
}
10+
11+
impl Bar {
12+
#[doc(alias = "zzz")]
13+
pub fn baz() {}
14+
}
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': 'Foo',
6+
'others': [
7+
{ 'path': 'alias_rank_lower', 'name': 'Foo' },
8+
{ 'path': 'alias_rank_lower', 'name': 'Bar' },
9+
],
10+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_name = "alias_rank_lower"]
2+
3+
pub struct Foo;
4+
5+
#[doc(alias = "Foo")]
6+
pub struct Bar;

0 commit comments

Comments
 (0)