Skip to content

Commit 8596751

Browse files
authored
Rollup merge of #107629 - pitaj:rustdoc-search-deprecated, r=jsha
rustdoc: sort deprecated items lower in search closes #98759 ### Screenshots `i32::MAX` show sup above `std::i32::MAX` and `core::i32::MAX` ![image](https://user-images.githubusercontent.com/803701/216725619-40afb7b0-e984-4a2e-ab5b-a95b24736b0e.png) If just searching for `min`, the deprecated results show up far below other things: ![image](https://user-images.githubusercontent.com/803701/216725672-e4325d37-9bfe-47eb-a1fe-0e57092aa811.png) one page later ![image](https://user-images.githubusercontent.com/803701/216725932-cd1c4a42-d527-44fb-a4ab-5a6d243659cc.png) ~~And, as you can see, the "Deprecation planned" message shows up in the search results. The same is true for fully-deprecated items like `mem::uninitialized`: ![image](https://user-images.githubusercontent.com/803701/216726268-1657e77a-563f-45a0-85a7-3a0cf4d66d6f.png)~~ Edit: the deprecation message change was removed from this PR. Only the sorting is changed.
2 parents 790d9f3 + d2e4b59 commit 8596751

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

src/librustdoc/formats/cache.rs

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
346346
self.cache,
347347
),
348348
aliases: item.attrs.get_doc_aliases(),
349+
deprecation: item.deprecation(self.tcx),
349350
});
350351
}
351352
}

src/librustdoc/html/render/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub(crate) struct IndexItem {
104104
pub(crate) parent_idx: Option<usize>,
105105
pub(crate) search_type: Option<IndexItemFunctionType>,
106106
pub(crate) aliases: Box<[Symbol]>,
107+
pub(crate) deprecation: Option<Deprecation>,
107108
}
108109

109110
/// A type used for the search index.

src/librustdoc/html/render/print_item.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,11 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
470470

471471
// The trailing space after each tag is to space it properly against the rest of the docs.
472472
if let Some(depr) = &item.deprecation(tcx) {
473-
let mut message = "Deprecated";
474-
if !stability::deprecation_in_effect(depr) {
475-
message = "Deprecation planned";
476-
}
473+
let message = if stability::deprecation_in_effect(depr) {
474+
"Deprecated"
475+
} else {
476+
"Deprecation planned"
477+
};
477478
tags += &tag_html("deprecated", "", message);
478479
}
479480

src/librustdoc/html/render/search_index.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub(crate) fn build_index<'tcx>(
4040
parent_idx: None,
4141
search_type: get_function_type_for_search(item, tcx, impl_generics.as_ref(), cache),
4242
aliases: item.attrs.get_doc_aliases(),
43+
deprecation: item.deprecation(tcx),
4344
});
4445
}
4546
}
@@ -251,7 +252,17 @@ pub(crate) fn build_index<'tcx>(
251252
)?;
252253
crate_data.serialize_field(
253254
"q",
254-
&self.items.iter().map(|item| &item.path).collect::<Vec<_>>(),
255+
&self
256+
.items
257+
.iter()
258+
.enumerate()
259+
// Serialize as an array of item indices and full paths
260+
.filter_map(
261+
|(index, item)| {
262+
if item.path.is_empty() { None } else { Some((index, &item.path)) }
263+
},
264+
)
265+
.collect::<Vec<_>>(),
255266
)?;
256267
crate_data.serialize_field(
257268
"d",
@@ -304,6 +315,16 @@ pub(crate) fn build_index<'tcx>(
304315
})
305316
.collect::<Vec<_>>(),
306317
)?;
318+
crate_data.serialize_field(
319+
"c",
320+
&self
321+
.items
322+
.iter()
323+
.enumerate()
324+
// Serialize as an array of deprecated item indices
325+
.filter_map(|(index, item)| item.deprecation.map(|_| index))
326+
.collect::<Vec<_>>(),
327+
)?;
307328
crate_data.serialize_field(
308329
"p",
309330
&self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::<Vec<_>>(),

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,13 @@ function initSearch(rawSearchIndex) {
881881
return a - b;
882882
}
883883

884+
// sort deprecated items later
885+
a = aaa.item.deprecated;
886+
b = bbb.item.deprecated;
887+
if (a !== b) {
888+
return a - b;
889+
}
890+
884891
// sort by crate (current crate comes first)
885892
a = (aaa.item.crate !== preferredCrate);
886893
b = (bbb.item.crate !== preferredCrate);
@@ -1244,6 +1251,7 @@ function initSearch(rawSearchIndex) {
12441251
parent: item.parent,
12451252
type: item.type,
12461253
is_alias: true,
1254+
deprecated: item.deprecated,
12471255
};
12481256
}
12491257

@@ -2064,10 +2072,11 @@ function initSearch(rawSearchIndex) {
20642072
* n: Array<string>,
20652073
* t: String,
20662074
* d: Array<string>,
2067-
* q: Array<string>,
2075+
* q: Array<[Number, string]>,
20682076
* i: Array<Number>,
20692077
* f: Array<RawFunctionSearchType>,
20702078
* p: Array<Object>,
2079+
* c: Array<Number>
20712080
* }}
20722081
*/
20732082
const crateCorpus = rawSearchIndex[crate];
@@ -2086,6 +2095,7 @@ function initSearch(rawSearchIndex) {
20862095
type: null,
20872096
id: id,
20882097
normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""),
2098+
deprecated: null,
20892099
};
20902100
id += 1;
20912101
searchIndex.push(crateRow);
@@ -2095,14 +2105,20 @@ function initSearch(rawSearchIndex) {
20952105
const itemTypes = crateCorpus.t;
20962106
// an array of (String) item names
20972107
const itemNames = crateCorpus.n;
2098-
// an array of (String) full paths (or empty string for previous path)
2099-
const itemPaths = crateCorpus.q;
2108+
// an array of [(Number) item index,
2109+
// (String) full path]
2110+
// an item whose index is not present will fall back to the previous present path
2111+
// i.e. if indices 4 and 11 are present, but 5-10 and 12-13 are not present,
2112+
// 5-10 will fall back to the path for 4 and 12-13 will fall back to the path for 11
2113+
const itemPaths = new Map(crateCorpus.q);
21002114
// an array of (String) descriptions
21012115
const itemDescs = crateCorpus.d;
21022116
// an array of (Number) the parent path index + 1 to `paths`, or 0 if none
21032117
const itemParentIdxs = crateCorpus.i;
21042118
// an array of (Object | null) the type of the function, if any
21052119
const itemFunctionSearchTypes = crateCorpus.f;
2120+
// an array of (Number) indices for the deprecated items
2121+
const deprecatedItems = new Set(crateCorpus.c);
21062122
// an array of [(Number) item type,
21072123
// (String) name]
21082124
const paths = crateCorpus.p;
@@ -2142,12 +2158,13 @@ function initSearch(rawSearchIndex) {
21422158
crate: crate,
21432159
ty: itemTypes.charCodeAt(i) - charA,
21442160
name: itemNames[i],
2145-
path: itemPaths[i] ? itemPaths[i] : lastPath,
2161+
path: itemPaths.has(i) ? itemPaths.get(i) : lastPath,
21462162
desc: itemDescs[i],
21472163
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
21482164
type: buildFunctionSearchType(itemFunctionSearchTypes[i], lowercasePaths),
21492165
id: id,
21502166
normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""),
2167+
deprecated: deprecatedItems.has(i),
21512168
};
21522169
id += 1;
21532170
searchIndex.push(row);

0 commit comments

Comments
 (0)