Skip to content

Commit 738dcc9

Browse files
committed
WIP: don't show multiple instances of the same trait item in search
1 parent 41a79f1 commit 738dcc9

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

src/librustdoc/formats/cache.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ impl Cache {
148148
Cache { document_private, document_hidden, ..Cache::default() }
149149
}
150150

151+
fn parent_stack_last_impl_and_trait_id(&self) -> (Option<DefId>, Option<DefId>) {
152+
if let Some(ParentStackItem::Impl { item_id, trait_, .. }) = self.parent_stack.last() {
153+
(item_id.as_def_id(), trait_.as_ref().map(|tr| tr.def_id()))
154+
} else {
155+
(None, None)
156+
}
157+
}
158+
151159
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
152160
/// in `krate` due to the data being moved into the `Cache`.
153161
pub(crate) fn populate(cx: &mut DocContext<'_>, mut krate: clean::Crate) -> clean::Crate {
@@ -573,11 +581,8 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
573581
clean::ItemKind::ImportItem(import) => import.source.did.unwrap_or(item_def_id),
574582
_ => item_def_id,
575583
};
576-
let impl_id = if let Some(ParentStackItem::Impl { item_id, .. }) = cache.parent_stack.last() {
577-
item_id.as_def_id()
578-
} else {
579-
None
580-
};
584+
let (impl_id, trait_parent) =
585+
cache.parent_stack_last_impl_and_trait_id();
581586
let search_type = get_function_type_for_search(
582587
item,
583588
tcx,
@@ -595,12 +600,18 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
595600
desc,
596601
parent: parent_did,
597602
parent_idx: None,
603+
trait_parent,
604+
trait_parent_idx: None,
598605
exact_module_path: None,
599606
impl_id,
600607
search_type,
601608
aliases,
602609
deprecation,
603610
};
611+
if item.name.map(|sym| sym.as_str() == "last").unwrap_or(false) {
612+
dbg!(&index_item);
613+
}
614+
604615
cache.search_index.push(index_item);
605616
}
606617

@@ -609,19 +620,19 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
609620
/// See [`Cache::orphan_impl_items`].
610621
fn handle_orphan_impl_child(cache: &mut Cache, item: &clean::Item, parent_did: DefId) {
611622
let impl_generics = clean_impl_generics(cache.parent_stack.last());
612-
let impl_id = if let Some(ParentStackItem::Impl { item_id, .. }) = cache.parent_stack.last() {
613-
item_id.as_def_id()
614-
} else {
615-
None
616-
};
623+
let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
624+
if item.name.map(|sym| sym.as_str() == "last").unwrap_or(false) {
625+
dbg!(item.item_id, trait_parent);
626+
}
617627
let orphan_item =
618-
OrphanImplItem { parent: parent_did, item: item.clone(), impl_generics, impl_id };
628+
OrphanImplItem { parent: parent_did, trait_parent, item: item.clone(), impl_generics, impl_id };
619629
cache.orphan_impl_items.push(orphan_item);
620630
}
621631

