1
Fork 0

rustdoc: convert render_attributes_in_pre to return a Display

This commit is contained in:
Michael Howell 2023-04-03 15:35:27 -07:00
parent fc5de13d31
commit 94faa5c739
2 changed files with 27 additions and 20 deletions

View file

@ -50,6 +50,7 @@ use std::string::ToString;
use askama::Template; use askama::Template;
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_attr::{ConstStability, Deprecation, StabilityLevel}; use rustc_attr::{ConstStability, Deprecation, StabilityLevel};
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::{DefId, DefIdSet}; use rustc_hir::def_id::{DefId, DefIdSet};
use rustc_hir::Mutability; use rustc_hir::Mutability;
@ -842,7 +843,7 @@ fn assoc_method(
let (indent, indent_str, end_newline) = if parent == ItemType::Trait { let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
header_len += 4; header_len += 4;
let indent_str = " "; let indent_str = " ";
render_attributes_in_pre(w, meth, indent_str); write!(w, "{}", render_attributes_in_pre(meth, indent_str));
(4, indent_str, Ending::NoNewline) (4, indent_str, Ending::NoNewline)
} else { } else {
render_attributes_in_code(w, meth); render_attributes_in_code(w, meth);
@ -1038,10 +1039,16 @@ fn attributes(it: &clean::Item) -> Vec<String> {
// When an attribute is rendered inside a `<pre>` tag, it is formatted using // When an attribute is rendered inside a `<pre>` tag, it is formatted using
// a whitespace prefix and newline. // a whitespace prefix and newline.
fn render_attributes_in_pre(w: &mut Buffer, it: &clean::Item, prefix: &str) { fn render_attributes_in_pre<'a>(
for a in attributes(it) { it: &'a clean::Item,
writeln!(w, "{}{}", prefix, a); prefix: &'a str,
} ) -> impl fmt::Display + Captures<'a> {
crate::html::format::display_fn(move |f| {
for a in attributes(it) {
writeln!(f, "{}{}", prefix, a)?;
}
Ok(())
})
} }
// When an attribute is rendered inside a <code> tag, it is formatted using // When an attribute is rendered inside a <code> tag, it is formatted using

View file

@ -544,12 +544,12 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
f.decl.output.as_return().and_then(|output| notable_traits_button(output, cx)); f.decl.output.as_return().and_then(|output| notable_traits_button(output, cx));
wrap_item(w, |w| { wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
w.reserve(header_len); w.reserve(header_len);
write!( write!(
w, w,
"{vis}{constness}{asyncness}{unsafety}{abi}fn \ "{attrs}{vis}{constness}{asyncness}{unsafety}{abi}fn \
{name}{generics}{decl}{notable_traits}{where_clause}", {name}{generics}{decl}{notable_traits}{where_clause}",
attrs = render_attributes_in_pre(it, ""),
vis = visibility, vis = visibility,
constness = constness, constness = constness,
asyncness = asyncness, asyncness = asyncness,
@ -581,16 +581,16 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
// Output the trait definition // Output the trait definition
wrap_item(w, |w| { wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!( write!(
w, w,
"{}{}{}trait {}{}{}", "{attrs}{}{}{}trait {}{}{}",
visibility_print_with_space(it.visibility(tcx), it.item_id, cx), visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
t.unsafety(tcx).print_with_space(), t.unsafety(tcx).print_with_space(),
if t.is_auto(tcx) { "auto " } else { "" }, if t.is_auto(tcx) { "auto " } else { "" },
it.name.unwrap(), it.name.unwrap(),
t.generics.print(cx), t.generics.print(cx),
bounds bounds,
attrs = render_attributes_in_pre(it, ""),
); );
if !t.generics.where_predicates.is_empty() { if !t.generics.where_predicates.is_empty() {
@ -1057,14 +1057,14 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) { fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) {
wrap_item(w, |w| { wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!( write!(
w, w,
"trait {}{}{} = {};", "{attrs}trait {}{}{} = {};",
it.name.unwrap(), it.name.unwrap(),
t.generics.print(cx), t.generics.print(cx),
print_where_clause(&t.generics, cx, 0, Ending::Newline), print_where_clause(&t.generics, cx, 0, Ending::Newline),
bounds(&t.bounds, true, cx) bounds(&t.bounds, true, cx),
attrs = render_attributes_in_pre(it, ""),
); );
}); });
@ -1079,14 +1079,14 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) { fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) {
wrap_item(w, |w| { wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!( write!(
w, w,
"type {}{}{where_clause} = impl {bounds};", "{attrs}type {}{}{where_clause} = impl {bounds};",
it.name.unwrap(), it.name.unwrap(),
t.generics.print(cx), t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline), where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
bounds = bounds(&t.bounds, false, cx), bounds = bounds(&t.bounds, false, cx),
attrs = render_attributes_in_pre(it, ""),
); );
}); });
@ -1102,15 +1102,15 @@ fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &cl
fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef) { fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef) {
fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) { fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
wrap_item(w, |w| { wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!( write!(
w, w,
"{}type {}{}{where_clause} = {type_};", "{attrs}{}type {}{}{where_clause} = {type_};",
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx), visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
it.name.unwrap(), it.name.unwrap(),
t.generics.print(cx), t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline), where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
type_ = t.type_.print(cx), type_ = t.type_.print(cx),
attrs = render_attributes_in_pre(it, ""),
); );
}); });
} }
@ -1130,7 +1130,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) { fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
wrap_item(w, |w| { wrap_item(w, |w| {
render_attributes_in_pre(w, it, ""); write!(w, "{}", render_attributes_in_pre(it, ""));
render_union(w, it, Some(&s.generics), &s.fields, cx); render_union(w, it, Some(&s.generics), &s.fields, cx);
}); });
@ -1197,13 +1197,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
let tcx = cx.tcx(); let tcx = cx.tcx();
let count_variants = e.variants().count(); let count_variants = e.variants().count();
wrap_item(w, |w| { wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!( write!(
w, w,
"{}enum {}{}", "{attrs}{}enum {}{}",
visibility_print_with_space(it.visibility(tcx), it.item_id, cx), visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
it.name.unwrap(), it.name.unwrap(),
e.generics.print(cx), e.generics.print(cx),
attrs = render_attributes_in_pre(it, ""),
); );
if !print_where_clause_and_check(w, &e.generics, cx) { if !print_where_clause_and_check(w, &e.generics, cx) {
// If there wasn't a `where` clause, we add a whitespace. // If there wasn't a `where` clause, we add a whitespace.