Skip to content

Commit 2cddda3

Browse files
authored
Rollup merge of #84380 - Smittyvb:rdoc-title-order, r=jsha
Write Rustdoc titles like "x in crate::mod - Rust" This makes Rustdoc titles for items be like "Widget in cratename::blah::foo - Rust". Titles for modules and other non-items are unchanged, and still read like "cratename::blah::foo - Rust". This makes managing several open Rustdoc tabs easier. ![A screenshot of several open Rustdoc tabs](https://user-images.githubusercontent.com/10530973/115457675-d608f180-a1f2-11eb-87a8-838a32b4e3f7.png) This also adds some tests for the new title behavior. Closes #84371.
2 parents e7f2033 + df147c7 commit 2cddda3

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

src/librustdoc/html/render/context.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,18 @@ impl<'tcx> Context<'tcx> {
167167
"../".repeat(self.current.len())
168168
}
169169

170-
fn render_item(&self, it: &clean::Item, pushname: bool) -> String {
171-
let mut title = if it.is_primitive() || it.is_keyword() {
172-
// No need to include the namespace for primitive types and keywords
173-
String::new()
174-
} else {
175-
self.current.join("::")
176-
};
177-
if pushname {
178-
if !title.is_empty() {
179-
title.push_str("::");
180-
}
170+
fn render_item(&self, it: &clean::Item, is_module: bool) -> String {
171+
let mut title = String::new();
172+
if !is_module {
181173
title.push_str(&it.name.unwrap().as_str());
182174
}
175+
if !it.is_primitive() && !it.is_keyword() {
176+
if !is_module {
177+
title.push_str(" in ");
178+
}
179+
// No need to include the namespace for primitive types and keywords
180+
title.push_str(&self.current.join("::"));
181+
};
183182
title.push_str(" - Rust");
184183
let tyname = it.type_();
185184
let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(&doc));
@@ -598,7 +597,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
598597

599598
info!("Recursing into {}", self.dst.display());
600599

601-
let buf = self.render_item(item, false);
600+
let buf = self.render_item(item, true);
602601
// buf will be empty if the module is stripped and there is no redirect for it
603602
if !buf.is_empty() {
604603
self.shared.ensure_dir(&self.dst)?;
@@ -641,7 +640,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
641640
self.render_redirect_pages = item.is_stripped();
642641
}
643642

644-
let buf = self.render_item(&item, true);
643+
let buf = self.render_item(&item, false);
645644
// buf will be empty if the item is stripped and there is no redirect for it
646645
if !buf.is_empty() {
647646
let name = item.name.as_ref().unwrap();

src/test/rustdoc/prim-title.rs

-7
This file was deleted.

src/test/rustdoc/tab_title.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#![crate_name = "foo"]
2+
#![feature(doc_keyword)]
3+
4+
// tests for the html <title> element
5+
6+
// @has foo/index.html '//head/title' 'foo - Rust'
7+
8+
// @has foo/fn.widget_count.html '//head/title' 'widget_count in foo - Rust'
9+
/// blah
10+
pub fn widget_count() {}
11+
12+
// @has foo/struct.Widget.html '//head/title' 'Widget in foo - Rust'
13+
pub struct Widget;
14+
15+
// @has foo/constant.ANSWER.html '//head/title' 'ANSWER in foo - Rust'
16+
pub const ANSWER: u8 = 42;
17+
18+
// @has foo/blah/index.html '//head/title' 'foo::blah - Rust'
19+
pub mod blah {
20+
// @has foo/blah/struct.Widget.html '//head/title' 'Widget in foo::blah - Rust'
21+
pub struct Widget;
22+
23+
// @has foo/blah/trait.Awesome.html '//head/title' 'Awesome in foo::blah - Rust'
24+
pub trait Awesome {}
25+
26+
// @has foo/blah/fn.make_widget.html '//head/title' 'make_widget in foo::blah - Rust'
27+
pub fn make_widget() {}
28+
29+
// @has foo/macro.cool_macro.html '//head/title' 'cool_macro in foo - Rust'
30+
#[macro_export]
31+
macro_rules! cool_macro {
32+
($t:tt) => { $t }
33+
}
34+
}
35+
36+
// @has foo/keyword.continue.html '//head/title' 'continue - Rust'
37+
#[doc(keyword = "continue")]
38+
mod continue_keyword {}
39+
40+
// @has foo/primitive.u8.html '//head/title' 'u8 - Rust'
41+
// @!has - '//head/title' 'foo'
42+
#[doc(primitive = "u8")]
43+
/// `u8` docs
44+
mod u8 {}

0 commit comments

Comments
 (0)