622632
pub(crate) struct OrphanImplItem {
623633
pub(crate) parent: DefId,
624634
pub(crate) impl_id: Option<DefId>,
635+
pub(crate) trait_parent: Option<DefId>,
625636
pub(crate) item: clean::Item,
626637
pub(crate) impl_generics: Option<(clean::Type, clean::Generics)>,
627638
}

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ pub(crate) struct IndexItem {
134134
pub(crate) desc: String,
135135
pub(crate) parent: Option<DefId>,
136136
pub(crate) parent_idx: Option<usize>,
137+
pub(crate) trait_parent: Option<DefId>,
138+
pub(crate) trait_parent_idx: Option<usize>,
137139
pub(crate) exact_module_path: Option<Vec<Symbol>>,
138140
pub(crate) impl_id: Option<DefId>,
139141
pub(crate) search_type: Option<IndexItemFunctionType>,

src/librustdoc/html/render/search_index.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ impl SerializedSearchIndex {
520520
ty,
521521
module_path,
522522
exact_module_path,
523-
parent,
523+
parent,
524+
trait_parent,
524525
deprecated,
525526
associated_item_disambiguator,
526527
}| EntryData {
@@ -530,6 +531,7 @@ impl SerializedSearchIndex {
530531
exact_module_path: exact_module_path
531532
.and_then(|path_id| map.get(&path_id).copied()),
532533
parent: parent.and_then(|path_id| map.get(&path_id).copied()),
534+
trait_parent: trait_parent.and_then(|path_id| map.get(&path_id).copied()),
533535
deprecated: *deprecated,
534536
associated_item_disambiguator: associated_item_disambiguator.clone(),
535537
},
@@ -788,6 +790,7 @@ struct EntryData {
788790
module_path: Option<usize>,
789791
exact_module_path: Option<usize>,
790792
parent: Option<usize>,
793+
trait_parent: Option<usize>,
791794
deprecated: bool,
792795
associated_item_disambiguator: Option<String>,
793796
}
@@ -803,6 +806,7 @@ impl Serialize for EntryData {
803806
seq.serialize_element(&self.module_path.map(|id| id + 1).unwrap_or(0))?;
804807
seq.serialize_element(&self.exact_module_path.map(|id| id + 1).unwrap_or(0))?;
805808
seq.serialize_element(&self.parent.map(|id| id + 1).unwrap_or(0))?;
809+
seq.serialize_element(&self.trait_parent.map(|id| id + 1).unwrap_or(0))?;
806810
seq.serialize_element(&if self.deprecated { 1 } else { 0 })?;
807811
if let Some(disambig) = &self.associated_item_disambiguator {
808812
seq.serialize_element(&disambig)?;
@@ -834,6 +838,9 @@ impl<'de> Deserialize<'de> for EntryData {
834838
.ok_or_else(|| A::Error::missing_field("exact_module_path"))?;
835839
let parent: SerializedOptional32 =
836840
v.next_element()?.ok_or_else(|| A::Error::missing_field("parent"))?;
841+
let trait_parent: SerializedOptional32 =
842+
v.next_element()?.ok_or_else(|| A::Error::missing_field("trait_parent"))?;
843+
837844
let deprecated: u32 = v.next_element()?.unwrap_or(0);
838845
let associated_item_disambiguator: Option<String> = v.next_element()?;
839846
Ok(EntryData {
@@ -843,6 +850,7 @@ impl<'de> Deserialize<'de> for EntryData {
843850
exact_module_path: Option::<i32>::from(exact_module_path)
844851
.map(|path| path as usize),
845852
parent: Option::<i32>::from(parent).map(|path| path as usize),
853+
trait_parent: Option::<i32>::from(trait_parent).map(|path| path as usize),
846854
deprecated: deprecated != 0,
847855
associated_item_disambiguator,
848856
})
@@ -1167,7 +1175,7 @@ pub(crate) fn build_index(
11671175

11681176
// Attach all orphan items to the type's definition if the type
11691177
// has since been learned.
1170-
for &OrphanImplItem { impl_id, parent, ref item, ref impl_generics } in &cache.orphan_impl_items
1178+
for &OrphanImplItem { impl_id, parent, trait_parent, ref item, ref impl_generics } in &cache.orphan_impl_items
11711179
{
11721180
if let Some((fqp, _)) = cache.paths.get(&parent) {
11731181
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
@@ -1179,6 +1187,8 @@ pub(crate) fn build_index(
11791187
desc,
11801188
parent: Some(parent),
11811189
parent_idx: None,
1190+
trait_parent,
1191+
trait_parent_idx: None,
11821192
exact_module_path: None,
11831193
impl_id,
11841194
search_type: get_function_type_for_search(
@@ -1275,6 +1285,7 @@ pub(crate) fn build_index(
12751285
module_path: None,
12761286
exact_module_path: None,
12771287
parent: None,
1288+
trait_parent: None,
12781289
deprecated: false,
12791290
associated_item_disambiguator: None,
12801291
}),
@@ -1288,11 +1299,11 @@ pub(crate) fn build_index(
12881299
}
12891300
};
12901301

1291-
// First, populate associated item parents
1302+
// First, populate associated item parents and trait parents
12921303
let crate_items: Vec<&mut IndexItem> = search_index
12931304
.iter_mut()
12941305
.map(|item| {
1295-
item.parent_idx = item.parent.and_then(|defid| {
1306+
let mut defid_to_rowid = |defid| {
12961307
cache.paths.get(&defid).map(|&(ref fqp, ty)| {
12971308
let pathid = serialized_index.names.len();
12981309
match serialized_index.crate_paths_index.entry((ty, fqp.clone())) {
@@ -1319,8 +1330,10 @@ pub(crate) fn build_index(
13191330
usize::try_from(pathid).unwrap()
13201331
}
13211332
}
1322-
})
1323-
});
1333+
})};
1334+
item.parent_idx = item.parent.and_then(&mut defid_to_rowid);
1335+
item.trait_parent_idx = item.trait_parent.and_then(&mut defid_to_rowid);
1336+
13241337

13251338
if let Some(defid) = item.defid
13261339
&& item.parent_idx.is_none()
@@ -1403,6 +1416,7 @@ pub(crate) fn build_index(
14031416
Some(EntryData {
14041417
ty: item.ty,
14051418
parent: item.parent_idx,
1419+
trait_parent: item.trait_parent_idx,
14061420
module_path,
14071421
exact_module_path,
14081422
deprecated: item.deprecation.is_some(),

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ declare namespace rustdoc {
241241
modulePath: number?,
242242
exactModulePath: number?,
243243
parent: number?,
244+
traitParent: number?,
244245
deprecated: boolean,
245246
associatedItemDisambiguator: string?,
246247
}
@@ -288,7 +289,8 @@ declare namespace rustdoc {
288289
path: PathData?,
289290
type: FunctionData?,
290291
deprecated: boolean,
291-
parent: { path: PathData, name: string}?,
292+
parent: { path: PathData, name: string }?,
293+
traitParent: { path: PathData, name: string }?,
292294
}
293295

294296
type ItemType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,7 @@ class DocSearch {
15551555
* module_path,
15561556
* exact_module_path,
15571557
* parent,
1558+
* trait_parent,
15581559
* deprecated,
15591560
* associated_item_disambiguator
15601561
* @type {rustdoc.ArrayWithOptionals<[
@@ -1564,6 +1565,7 @@ class DocSearch {
15641565
* number,
15651566
* number,
15661567
* number,
1568+
* number,
15671569
* ], [string]>}
15681570
*/
15691571
const raw = JSON.parse(encoded);
@@ -1573,8 +1575,9 @@ class DocSearch {
15731575
modulePath: raw[2] === 0 ? null : raw[2] - 1,
15741576
exactModulePath: raw[3] === 0 ? null : raw[3] - 1,
15751577
parent: raw[4] === 0 ? null : raw[4] - 1,
1576-
deprecated: raw[5] === 1 ? true : false,
1577-
associatedItemDisambiguator: raw.length === 6 ? null : raw[6],
1578+
traitParent: raw[5] === 0 ? null : raw[5] - 1,
1579+
deprecated: raw[6] === 1 ? true : false,
1580+
associatedItemDisambiguator: raw.length === 7 ? null : raw[7],
15781581
};
15791582
}
15801583

@@ -1825,6 +1828,10 @@ class DocSearch {
18251828
const [parentName, parentPath] = entry !== null && entry.parent !== null ?
18261829
await Promise.all([this.getName(entry.parent), this.getPathData(entry.parent)]) :
18271830
[null, null];
1831+
const [traitParentName, traitParentPath] = entry !== null && entry.traitParent !== null ?
1832+
await Promise.all([this.getName(entry.traitParent), this.getPathData(entry.traitParent)]) :
1833+
[null, null];
1834+
18281835
return {
18291836
id,
18301837
crate: entry ? nonnull(await this.getName(entry.krate)) : "",
@@ -1843,6 +1850,10 @@ class DocSearch {
18431850
parent: parentName !== null && parentPath !== null ?
18441851
{ name: parentName, path: parentPath } :
18451852
null,
1853+
traitParent: traitParentName !== null && traitParentPath !== null ?
1854+
{ name: traitParentName, path: traitParentPath } :
1855+
null,
1856+
18461857
};
18471858
}
18481859

@@ -2102,6 +2113,10 @@ class DocSearch {
21022113
href = this.rootPath + item.modulePath.replace(/::/g, "/") +
21032114
"/" + type + "." + name + ".html";
21042115
}
2116+
if (item.traitParent) {
2117+
const tparent = item.traitParent;
2118+
exactPath = `${tparent.path.exactModulePath}::${tparent.name}`;
2119+
}
21052120
return [displayPath, href, `${exactPath}::${name}`];
21062121
};
21072122

@@ -2531,10 +2546,14 @@ class DocSearch {
25312546
*/
25322547
const transformResults = (results, typeInfo, duplicates) => {
25332548
const out = [];
2549+
2550+
// if there are duplicates, those from impl blocks should not be shown
2551+
results.sort(([_a, a], [_b, b]) => Number(!!a.traitParent) - Number(!!b.traitParent))
25342552

25352553
for (const [result, item] of results) {
25362554
if (item.id !== -1) {
25372555
const res = buildHrefAndPath(item);
2556+
console.log(res);
25382557
// many of these properties don't strictly need to be
25392558
// copied over, but copying them over satisfies tsc,
25402559
// and hopefully plays nice with the shape optimization
@@ -2567,6 +2586,7 @@ class DocSearch {
25672586

25682587
// To be sure than it some items aren't considered as duplicate.
25692588
obj.fullPath = res[2] + "|" + obj.item.ty;
2589+
console.log(obj.fullPath);
25702590

25712591
if (duplicates.has(obj.fullPath)) {
25722592
continue;

0 commit comments

Comments
 (0)