Skip to content
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

Add color on item name in item declaration #117643

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ fn render_attributes_in_pre<'a, 'tcx: 'a>(
// a div to produce a newline after it.
fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, cx: &Context<'_>) {
for attr in it.attributes(cx.tcx(), cx.cache(), false) {
write!(w, "<div class=\"code-attribute\">{attr}</div>").unwrap();
write!(w, "<div class=\"code-attribute attr\">{attr}</div>").unwrap();
}
}

Expand Down
49 changes: 31 additions & 18 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
let abi = print_abi_with_space(header.abi).to_string();
let asyncness = header.asyncness.print_with_space();
let visibility = visibility_print_with_space(it.visibility(tcx), it.item_id, cx).to_string();
let name = it.name.unwrap();
let name = format!("{}", render_type_name(it.name.unwrap(), "fn"));

let generics_len = format!("{:#}", f.generics.print(cx)).len();
let header_len = "fn ".len()
Expand All @@ -642,7 +642,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
+ asyncness.len()
+ unsafety.len()
+ abi.len()
+ name.as_str().len()
+ name.len()
+ generics_len;

let notable_traits = notable_traits_button(&f.decl.output, cx);
Expand Down Expand Up @@ -692,7 +692,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
unsafety = t.unsafety(tcx).print_with_space(),
is_auto = if t.is_auto(tcx) { "auto " } else { "" },
name = it.name.unwrap(),
name = render_type_name(it.name.unwrap(), "trait"),
generics = t.generics.print(cx),
);

Expand Down Expand Up @@ -1186,7 +1186,7 @@ fn item_trait_alias(
w,
"{attrs}trait {name}{generics}{where_b} = {bounds};",
attrs = render_attributes_in_pre(it, "", cx),
name = it.name.unwrap(),
name = render_type_name(it.name.unwrap(), "traitalias"),
generics = t.generics.print(cx),
where_b = print_where_clause(&t.generics, cx, 0, Ending::Newline),
bounds = bounds(&t.bounds, true, cx),
Expand Down Expand Up @@ -1214,7 +1214,7 @@ fn item_opaque_ty(
w,
"{attrs}type {name}{generics}{where_clause} = impl {bounds};",
attrs = render_attributes_in_pre(it, "", cx),
name = it.name.unwrap(),
name = render_type_name(it.name.unwrap(), "type"),
generics = t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
bounds = bounds(&t.bounds, false, cx),
Expand All @@ -1240,7 +1240,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
"{attrs}{vis}type {name}{generics}{where_clause} = {type_};",
attrs = render_attributes_in_pre(it, "", cx),
vis = visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
name = it.name.unwrap(),
name = render_type_name(it.name.unwrap(), "type"),
generics = t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
type_ = t.type_.print(cx),
Expand Down Expand Up @@ -1528,7 +1528,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
w,
"{}enum {}{}",
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
it.name.unwrap(),
render_type_name(it.name.unwrap(), "enum"),
e.generics.print(cx),
);

Expand Down Expand Up @@ -1848,7 +1848,7 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
w,
"{vis}const {name}{generics}: {typ}{where_clause}",
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
name = it.name.unwrap(),
name = render_type_name(it.name.unwrap(), "constant"),
generics = c.generics.print(cx),
typ = c.type_.print(cx),
where_clause = print_where_clause(&c.generics, cx, 0, Ending::NoNewline),
Expand Down Expand Up @@ -1957,7 +1957,7 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
"{vis}static {mutability}{name}: {typ}",
vis = visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
mutability = s.mutability.print_with_space(),
name = it.name.unwrap(),
name = render_type_name(it.name.unwrap(), "static"),
typ = s.type_.print(cx)
)
.unwrap();
Expand All @@ -1974,7 +1974,7 @@ fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::
buffer,
" {}type {};\n}}",
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
it.name.unwrap(),
render_type_name(it.name.unwrap(), "foreigntype"),
)
.unwrap();
});
Expand Down Expand Up @@ -2123,6 +2123,10 @@ fn render_implementor(
);
}

fn render_type_name<'a>(name: Symbol, class: &'a str) -> impl fmt::Display + 'a {
display_fn(move |f| write!(f, "<a class=\"{class}\" href=\"#\">{name}</a>"))
}

fn render_union<'a, 'cx: 'a>(
it: &'a clean::Item,
g: Option<&'a clean::Generics>,
Expand All @@ -2135,7 +2139,7 @@ fn render_union<'a, 'cx: 'a>(
f,
"{}union {}",
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
it.name.unwrap(),
render_type_name(it.name.unwrap(), "union"),
)?;

let where_displayed = g
Expand Down Expand Up @@ -2195,13 +2199,22 @@ fn render_struct(
cx: &Context<'_>,
) {
let tcx = cx.tcx();
write!(
w,
"{}{}{}",
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
if structhead { "struct " } else { "" },
it.name.unwrap()
);
if structhead {
write!(
w,
"{}{}{}",
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
"struct ",
render_type_name(it.name.unwrap(), "struct"),
);
} else {
write!(
w,
"{}{}",
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
it.name.unwrap(),
);
}
if let Some(g) = g {
write!(w, "{}", g.print(cx))
}
Expand Down
7 changes: 4 additions & 3 deletions tests/rustdoc-gui/item-decl-colors.goml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ define-function: (
set-local-storage: {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}
reload:
assert-css: (".item-decl .code-attribute", {"color": |attr_color|}, ALL)
assert-css: (".item-decl .struct[href='#']", {"color": |struct_color|}, ALL)
assert-css: (".item-decl .trait", {"color": |trait_color|}, ALL)
// We need to add `code` here because otherwise it would select the parent too.
assert-css: (".item-decl code .struct", {"color": |struct_color|}, ALL)
Expand All @@ -41,7 +42,7 @@ call-function: (
"check-colors",
{
"theme": "ayu",
"attr_color": "#999",
"attr_color": "#e6e1cf",
"trait_color": "#39afd7",
"struct_color": "#ffa0a5",
"enum_color": "#ffa0a5",
Expand All @@ -55,7 +56,7 @@ call-function: (
"check-colors",
{
"theme": "dark",
"attr_color": "#999",
"attr_color": "#ee6868",
"trait_color": "#b78cf2",
"struct_color": "#2dbfb8",
"enum_color": "#2dbfb8",
Expand All @@ -69,7 +70,7 @@ call-function: (
"check-colors",
{
"theme": "light",
"attr_color": "#999",
"attr_color": "#c82829",
"trait_color": "#6e4fc9",
"struct_color": "#ad378a",
"enum_color": "#ad378a",
Expand Down
Loading