Skip to content

Commit 3f10604

Browse files
authored
Rollup merge of rust-lang#113623 - GuillaumeGomez:add-jump-to-doc, r=notriddle
Add jump to doc I'm using the source code pages of the compiler quite a lot, but one thing missing is the possibility to jump back from the source code to the item documentation. Since I also got a few others complaining about it, I think it's fine to add it since this option is nightly only. This PR adds a link to jump back to item's documentation on the item definition (so on `Bar` in `struct Bar {... }`, as described in the unofficial [RFC](https://github.com/GuillaumeGomez/rfcs/blob/rustdoc-jump-to-definition/text/000-rustdoc-jump-to-definition.md)). r? `@notriddle`
2 parents fa1ad0a + 6d44879 commit 3f10604

File tree

6 files changed

+148
-37
lines changed

6 files changed

+148
-37
lines changed

src/librustdoc/html/highlight.rs

+5
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,11 @@ fn string_without_closing_tag<T: Display>(
988988
)
989989
.ok()
990990
.map(|(url, _, _)| url),
991+
LinkFromSrc::Doc(def_id) => {
992+
format::href_with_root_path(*def_id, context, Some(&href_context.root_path))
993+
.ok()
994+
.map(|(doc_link, _, _)| doc_link)
995+
}
991996
}
992997
})
993998
{

src/librustdoc/html/render/context.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,12 @@ impl<'tcx> Context<'tcx> {
349349
let e = ExternalCrate { crate_num: cnum };
350350
(e.name(self.tcx()), e.src_root(self.tcx()))
351351
}
352-
ExternalLocation::Unknown => return None,
352+
ExternalLocation::Unknown => {
353+
let e = ExternalCrate { crate_num: cnum };
354+
let name = e.name(self.tcx());
355+
root = name.to_string();
356+
(name, e.src_root(self.tcx()))
357+
}
353358
};
354359

355360
let href = RefCell::new(PathBuf::new());

src/librustdoc/html/render/span_map.rs

+73-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::clean::{self, PrimitiveType};
1+
use crate::clean::{self, rustc_span, PrimitiveType};
22
use crate::html::sources;
33

44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir::def::{DefKind, Res};
6-
use rustc_hir::def_id::DefId;
6+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
77
use rustc_hir::intravisit::{self, Visitor};
8-
use rustc_hir::{ExprKind, HirId, Mod, Node};
8+
use rustc_hir::{ExprKind, HirId, Item, ItemKind, Mod, Node};
99
use rustc_middle::hir::nested_filter;
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_span::hygiene::MacroKind;
@@ -25,6 +25,7 @@ pub(crate) enum LinkFromSrc {
2525
Local(clean::Span),
2626
External(DefId),
2727
Primitive(PrimitiveType),
28+
Doc(DefId),
2829
}
2930

