1
Fork 0

rustdoc: Render visibilities succinctly

This commit is contained in:
Camelid 2020-12-25 11:48:12 -08:00
parent 3d10d3e49d
commit d3f4c48b49
3 changed files with 38 additions and 22 deletions

View file

@ -2299,14 +2299,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
if matchers.len() <= 1 { if matchers.len() <= 1 {
format!( format!(
"{}macro {}{} {{\n ...\n}}", "{}macro {}{} {{\n ...\n}}",
vis.print_with_space(cx.tcx), vis.print_with_space(cx.tcx, item.hir_id.owner),
name, name,
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(), matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
) )
} else { } else {
format!( format!(
"{}macro {} {{\n{}}}", "{}macro {} {{\n{}}}",
vis.print_with_space(cx.tcx), vis.print_with_space(cx.tcx, item.hir_id.owner),
name, name,
matchers matchers
.iter() .iter()

View file

@ -12,7 +12,7 @@ use std::fmt;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX}; use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use crate::clean::{self, PrimitiveType}; use crate::clean::{self, PrimitiveType};
@ -1085,12 +1085,21 @@ impl Function<'_> {
} }
impl clean::Visibility { impl clean::Visibility {
crate fn print_with_space<'tcx>(self, tcx: TyCtxt<'tcx>) -> impl fmt::Display + 'tcx { crate fn print_with_space<'tcx>(
self,
tcx: TyCtxt<'tcx>,
item_did: LocalDefId,
) -> impl fmt::Display + 'tcx {
use rustc_span::symbol::kw; use rustc_span::symbol::kw;
display_fn(move |f| match self { display_fn(move |f| match self {
clean::Public => f.write_str("pub "), clean::Public => f.write_str("pub "),
clean::Inherited => Ok(()), clean::Inherited => Ok(()),
clean::Visibility::Restricted(did)
if did.index == tcx.parent_module_from_def_id(item_did).local_def_index =>
{
Ok(())
}
clean::Visibility::Restricted(did) if did.index == CRATE_DEF_INDEX => { clean::Visibility::Restricted(did) if did.index == CRATE_DEF_INDEX => {
write!(f, "pub(crate) ") write!(f, "pub(crate) ")
} }

View file

@ -2157,14 +2157,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
Some(ref src) => write!( Some(ref src) => write!(
w, w,
"<tr><td><code>{}extern crate {} as {};", "<tr><td><code>{}extern crate {} as {};",
myitem.visibility.print_with_space(cx.tcx()), myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()),
anchor(myitem.def_id, &*src.as_str()), anchor(myitem.def_id, &*src.as_str()),
name name
), ),
None => write!( None => write!(
w, w,
"<tr><td><code>{}extern crate {};", "<tr><td><code>{}extern crate {};",
myitem.visibility.print_with_space(cx.tcx()), myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()),
anchor(myitem.def_id, &*name.as_str()) anchor(myitem.def_id, &*name.as_str())
), ),
} }
@ -2175,7 +2175,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
write!( write!(
w, w,
"<tr><td><code>{}{}</code></td></tr>", "<tr><td><code>{}{}</code></td></tr>",
myitem.visibility.print_with_space(cx.tcx()), myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()),
import.print() import.print()
); );
} }
@ -2392,7 +2392,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::
write!( write!(
w, w,
"{vis}const {name}: {typ}", "{vis}const {name}: {typ}",
vis = it.visibility.print_with_space(cx.tcx()), vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
name = it.name.as_ref().unwrap(), name = it.name.as_ref().unwrap(),
typ = c.type_.print(), typ = c.type_.print(),
); );
@ -2426,7 +2426,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
write!( write!(
w, w,
"{vis}static {mutability}{name}: {typ}</pre>", "{vis}static {mutability}{name}: {typ}</pre>",
vis = it.visibility.print_with_space(cx.tcx()), vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
mutability = s.mutability.print_with_space(), mutability = s.mutability.print_with_space(),
name = it.name.as_ref().unwrap(), name = it.name.as_ref().unwrap(),
typ = s.type_.print() typ = s.type_.print()
@ -2437,7 +2437,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
let header_len = format!( let header_len = format!(
"{}{}{}{}{:#}fn {}{:#}", "{}{}{}{}{:#}fn {}{:#}",
it.visibility.print_with_space(cx.tcx()), it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
f.header.constness.print_with_space(), f.header.constness.print_with_space(),
f.header.asyncness.print_with_space(), f.header.asyncness.print_with_space(),
f.header.unsafety.print_with_space(), f.header.unsafety.print_with_space(),
@ -2452,7 +2452,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::
w, w,
"{vis}{constness}{asyncness}{unsafety}{abi}fn \ "{vis}{constness}{asyncness}{unsafety}{abi}fn \
{name}{generics}{decl}{spotlight}{where_clause}</pre>", {name}{generics}{decl}{spotlight}{where_clause}</pre>",
vis = it.visibility.print_with_space(cx.tcx()), vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
constness = f.header.constness.print_with_space(), constness = f.header.constness.print_with_space(),
asyncness = f.header.asyncness.print_with_space(), asyncness = f.header.asyncness.print_with_space(),
unsafety = f.header.unsafety.print_with_space(), unsafety = f.header.unsafety.print_with_space(),
@ -2578,7 +2578,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
write!( write!(
w, w,
"{}{}{}trait {}{}{}", "{}{}{}trait {}{}{}",
it.visibility.print_with_space(cx.tcx()), it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
t.unsafety.print_with_space(), t.unsafety.print_with_space(),
if t.is_auto { "auto " } else { "" }, if t.is_auto { "auto " } else { "" },
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
@ -2896,7 +2896,7 @@ fn assoc_const(
w, w,
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}", "{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
extra, extra,
it.visibility.print_with_space(cx.tcx()), it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
naive_assoc_href(it, link), naive_assoc_href(it, link),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
ty.print() ty.print()
@ -3015,7 +3015,7 @@ fn render_assoc_item(
}; };
let mut header_len = format!( let mut header_len = format!(
"{}{}{}{}{}{:#}fn {}{:#}", "{}{}{}{}{}{:#}fn {}{:#}",
meth.visibility.print_with_space(cx.tcx()), meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()),
header.constness.print_with_space(), header.constness.print_with_space(),
header.asyncness.print_with_space(), header.asyncness.print_with_space(),
header.unsafety.print_with_space(), header.unsafety.print_with_space(),
@ -3037,7 +3037,7 @@ fn render_assoc_item(
"{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\ "{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
{generics}{decl}{spotlight}{where_clause}", {generics}{decl}{spotlight}{where_clause}",
if parent == ItemType::Trait { " " } else { "" }, if parent == ItemType::Trait { " " } else { "" },
meth.visibility.print_with_space(cx.tcx()), meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()),
header.constness.print_with_space(), header.constness.print_with_space(),
header.asyncness.print_with_space(), header.asyncness.print_with_space(),
header.unsafety.print_with_space(), header.unsafety.print_with_space(),
@ -3189,7 +3189,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
write!( write!(
w, w,
"{}enum {}{}{}", "{}enum {}{}{}",
it.visibility.print_with_space(cx.tcx()), it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
e.generics.print(), e.generics.print(),
WhereClause { gens: &e.generics, indent: 0, end_newline: true } WhereClause { gens: &e.generics, indent: 0, end_newline: true }
@ -3364,7 +3364,7 @@ fn render_struct(
write!( write!(
w, w,
"{}{}{}", "{}{}{}",
it.visibility.print_with_space(cx.tcx()), it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
if structhead { "struct " } else { "" }, if structhead { "struct " } else { "" },
it.name.as_ref().unwrap() it.name.as_ref().unwrap()
); );
@ -3384,7 +3384,7 @@ fn render_struct(
w, w,
"\n{} {}{}: {},", "\n{} {}{}: {},",
tab, tab,
field.visibility.print_with_space(cx.tcx()), field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()),
field.name.as_ref().unwrap(), field.name.as_ref().unwrap(),
ty.print() ty.print()
); );
@ -3413,7 +3413,14 @@ fn render_struct(
match field.kind { match field.kind {
clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"), clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"),
clean::StructFieldItem(ref ty) => { clean::StructFieldItem(ref ty) => {
write!(w, "{}{}", field.visibility.print_with_space(cx.tcx()), ty.print()) write!(
w,
"{}{}",
field
.visibility
.print_with_space(cx.tcx(), field.def_id.expect_local()),
ty.print()
)
} }
_ => unreachable!(), _ => unreachable!(),
} }
@ -3446,7 +3453,7 @@ fn render_union(
write!( write!(
w, w,
"{}{}{}", "{}{}{}",
it.visibility.print_with_space(cx.tcx()), it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
if structhead { "union " } else { "" }, if structhead { "union " } else { "" },
it.name.as_ref().unwrap() it.name.as_ref().unwrap()
); );
@ -3461,7 +3468,7 @@ fn render_union(
write!( write!(
w, w,
" {}{}: {},\n{}", " {}{}: {},\n{}",
field.visibility.print_with_space(cx.tcx()), field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()),
field.name.as_ref().unwrap(), field.name.as_ref().unwrap(),
ty.print(), ty.print(),
tab tab
@ -4100,7 +4107,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, cache:
write!( write!(
w, w,
" {}type {};\n}}</pre>", " {}type {};\n}}</pre>",
it.visibility.print_with_space(cx.tcx()), it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
); );