Refactor HiddenStructField into StrippedItem

This commit is contained in:
mitaa 2016-04-02 08:17:59 +02:00
parent b1543a1aac
commit 0ef85c1e6a
4 changed files with 76 additions and 56 deletions

View File

@ -14,7 +14,6 @@
pub use self::Type::*;
pub use self::PrimitiveType::*;
pub use self::TypeKind::*;
pub use self::StructField::*;
pub use self::VariantKind::*;
pub use self::Mutability::*;
pub use self::Import::*;
@ -309,6 +308,15 @@ impl Item {
pub fn is_stripped(&self) -> bool {
match self.inner { StrippedItem(..) => true, _ => false }
}
pub fn has_stripped_fields(&self) -> Option<bool> {
match self.inner {
StructItem(ref _struct) => Some(_struct.fields_stripped),
VariantItem(Variant { kind: StructVariant(ref vstruct)} ) => {
Some(vstruct.fields_stripped)
},
_ => None,
}
}
pub fn stability_class(&self) -> String {
self.stability.as_ref().map(|ref s| {
@ -346,7 +354,7 @@ pub enum ItemEnum {
TyMethodItem(TyMethod),
/// A method with a body.
MethodItem(Method),
StructFieldItem(StructField),
StructFieldItem(Type),
VariantItem(Variant),
/// `fn`s from an extern block
ForeignFunctionItem(Function),
@ -1740,12 +1748,6 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
}
}
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum StructField {
HiddenStructField, // inserted later by strip passes
TypedStructField(Type),
}
impl Clean<Item> for hir::StructField {
fn clean(&self, cx: &DocContext) -> Item {
Item {
@ -1756,7 +1758,7 @@ impl Clean<Item> for hir::StructField {
stability: get_stability(cx, cx.map.local_def_id(self.id)),
deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)),
def_id: cx.map.local_def_id(self.id),
inner: StructFieldItem(TypedStructField(self.ty.clean(cx))),
inner: StructFieldItem(self.ty.clean(cx)),
}
}
}
@ -1773,7 +1775,7 @@ impl<'tcx> Clean<Item> for ty::FieldDefData<'tcx, 'static> {
stability: get_stability(cx, self.did),
deprecation: get_deprecation(cx, self.did),
def_id: self.did,
inner: StructFieldItem(TypedStructField(self.unsubst_ty().clean(cx))),
inner: StructFieldItem(self.unsubst_ty().clean(cx)),
}
}
}
@ -1904,9 +1906,7 @@ impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {
def_id: field.did,
stability: get_stability(cx, field.did),
deprecation: get_deprecation(cx, field.did),
inner: StructFieldItem(
TypedStructField(field.unsubst_ty().clean(cx))
)
inner: StructFieldItem(field.unsubst_ty().clean(cx))
}
}).collect()
})

View File

