Skip to content

Link to corresponding sections for enum variants in rustdoc #75178

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

Closed
wants to merge 3 commits into from
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
40 changes: 28 additions & 12 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2857,7 +2857,7 @@ fn item_struct(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Struct
wrap_into_docblock(w, |w| {
write!(w, "<pre class='rust struct'>");
render_attributes(w, it, true);
render_struct(w, it, Some(&s.generics), s.struct_type, &s.fields, "", true);
render_struct(w, it, Some(&s.generics), s.struct_type, &s.fields, "", false);
write!(w, "</pre>")
});

Expand Down Expand Up @@ -2970,9 +2970,19 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum, ca
let name = v.name.as_ref().unwrap();
match v.inner {
clean::VariantItem(ref var) => match var.kind {
clean::VariantKind::CLike => write!(w, "{}", name),
clean::VariantKind::CLike => {
write!(
w,
"<a href=\"#variant.{}\" class=\"variant\">{}</a>",
name, name
);
}
clean::VariantKind::Tuple(ref tys) => {
write!(w, "{}(", name);
write!(
w,
"<a href=\"#variant.{}\" class=\"variant\">{}</a>(",
name, name
);
for (i, ty) in tys.iter().enumerate() {
if i > 0 {
write!(w, ",&nbsp;")
Expand All @@ -2982,7 +2992,7 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum, ca
write!(w, ")");
}
clean::VariantKind::Struct(ref s) => {
render_struct(w, v, None, s.struct_type, &s.fields, " ", false);
render_struct(w, v, None, s.struct_type, &s.fields, " ", true);
}
},
_ => unreachable!(),
Expand Down Expand Up @@ -3126,15 +3136,21 @@ fn render_struct(
ty: doctree::StructType,
fields: &[clean::Item],
tab: &str,
structhead: bool,
enum_variant: bool,
) {
write!(
w,
"{}{}{}",
it.visibility.print_with_space(),
if structhead { "struct " } else { "" },
it.name.as_ref().unwrap()
);
write!(w, "{}", it.visibility.print_with_space());
// If this is an enum variant we should not write `struct` and the
// name should be a link to the description of the variant.
if enum_variant {
write!(
w,
"<a href=\"#variant.{}\" class=\"variant\">{}</a>",
it.name.as_ref().unwrap(),
it.name.as_ref().unwrap()
);
} else {
write!(w, "struct {}", it.name.as_ref().unwrap());
}
if let Some(g) = g {
write!(w, "{}", g.print())
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/html/static/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ pre {
.content span.enum, .content a.enum {
color: #99e0c9;
}
.content span.variant, .content a.variant {
color: #6380a0;
}
.content span.trait, .content a.trait {
color: #39AFD7;
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pre {
.content .stability::before { color: #ccc; }

.content span.enum, .content a.enum, .block a.current.enum { color: #82b089; }
.content span.variant, .content a.variant, .block a.current.variant { color: #82a5c9; }
.content span.struct, .content a.struct, .block a.current.struct { color: #2dbfb8; }
.content span.type, .content a.type, .block a.current.type { color: #ff7f00; }
.content span.foreigntype, .content a.foreigntype, .block a.current.foreigntype { color: #dd7de8; }
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/static/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pre {
.content .stability::before { color: #ccc; }

.content span.enum, .content a.enum, .block a.current.enum { color: #508157; }
.content span.variant, .content a.variant, .block a.current.variant { color: #546e8a; }
.content span.struct, .content a.struct, .block a.current.struct { color: #ad448e; }
.content span.type, .content a.type, .block a.current.type { color: #ba5d00; }
.content span.foreigntype, .content a.foreigntype, .block a.current.foreigntype { color: #cd00e2; }
Expand Down