1
Fork 0

Add missing root_path when generating links using href

This commit is contained in:
Guillaume Gomez 2021-06-12 22:08:43 +02:00 committed by Guillaume Gomez
parent 1a48d1a4de
commit fd69fa8670
5 changed files with 35 additions and 5 deletions

View file

@ -484,7 +484,11 @@ crate enum HrefError {
NotInExternalCache, NotInExternalCache,
} }
crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<String>), HrefError> { crate fn href_with_root_path(
did: DefId,
cx: &Context<'_>,
root_path: Option<&str>,
) -> Result<(String, ItemType, Vec<String>), HrefError> {
let cache = &cx.cache(); let cache = &cx.cache();
let relative_to = &cx.current; let relative_to = &cx.current;
fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] { fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] {
@ -495,6 +499,7 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
return Err(HrefError::Private); return Err(HrefError::Private);
} }
let mut is_remote = false;
let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) { let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) {
Some(&(ref fqp, shortty)) => (fqp, shortty, { Some(&(ref fqp, shortty)) => (fqp, shortty, {
let module_fqp = to_module_fqp(shortty, fqp); let module_fqp = to_module_fqp(shortty, fqp);
@ -508,6 +513,7 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
shortty, shortty,
match cache.extern_locations[&did.krate] { match cache.extern_locations[&did.krate] {
ExternalLocation::Remote(ref s) => { ExternalLocation::Remote(ref s) => {
is_remote = true;
let s = s.trim_end_matches('/'); let s = s.trim_end_matches('/');
let mut s = vec![s]; let mut s = vec![s];
s.extend(module_fqp[..].iter().map(String::as_str)); s.extend(module_fqp[..].iter().map(String::as_str));
@ -522,6 +528,12 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
} }
} }
}; };
if !is_remote {
if let Some(root_path) = root_path {
let root = root_path.trim_end_matches('/');
url_parts.insert(0, root);
}
}
let last = &fqp.last().unwrap()[..]; let last = &fqp.last().unwrap()[..];
let filename; let filename;
match shortty { match shortty {
@ -536,6 +548,10 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
Ok((url_parts.join("/"), shortty, fqp.to_vec())) Ok((url_parts.join("/"), shortty, fqp.to_vec()))
} }
crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<String>), HrefError> {
href_with_root_path(did, cx, None)
}
/// Both paths should only be modules. /// Both paths should only be modules.
/// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will /// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will
/// both need `../iter/trait.Iterator.html` to get at the iterator trait. /// both need `../iter/trait.Iterator.html` to get at the iterator trait.

View file

@ -580,12 +580,19 @@ fn string<T: Display>(
if let Some(href) = if let Some(href) =
context_info.context.shared.span_correspondance_map.get(&def_span).and_then(|href| { context_info.context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
let context = context_info.context; let context = context_info.context;
// FIXME: later on, it'd be nice to provide two links (if possible) for all items:
// one to the documentation page and one to the source definition.
// FIXME: currently, external items only generate a link to their documentation,
// a link to their definition can be generated using this:
// https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338
match href { match href {
LinkFromSrc::Local(span) => context LinkFromSrc::Local(span) => context
.href_from_span(*span) .href_from_span(*span)
.map(|s| format!("{}{}", context_info.root_path, s)), .map(|s| format!("{}{}", context_info.root_path, s)),
LinkFromSrc::External(def_id) => { LinkFromSrc::External(def_id) => {
format::href(*def_id, context).map(|(url, _, _)| url) format::href_with_root_path(*def_id, context, Some(context_info.root_path))
.ok()
.map(|(url, _, _)| url)
} }
} }
}) })

View file

@ -110,6 +110,7 @@ impl<'tcx> SpanMapVisitor<'tcx> {
Some(def_id) Some(def_id)
} }
Res::Local(_) => None, Res::Local(_) => None,
Res::Err => return,
_ => return, _ => return,
}; };
if let Some(span) = self.tcx.hir().res_span(path.res) { if let Some(span) = self.tcx.hir().res_span(path.res) {

View file

@ -0,0 +1 @@
pub struct SourceCode;

View file

@ -1,7 +1,11 @@
// compile-flags: -Zunstable-options --generate-link-to-definition // compile-flags: -Zunstable-options --generate-link-to-definition
// aux-build:source_code.rs
// build-aux-docs
#![crate_name = "foo"] #![crate_name = "foo"]
extern crate source_code;
// @has 'src/foo/check-source-code-urls-to-def.rs.html' // @has 'src/foo/check-source-code-urls-to-def.rs.html'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#1-17"]' 'bar' // @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#1-17"]' 'bar'
@ -23,13 +27,14 @@ impl Foo {
fn babar() {} fn babar() {}
// @has - '//a[@href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html"]' 'String' // @has - '//a[@href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html"]' 'String'
// @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#17"]' 5 // @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#21"]' 5
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar) { // @has - '//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
let x = 12; let x = 12;
let y: Foo = Foo; let y: Foo = Foo;
let z: Bar = bar::Bar { field: Foo }; let z: Bar = bar::Bar { field: Foo };
babar(); babar();
// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#20"]' 'hello' // @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#24"]' 'hello'
y.hello(); y.hello();
} }