From cbe4ac30797f9e994b3ae275e3edf12b2d450800 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Wed, 4 Oct 2017 15:51:35 -0500 Subject: [PATCH 1/7] spotlight Iterator/Read/Write impls on function return types --- src/libcore/iter/iterator.rs | 1 + src/librustdoc/clean/inline.rs | 2 + src/librustdoc/clean/mod.rs | 21 +++++-- src/librustdoc/html/render.rs | 79 +++++++++++++++++++++++--- src/librustdoc/html/static/main.js | 4 +- src/librustdoc/html/static/rustdoc.css | 5 +- src/libstd/io/mod.rs | 2 + 7 files changed, 99 insertions(+), 15 deletions(-) diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index 6a4dba31b62a9..a2bd15bdf05c7 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -30,6 +30,7 @@ fn _assert_is_object_safe(_: &Iterator) {} #[stable(feature = "rust1", since = "1.0.0")] #[rustc_on_unimplemented = "`{Self}` is not an iterator; maybe try calling \ `.iter()` or a similar method"] +#[doc(spotlight)] pub trait Iterator { /// The type of the elements being iterated over. #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 9fb9437e1bc9a..ce04be67aa4bc 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -140,11 +140,13 @@ pub fn build_external_trait(cx: &DocContext, did: DefId) -> clean::Trait { let generics = (cx.tcx.generics_of(did), &predicates).clean(cx); let generics = filter_non_trait_generics(did, generics); let (generics, supertrait_bounds) = separate_supertrait_bounds(generics); + let is_spotlight = load_attrs(cx, did).has_doc_flag("spotlight"); clean::Trait { unsafety: cx.tcx.trait_def(did).unsafety, generics, items: trait_items, bounds: supertrait_bounds, + is_spotlight, } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 1d107c169b046..69226239c96e8 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -151,7 +151,7 @@ impl<'a, 'tcx> Clean for visit_ast::RustdocVisitor<'a, 'tcx> { match module.inner { ModuleItem(ref module) => { for it in &module.items { - if it.is_extern_crate() && it.attrs.has_doc_masked() { + if it.is_extern_crate() && it.attrs.has_doc_flag("masked") { masked_crates.insert(it.def_id.krate); } } @@ -596,12 +596,12 @@ impl Attributes { None } - pub fn has_doc_masked(&self) -> bool { + pub fn has_doc_flag(&self, flag: &str) -> bool { for attr in &self.other_attrs { if !attr.check_name("doc") { continue; } if let Some(items) = attr.meta_item_list() { - if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name("masked")) { + if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) { return true; } } @@ -1331,19 +1331,31 @@ impl Clean for hir::FunctionRetTy { } } +impl GetDefId for FunctionRetTy { + fn def_id(&self) -> Option { + match *self { + Return(ref ty) => ty.def_id(), + DefaultReturn => None, + } + } +} + #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct Trait { pub unsafety: hir::Unsafety, pub items: Vec, pub generics: Generics, pub bounds: Vec, + pub is_spotlight: bool, } impl Clean for doctree::Trait { fn clean(&self, cx: &DocContext) -> Item { + let attrs = self.attrs.clean(cx); + let is_spotlight = attrs.has_doc_flag("spotlight"); Item { name: Some(self.name.clean(cx)), - attrs: self.attrs.clean(cx), + attrs: attrs, source: self.whence.clean(cx), def_id: cx.tcx.hir.local_def_id(self.id), visibility: self.vis.clean(cx), @@ -1354,6 +1366,7 @@ impl Clean for doctree::Trait { items: self.items.clean(cx), generics: self.generics.clean(cx), bounds: self.bounds.clean(cx), + is_spotlight: is_spotlight, }), } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index e848a8d3853b9..d43a73c986453 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1816,7 +1816,8 @@ fn plain_summary_line(s: Option<&str>) -> String { fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Result { document_stability(w, cx, item)?; - let prefix = render_assoc_const_value(item); + let mut prefix = render_assoc_const_value(item); + prefix.push_str(&render_spotlight_traits(item)?); document_full(w, item, cx, &prefix)?; Ok(()) } @@ -2603,10 +2604,10 @@ fn assoc_const(w: &mut fmt::Formatter, Ok(()) } -fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item, - bounds: &Vec, - default: Option<&clean::Type>, - link: AssocItemLink) -> fmt::Result { +fn assoc_type(w: &mut W, it: &clean::Item, + bounds: &Vec, + default: Option<&clean::Type>, + link: AssocItemLink) -> fmt::Result { write!(w, "type {}", naive_assoc_href(it, link), it.name.as_ref().unwrap())?; @@ -3236,6 +3237,62 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool) -> bool { } } +fn render_spotlight_traits(item: &clean::Item) -> Result { + let mut out = String::new(); + + match item.inner { + clean::FunctionItem(clean::Function { ref decl, .. }) | + clean::TyMethodItem(clean::TyMethod { ref decl, .. }) | + clean::MethodItem(clean::Method { ref decl, .. }) | + clean::ForeignFunctionItem(clean::Function { ref decl, .. }) => { + out = spotlight_decl(decl)?; + } + _ => {} + } + + Ok(out) +} + +fn spotlight_decl(decl: &clean::FnDecl) -> Result { + let mut out = String::new(); + + if let Some(did) = decl.output.def_id() { + let c = cache(); + if let Some(impls) = c.impls.get(&did) { + for i in impls { + let impl_ = i.inner_impl(); + if impl_.trait_.def_id().and_then(|d| c.traits.get(&d)) + .map_or(false, |t| t.is_spotlight) { + if out.is_empty() { + out.push_str(""); + out.push_str(&format!("

