Skip to content

Commit ed454bf

Browse files
committed
Fix alias and unstable sorting
1 parent 21c4839 commit ed454bf

File tree

6 files changed

+65
-36
lines changed

6 files changed

+65
-36
lines changed

src/librustdoc/html/render/search_index.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ impl SerializedSearchIndex {
490490
exact_module_path,
491491
parent,
492492
deprecated,
493+
is_unstable,
493494
associated_item_disambiguator,
494495
}| EntryData {
495496
krate: *map.get(krate).unwrap(),
@@ -499,6 +500,7 @@ impl SerializedSearchIndex {
499500
.and_then(|path_id| map.get(&path_id).copied()),
500501
parent: parent.and_then(|path_id| map.get(&path_id).copied()),
501502
deprecated: *deprecated,
503+
is_unstable: *is_unstable,
502504
associated_item_disambiguator: associated_item_disambiguator.clone(),
503505
},
504506
),
@@ -753,6 +755,7 @@ struct EntryData {
753755
exact_module_path: Option<usize>,
754756
parent: Option<usize>,
755757
deprecated: bool,
758+
is_unstable: bool,
756759
associated_item_disambiguator: Option<String>,
757760
}
758761

@@ -768,6 +771,7 @@ impl Serialize for EntryData {
768771
seq.serialize_element(&self.exact_module_path.map(|id| id + 1).unwrap_or(0))?;
769772
seq.serialize_element(&self.parent.map(|id| id + 1).unwrap_or(0))?;
770773
seq.serialize_element(&if self.deprecated { 1 } else { 0 })?;
774+
seq.serialize_element(&if self.is_unstable { 1 } else { 0 })?;
771775
if let Some(disambig) = &self.associated_item_disambiguator {
772776
seq.serialize_element(&disambig)?;
773777
}
@@ -799,6 +803,7 @@ impl<'de> Deserialize<'de> for EntryData {
799803
let parent: SerializedOptional32 =
800804
v.next_element()?.ok_or_else(|| A::Error::missing_field("parent"))?;
801805
let deprecated: u32 = v.next_element()?.unwrap_or(0);
806+
let is_unstable: u32 = v.next_element()?.unwrap_or(0);
802807
let associated_item_disambiguator: Option<String> = v.next_element()?;
803808
Ok(EntryData {
804809
krate,
@@ -808,6 +813,7 @@ impl<'de> Deserialize<'de> for EntryData {
808813
.map(|path| path as usize),
809814
parent: Option::<i32>::from(parent).map(|path| path as usize),
810815
deprecated: deprecated != 0,
816+
is_unstable: is_unstable != 0,
811817
associated_item_disambiguator,
812818
})
813819
}
@@ -1226,6 +1232,7 @@ pub(crate) fn build_index(
12261232
exact_module_path: None,
12271233
parent: None,
12281234
deprecated: false,
1235+
is_unstable: false,
12291236
associated_item_disambiguator: None,
12301237
}),
12311238
crate_doc,
@@ -1356,6 +1363,7 @@ pub(crate) fn build_index(
13561363
module_path,
13571364
exact_module_path,
13581365
deprecated: item.deprecation.is_some(),
1366+
is_unstable: item.is_unstable,
13591367
associated_item_disambiguator: if let Some(impl_id) = item.impl_id
13601368
&& let Some(parent_idx) = item.parent_idx
13611369
&& associated_item_duplicates
@@ -1701,9 +1709,6 @@ pub(crate) fn build_index(
17011709
serialized_index.generic_inverted_index[generic_id]
17021710
.push(u32::try_from(new_entry_id).unwrap());
17031711
}
1704-
if item.is_unstable {
1705-
unstable.push(u32::try_from(index + 1).unwrap());
1706-
}
17071712
}
17081713
}
17091714
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ declare namespace rustdoc {
235235
exactModulePath: number?,
236236
parent: number?,
237237
deprecated: boolean,
238+
isUnstable: boolean,
238239
associatedItemDisambiguator: string?,
239240
}
240241

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

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ class DocSearch {
15391539
* exact_module_path,
15401540
* parent,
15411541
* deprecated,
1542+
* isUnstable,
15421543
* associated_item_disambiguator
15431544
* @type {[
15441545
* number,
@@ -1547,6 +1548,7 @@ class DocSearch {
15471548
* number,
15481549
* number,
15491550
* number,
1551+
* number,
15501552
* string,
15511553
* ] | [
15521554
* number,
@@ -1555,6 +1557,7 @@ class DocSearch {
15551557
* number,
15561558
* number,
15571559
* number,
1560+
* number,
15581561
* ]}
15591562
*/
15601563
const raw = JSON.parse(encoded);
@@ -1565,7 +1568,8 @@ class DocSearch {
15651568
exactModulePath: raw[3] === 0 ? null : raw[3] - 1,
15661569
parent: raw[4] === 0 ? null : raw[4] - 1,
15671570
deprecated: raw[5] === 1 ? true : false,
1568-
associatedItemDisambiguator: raw.length === 6 ? null : raw[6],
1571+
isUnstable: raw[6] === 1 ? true : false,
1572+
associatedItemDisambiguator: raw.length === 7 ? null : raw[7],
15691573
};
15701574
}
15711575

@@ -1708,7 +1712,7 @@ class DocSearch {
17081712
*/
17091713
const raw = JSON.parse(encoded);
17101714

1711-
if (!raw || raw[0] === undefined) {
1715+
if (!raw || raw.length === 0) {
17121716
return null;
17131717
}
17141718

@@ -2649,12 +2653,6 @@ class DocSearch {
26492653
}
26502654
}
26512655

2652-
a = Number(aaa.is_alias);
2653-
b = Number(bbb.is_alias);
2654-
if (a !== b) {
2655-
return b - a;
2656-
}
2657-
26582656
// sort by exact match with regard to the last word (mismatch goes later)
26592657
a = Number(aai.normalizedName !== normalizedUserQuery);
26602658
b = Number(bbi.normalizedName !== normalizedUserQuery);
@@ -2693,13 +2691,27 @@ class DocSearch {
26932691
return a - b;
26942692
}
26952693

2694+
// sort aliases lower
2695+
a = Number(aaa.is_alias);
2696+
b = Number(bbb.is_alias);
2697+
if (a !== b) {
2698+
return a - b;
2699+
}
2700+
26962701
// sort deprecated items later
26972702
a = Number(aai.deprecated);
26982703
b = Number(bbi.deprecated);
26992704
if (a !== b) {
27002705
return a - b;
27012706
}
27022707

2708+
// sort unstable items later
2709+
a = Number(aai.entry && aai.entry.isUnstable);
2710+
b = Number(bbi.entry && bbi.entry.isUnstable);
2711+
if (a !== b) {
2712+
return a - b;
2713+
}
2714+
27032715
// sort by crate (current crate comes first)
27042716
a = Number(aai.crate !== preferredCrate);
27052717
b = Number(bbi.crate !== preferredCrate);
@@ -3719,14 +3731,16 @@ class DocSearch {
37193731
/**
37203732
* @param {string} name
37213733
* @param {number} alias
3734+
* @param {number} dist
3735+
* @param {number} index
37223736
* @returns {Promise<rustdoc.PlainResultObject?>}
37233737
*/
3724-
const handleAlias = async(name, alias) => {
3738+
const handleAlias = async(name, alias, dist, index) => {
37253739
return {
37263740
id: alias,
3727-
dist: 0,
3741+
dist,
37283742
path_dist: 0,
3729-
index: 0,
3743+
index,
37303744
alias: name,
37313745
is_alias: true,
37323746
elems: [], // only used in type-based queries
@@ -3771,7 +3785,7 @@ class DocSearch {
37713785
!idDuplicates.has(id) &&
37723786
name.replace(/[_"]/g, "").toLowerCase() === normalizedUserQuery
37733787
) {
3774-
prefixResults.push(handleAlias(name, alias));
3788+
prefixResults.push(handleAlias(name, alias, 0, 0));
37753789
idDuplicates.add(id);
37763790
}
37773791
}
@@ -3858,7 +3872,7 @@ class DocSearch {
38583872
this.getAliasPointer(id),
38593873
]);
38603874
if (name !== null && alias !== null) {
3861-
prefixResults.push(handleAlias(name, alias));
3875+
prefixResults.push(handleAlias(name, alias, 0, 0));
38623876
}
38633877
}
38643878
}
@@ -3882,7 +3896,12 @@ class DocSearch {
38823896
this.getAliasPointer(id),
38833897
]);
38843898
if (name !== null && alias !== null) {
3885-
levResults.push(handleAlias(name, alias));
3899+
levResults.push(handleAlias(
3900+
name,
3901+
alias,
3902+
editDistance(elem.normalizedPathLast, name, maxEditDistance),
3903+
name.indexOf(elem.normalizedPathLast),
3904+
));
38863905
}
38873906
}
38883907
}
@@ -3900,7 +3919,12 @@ class DocSearch {
39003919
this.getAliasPointer(id),
39013920
]);
39023921
if (name !== null && alias !== null) {
3903-
substringResults.push(handleAlias(name, alias));
3922+
levResults.push(handleAlias(
3923+
name,
3924+
alias,
3925+
editDistance(elem.normalizedPathLast, name, maxEditDistance),
3926+
name.indexOf(elem.normalizedPathLast),
3927+
));
39043928
}
39053929
}
39063930
}

