From 0c193f82e7525cba88d67320fc3d706683a9cb9b Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 17:31:18 -0400 Subject: 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 05d20013859..3fcf972a5b8 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 00000000000..6f96f98e707 --- /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 00000000000..d0fbdf95ddc --- /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 00000000000..6b7f67d17ab --- /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() {} +} -- cgit 1.4.1-3-g733a5 From 7cf4f4276f691b3052df64930a9f02e33e2783df Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 18:53:15 -0400 Subject: 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 3fcf972a5b8..397e03afae6 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 -- cgit 1.4.1-3-g733a5 From a9ff7ac9c3e583e7723722319d48caf35bdc0efb Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 18:57:26 -0400 Subject: 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 d0fbdf95ddc..4c0d233fbec 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 6b7f67d17ab..00000000000 --- 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() {} -} -- cgit 1.4.1-3-g733a5 From 05121a22e61c02933ea1c619e4126b4dafe2a2d1 Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 19:10:00 -0400 Subject: 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 397e03afae6..0aa7aa763c2 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(); -- cgit 1.4.1-3-g733a5 From 3ddafb2d7c96787e692e90c76f2ca5bdb936cb0c Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 19:53:44 -0400 Subject: Add test for title of root page in item-title.rs --- src/test/rustdoc/crate-title.rs | 3 --- src/test/rustdoc/item-title.rs | 34 ---------------------------------- src/test/rustdoc/title.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 37 deletions(-) delete mode 100644 src/test/rustdoc/crate-title.rs delete mode 100644 src/test/rustdoc/item-title.rs create mode 100644 src/test/rustdoc/title.rs diff --git a/src/test/rustdoc/crate-title.rs b/src/test/rustdoc/crate-title.rs deleted file mode 100644 index 6f96f98e707..00000000000 --- 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/item-title.rs deleted file mode 100644 index 4c0d233fbec..00000000000 --- a/src/test/rustdoc/item-title.rs +++ /dev/null @@ -1,34 +0,0 @@ -#![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; - -// @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 {} diff --git a/src/test/rustdoc/title.rs b/src/test/rustdoc/title.rs new file mode 100644 index 00000000000..b0e22af0b1c --- /dev/null +++ b/src/test/rustdoc/title.rs @@ -0,0 +1,36 @@ +#![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() {} + +// @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 {} -- cgit 1.4.1-3-g733a5 From df147c718c5819631eefc774e6e40d4515f30c90 Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 20 Apr 2021 19:56:28 -0400 Subject: Just merge all of the tests into one --- src/test/rustdoc/prim-title.rs | 7 ------- src/test/rustdoc/tab_title.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ src/test/rustdoc/title.rs | 36 ---------------------------------- 3 files changed, 44 insertions(+), 43 deletions(-) delete mode 100644 src/test/rustdoc/prim-title.rs create mode 100644 src/test/rustdoc/tab_title.rs delete mode 100644 src/test/rustdoc/title.rs diff --git a/src/test/rustdoc/prim-title.rs b/src/test/rustdoc/prim-title.rs deleted file mode 100644 index fa3fd512fad..00000000000 --- 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/tab_title.rs b/src/test/rustdoc/tab_title.rs new file mode 100644 index 00000000000..7dce6092dea --- /dev/null +++ b/src/test/rustdoc/tab_title.rs @@ -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 {} diff --git a/src/test/rustdoc/title.rs b/src/test/rustdoc/title.rs deleted file mode 100644 index b0e22af0b1c..00000000000 --- a/src/test/rustdoc/title.rs +++ /dev/null @@ -1,36 +0,0 @@ -#![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() {} - -// @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 {} -- cgit 1.4.1-3-g733a5