Important traits for {}

", impl_.for_)); + out.push_str(""); + } + + //use the "where" class here to make it small + out.push_str(&format!("{}", impl_)); + let t_did = impl_.trait_.def_id().unwrap(); + for it in &impl_.items { + if let clean::TypedefItem(ref tydef, _) = it.inner { + out.push_str(" "); + assoc_type(&mut out, it, &vec![], + Some(&tydef.type_), + AssocItemLink::GotoSource(t_did, &FxHashSet()))?; + out.push_str(";"); + } + } + } + } + } + } + + if !out.is_empty() { + out.push_str("
"); + } + + Ok(out) +} + fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLink, render_mode: RenderMode, outer_version: Option<&str>, show_def_docs: bool) -> fmt::Result { @@ -3270,6 +3327,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi trait_: Option<&clean::Trait>, show_def_docs: bool) -> fmt::Result { let item_type = item.type_(); let name = item.name.as_ref().unwrap(); + let mut method_prefix: Option = None; let render_method_item: bool = match render_mode { RenderMode::Normal => true, @@ -3277,7 +3335,8 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi }; match item.inner { - clean::MethodItem(..) | clean::TyMethodItem(..) => { + clean::MethodItem(clean::Method { ref decl, .. }) | + clean::TyMethodItem(clean::TyMethod{ ref decl, .. }) => { // Only render when the method is not static or we allow static methods if render_method_item { let id = derive_id(format!("{}.{}", item_type, name)); @@ -3297,6 +3356,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi render_stability_since_raw(w, item.stable_since(), outer_version)?; } write!(w, "\n")?; + method_prefix = Some(spotlight_decl(decl)?); } } clean::TypedefItem(ref tydef, _) => { @@ -3328,7 +3388,12 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi } if render_method_item || render_mode == RenderMode::Normal { - let prefix = render_assoc_const_value(item); + let mut prefix = render_assoc_const_value(item); + + if let Some(method_prefix) = method_prefix { + prefix.push_str(&method_prefix); + } + if !is_default_item { if let Some(t) = trait_ { // The trait item may have been stripped so we might not diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 8d0faf261f6c9..49dfa891c0ade 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1624,9 +1624,7 @@ } onEach(document.getElementById('main').getElementsByClassName('docblock'), function(e) { - if (e.parentNode.id === "main") { - e.parentNode.insertBefore(createToggle(), e); - } + e.parentNode.insertBefore(createToggle(), e); }); onEach(document.getElementsByClassName('docblock'), function(e) { diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 55acc57515237..29bec231b84fc 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -141,9 +141,12 @@ code, pre { border-radius: 3px; padding: 0 0.2em; } -.docblock pre code, .docblock-short pre code { +.docblock pre code, .docblock-short pre code, .docblock code.spotlight { padding: 0; } +.docblock code.spotlight :last-child { + padding-bottom: 0.6em; +} pre { padding: 14px; } diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 57f8c39756e3c..7eeb1bd61e34c 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -450,6 +450,7 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result /// # } /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[doc(spotlight)] pub trait Read { /// Pull some bytes from this source into the specified buffer, returning /// how many bytes were read. @@ -968,6 +969,7 @@ impl Initializer { /// # } /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[doc(spotlight)] pub trait Write { /// Write a buffer into this object, returning how many bytes were written. /// From 831fd783416d9f87ec9308ed56e891d0b1ffdbcd Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Wed, 4 Oct 2017 22:00:22 -0500 Subject: [PATCH 2/7] add doc_highlight feature flag and tests --- .../src/language-features/doc-spotlight.md | 27 +++++++++++ src/libcore/lib.rs | 18 ++++++++ src/libstd/lib.rs | 1 + src/libsyntax/feature_gate.rs | 6 +++ .../feature-gate-doc_spotlight.rs | 14 ++++++ src/test/rustdoc/doc-spotlight.rs | 46 +++++++++++++++++++ 6 files changed, 112 insertions(+) create mode 100644 src/doc/unstable-book/src/language-features/doc-spotlight.md create mode 100644 src/test/compile-fail/feature-gate-doc_spotlight.rs create mode 100644 src/test/rustdoc/doc-spotlight.rs diff --git a/src/doc/unstable-book/src/language-features/doc-spotlight.md b/src/doc/unstable-book/src/language-features/doc-spotlight.md new file mode 100644 index 0000000000000..8aca01bb6384d --- /dev/null +++ b/src/doc/unstable-book/src/language-features/doc-spotlight.md @@ -0,0 +1,27 @@ +# `doc_spotlight` + +The tracking issue for this feature is: [TODO] + +The `doc_spotlight` feature allows the use of the `spotlight` parameter to the `#[doc]` attribute, +to "spotlight" a specific trait on the return values of functions. Adding a `#[doc(spotlight)]` +attribute to a trait definition will make rustdoc print extra information for functions which return +a type that implements that trait. This attribute is applied to the `Iterator`, `io::Read`, and +`io::Write` traits in the standard library. + +You can do this on your own traits, like this: + +``` +#![feature(doc_spotlight)] + +#[doc(spotlight)] +pub trait MyTrait {} + +pub struct MyStruct; +impl MyTrait for MyStruct {} + +/// The docs for this function will have an extra line about `MyStruct` implementing `MyTrait`, +/// without having to write that yourself! +pub fn my_fn() -> MyStruct { MyStruct } +``` + +This feature was originally implemented in PR [TODO]. diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 4a57417e86a1c..631b9f98589f6 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -107,6 +107,24 @@ #![feature(const_unsafe_cell_new)] #![feature(const_cell_new)] #![feature(const_nonzero_new)] +#![cfg_attr(not(stage0), feature(doc_spotlight))] + +#![cfg_attr(not(stage0), feature(const_min_value))] +#![cfg_attr(not(stage0), feature(const_max_value))] +#![cfg_attr(not(stage0), feature(const_atomic_bool_new))] +#![cfg_attr(not(stage0), feature(const_atomic_isize_new))] +#![cfg_attr(not(stage0), feature(const_atomic_usize_new))] +#![cfg_attr(not(stage0), feature(const_atomic_i8_new))] +#![cfg_attr(not(stage0), feature(const_atomic_u8_new))] +#![cfg_attr(not(stage0), feature(const_atomic_i16_new))] +#![cfg_attr(not(stage0), feature(const_atomic_u16_new))] +#![cfg_attr(not(stage0), feature(const_atomic_i32_new))] +#![cfg_attr(not(stage0), feature(const_atomic_u32_new))] +#![cfg_attr(not(stage0), feature(const_atomic_i64_new))] +#![cfg_attr(not(stage0), feature(const_atomic_u64_new))] +#![cfg_attr(not(stage0), feature(const_unsafe_cell_new))] +#![cfg_attr(not(stage0), feature(const_cell_new))] +#![cfg_attr(not(stage0), feature(const_nonzero_new))] #[prelude_import] #[allow(unused)] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 37cc7a49b5271..ccc89ccdcf4c3 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -329,6 +329,7 @@ #![feature(vec_push_all)] #![feature(doc_cfg)] #![feature(doc_masked)] +#![feature(doc_spotlight)] #![cfg_attr(test, feature(update_panic_count))] #![cfg_attr(windows, feature(const_atomic_ptr_new))] diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 97eec3a21e9d1..9bfb3bdbdb4de 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -381,6 +381,8 @@ declare_features! ( (active, doc_cfg, "1.21.0", Some(43781)), // #[doc(masked)] (active, doc_masked, "1.21.0", Some(44027)), + // #[doc(spotlight)] + (active, doc_spotlight, "1.22.0", None), // allow `#[must_use]` on functions and comparison operators (RFC 1940) (active, fn_must_use, "1.21.0", Some(43302)), @@ -1292,6 +1294,10 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { gate_feature_post!(&self, doc_masked, attr.span, "#[doc(masked)] is experimental" ); + } else if content.iter().any(|c| c.check_name("spotlight")) { + gate_feature_post!(&self, doc_spotlight, attr.span, + "#[doc(spotlight)] is experimental" + ); } } } diff --git a/src/test/compile-fail/feature-gate-doc_spotlight.rs b/src/test/compile-fail/feature-gate-doc_spotlight.rs new file mode 100644 index 0000000000000..6369358538d50 --- /dev/null +++ b/src/test/compile-fail/feature-gate-doc_spotlight.rs @@ -0,0 +1,14 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[doc(spotlight)] //~ ERROR: #[doc(spotlight)] is experimental +trait SomeTrait {} + +fn main() {} diff --git a/src/test/rustdoc/doc-spotlight.rs b/src/test/rustdoc/doc-spotlight.rs new file mode 100644 index 0000000000000..eb05b261fcc86 --- /dev/null +++ b/src/test/rustdoc/doc-spotlight.rs @@ -0,0 +1,46 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(doc_spotlight)] + +pub struct Wrapper { + inner: T, +} + +impl SomeTrait for Wrapper {} + +#[doc(spotlight)] +pub trait SomeTrait { + // @has doc_spotlight/trait.SomeTrait.html + // @has - '//code[@class="spotlight"]' 'impl SomeTrait for Wrapper' + fn wrap_me(self) -> Wrapper where Self: Sized { + Wrapper { + inner: self, + } + } +} + +pub struct SomeStruct; +impl SomeTrait for SomeStruct {} + +impl SomeStruct { + // @has doc_spotlight/struct.SomeStruct.html + // @has - '//code[@class="spotlight"]' 'impl SomeTrait for SomeStruct' + // @has - '//code[@class="spotlight"]' 'impl SomeTrait for Wrapper' + pub fn new() -> SomeStruct { + SomeStruct + } +} + +// @has doc_spotlight/fn.bare_fn.html +// @has - '//code[@class="spotlight"]' 'impl SomeTrait for SomeStruct' +pub fn bare_fn() -> SomeStruct { + SomeStruct +} From be68d34a7eac8d653f453d7174f30e8ac1f73e62 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Wed, 4 Oct 2017 22:18:52 -0500 Subject: [PATCH 3/7] update links and tracking issue for doc_spotlight --- .../unstable-book/src/language-features/doc-spotlight.md | 7 +++++-- src/libsyntax/feature_gate.rs | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/doc-spotlight.md b/src/doc/unstable-book/src/language-features/doc-spotlight.md index 8aca01bb6384d..8117755fef1c8 100644 --- a/src/doc/unstable-book/src/language-features/doc-spotlight.md +++ b/src/doc/unstable-book/src/language-features/doc-spotlight.md @@ -1,6 +1,6 @@ # `doc_spotlight` -The tracking issue for this feature is: [TODO] +The tracking issue for this feature is: [#45040] The `doc_spotlight` feature allows the use of the `spotlight` parameter to the `#[doc]` attribute, to "spotlight" a specific trait on the return values of functions. Adding a `#[doc(spotlight)]` @@ -24,4 +24,7 @@ impl MyTrait for MyStruct {} pub fn my_fn() -> MyStruct { MyStruct } ``` -This feature was originally implemented in PR [TODO]. +This feature was originally implemented in PR [#45039]. + +[#45040]: https://github.com/rust-lang/rust/issues/45040 +[#45039]: https://github.com/rust-lang/rust/pull/45039 diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 9bfb3bdbdb4de..df75e033ab4c4 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -382,7 +382,7 @@ declare_features! ( // #[doc(masked)] (active, doc_masked, "1.21.0", Some(44027)), // #[doc(spotlight)] - (active, doc_spotlight, "1.22.0", None), + (active, doc_spotlight, "1.22.0", Some(45040)), // allow `#[must_use]` on functions and comparison operators (RFC 1940) (active, fn_must_use, "1.21.0", Some(43302)), From aca1bd7d7ed90febe83508d2e0b93b6d99da26fb Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 15 Nov 2017 20:04:02 +0100 Subject: [PATCH 4/7] First step for important traits UI --- src/librustdoc/html/render.rs | 27 +++++++++++----------- src/librustdoc/html/static/main.js | 17 +++++++++++++- src/librustdoc/html/static/rustdoc.css | 31 ++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index d43a73c986453..eed0db032fec0 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1816,8 +1816,7 @@ fn plain_summary_line(s: Option<&str>) -> String { fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Result { document_stability(w, cx, item)?; - let mut prefix = render_assoc_const_value(item); - prefix.push_str(&render_spotlight_traits(item)?); + let prefix = render_assoc_const_value(item); document_full(w, item, cx, &prefix)?; Ok(()) } @@ -2267,10 +2266,15 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, AbiSpace(f.abi), it.name.as_ref().unwrap(), f.generics).len(); + let mut extra = render_spotlight_traits(it)?; + if !extra.is_empty() { + extra.insert_str(0, "
"); + extra.push_str("
"); + }; write!(w, "
")?;
     render_attributes(w, it)?;
     write!(w, "{vis}{constness}{unsafety}{abi}fn \
-               {name}{generics}{decl}{where_clause}
", + {name}{generics}{decl}{where_clause}{extra}", vis = VisSpace(&it.visibility), constness = ConstnessSpace(f.constness), unsafety = UnsafetySpace(f.unsafety), @@ -2282,7 +2286,8 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, decl: &f.decl, name_len, indent: 0, - })?; + }, + extra = extra)?; document(w, cx, it) } @@ -3266,7 +3271,7 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result { if out.is_empty() { out.push_str(""); out.push_str(&format!("

Important traits for {}

", impl_.for_)); - out.push_str(""); + out.push_str(""); } //use the "where" class here to make it small @@ -3287,7 +3292,8 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result { } if !out.is_empty() { - out.push_str("
"); + out.insert_str(0, "
"); + out.push_str("
"); } Ok(out) @@ -3327,7 +3333,6 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi trait_: Option<&clean::Trait>, show_def_docs: bool) -> fmt::Result { let item_type = item.type_(); let name = item.name.as_ref().unwrap(); - let mut method_prefix: Option = None; let render_method_item: bool = match render_mode { RenderMode::Normal => true, @@ -3342,6 +3347,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi let id = derive_id(format!("{}.{}", item_type, name)); let ns_id = derive_id(format!("{}.{}", name, item_type.name_space())); write!(w, "

", id, item_type)?; + write!(w, "{}", spotlight_decl(decl)?)?; write!(w, "

\n")?; - method_prefix = Some(spotlight_decl(decl)?); } } clean::TypedefItem(ref tydef, _) => { @@ -3388,11 +3393,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi } if render_method_item || render_mode == RenderMode::Normal { - let mut prefix = render_assoc_const_value(item); - - if let Some(method_prefix) = method_prefix { - prefix.push_str(&method_prefix); - } + let prefix = render_assoc_const_value(item); if !is_default_item { if let Some(t) = trait_ { diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 49dfa891c0ade..72512bda41ea1 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1624,7 +1624,9 @@ } onEach(document.getElementById('main').getElementsByClassName('docblock'), function(e) { - e.parentNode.insertBefore(createToggle(), e); + if (e.parentNode.id === "main") { + e.parentNode.insertBefore(createToggle(), e); + } }); onEach(document.getElementsByClassName('docblock'), function(e) { @@ -1711,6 +1713,19 @@ } }); + function showModal(content) { + var modal = document.createElement('div'); + addClass(modal, 'modal'); + modal.innerHTML = '"; + document.getElementsByTagName('body')[0].appendChild(modal); + } + + onEach(document.getElementsByClassName('important-traits'), function(e) { + e.onclick = function() { + showModal(e.firstElementChild.innerHTML); + }; + }); + var search_input = document.getElementsByClassName("search-input")[0]; if (search_input) { diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 29bec231b84fc..63792714d473f 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -954,3 +954,34 @@ pre.rust { color: #888; font-size: 16px; } + +h4 > .important-traits { + position: absolute; + left: -44px; + top: 2px; + cursor: pointer; +} + +.modal { + position: fixed; + width: 100vw; + height: 100vh; + background-color: rgba(0,0,0,0.3); + z-index: 10000; + top: 0; + left: 0; +} + +.modal-content { + display: block; + max-width: 60%; + min-width: 200px; + background-color: white; + padding: 5px; + top: 10%; + position: absolute; + left: 50%; + transform: translate(-50%, -50%); + border: 1px solid #999; + border-radius: 4px; +} \ No newline at end of file From 85dcf2ecb601c6be35460750e8bc592e0adfd567 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 16 Nov 2017 01:28:09 +0100 Subject: [PATCH 5/7] Improve modal display and add JS events --- src/librustdoc/html/render.rs | 17 +++--- src/librustdoc/html/static/main.js | 17 +++++- src/librustdoc/html/static/rustdoc.css | 73 +++++++++++++++++++++++--- 3 files changed, 89 insertions(+), 18 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index eed0db032fec0..dc83b3da4903d 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2266,15 +2266,10 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, AbiSpace(f.abi), it.name.as_ref().unwrap(), f.generics).len(); - let mut extra = render_spotlight_traits(it)?; - if !extra.is_empty() { - extra.insert_str(0, "
"); - extra.push_str("
"); - }; - write!(w, "
")?;
+    write!(w, "{}
", render_spotlight_traits(it)?)?;
     render_attributes(w, it)?;
     write!(w, "{vis}{constness}{unsafety}{abi}fn \
-               {name}{generics}{decl}{where_clause}
{extra}", + {name}{generics}{decl}{where_clause}
", vis = VisSpace(&it.visibility), constness = ConstnessSpace(f.constness), unsafety = UnsafetySpace(f.unsafety), @@ -2286,8 +2281,7 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, decl: &f.decl, name_len, indent: 0, - }, - extra = extra)?; + })?; document(w, cx, it) } @@ -3269,8 +3263,9 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result { if impl_.trait_.def_id().and_then(|d| c.traits.get(&d)) .map_or(false, |t| t.is_spotlight) { if out.is_empty() { - out.push_str(""); - out.push_str(&format!("

Important traits for {}

", impl_.for_)); + out.push_str( + &format!("

Important traits for {}

", + impl_.for_)); out.push_str(""); } diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 72512bda41ea1..c3f2ec253087d 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -215,6 +215,7 @@ var help = document.getElementById("help"); switch (getVirtualKey(ev)) { case "Escape": + hideModal(); var search = document.getElementById("search"); if (!hasClass(help, "hidden")) { displayHelp(false, ev); @@ -228,6 +229,7 @@ case "s": case "S": displayHelp(false, ev); + hideModal(); ev.preventDefault(); focusSearchBar(); break; @@ -240,6 +242,7 @@ case "?": if (ev.shiftKey) { + hideModal(); displayHelp(true, ev); } break; @@ -1715,9 +1718,21 @@ function showModal(content) { var modal = document.createElement('div'); + modal.id = "important"; addClass(modal, 'modal'); - modal.innerHTML = '"; + modal.innerHTML = ''; document.getElementsByTagName('body')[0].appendChild(modal); + document.getElementById('modal-close').onclick = hideModal; + modal.onclick = hideModal; + } + + function hideModal() { + var modal = document.getElementById("important"); + if (modal) { + modal.parentNode.removeChild(modal); + } } onEach(document.getElementsByClassName('important-traits'), function(e) { diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 63792714d473f..89c75818ddf2a 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -89,7 +89,7 @@ h2 { h3 { font-size: 1.3em; } -h1, h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod):not(.associatedconstant) { +h1, h2, h3:not(.impl):not(.method):not(.type):not(.tymethod):not(.important), h4:not(.method):not(.type):not(.tymethod):not(.associatedconstant) { font-weight: 500; margin: 20px 0 15px 0; padding-bottom: 6px; @@ -955,11 +955,14 @@ pre.rust { font-size: 16px; } +.important-traits { + cursor: pointer; +} + h4 > .important-traits { position: absolute; left: -44px; top: 2px; - cursor: pointer; } .modal { @@ -976,12 +979,70 @@ h4 > .important-traits { display: block; max-width: 60%; min-width: 200px; - background-color: white; - padding: 5px; - top: 10%; + background-color: #eee; + padding: 8px; + top: 40%; position: absolute; left: 50%; - transform: translate(-50%, -50%); + transform: translate(-50%, -40%); border: 1px solid #999; border-radius: 4px; + border-top-right-radius: 0; +} + +.modal-content > .docblock { + margin: 0; +} + +h3.important { + margin: 0; + margin-bottom: 13px; + font-size: 19px; +} + +.modal-content > .docblock > code.content { + margin: 0; + padding: 0; + font-size: 20px; +} + +.modal-content > .close { + position: absolute; + font-weight: 900; + right: -25px; + top: -1px; + font-size: 18px; + background-color: #eee; + width: 25px; + padding-right: 2px; + border-top-right-radius: 5px; + border-bottom-right-radius: 5px; + text-align: center; + border: 1px solid #999; + border-right: 0; + cursor: pointer; +} + +.modal-content > .close:hover { + background-color: #ff1f1f; + color: white; +} + +.modal-content > .whiter { + height: 25px; + position: absolute; + width: 3px; + background-color: #eee; + right: -2px; + top: 0px; +} + +.modal-content > .close:hover + .whiter { + background-color: #ff1f1f; +} + +#main > div.important-traits { + position: absolute; + left: -24px; + margin-top: 16px; } \ No newline at end of file From d86621f69e827361e47bc6c4b2c7fd5319155227 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 16 Nov 2017 23:52:12 +0100 Subject: [PATCH 6/7] Add trait methods as well --- src/librustdoc/html/render.rs | 3 ++- src/librustdoc/html/static/rustdoc.css | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index dc83b3da4903d..715855902711a 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2400,8 +2400,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, let item_type = m.type_(); let id = derive_id(format!("{}.{}", item_type, name)); let ns_id = derive_id(format!("{}.{}", name, item_type.name_space())); - write!(w, "

\ + write!(w, "{extra}

\