]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: Inline enums across crates
authorAlex Crichton <alex@alexcrichton.com>
Fri, 23 May 2014 23:14:54 +0000 (16:14 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sun, 25 May 2014 08:17:57 +0000 (01:17 -0700)
src/librustdoc/clean.rs

index 5497ca7b135a0e55d75ff692165389ac24a93e3e..77b835b232d9c8dc90b9439eb8ed90afb6f1ee67 100644 (file)
@@ -1204,6 +1204,49 @@ fn clean(&self) -> Item {
     }
 }
 
+impl Clean<Item> 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<Vec<Item>> {
             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<Item> {
     ty::populate_implementations_for_type_if_necessary(tcx, did);