Skip to content

Commit 048d3a9

Browse files
Rollup merge of rust-lang#90018 - GuillaumeGomez:too-long-item-names, r=jsha
Fix rustdoc UI for very long type names Fixes rust-lang#89972. While working on it, I also discovered that when the item name is too long, it also breaks the flow of the page. To make things right, I also renamed the `type-decl` CSS class into `item-decl` (because this PR also generates it for more than type declarations). So here are the before/after screenshots: ![Screenshot from 2021-10-18 16-58-03](https://user-images.githubusercontent.com/3050060/137757247-637fcf04-4406-49c6-8a8a-18c2074aacd9.png) ![Screenshot from 2021-10-18 16-58-26](https://user-images.githubusercontent.com/3050060/137757252-17935e63-53b3-449f-a535-7be91ff0e257.png) ![Screenshot from 2021-10-18 16-58-07](https://user-images.githubusercontent.com/3050060/137757278-8b12e348-2980-4fc4-8853-bef99d58981f.png) ![Screenshot from 2021-10-18 16-58-28](https://user-images.githubusercontent.com/3050060/137757282-534a0e1b-3016-49ba-b3ac-e45bdb9035cb.png) r? `@jsha`
2 parents d6af6c9 + 77c2929 commit 048d3a9

15 files changed

+232
-154
lines changed

src/librustdoc/html/render/print_item.rs

+153-122
Original file line numberDiff line numberDiff line change
@@ -482,24 +482,26 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::
482482
+ name.as_str().len()
483483
+ generics_len;
484484

485-
wrap_item(w, "fn", |w| {
486-
render_attributes_in_pre(w, it, "");
487-
w.reserve(header_len);
488-
write!(
489-
w,
490-
"{vis}{constness}{asyncness}{unsafety}{abi}fn \
491-
{name}{generics}{decl}{notable_traits}{where_clause}",
492-
vis = vis,
493-
constness = constness,
494-
asyncness = asyncness,
495-
unsafety = unsafety,
496-
abi = abi,
497-
name = name,
498-
generics = f.generics.print(cx),
499-
where_clause = print_where_clause(&f.generics, cx, 0, true),
500-
decl = f.decl.full_print(header_len, 0, f.header.asyncness, cx),
501-
notable_traits = notable_traits_decl(&f.decl, cx),
502-
);
485+
wrap_into_docblock(w, |w| {
486+
wrap_item(w, "fn", |w| {
487+
render_attributes_in_pre(w, it, "");
488+
w.reserve(header_len);
489+
write!(
490+
w,
491+
"{vis}{constness}{asyncness}{unsafety}{abi}fn \
492+
{name}{generics}{decl}{notable_traits}{where_clause}",
493+
vis = vis,
494+
constness = constness,
495+
asyncness = asyncness,
496+
unsafety = unsafety,
497+
abi = abi,
498+
name = name,
499+
generics = f.generics.print(cx),
500+
where_clause = print_where_clause(&f.generics, cx, 0, true),
501+
decl = f.decl.full_print(header_len, 0, f.header.asyncness, cx),
502+
notable_traits = notable_traits_decl(&f.decl, cx),
503+
);
504+
});
503505
});
504506
document(w, cx, it, None, HeadingOffset::H2)
505507
}
@@ -844,16 +846,18 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
844846
}
845847