3031
/// This function will do at most two things:
@@ -65,24 +66,43 @@ struct SpanMapVisitor<'tcx> {
6566
impl<'tcx> SpanMapVisitor<'tcx> {
6667
/// This function is where we handle `hir::Path` elements and add them into the "span map".
6768
fn handle_path(&mut self, path: &rustc_hir::Path<'_>) {
68-
let info = match path.res {
69+
match path.res {
6970
// FIXME: For now, we handle `DefKind` if it's not a `DefKind::TyParam`.
7071
// Would be nice to support them too alongside the other `DefKind`
7172
// (such as primitive types!).
72-
Res::Def(kind, def_id) if kind != DefKind::TyParam => Some(def_id),
73-
Res::Local(_) => None,
73+
Res::Def(kind, def_id) if kind != DefKind::TyParam => {
74+
let link = if def_id.as_local().is_some() {
75+
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
76+
} else {
77+
LinkFromSrc::External(def_id)
78+
};
79+
self.matches.insert(path.span, link);
80+
}
81+
Res::Local(_) => {
82+
if let Some(span) = self.tcx.hir().res_span(path.res) {
83+
self.matches.insert(path.span, LinkFromSrc::Local(clean::Span::new(span)));
84+
}
85+
}
7486
Res::PrimTy(p) => {
7587
// FIXME: Doesn't handle "path-like" primitives like arrays or tuples.
7688
self.matches.insert(path.span, LinkFromSrc::Primitive(PrimitiveType::from(p)));
77-
return;
7889
}
79-
Res::Err => return,
80-
_ => return,
81-
};
82-
if let Some(span) = self.tcx.hir().res_span(path.res) {
83-
self.matches.insert(path.span, LinkFromSrc::Local(clean::Span::new(span)));
84-
} else if let Some(def_id) = info {
85-
self.matches.insert(path.span, LinkFromSrc::External(def_id));
90+
Res::Err => {}
91+
_ => {}
92+
}
93+
}
94+
95+
/// Used to generate links on items' definition to go to their documentation page.
96+
pub(crate) fn extract_info_from_hir_id(&mut self, hir_id: HirId) {
97+
if let Some(Node::Item(item)) = self.tcx.hir().find(hir_id) {
98+
if let Some(span) = self.tcx.def_ident_span(item.owner_id) {
99+
let cspan = clean::Span::new(span);
100+
// If the span isn't from the current crate, we ignore it.
101+
if cspan.inner().is_dummy() || cspan.cnum(self.tcx.sess) != LOCAL_CRATE {
102+
return;
103+
}
104+
self.matches.insert(span, LinkFromSrc::Doc(item.owner_id.to_def_id()));
105+
}
86106
}
87107
}
88108

@@ -117,10 +137,13 @@ impl<'tcx> SpanMapVisitor<'tcx> {
117137
_ => return true,
118138
};
119139
let link_from_src = match data.macro_def_id {
120-
Some(macro_def_id) if macro_def_id.is_local() => {
121-
LinkFromSrc::Local(clean::Span::new(data.def_site))
140+
Some(macro_def_id) => {
141+
if macro_def_id.is_local() {
142+
LinkFromSrc::Local(clean::Span::new(data.def_site))
143+
} else {
144+
LinkFromSrc::External(macro_def_id)
145+
}
122146
}
123-
Some(macro_def_id) => LinkFromSrc::External(macro_def_id),
124147
None => return true,
125148
};
126149
let new_span = data.call_site;
@@ -160,6 +183,9 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
160183
LinkFromSrc::Local(clean::Span::new(m.spans.inner_span)),
161184
);
162185
}
186+
} else {
187+
// If it's a "mod foo {}", we want to look to its documentation page.
188+
self.extract_info_from_hir_id(id);
163189
}
164190
intravisit::walk_mod(self, m, id);
165191
}
@@ -176,18 +202,41 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
176202
.tcx
177203
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
178204
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
179-
self.matches.insert(
180-
segment.ident.span,
181-
match hir.span_if_local(def_id) {
182-
Some(span) => LinkFromSrc::Local(clean::Span::new(span)),
183-
None => LinkFromSrc::External(def_id),
184-
},
185-
);
205+
let link = if def_id.as_local().is_some() {
206+
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
207+
} else {
208+
LinkFromSrc::External(def_id)
209+
};
210+
self.matches.insert(segment.ident.span, link);
186211
}
187212
} else if self.handle_macro(expr.span) {
188213
// We don't want to go deeper into the macro.
189214
return;
190215
}
191216
intravisit::walk_expr(self, expr);
192217
}
218+
219+
fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
220+
match item.kind {
221+
ItemKind::Static(_, _, _)
222+
| ItemKind::Const(_, _)
223+
| ItemKind::Fn(_, _, _)
224+
| ItemKind::Macro(_, _)
225+
| ItemKind::TyAlias(_, _)
226+
| ItemKind::Enum(_, _)
227+
| ItemKind::Struct(_, _)
228+
| ItemKind::Union(_, _)
229+
| ItemKind::Trait(_, _, _, _, _)
230+
| ItemKind::TraitAlias(_, _) => self.extract_info_from_hir_id(item.hir_id()),
231+
ItemKind::Impl(_)
232+
| ItemKind::Use(_, _)
233+
| ItemKind::ExternCrate(_)
234+
| ItemKind::ForeignMod { .. }
235+
| ItemKind::GlobalAsm(_)
236+
| ItemKind::OpaqueTy(_)
237+
// We already have "visit_mod" above so no need to check it here.
238+
| ItemKind::Mod(_) => {}
239+
}
240+
intravisit::walk_item(self, item);
241+
}
193242
}

tests/rustdoc-gui/source-anchor-scroll.goml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ set-window-size: (600, 800)
77
// We check that the scroll is at the top first.
88
assert-property: ("html", {"scrollTop": "0"})
99

10-
click: '//a[text() = "barbar"]'
10+
click: '//a[text() = "barbar" and @href="#5-7"]'
1111
assert-property: ("html", {"scrollTop": "149"})
12-
click: '//a[text() = "bar"]'
12+
click: '//a[text() = "bar" and @href="#28-36"]'
1313
assert-property: ("html", {"scrollTop": "180"})
14-
click: '//a[text() = "sub_fn"]'
14+
click: '//a[text() = "sub_fn" and @href="#2-4"]'
1515
assert-property: ("html", {"scrollTop": "77"})
1616

