From 0c193f82e7525cba88d67320fc3d706683a9cb9b Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 17:31:18 -0400 Subject: [PATCH 1/6] Write Rustdoc titles like "x in crate::mod - Rust" This makes Rustdoc titles for items read like "x in cratename::blah::foo - Rust". Title for modules and other non-items are unchanged, and still read like "doccratenameconst::blah::foo - Rust". This makes managing several open Rustdoc tabs easier. Closes #84371. --- src/librustdoc/html/render/context.rs | 17 +++++++------- src/test/rustdoc/crate-title.rs | 3 +++ src/test/rustdoc/item-title.rs | 33 +++++++++++++++++++++++++++ src/test/rustdoc/mod-title.rs | 12 ++++++++++ 4 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 src/test/rustdoc/crate-title.rs create mode 100644 src/test/rustdoc/item-title.rs create mode 100644 src/test/rustdoc/mod-title.rs diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 05d2001385929..3fcf972a5b887 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -168,18 +168,17 @@ impl<'tcx> Context<'tcx> { } 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("::") - }; + let mut title = String::new(); if pushname { - if !title.is_empty() { - title.push_str("::"); - } title.push_str(&it.name.unwrap().as_str()); } + if !it.is_primitive() && !it.is_keyword() { + if pushname { + 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)); diff --git a/src/test/rustdoc/crate-title.rs b/src/test/rustdoc/crate-title.rs new file mode 100644 index 0000000000000..6f96f98e7074f --- /dev/null +++ b/src/test/rustdoc/crate-title.rs @@ -0,0 +1,3 @@ +#![crate_name = "foo"] + +// @has foo/index.html '//head/title' 'foo - Rust' diff --git a/src/test/rustdoc/item-title.rs b/src/test/rustdoc/item-title.rs new file mode 100644 index 0000000000000..d0fbdf95ddce1 --- /dev/null +++ b/src/test/rustdoc/item-title.rs @@ -0,0 +1,33 @@ +#![crate_name = "foo"] +#![feature(doc_keyword)] + +// @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; + +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 {} diff --git a/src/test/rustdoc/mod-title.rs b/src/test/rustdoc/mod-title.rs new file mode 100644 index 0000000000000..6b7f67d17ab3a --- /dev/null +++ b/src/test/rustdoc/mod-title.rs @@ -0,0 +1,12 @@ +#![crate_name = "foo"] + +// @has foo/bar/index.html '//head/title' 'foo::bar - Rust' +/// blah +pub mod bar { + pub fn a() {} +} + +// @has foo/baz/index.html '//head/title' 'foo::baz - Rust' +pub mod baz { + pub fn a() {} +} From 7cf4f4276f691b3052df64930a9f02e33e2783df Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 18:53:15 -0400 Subject: [PATCH 2/6] Rename pushname to is_module --- src/librustdoc/html/render/context.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 3fcf972a5b887..397e03afae67d 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -167,13 +167,13 @@ impl<'tcx> Context<'tcx> { "../".repeat(self.current.len()) } - fn render_item(&self, it: &clean::Item, pushname: bool) -> String { + fn render_item(&self, it: &clean::Item, is_module: bool) -> String { let mut title = String::new(); - if pushname { + if is_module { title.push_str(&it.name.unwrap().as_str()); } if !it.is_primitive() && !it.is_keyword() { - if pushname { + if is_module { title.push_str(" in "); } // No need to include the namespace for primitive types and keywords From a9ff7ac9c3e583e7723722319d48caf35bdc0efb Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 18:57:26 -0400 Subject: [PATCH 3/6] Merge mod-title and item-title tests --- src/test/rustdoc/item-title.rs | 1 + src/test/rustdoc/mod-title.rs | 12 ------------ 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 src/test/rustdoc/mod-title.rs diff --git a/src/test/rustdoc/item-title.rs b/src/test/rustdoc/item-title.rs index d0fbdf95ddce1..4c0d233fbec31 100644 --- a/src/test/rustdoc/item-title.rs +++ b/src/test/rustdoc/item-title.rs @@ -11,6 +11,7 @@ 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; diff --git a/src/test/rustdoc/mod-title.rs b/src/test/rustdoc/mod-title.rs deleted file mode 100644 index 6b7f67d17ab3a..0000000000000 --- a/src/test/rustdoc/mod-title.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![crate_name = "foo"] - -// @has foo/bar/index.html '//head/title' 'foo::bar - Rust' -/// blah -pub mod bar { - pub fn a() {} -} - -// @has foo/baz/index.html '//head/title' 'foo::baz - Rust' -pub mod baz { - pub fn a() {} -} From 05121a22e61c02933ea1c619e4126b4dafe2a2d1 Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 19:10:00 -0400 Subject: [PATCH 4/6] fix is_module check --- src/librustdoc/html/render/context.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 397e03afae67d..0aa7aa763c2af 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -169,11 +169,11 @@ impl<'tcx> Context<'tcx> { fn render_item(&self, it: &clean::Item, is_module: bool) -> String { let mut title = String::new(); - if is_module { + if !is_module { title.push_str(&it.name.unwrap().as_str()); } if !it.is_primitive() && !it.is_keyword() { - if is_module { + if !is_module { title.push_str(" in "); } // No need to include the namespace for primitive types and keywords @@ -597,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)?; @@ -640,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(); From 3ddafb2d7c96787e692e90c76f2ca5bdb936cb0c Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 19:53:44 -0400 Subject: [PATCH 5/6] Add test for title of root page in item-title.rs --- src/test/rustdoc/crate-title.rs | 3 --- src/test/rustdoc/{item-title.rs => title.rs} | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) delete mode 100644 src/test/rustdoc/crate-title.rs rename src/test/rustdoc/{item-title.rs => title.rs} (95%) diff --git a/src/test/rustdoc/crate-title.rs b/src/test/rustdoc/crate-title.rs deleted file mode 100644 index 6f96f98e7074f..0000000000000 --- a/src/test/rustdoc/crate-title.rs +++ /dev/null @@ -1,3 +0,0 @@ -#![crate_name = "foo"] - -// @has foo/index.html '//head/title' 'foo - Rust' diff --git a/src/test/rustdoc/item-title.rs b/src/test/rustdoc/title.rs similarity index 95% rename from src/test/rustdoc/item-title.rs rename to src/test/rustdoc/title.rs index 4c0d233fbec31..b0e22af0b1c7f 100644 --- a/src/test/rustdoc/item-title.rs +++ b/src/test/rustdoc/title.rs @@ -1,6 +1,8 @@ #![crate_name = "foo"] #![feature(doc_keyword)] +// @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() {} From df147c718c5819631eefc774e6e40d4515f30c90 Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 19:56:28 -0400 Subject: [PATCH 6/6] Just merge all of the tests into one --- src/test/rustdoc/prim-title.rs | 7 ------- src/test/rustdoc/{title.rs => tab_title.rs} | 8 ++++++++ 2 files changed, 8 insertions(+), 7 deletions(-) delete mode 100644 src/test/rustdoc/prim-title.rs rename src/test/rustdoc/{title.rs => tab_title.rs} (86%) diff --git a/src/test/rustdoc/prim-title.rs b/src/test/rustdoc/prim-title.rs deleted file mode 100644 index fa3fd512fada6..0000000000000 --- a/src/test/rustdoc/prim-title.rs +++ /dev/null @@ -1,7 +0,0 @@ -#![crate_name = "foo"] - -// @has foo/primitive.u8.html '//head/title' 'u8 - Rust' -// @!has - '//head/title' 'foo' -#[doc(primitive = "u8")] -/// `u8` docs -mod u8 {} diff --git a/src/test/rustdoc/title.rs b/src/test/rustdoc/tab_title.rs similarity index 86% rename from src/test/rustdoc/title.rs rename to src/test/rustdoc/tab_title.rs index b0e22af0b1c7f..7dce6092deaed 100644 --- a/src/test/rustdoc/title.rs +++ b/src/test/rustdoc/tab_title.rs @@ -1,6 +1,8 @@ #![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' @@ -34,3 +36,9 @@ pub mod blah { // @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 {}