@ -1020,13 +1020,9 @@ impl DocFolder for Cache {
}
_ => ((None, Some(&*self.stack)), false)
};
let hidden_field = match item.inner {
clean::StructFieldItem(clean::HiddenStructField) => true,
_ => false
};
match parent {
(parent, Some(path)) if is_method || (!self.stripped_mod && !hidden_field) => {
(parent, Some(path)) if is_method || (!self.stripped_mod) => {
// Needed to determine `self` type.
let parent_basename = self.parent_stack.first().and_then(|parent| {
match self.paths.get(parent) {
@ -1051,7 +1047,7 @@ impl DocFolder for Cache {
});
}
}
(Some(parent), None) if is_method || (!self.stripped_mod && !hidden_field)=> {
(Some(parent), None) if is_method || (!self.stripped_mod)=> {
if parent.is_local() {
// We have a parent, but we don't know where they're
// defined yet. Wait for later to index this item.
@ -2165,8 +2161,7 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
document(w, cx, it)?;
let mut fields = s.fields.iter().filter(|f| {
match f.inner {
clean::StructFieldItem(clean::HiddenStructField) => false,
clean::StructFieldItem(clean::TypedStructField(..)) => true,
clean::StructFieldItem(..) => true,
_ => false,
}
}).peekable();
@ -2256,7 +2251,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if let clean::VariantItem( Variant { kind: StructVariant(ref s) } ) = variant.inner {
let fields = s.fields.iter().filter(|f| {
match f.inner {
clean::StructFieldItem(clean::TypedStructField(..)) => true,
clean::StructFieldItem(..) => true,
_ => false,
}
});
@ -2315,24 +2310,17 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
match ty {
doctree::Plain => {
write!(w, " {{\n{}", tab)?;
let mut fields_stripped = false;
for field in fields {
match field.inner {
clean::StructFieldItem(clean::HiddenStructField) => {
fields_stripped = true;
}
clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
write!(w, " {}{}: {},\n{}",
VisSpace(field.visibility),
field.name.as_ref().unwrap(),
*ty,
tab)?;
}
_ => unreachable!(),
};
if let clean::StructFieldItem(ref ty) = field.inner {
write!(w, " {}{}: {},\n{}",
VisSpace(field.visibility),
field.name.as_ref().unwrap(),
*ty,
tab)?;
}
}
if fields_stripped {
if it.has_stripped_fields().unwrap() {
write!(w, " // some fields omitted\n{}", tab)?;
}
write!(w, "}}")?;
@ -2344,10 +2332,10 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
write!(w, ", ")?;
}
match field.inner {
clean::StructFieldItem(clean::HiddenStructField) => {
clean::StrippedItem(box clean::StructFieldItem(..)) => {
write!(w, "_")?
}
clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
clean::StructFieldItem(ref ty) => {
write!(w, "{}{}", VisSpace(field.visibility), *ty)?
}
_ => unreachable!()

View File

@ -40,13 +40,9 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
// use a dedicated hidden item for given item type if any
match i.inner {
clean::StructFieldItem(..) => {
return Some(clean::Item {
inner: clean::StructFieldItem(clean::HiddenStructField),
..i
});
clean::StructFieldItem(..) | clean::ModuleItem(..) => {
return Strip(i).fold()
}
clean::ModuleItem(..) => return Strip(i).fold(),
_ => return None,
}
}
@ -130,7 +126,8 @@ impl<'a> fold::DocFolder for Stripper<'a> {
clean::StructItem(..) | clean::EnumItem(..) |
clean::TraitItem(..) | clean::FunctionItem(..) |
clean::VariantItem(..) | clean::MethodItem(..) |
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) => {
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) |
clean::ConstantItem(..) => {
if i.def_id.is_local() {
if !self.access_levels.is_exported(i.def_id) {
return None;
@ -138,18 +135,9 @@ impl<'a> fold::DocFolder for Stripper<'a> {
}
}
clean::ConstantItem(..) => {
if i.def_id.is_local() && !self.access_levels.is_exported(i.def_id) {
return None;
}
}
clean::StructFieldItem(..) => {
if i.visibility != Some(hir::Public) {
return Some(clean::Item {
inner: clean::StructFieldItem(clean::HiddenStructField),
..i
})
return Strip(i).fold();
}
}

View File

@ -0,0 +1,44 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// @has structfields/struct.Foo.html
pub struct Foo {
// @has - //pre "pub a: ()"
pub a: (),
// @has - //pre "// some fields omitted"
// @!has - //pre "b: ()"
b: (),
// @!has - //pre "c: usize"
#[doc(hidden)]
c: usize,
// @has - //pre "pub d: usize"
pub d: usize,
}
// @has structfields/struct.Bar.html
pub struct Bar {
// @has - //pre "pub a: ()"
pub a: (),
// @!has - //pre "// some fields omitted"
}
// @has structfields/enum.Qux.html
pub enum Qux {
Quz {
// @has - //pre "a: ()"
a: (),
// @!has - //pre "b: ()"
#[doc(hidden)]
b: (),
// @has - //pre "c: usize"
c: usize,
// @has - //pre "// some fields omitted"
},
}