1717
// We now check that clicking on lines doesn't change the scroll

tests/rustdoc/check-source-code-urls-to-def.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ extern crate source_code;
1414
#[path = "auxiliary/source-code-bar.rs"]
1515
pub mod bar;
1616

17-
// @count - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#5"]' 4
17+
// @count - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#5-7"]' 4
1818
use bar::Bar;
19-
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#13"]' 'self'
20-
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
19+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#13-17"]' 'self'
20+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14-16"]' 'Trait'
2121
use bar::sub::{self, Trait};
2222

2323
pub struct Foo;
@@ -32,7 +32,8 @@ fn babar() {}
3232
// @has - '//pre[@class="rust"]//a/@href' '/primitive.u32.html'
3333
// @has - '//pre[@class="rust"]//a/@href' '/primitive.str.html'
3434
// @count - '//pre[@class="rust"]//a[@href="#23"]' 5
35-
// @has - '//pre[@class="rust"]//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
35+
// @has - '//pre[@class="rust"]//a[@href="../../source_code/struct.SourceCode.html"]' \
36+
// 'source_code::SourceCode'
3637
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
3738
let x = 12;
3839
let y: Foo = Foo;
@@ -42,15 +43,15 @@ pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::Sour
4243
y.hello();
4344
}
4445

45-
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'bar::sub::Trait'
46-
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
46+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14-16"]' 'bar::sub::Trait'
47+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14-16"]' 'Trait'
4748
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {}
4849

4950
pub trait AnotherTrait {}
5051
pub trait WhyNot {}
5152

52-
// @has - '//pre[@class="rust"]//a[@href="#49"]' 'AnotherTrait'
53-
// @has - '//pre[@class="rust"]//a[@href="#50"]' 'WhyNot'
53+
// @has - '//pre[@class="rust"]//a[@href="#50"]' 'AnotherTrait'
54+
// @has - '//pre[@class="rust"]//a[@href="#51"]' 'WhyNot'
5455
pub fn foo3<T, V>(t: &T, v: &V)
5556
where
5657
T: AnotherTrait,
@@ -59,7 +60,7 @@ where
5960

6061
pub trait AnotherTrait2 {}
6162

62-
// @has - '//pre[@class="rust"]//a[@href="#60"]' 'AnotherTrait2'
63+
// @has - '//pre[@class="rust"]//a[@href="#61"]' 'AnotherTrait2'
6364
pub fn foo4() {
6465
let x: Vec<AnotherTrait2> = Vec::new();
6566
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// compile-flags: -Zunstable-options --generate-link-to-definition
2+
3+
#![crate_name = "foo"]
4+
5+
// @has 'src/foo/jump-to-def-doc-links.rs.html'
6+
7+
// @has - '//a[@href="../../foo/struct.Bar.html"]' 'Bar'
8+
// @has - '//a[@href="../../foo/struct.Foo.html"]' 'Foo'
9+
pub struct Bar; pub struct Foo;
10+
11+
// @has - '//a[@href="../../foo/enum.Enum.html"]' 'Enum'
12+
pub enum Enum {
13+
Variant1(String),
14+
Variant2(u8),
15+
}
16+
17+
// @has - '//a[@href="../../foo/struct.Struct.html"]' 'Struct'
18+
pub struct Struct {
19+
pub a: u8,
20+
b: Foo,
21+
}
22+
23+
impl Struct {
24+
pub fn foo() {}
25+
pub fn foo2(&self) {}
26+
fn bar() {}
27+
fn bar(&self) {}
28+
}
29+
30+
// @has - '//a[@href="../../foo/trait.Trait.html"]' 'Trait'
31+
pub trait Trait {
32+
fn foo();
33+
}
34+
35+
impl Trait for Struct {
36+
fn foo() {}
37+
}
38+
39+
// @has - '//a[@href="../../foo/union.Union.html"]' 'Union'
40+
pub union Union {
41+
pub a: u16,
42+
pub f: u32,
43+
}
44+
45+
// @has - '//a[@href="../../foo/fn.bar.html"]' 'bar'
46+
pub fn bar(b: Bar) {
47+
let x = Foo;
48+
}
49+
50+
// @has - '//a[@href="../../foo/bar/index.html"]' 'bar'
51+
pub mod bar {}

0 commit comments

Comments
 (0)