Skip to content

Commit d2e4b59

Browse files
committed
rustdoc: sort deprecated items lower in search
serialize `q` (`itemPaths`) sparsely overall 4% reduction in search index size
1 parent 6c991b0 commit d2e4b59

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

src/librustdoc/formats/cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
337337
self.cache,
338338
),
339339
aliases: item.attrs.get_doc_aliases(),
340+
deprecation: item.deprecation(self.tcx),
340341
});
341342
}
342343
}

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub(crate) struct IndexItem {
107107
pub(crate) parent_idx: Option<usize>,
108108
pub(crate) search_type: Option<IndexItemFunctionType>,
109109
pub(crate) aliases: Box<[Symbol]>,
110+
pub(crate) deprecation: Option<Deprecation>,
110111
}
111112

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

src/librustdoc/html/render/print_item.rs

Lines changed: 5 additions & 4 deletions
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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub(crate) fn build_index<'tcx>(
4242
parent_idx: None,
4343
search_type: get_function_type_for_search(item, tcx, impl_generics.as_ref(), cache),
4444
aliases: item.attrs.get_doc_aliases(),
45+
deprecation: item.deprecation(tcx),
4546
});
4647
}
4748
}
@@ -244,7 +245,17 @@ pub(crate) fn build_index<'tcx>(
244245
)?;
245246
crate_data.serialize_field(
246247
"q",
247-
&self.items.iter().map(|item| &item.path).collect::<Vec<_>>(),
248+
&self
249+
.items
250+
.iter()
251+
.enumerate()
252+
// Serialize as an array of item indices and full paths
253+
.filter_map(
254+
|(index, item)| {
255+
if item.path.is_empty() { None } else { Some((index, &item.path)) }
256+
},
257+
)
258+
.collect::<Vec<_>>(),
248259
)?;
249260
crate_data.serialize_field(
250261
"d",
@@ -297,6 +308,16 @@ pub(crate) fn build_index<'tcx>(
297308
})
298309
.collect::<Vec<_>>(),
299310
)?;
311+
crate_data.serialize_field(
312+
"c",
313+
&self
314+
.items
315+
.iter()
316+
.enumerate()
317+
// Serialize as an array of deprecated item indices
318+
.filter_map(|(index, item)| item.deprecation.map(|_| index))
319+
.collect::<Vec<_>>(),
320+
)?;
300321
crate_data.serialize_field(
301322
"p",
302323
&self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::<Vec<_>>(),

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,13 @@ function initSearch(rawSearchIndex) {
811811
return a - b;
812812
}
813813

814+
// sort deprecated items later
815+
a = aaa.item.deprecated;
816+
b = bbb.item.deprecated;
817+
if (a !== b) {
818+
return a - b;
819+
}
820+
814821
// sort by crate (current crate comes first)
815822
a = (aaa.item.crate !== preferredCrate);
816823
b = (bbb.item.crate !== preferredCrate);
@@ -1170,6 +1177,7 @@ function initSearch(rawSearchIndex) {
11701177
parent: item.parent,
11711178
type: item.type,
11721179
is_alias: true,
1180+
deprecated: item.deprecated,
11731181
};
11741182
}
11751183

@@ -1965,10 +1973,11 @@ function initSearch(rawSearchIndex) {
19651973
* n: Array<string>,
19661974
* t: Array<Number>,
19671975
* d: Array<string>,
1968-
* q: Array<string>,
1976+
* q: Array<[Number, string]>,
19691977
* i: Array<Number>,
19701978
* f: Array<RawFunctionSearchType>,
19711979
* p: Array<Object>,
1980+
* c: Array<Number>
19721981
* }}
19731982
*/
19741983
const crateCorpus = rawSearchIndex[crate];
@@ -1987,6 +1996,7 @@ function initSearch(rawSearchIndex) {
19871996
type: null,
19881997
id: id,
19891998
normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""),
1999+
deprecated: null,
19902000
};
19912001
id += 1;
19922002
searchIndex.push(crateRow);
@@ -1996,14 +2006,20 @@ function initSearch(rawSearchIndex) {
19962006
const itemTypes = crateCorpus.t;
19972007
// an array of (String) item names
19982008
const itemNames = crateCorpus.n;
1999-
// an array of (String) full paths (or empty string for previous path)
2000-
const itemPaths = crateCorpus.q;
2009+
// an array of [(Number) item index,
2010+
// (String) full path]
2011+
// an item whose index is not present will fall back to the previous present path
2012+
// i.e. if indices 4 and 11 are present, but 5-10 and 12-13 are not present,
2013+
// 5-10 will fall back to the path for 4 and 12-13 will fall back to the path for 11
2014+
const itemPaths = new Map(crateCorpus.q);
20012015
// an array of (String) descriptions
20022016
const itemDescs = crateCorpus.d;
20032017
// an array of (Number) the parent path index + 1 to `paths`, or 0 if none
20042018
const itemParentIdxs = crateCorpus.i;
20052019
// an array of (Object | null) the type of the function, if any
20062020
const itemFunctionSearchTypes = crateCorpus.f;
2021+
// an array of (Number) indices for the deprecated items
2022+
const deprecatedItems = new Set(crateCorpus.c);
20072023
// an array of [(Number) item type,
20082024
// (String) name]
20092025
const paths = crateCorpus.p;
@@ -2045,12 +2061,13 @@ function initSearch(rawSearchIndex) {
20452061
crate: crate,
20462062
ty: itemTypes[i],
20472063
name: itemNames[i],
2048-
path: itemPaths[i] ? itemPaths[i] : lastPath,
2064+
path: itemPaths.has(i) ? itemPaths.get(i) : lastPath,
20492065
desc: itemDescs[i],
20502066
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
20512067
type: buildFunctionSearchType(itemFunctionSearchTypes[i], lowercasePaths),
20522068
id: id,
20532069
normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""),
2070+
deprecated: deprecatedItems.has(i),
20542071
};
20552072
id += 1;
20562073
searchIndex.push(row);

0 commit comments

Comments
 (0)