Skip to content

Fix attrs pos #60093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3002,7 +3002,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
fn item_constant(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
c: &clean::Constant) -> fmt::Result {
write!(w, "<pre class='rust const'>")?;
render_attributes(w, it)?;
render_attributes(w, it, false)?;
write!(w, "{vis}const \
{name}: {typ}</pre>",
vis = VisSpace(&it.visibility),
Expand All @@ -3014,7 +3014,7 @@ fn item_constant(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
fn item_static(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
s: &clean::Static) -> fmt::Result {
write!(w, "<pre class='rust static'>")?;
render_attributes(w, it)?;
render_attributes(w, it, false)?;
write!(w, "{vis}static {mutability}\
{name}: {typ}</pre>",
vis = VisSpace(&it.visibility),
Expand All @@ -3037,7 +3037,7 @@ fn item_function(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
f.generics
).len();
write!(w, "{}<pre class='rust fn'>", render_spotlight_traits(it)?)?;
render_attributes(w, it)?;
render_attributes(w, it, false)?;
write!(w,
"{vis}{constness}{unsafety}{asyncness}{abi}fn \
{name}{generics}{decl}{where_clause}</pre>",
Expand Down Expand Up @@ -3126,7 +3126,7 @@ fn item_trait(
// Output the trait definition
wrap_into_docblock(w, |w| {
write!(w, "<pre class='rust trait'>")?;
render_attributes(w, it)?;
render_attributes(w, it, true)?;
write!(w, "{}{}{}trait {}{}{}",
VisSpace(&it.visibility),
UnsafetySpace(t.unsafety),
Expand Down Expand Up @@ -3389,8 +3389,10 @@ fn assoc_const(w: &mut fmt::Formatter<'_>,
it: &clean::Item,
ty: &clean::Type,
_default: Option<&String>,
link: AssocItemLink<'_>) -> fmt::Result {
write!(w, "{}const <a href='{}' class=\"constant\"><b>{}</b></a>: {}",
link: AssocItemLink<'_>,
extra: &str) -> fmt::Result {
write!(w, "{}{}const <a href='{}' class=\"constant\"><b>{}</b></a>: {}",
extra,
VisSpace(&it.visibility),
naive_assoc_href(it, link),
it.name.as_ref().unwrap(),
Expand All @@ -3401,8 +3403,10 @@ fn assoc_const(w: &mut fmt::Formatter<'_>,
fn assoc_type<W: fmt::Write>(w: &mut W, it: &clean::Item,
bounds: &[clean::GenericBound],
default: Option<&clean::Type>,
link: AssocItemLink<'_>) -> fmt::Result {
write!(w, "type <a href='{}' class=\"type\">{}</a>",
link: AssocItemLink<'_>,
extra: &str) -> fmt::Result {
write!(w, "{}type <a href='{}' class=\"type\">{}</a>",
extra,
naive_assoc_href(it, link),
it.name.as_ref().unwrap())?;
if !bounds.is_empty() {
Expand Down Expand Up @@ -3479,7 +3483,7 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
} else {
(0, true)
};
render_attributes(w, meth)?;
render_attributes(w, meth, false)?;
write!(w, "{}{}{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}",
if parent == ItemType::Trait { " " } else { "" },
Expand Down Expand Up @@ -3513,10 +3517,12 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
method(w, item, m.header, &m.generics, &m.decl, link, parent)
}
clean::AssociatedConstItem(ref ty, ref default) => {
assoc_const(w, item, ty, default.as_ref(), link)
assoc_const(w, item, ty, default.as_ref(), link,
if parent == ItemType::Trait { " " } else { "" })
}
clean::AssociatedTypeItem(ref bounds, ref default) => {
assoc_type(w, item, bounds, default.as_ref(), link)
assoc_type(w, item, bounds, default.as_ref(), link,
if parent == ItemType::Trait { " " } else { "" })
}
_ => panic!("render_assoc_item called on non-associated-item")
}
Expand All @@ -3526,7 +3532,7 @@ fn item_struct(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
s: &clean::Struct) -> fmt::Result {
wrap_into_docblock(w, |w| {
write!(w, "<pre class='rust struct'>")?;
render_attributes(w, it)?;
render_attributes(w, it, true)?;
render_struct(w,
it,
Some(&s.generics),
Expand Down Expand Up @@ -3577,7 +3583,7 @@ fn item_union(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
s: &clean::Union) -> fmt::Result {
wrap_into_docblock(w, |w| {
write!(w, "<pre class='rust union'>")?;
render_attributes(w, it)?;
render_attributes(w, it, true)?;
render_union(w,
it,
Some(&s.generics),
Expand Down Expand Up @@ -3622,7 +3628,7 @@ fn item_enum(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
e: &clean::Enum) -> fmt::Result {
wrap_into_docblock(w, |w| {
write!(w, "<pre class='rust enum'>")?;
render_attributes(w, it)?;
render_attributes(w, it, true)?;
write!(w, "{}enum {}{}{}",
VisSpace(&it.visibility),
it.name.as_ref().unwrap(),
Expand Down Expand Up @@ -3783,7 +3789,15 @@ const ATTRIBUTE_WHITELIST: &'static [&'static str] = &[
"non_exhaustive"
];

fn render_attributes(w: &mut dyn fmt::Write, it: &clean::Item) -> fmt::Result {
// The `top` parameter is used when generating the item declaration to ensure it doesn't have a
// left padding. For example:
//
// #[foo] <----- "top" attribute
// struct Foo {
// #[bar] <---- not "top" attribute
// bar: usize,
// }
fn render_attributes(w: &mut dyn fmt::Write, it: &clean::Item, top: bool) -> fmt::Result {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need comments explaining what this parameter does.

let mut attrs = String::new();

for attr in &it.attrs.other_attrs {
Expand All @@ -3795,7 +3809,8 @@ fn render_attributes(w: &mut dyn fmt::Write, it: &clean::Item) -> fmt::Result {
}
}
if attrs.len() > 0 {
write!(w, "<div class=\"docblock attributes\">{}</div>", &attrs)?;
write!(w, "<div class=\"docblock attributes{}\">{}</div>",
if top { " top-attr" } else { "" }, &attrs)?;
}
Ok(())
}
Expand Down Expand Up @@ -4128,7 +4143,8 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result<String, fmt::Error> {
out.push_str("<span class=\"where fmt-newline\"> ");
assoc_type(&mut out, it, &[],
Some(&tydef.type_),
AssocItemLink::GotoSource(t_did, &FxHashSet::default()))?;
AssocItemLink::GotoSource(t_did, &FxHashSet::default()),
"")?;
out.push_str(";</span>");
}
}
Expand Down Expand Up @@ -4164,7 +4180,8 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
if let clean::TypedefItem(ref tydef, _) = it.inner {
write!(w, "<span class=\"where fmt-newline\"> ")?;
assoc_type(w, it, &vec![], Some(&tydef.type_),
AssocItemLink::Anchor(None))?;
AssocItemLink::Anchor(None),
"")?;
write!(w, ";</span>")?;
}
}
Expand Down Expand Up @@ -4234,15 +4251,15 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<code id='{}'>", ns_id)?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id), "")?;
write!(w, "</code></h4>")?;
}
clean::AssociatedConstItem(ref ty, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<code id='{}'>", ns_id)?;
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "")?;
write!(w, "</code>")?;
render_stability_since_raw(w, item.stable_since(), outer_version)?;
if let Some(l) = (Item { cx, item }).src_href() {
Expand All @@ -4256,7 +4273,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<code id='{}'>", ns_id)?;
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id), "")?;
write!(w, "</code></h4>")?;
}
clean::StrippedItem(..) => return Ok(()),
Expand Down Expand Up @@ -4344,7 +4361,7 @@ fn item_existential(
t: &clean::Existential,
) -> fmt::Result {
write!(w, "<pre class='rust existential'>")?;
render_attributes(w, it)?;
render_attributes(w, it, false)?;
write!(w, "existential type {}{}{where_clause}: {bounds};</pre>",
it.name.as_ref().unwrap(),
t.generics,
Expand All @@ -4363,7 +4380,7 @@ fn item_existential(
fn item_trait_alias(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
t: &clean::TraitAlias) -> fmt::Result {
write!(w, "<pre class='rust trait-alias'>")?;
render_attributes(w, it)?;
render_attributes(w, it, false)?;
write!(w, "trait {}{}{} = {};</pre>",
it.name.as_ref().unwrap(),
t.generics,
Expand All @@ -4382,7 +4399,7 @@ fn item_trait_alias(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
fn item_typedef(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
t: &clean::Typedef) -> fmt::Result {
write!(w, "<pre class='rust typedef'>")?;
render_attributes(w, it)?;
render_attributes(w, it, false)?;
write!(w, "type {}{}{where_clause} = {type_};</pre>",
it.name.as_ref().unwrap(),
t.generics,
Expand All @@ -4400,7 +4417,7 @@ fn item_typedef(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,

fn item_foreign_type(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item) -> fmt::Result {
writeln!(w, "<pre class='rust foreigntype'>extern {{")?;
render_attributes(w, it)?;
render_attributes(w, it, false)?;
write!(
w,
" {}type {};\n}}</pre>",
Expand Down
6 changes: 5 additions & 1 deletion src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,11 @@ if (!DOMTokenList.prototype.remove) {
}
var attributesToggle = createToggleWrapper(createSimpleToggle(false));
onEachLazy(main.getElementsByClassName("attributes"), function(i_e) {
i_e.parentNode.insertBefore(attributesToggle.cloneNode(true), i_e);
var attr_tog = attributesToggle.cloneNode(true);
if (hasClass(i_e, "top-attr") === true) {
addClass(attr_tog, "top-attr");
}
i_e.parentNode.insertBefore(attr_tog, i_e);
itemAttributesFunc(i_e);
});

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1579,10 +1579,10 @@ div.name.expand::before {
}

/* This part is to fix the "Expand attributes" part in the type declaration. */
.type-decl > pre > :first-child {
.type-decl > pre > .toggle-wrapper.toggle-attributes.top-attr {
margin-left: 0 !important;
}
.type-decl > pre > :nth-child(2) {
.type-decl > pre > .docblock.attributes.top-attr {
margin-left: 1.8em !important;
}
.type-decl > pre > .toggle-attributes {
Expand Down
4 changes: 2 additions & 2 deletions src/test/rustdoc/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub extern "C" fn f() {}
#[export_name = "bar"]
pub extern "C" fn g() {}

// @has foo/enum.Foo.html '//*[@class="docblock attributes"]' '#[repr(i64)]'
// @has foo/enum.Foo.html '//*[@class="docblock attributes"]' '#[must_use]'
// @has foo/enum.Foo.html '//*[@class="docblock attributes top-attr"]' '#[repr(i64)]'
// @has foo/enum.Foo.html '//*[@class="docblock attributes top-attr"]' '#[must_use]'
#[repr(i64)]
#[must_use]
pub enum Foo {
Expand Down