Skip to content

Commit 234ec95

Browse files
committed
Render longhand multiple crate/target features nicer
1 parent a8de713 commit 234ec95

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/librustdoc/clean/cfg.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,21 +324,68 @@ impl<'a> fmt::Display for Html<'a> {
324324

325325
Cfg::Any(ref sub_cfgs) => {
326326
let separator = if sub_cfgs.iter().all(Cfg::is_simple) { " or " } else { ", or " };
327+
328+
let short_longhand = !self.1 && {
329+
let all_crate_features = sub_cfgs
330+
.iter()
331+
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
332+
let all_target_features = sub_cfgs
333+
.iter()
334+
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));
335+
336+
if all_crate_features {
337+
fmt.write_str("crate features ")?;
338+
true
339+
} else if all_target_features {
340+
fmt.write_str("target features ")?;
341+
true
342+
} else {
343+
false
344+
}
345+
};
346+
327347
for (i, sub_cfg) in sub_cfgs.iter().enumerate() {
328348
if i != 0 {
329349
fmt.write_str(separator)?;
330350
}
331-
write_with_opt_paren(fmt, !sub_cfg.is_all(), Html(sub_cfg, self.1))?;
351+
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
352+
write!(fmt, "<code>{}</code>", feat)?;
353+
} else {
354+
write_with_opt_paren(fmt, !sub_cfg.is_all(), Html(sub_cfg, self.1))?;
355+
}
332356
}
333357
Ok(())
334358
}
335359

336360
Cfg::All(ref sub_cfgs) => {
361+
let short_longhand = !self.1 && {
362+
let all_crate_features = sub_cfgs
363+
.iter()
364+
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
365+
let all_target_features = sub_cfgs
366+
.iter()
367+
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));
368+
369+
if all_crate_features {
370+
fmt.write_str("crate features ")?;
371+
true
372+
} else if all_target_features {
373+
fmt.write_str("target features ")?;
374+
true
375+
} else {
376+
false
377+
}
378+
};
379+
337380
for (i, sub_cfg) in sub_cfgs.iter().enumerate() {
338381
if i != 0 {
339382
fmt.write_str(" and ")?;
340383
}
341-
write_with_opt_paren(fmt, !sub_cfg.is_simple(), Html(sub_cfg, self.1))?;
384+
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
385+
write!(fmt, "<code>{}</code>", feat)?;
386+
} else {
387+
write_with_opt_paren(fmt, !sub_cfg.is_simple(), Html(sub_cfg, self.1))?;
388+
}
342389
}
343390
Ok(())
344391
}

src/test/rustdoc/duplicate-cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ pub mod bar {
1818
}
1919

2020
// @has 'foo/baz/struct.Baz.html'
21-
// @has '-' '//*[@class="stab portability"]' 'This is supported on crate feature sync and crate feature send only.'
21+
// @has '-' '//*[@class="stab portability"]' 'This is supported on crate features sync and send only.'
2222
#[doc(cfg(all(feature = "sync", feature = "send")))]
2323
pub mod baz {
2424
#[doc(cfg(feature = "sync"))]
2525
pub struct Baz;
2626
}
2727

2828
// @has 'foo/qux/struct.Qux.html'
29-
// @has '-' '//*[@class="stab portability"]' 'This is supported on crate feature sync and crate feature send only.'
29+
// @has '-' '//*[@class="stab portability"]' 'This is supported on crate features sync and send only.'
3030
#[doc(cfg(feature = "sync"))]
3131
pub mod qux {
3232
#[doc(cfg(all(feature = "sync", feature = "send")))]

0 commit comments

Comments
 (0)