846848
fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::TraitAlias) {
847-
wrap_item(w, "trait-alias", |w| {
848-
render_attributes_in_pre(w, it, "");
849-
write!(
850-
w,
851-
"trait {}{}{} = {};",
852-
it.name.as_ref().unwrap(),
853-
t.generics.print(cx),
854-
print_where_clause(&t.generics, cx, 0, true),
855-
bounds(&t.bounds, true, cx)
856-
);
849+
wrap_into_docblock(w, |w| {
850+
wrap_item(w, "trait-alias", |w| {
851+
render_attributes_in_pre(w, it, "");
852+
write!(
853+
w,
854+
"trait {}{}{} = {};",
855+
it.name.as_ref().unwrap(),
856+
t.generics.print(cx),
857+
print_where_clause(&t.generics, cx, 0, true),
858+
bounds(&t.bounds, true, cx)
859+
);
860+
});
857861
});
858862

859863
document(w, cx, it, None, HeadingOffset::H2);
@@ -866,16 +870,18 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clea
866870
}
867871

868872
fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) {
869-
wrap_item(w, "opaque", |w| {
870-
render_attributes_in_pre(w, it, "");
871-
write!(
872-
w,
873-
"type {}{}{where_clause} = impl {bounds};",
874-
it.name.as_ref().unwrap(),
875-
t.generics.print(cx),
876-
where_clause = print_where_clause(&t.generics, cx, 0, true),
877-
bounds = bounds(&t.bounds, false, cx),
878-
);
873+
wrap_into_docblock(w, |w| {
874+
wrap_item(w, "opaque", |w| {
875+
render_attributes_in_pre(w, it, "");
876+
write!(
877+
w,
878+
"type {}{}{where_clause} = impl {bounds};",
879+
it.name.as_ref().unwrap(),
880+
t.generics.print(cx),
881+
where_clause = print_where_clause(&t.generics, cx, 0, true),
882+
bounds = bounds(&t.bounds, false, cx),
883+
);
884+
});
879885
});
880886

881887
document(w, cx, it, None, HeadingOffset::H2);
@@ -894,20 +900,37 @@ fn item_typedef(
894900
t: &clean::Typedef,
895901
is_associated: bool,
896902
) {
897-
wrap_item(w, "typedef", |w| {
898-
render_attributes_in_pre(w, it, "");
899-
if !is_associated {
900-
write!(w, "{}", it.visibility.print_with_space(it.def_id, cx));
901-
}
902-
write!(
903-
w,
904-
"type {}{}{where_clause} = {type_};",
905-
it.name.as_ref().unwrap(),
906-
t.generics.print(cx),
907-
where_clause = print_where_clause(&t.generics, cx, 0, true),
908-
type_ = t.type_.print(cx),
909-
);
910-
});
903+
fn write_content(
904+
w: &mut Buffer,
905+
cx: &Context<'_>,
906+
it: &clean::Item,
907+
t: &clean::Typedef,
908+
is_associated: bool,
909+
) {
910+
wrap_item(w, "typedef", |w| {
911+
render_attributes_in_pre(w, it, "");
912+
if !is_associated {
913+
write!(w, "{}", it.visibility.print_with_space(it.def_id, cx));
914+
}
915+
write!(
916+
w,
917+
"type {}{}{where_clause} = {type_};",
918+
it.name.as_ref().unwrap(),
919+
t.generics.print(cx),
920+
where_clause = print_where_clause(&t.generics, cx, 0, true),
921+
type_ = t.type_.print(cx),
922+
);
923+
});
924+
}
925+
926+
// If this is an associated typedef, we don't want to wrap it into a docblock.
927+
if is_associated {
928+
write_content(w, cx, it, t, is_associated);
929+
} else {
930+
wrap_into_docblock(w, |w| {
931+
write_content(w, cx, it, t, is_associated);
932+
});
933+
}
911934

912935
document(w, cx, it, None, HeadingOffset::H2);
913936

@@ -1142,32 +1165,34 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
11421165
}
11431166

11441167
fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) {
1145-
let name = it.name.as_ref().expect("proc-macros always have names");
1146-
match m.kind {
1147-
MacroKind::Bang => {
1148-
wrap_item(w, "macro", |w| {
1149-
write!(w, "{}!() {{ /* proc-macro */ }}", name);
1150-
});
1151-
}
1152-
MacroKind::Attr => {
1153-
wrap_item(w, "attr", |w| {
1154-
write!(w, "#[{}]", name);
1155-
});
1156-
}
1157-
MacroKind::Derive => {
1158-
wrap_item(w, "derive", |w| {
1159-
write!(w, "#[derive({})]", name);
1160-
if !m.helpers.is_empty() {
1161-
w.push_str("\n{\n");
1162-
w.push_str(" // Attributes available to this derive:\n");
1163-
for attr in &m.helpers {
1164-
writeln!(w, " #[{}]", attr);
1168+
wrap_into_docblock(w, |w| {
1169+
let name = it.name.as_ref().expect("proc-macros always have names");
1170+
match m.kind {
1171+
MacroKind::Bang => {
1172+
wrap_item(w, "macro", |w| {
1173+
write!(w, "{}!() {{ /* proc-macro */ }}", name);
1174+
});
1175+
}
1176+
MacroKind::Attr => {
1177+
wrap_item(w, "attr", |w| {
1178+
write!(w, "#[{}]", name);
1179+
});
1180+
}
1181+
MacroKind::Derive => {
1182+
wrap_item(w, "derive", |w| {
1183+
write!(w, "#[derive({})]", name);
1184+
if !m.helpers.is_empty() {
1185+
w.push_str("\n{\n");
1186+
w.push_str(" // Attributes available to this derive:\n");
1187+
for attr in &m.helpers {
1188+
writeln!(w, " #[{}]", attr);
1189+
}
1190+
w.push_str("}\n");
11651191
}
1166-
w.push_str("}\n");
1167-
}
1168-
});
1192+
});
1193+
}
11691194
}
1170-
}
1195+
});
11711196
document(w, cx, it, None, HeadingOffset::H2)
11721197
}
11731198

@@ -1177,38 +1202,40 @@ fn item_primitive(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
11771202
}
11781203

11791204
fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) {
1180-
wrap_item(w, "const", |w| {
1181-
render_attributes_in_code(w, it);
1205+
wrap_into_docblock(w, |w| {
1206+
wrap_item(w, "const", |w| {
1207+
render_attributes_in_code(w, it);
11821208

1183-
write!(
1184-
w,
1185-
"{vis}const {name}: {typ}",
1186-
vis = it.visibility.print_with_space(it.def_id, cx),
1187-
name = it.name.as_ref().unwrap(),
1188-
typ = c.type_.print(cx),
1189-
);
1209+
write!(
1210+
w,
1211+
"{vis}const {name}: {typ}",
1212+
vis = it.visibility.print_with_space(it.def_id, cx),
1213+
name = it.name.as_ref().unwrap(),
1214+
typ = c.type_.print(cx),
1215+
);
11901216

1191-
let value = c.value(cx.tcx());
1192-
let is_literal = c.is_literal(cx.tcx());
1193-
let expr = c.expr(cx.tcx());
1194-
if value.is_some() || is_literal {
1195-
write!(w, " = {expr};", expr = Escape(&expr));
1196-
} else {
1197-
w.write_str(";");
1198-
}
1217+
let value = c.value(cx.tcx());
1218+
let is_literal = c.is_literal(cx.tcx());
1219+
let expr = c.expr(cx.tcx());
1220+
if value.is_some() || is_literal {
1221+
write!(w, " = {expr};", expr = Escape(&expr));
1222+
} else {
1223+
w.write_str(";");
1224+
}
11991225

1200-
if !is_literal {
1201-
if let Some(value) = &value {
1202-
let value_lowercase = value.to_lowercase();
1203-
let expr_lowercase = expr.to_lowercase();
1226+
if !is_literal {
1227+
if let Some(value) = &value {
1228+
let value_lowercase = value.to_lowercase();
1229+
let expr_lowercase = expr.to_lowercase();
12041230

1205-
if value_lowercase != expr_lowercase
1206-
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
1207-
{
1208-
write!(w, " // {value}", value = Escape(value));
1231+
if value_lowercase != expr_lowercase
1232+
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
1233+
{
1234+
write!(w, " // {value}", value = Escape(value));
1235+
}
12091236
}
12101237
}
1211-
}
1238+
});
12121239
});
12131240

12141241
document(w, cx, it, None, HeadingOffset::H2)
@@ -1268,30 +1295,34 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
12681295
}
12691296

12701297
fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Static) {
1271-
wrap_item(w, "static", |w| {
1272-
render_attributes_in_code(w, it);
1273-
write!(
1274-
w,
1275-
"{vis}static {mutability}{name}: {typ}",
1276-
vis = it.visibility.print_with_space(it.def_id, cx),
1277-
mutability = s.mutability.print_with_space(),
1278-
name = it.name.as_ref().unwrap(),
1279-
typ = s.type_.print(cx)
1280-
);
1298+
wrap_into_docblock(w, |w| {
1299+
wrap_item(w, "static", |w| {
1300+
render_attributes_in_code(w, it);
1301+
write!(
1302+
w,
1303+
"{vis}static {mutability}{name}: {typ}",
1304+
vis = it.visibility.print_with_space(it.def_id, cx),
1305+
mutability = s.mutability.print_with_space(),
1306+
name = it.name.as_ref().unwrap(),
1307+
typ = s.type_.print(cx)
1308+
);
1309+
});
12811310
});
12821311
document(w, cx, it, None, HeadingOffset::H2)
12831312
}
12841313

12851314
fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
1286-
wrap_item(w, "foreigntype", |w| {
1287-
w.write_str("extern {\n");
1288-
render_attributes_in_code(w, it);
1289-
write!(
1290-
w,
1291-
" {}type {};\n}}",
1292-
it.visibility.print_with_space(it.def_id, cx),
1293-
it.name.as_ref().unwrap(),
1294-
);
1315+
wrap_into_docblock(w, |w| {
1316+
wrap_item(w, "foreigntype", |w| {
1317+
w.write_str("extern {\n");
1318+
render_attributes_in_code(w, it);
1319+
write!(
1320+
w,
1321+
" {}type {};\n}}",
1322+
it.visibility.print_with_space(it.def_id, cx),
1323+
it.name.as_ref().unwrap(),
1324+
);
1325+
});
12951326
});
12961327

12971328
document(w, cx, it, None, HeadingOffset::H2);
@@ -1374,7 +1405,7 @@ fn wrap_into_docblock<F>(w: &mut Buffer, f: F)
13741405
where
13751406
F: FnOnce(&mut Buffer),
13761407
{
1377-
w.write_str("<div class=\"docblock type-decl\">");
1408+
w.write_str("<div class=\"docblock item-decl\">");
13781409
f(w);
13791410
w.write_str("</div>")
13801411
}

src/librustdoc/html/static/css/rustdoc.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ code, pre, a.test-arrow, .code-header {
254254
pre {
255255
padding: 14px;
256256
}
257-
.type-decl pre {
257+
.docblock.item-decl {
258+
margin-left: 0;
259+
}
260+
.item-decl pre {
258261
overflow-x: auto;
259262
}
260263

@@ -550,6 +553,7 @@ nav.sub {
550553
flex-grow: 1;
551554
margin: 0px;
552555
padding: 0px;
556+
overflow-wrap: anywhere;
553557
}
554558

555559
.in-band > code, .in-band > .code-header {

src/librustdoc/html/static/css/themes/ayu.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ body.source .example-wrap pre.rust a {
220220
background: #333;
221221
}
222222

223-
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
223+
.docblock:not(.item-decl) a:not(.srclink):not(.test-arrow),
224224
.docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
225225
#help a {
226226
color: #39AFD7;

src/librustdoc/html/static/css/themes/dark.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ body.source .example-wrap pre.rust a {
181181
background: #333;
182182
}
183183

184-
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
184+
.docblock:not(.item-decl) a:not(.srclink):not(.test-arrow),
185185
.docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
186186
#help a {
187187
color: #D2991D;

src/librustdoc/html/static/css/themes/light.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ body.source .example-wrap pre.rust a {
176176
background: #eee;
177177
}
178178

179-
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
179+
.docblock:not(.item-decl) a:not(.srclink):not(.test-arrow),
180180
.docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
181181
#help a {
182182
color: #3873AD;

src/test/rustdoc-gui/basic.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
goto: file://|DOC_PATH|/test_docs/index.html
22
assert: ("#functions")
33
goto: ./struct.Foo.html
4-
assert: ("div.type-decl")
4+
assert: ("div.item-decl")

0 commit comments

Comments
 (0)