tests/rustdoc-js-std/alias-1.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const EXPECTED = {
22
'query': '&',
33
'others': [
4-
{
5-
'path': 'std::ops',
6-
'name': 'BitAnd',
7-
'desc': "The bitwise AND operator <code>&amp;</code>.",
8-
},
94
{
105
'path': 'std',
116
'name': 'reference',
127
'desc': "References, <code>&amp;T</code> and <code>&amp;mut T</code>.",
138
},
9+
{
10+
'path': 'std::ops',
11+
'name': 'BitAnd',
12+
'desc': "The bitwise AND operator <code>&amp;</code>.",
13+
},
1414
],
1515
};

tests/rustdoc-js/doc-alias.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ const EXPECTED = [
231231
{
232232
'query': 'UnionItem',
233233
'others': [
234+
{
235+
'path': 'doc_alias::Union',
236+
'name': 'union_item',
237+
'desc': 'Doc for <code>Union::union_item</code>',
238+
'href': '../doc_alias/union.Union.html#structfield.union_item'
239+
},
234240
{
235241
'path': 'doc_alias',
236242
'name': 'Union',
@@ -239,13 +245,6 @@ const EXPECTED = [
239245
'href': '../doc_alias/union.Union.html',
240246
'is_alias': true
241247
},
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-
},
249248
],
250249
},
251250
{

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ const EXPECTED = [
114114
{
115115
query: '加法',
116116
others: [
117+
{
118+
name: "加法",
119+
path: "non_english_identifier",
120+
href: "../non_english_identifier/trait.加法.html",
121+
desc: "Add"
122+
},
117123
{
118124
name: "add",
119125
path: "non_english_identifier",
@@ -128,12 +134,6 @@ const EXPECTED = [
128134
alias: "加法",
129135
href: "../non_english_identifier/macro.add.html"
130136
},
131-
{
132-
name: "加法",
133-
path: "non_english_identifier",
134-
href: "../non_english_identifier/trait.加法.html",
135-
desc: "Add"
136-
},
137137
],
138138
in_args: [{
139139
name: "加上",

0 commit comments

Comments
 (0)