Skip to content

Commit

Permalink
Rustdoc: include crate name in links for local primitives
Browse files Browse the repository at this point in the history
It makes the link easier to use in cases in which
the path of the page where it will be embedded is not
known beforehand such as when we generate impls
dynamically from `register_type_impls` method in
`main.js`

Earlier for local primitives we would generate a path
that was relative to the current page depth passed in `cx.current`
. e.g if the current page was `std::simd::prelude::Simd` the
generated path would be `../../primitive.<prim>.html`  After this
change the path will first take you to the the wesite root and add
the crate name. e.g. for `std::simd::prelude::Simd` the path now
will be `../../../std/primitive.<prim>.html`
  • Loading branch information
gurry committed Feb 23, 2024
1 parent a28d221 commit 147a678
Showing 1 changed file with 38 additions and 41 deletions.
79 changes: 38 additions & 41 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,49 +875,46 @@ fn primitive_link_fragment(
) -> fmt::Result {
let m = &cx.cache();
let mut needs_termination = false;
if !f.alternate() {
match m.primitive_locations.get(&prim) {
Some(&def_id) if def_id.is_local() => {
let len = cx.current.len();
let len = if len == 0 { 0 } else { len - 1 };
write!(
f,
"<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">",
"../".repeat(len),
prim.as_sym()
)?;
needs_termination = true;
}
Some(&def_id) => {
let loc = match m.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
let builder: UrlPartsBuilder =
[s.as_str().trim_end_matches('/'), cname_sym.as_str()]
.into_iter()
.collect();
Some(builder)
}
ExternalLocation::Local => {
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
Some(if cx.current.first() == Some(&cname_sym) {
iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
} else {
iter::repeat(sym::dotdot)
.take(cx.current.len())
.chain(iter::once(cname_sym))
.collect()
})
}
ExternalLocation::Unknown => None,
};
if let Some(mut loc) = loc {
loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
needs_termination = true;

if !f.alternate()
&& let Some(&def_id) = m.primitive_locations.get(&prim)
{
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
if def_id.is_local() {
let len = cx.current.len();
write!(
f,
"<a class=\"primitive\" href=\"{}{}/primitive.{}.html{fragment}\">",
"../".repeat(len),
cname_sym,
prim.as_sym()
)?;
needs_termination = true;
} else {
let loc = match m.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
let builder: UrlPartsBuilder =
[s.as_str().trim_end_matches('/'), cname_sym.as_str()]
.into_iter()
.collect();
Some(builder)
}
ExternalLocation::Local => Some(if cx.current.first() == Some(&cname_sym) {
iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
} else {
iter::repeat(sym::dotdot)
.take(cx.current.len())
.chain(iter::once(cname_sym))
.collect()
}),
ExternalLocation::Unknown => None,
};

if let Some(mut loc) = loc {
loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
needs_termination = true;
}
None => {}
}
}
Display::fmt(&name, f)?;
Expand Down

0 comments on commit 147a678

Please sign in to comment.