From 622c8f7b57b70a61dc5c9a4b288ce5bf2ff8ac04 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 May 2014 16:14:54 -0700 Subject: [PATCH] rustdoc: Inline enums across crates --- src/librustdoc/clean.rs | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/librustdoc/clean.rs b/src/librustdoc/clean.rs index 5497ca7b135..77b835b232d 100644 --- a/src/librustdoc/clean.rs +++ b/src/librustdoc/clean.rs @@ -1204,6 +1204,49 @@ fn clean(&self) -> Item { } } +impl Clean for ty::VariantInfo { + fn clean(&self) -> Item { + // use syntax::parse::token::special_idents::unnamed_field; + let cx = super::ctxtkey.get().unwrap(); + let tcx = match cx.maybe_typed { + core::Typed(ref tycx) => tycx, + core::NotTyped(_) => fail!("tcx not present"), + }; + let kind = match self.arg_names.as_ref().map(|s| s.as_slice()) { + None | Some([]) if self.args.len() == 0 => CLikeVariant, + None | Some([]) => { + TupleVariant(self.args.iter().map(|t| t.clean()).collect()) + } + Some(s) => { + StructVariant(VariantStruct { + struct_type: doctree::Plain, + fields_stripped: false, + fields: s.iter().zip(self.args.iter()).map(|(name, ty)| { + Item { + source: Span::empty(), + name: Some(name.clean()), + attrs: Vec::new(), + visibility: Some(ast::Public), + def_id: self.id, // FIXME: this is not accurate + inner: StructFieldItem( + TypedStructField(ty.clean()) + ) + } + }).collect() + }) + } + }; + Item { + name: Some(self.name.clean()), + attrs: load_attrs(tcx, self.id), + source: Span::empty(), + visibility: Some(ast::Public), + def_id: self.id, + inner: VariantItem(Variant { kind: kind }), + } + } +} + #[deriving(Clone, Encodable, Decodable)] pub enum VariantKind { CLikeVariant, @@ -1524,6 +1567,10 @@ fn try_inline(id: ast::NodeId) -> Option> { ret.extend(build_impls(tcx, did).move_iter()); StructItem(build_struct(tcx, did)) } + ast::DefTy(did) => { + ret.extend(build_impls(tcx, did).move_iter()); + build_type(tcx, did) + } _ => return None, }; let fqn = csearch::get_item_path(tcx, did); @@ -1822,6 +1869,25 @@ fn build_struct(tcx: &ty::ctxt, did: ast::DefId) -> Struct { } } +fn build_type(tcx: &ty::ctxt, did: ast::DefId) -> ItemEnum { + let t = ty::lookup_item_type(tcx, did); + match ty::get(t.ty).sty { + ty::ty_enum(edid, _) => { + return EnumItem(Enum { + generics: t.generics.clean(), + variants_stripped: false, + variants: ty::enum_variants(tcx, edid).clean(), + }) + } + _ => {} + } + + TypedefItem(Typedef { + type_: t.ty.clean(), + generics: t.generics.clean(), + }) +} + fn build_impls(tcx: &ty::ctxt, did: ast::DefId) -> Vec { ty::populate_implementations_for_type_if_necessary(tcx, did); -- 2.44.0