Skip to content

Commit

Permalink
Rollup merge of #84380 - Smittyvb:rdoc-title-order, r=jsha
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-ou-se authored Apr 21, 2021
2 parents e7f2033 + df147c7 commit 2cddda3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
25 changes: 12 additions & 13 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,18 @@ impl<'tcx> Context<'tcx> {
"../".repeat(self.current.len())
}

fn render_item(&self, it: &clean::Item, pushname: bool) -> String {
let mut title = if it.is_primitive() || it.is_keyword() {
// No need to include the namespace for primitive types and keywords
String::new()
} else {
self.current.join("::")
};
if pushname {
if !title.is_empty() {
title.push_str("::");
}
fn render_item(&self, it: &clean::Item, is_module: bool) -> String {
let mut title = String::new();
if !is_module {
title.push_str(&it.name.unwrap().as_str());
}
if !it.is_primitive() && !it.is_keyword() {
if !is_module {
title.push_str(" in ");
}
// No need to include the namespace for primitive types and keywords
title.push_str(&self.current.join("::"));
};
title.push_str(" - Rust");
let tyname = it.type_();
let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(&doc));
Expand Down Expand Up @@ -598,7 +597,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {

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

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

let buf = self.render_item(&item, true);
let buf = self.render_item(&item, false);
// buf will be empty if the item is stripped and there is no redirect for it
if !buf.is_empty() {
let name = item.name.as_ref().unwrap();
Expand Down
7 changes: 0 additions & 7 deletions src/test/rustdoc/prim-title.rs

This file was deleted.

44 changes: 44 additions & 0 deletions src/test/rustdoc/tab_title.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![crate_name = "foo"]
#![feature(doc_keyword)]

// tests for the html <title> element

// @has foo/index.html '//head/title' 'foo - Rust'

// @has foo/fn.widget_count.html '//head/title' 'widget_count in foo - Rust'
/// blah
pub fn widget_count() {}

// @has foo/struct.Widget.html '//head/title' 'Widget in foo - Rust'
pub struct Widget;

// @has foo/constant.ANSWER.html '//head/title' 'ANSWER in foo - Rust'
pub const ANSWER: u8 = 42;

// @has foo/blah/index.html '//head/title' 'foo::blah - Rust'
pub mod blah {
// @has foo/blah/struct.Widget.html '//head/title' 'Widget in foo::blah - Rust'
pub struct Widget;

// @has foo/blah/trait.Awesome.html '//head/title' 'Awesome in foo::blah - Rust'
pub trait Awesome {}

// @has foo/blah/fn.make_widget.html '//head/title' 'make_widget in foo::blah - Rust'
pub fn make_widget() {}

// @has foo/macro.cool_macro.html '//head/title' 'cool_macro in foo - Rust'
#[macro_export]
macro_rules! cool_macro {
($t:tt) => { $t }
}
}

// @has foo/keyword.continue.html '//head/title' 'continue - Rust'
#[doc(keyword = "continue")]
mod continue_keyword {}

// @has foo/primitive.u8.html '//head/title' 'u8 - Rust'
// @!has - '//head/title' 'foo'
#[doc(primitive = "u8")]
/// `u8` docs
mod u8 {}

0 comments on commit 2cddda3

Please sign in to comment.