]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: Implement constant documentation
authorAlex Crichton <alex@alexcrichton.com>
Tue, 7 Oct 2014 00:41:15 +0000 (17:41 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 9 Oct 2014 16:44:51 +0000 (09:44 -0700)
At the same time, migrate statics to constants.

src/librustdoc/clean/mod.rs
src/librustdoc/doctree.rs
src/librustdoc/flock.rs
src/librustdoc/html/item_type.rs
src/librustdoc/html/markdown.rs
src/librustdoc/html/render.rs
src/librustdoc/html/static/main.js
src/librustdoc/passes.rs
src/librustdoc/visit_ast.rs

index 5c78bb976f1e33f0e54d7569cef3381d7ac028db..7e9bb2844a7c7a802de8f015574b42fa4d867736 100644 (file)
@@ -300,6 +300,7 @@ pub enum ItemEnum {
     ModuleItem(Module),
     TypedefItem(Typedef),
     StaticItem(Static),
+    ConstantItem(Constant),
     TraitItem(Trait),
     ImplItem(Impl),
     /// `use` and `extern crate`
@@ -347,6 +348,7 @@ fn clean(&self, cx: &DocContext) -> Item {
             self.mods.clean(cx),
             self.typedefs.clean(cx),
             self.statics.clean(cx),
+            self.constants.clean(cx),
             self.traits.clean(cx),
             self.impls.clean(cx),
             self.view_items.clean(cx).into_iter()
@@ -1741,6 +1743,29 @@ fn clean(&self, cx: &DocContext) -> Item {
     }
 }
 
+#[deriving(Clone, Encodable, Decodable)]
+pub struct Constant {
+    pub type_: Type,
+    pub expr: String,
+}
+
+impl Clean<Item> for doctree::Constant {
+    fn clean(&self, cx: &DocContext) -> Item {
+        Item {
+            name: Some(self.name.clean(cx)),
+            attrs: self.attrs.clean(cx),
+            source: self.whence.clean(cx),
+            def_id: ast_util::local_def(self.id),
+            visibility: self.vis.clean(cx),
+            stability: self.stab.clean(cx),
+            inner: ConstantItem(Constant {
+                type_: self.type_.clean(cx),
+                expr: self.expr.span.to_src(cx),
+            }),
+        }
+    }
+}
+
 #[deriving(Show, Clone, Encodable, Decodable, PartialEq)]
 pub enum Mutability {
     Mutable,
index 72964609049bf5fa61a2aff488e5fb7c46009e5e..b173f0f16e30d17d6c6070dada268bdb69833637 100644 (file)
@@ -30,6 +30,7 @@ pub struct Module {
     pub id: NodeId,
     pub typedefs: Vec<Typedef>,
     pub statics: Vec<Static>,
+    pub constants: Vec<Constant>,
     pub traits: Vec<Trait>,
     pub vis: ast::Visibility,
     pub stab: Option<attr::Stability>,
@@ -56,6 +57,7 @@ pub fn new(name: Option<Ident>) -> Module {
             mods       : Vec::new(),
             typedefs   : Vec::new(),
             statics    : Vec::new(),
+            constants  : Vec::new(),
             traits     : Vec::new(),
             impls      : Vec::new(),
             view_items : Vec::new(),
@@ -151,6 +153,17 @@ pub struct Static {
     pub whence: Span,
 }
 
+pub struct Constant {
+    pub type_: P<ast::Ty>,
+    pub expr: P<ast::Expr>,
+    pub name: Ident,
+    pub attrs: Vec<ast::Attribute>,
+    pub vis: ast::Visibility,
+    pub stab: Option<attr::Stability>,
+    pub id: ast::NodeId,
+    pub whence: Span,
+}
+
 pub struct Trait {
     pub name: Ident,
     pub items: Vec<ast::TraitItem>, //should be TraitItem
index d1cc37497dc44ef061def74d0156d5e9d221d8fc..ef921a84cfb8eea5f7a240ddc3b63b36ef989f62 100644 (file)
@@ -38,10 +38,10 @@ pub struct flock {
             pub l_sysid: libc::c_int,
         }
 
-        pub static F_WRLCK: libc::c_short = 1;
-        pub static F_UNLCK: libc::c_short = 2;
-        pub static F_SETLK: libc::c_int = 6;
-        pub static F_SETLKW: libc::c_int = 7;
+        pub const F_WRLCK: libc::c_short = 1;
+        pub const F_UNLCK: libc::c_short = 2;
+        pub const F_SETLK: libc::c_int = 6;
+        pub const F_SETLKW: libc::c_int = 7;
     }
 
     #[cfg(target_os = "freebsd")]
@@ -57,10 +57,10 @@ pub struct flock {
             pub l_sysid: libc::c_int,
         }
 
-        pub static F_UNLCK: libc::c_short = 2;
-        pub static F_WRLCK: libc::c_short = 3;
-        pub static F_SETLK: libc::c_int = 12;
-        pub static F_SETLKW: libc::c_int = 13;
+        pub const F_UNLCK: libc::c_short = 2;
+        pub const F_WRLCK: libc::c_short = 3;
+        pub const F_SETLK: libc::c_int = 12;
+        pub const F_SETLKW: libc::c_int = 13;
     }
 
     #[cfg(target_os = "dragonfly")]
@@ -78,10 +78,10 @@ pub struct flock {
             pub l_sysid: libc::c_int,
         }
 
-        pub static F_UNLCK: libc::c_short = 2;
-        pub static F_WRLCK: libc::c_short = 3;
-        pub static F_SETLK: libc::c_int = 8;
-        pub static F_SETLKW: libc::c_int = 9;
+        pub const F_UNLCK: libc::c_short = 2;
+        pub const F_WRLCK: libc::c_short = 3;
+        pub const F_SETLK: libc::c_int = 8;
+        pub const F_SETLKW: libc::c_int = 9;
     }
 
     #[cfg(any(target_os = "macos", target_os = "ios"))]
@@ -99,10 +99,10 @@ pub struct flock {
             pub l_sysid: libc::c_int,
         }
 
-        pub static F_UNLCK: libc::c_short = 2;
-        pub static F_WRLCK: libc::c_short = 3;
-        pub static F_SETLK: libc::c_int = 8;
-        pub static F_SETLKW: libc::c_int = 9;
+        pub const F_UNLCK: libc::c_short = 2;
+        pub const F_WRLCK: libc::c_short = 3;
+        pub const F_SETLK: libc::c_int = 8;
+        pub const F_SETLKW: libc::c_int = 9;
     }
 
     pub struct Lock {
index e18ab0bd14f59802ca56f3c1f1b287792afe1d7b..0b35f8ddc6963b397df5a633200553eec11b9cdd 100644 (file)
@@ -39,6 +39,7 @@ pub enum ItemType {
     Macro           = 15,
     Primitive       = 16,
     AssociatedType  = 17,
+    Constant        = 18,
 }
 
 impl ItemType {
@@ -62,6 +63,7 @@ pub fn to_static_str(&self) -> &'static str {
             Macro           => "macro",
             Primitive       => "primitive",
             AssociatedType  => "associatedtype",
+            Constant        => "constant",
         }
     }
 }
@@ -86,6 +88,7 @@ pub fn shortty(item: &clean::Item) -> ItemType {
         clean::FunctionItem(..)        => Function,
         clean::TypedefItem(..)         => Typedef,
         clean::StaticItem(..)          => Static,
+        clean::ConstantItem(..)        => Constant,
         clean::TraitItem(..)           => Trait,
         clean::ImplItem(..)            => Impl,
         clean::ViewItemItem(..)        => ViewItem,
index faf361f029059819665f7c5cc10b114ae7ab5f73..a5c6f79ef6b17fb7f6b0694655c44157a333d5de 100644 (file)
 /// table of contents.
 pub struct MarkdownWithToc<'a>(pub &'a str);
 
-static DEF_OUNIT: libc::size_t = 64;
-static HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 10;
-static HOEDOWN_EXT_TABLES: libc::c_uint = 1 << 0;
-static HOEDOWN_EXT_FENCED_CODE: libc::c_uint = 1 << 1;
-static HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
-static HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4;
-static HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8;
-static HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2;
-
-static HOEDOWN_EXTENSIONS: libc::c_uint =
+const DEF_OUNIT: libc::size_t = 64;
+const HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 10;
+const HOEDOWN_EXT_TABLES: libc::c_uint = 1 << 0;
+const HOEDOWN_EXT_FENCED_CODE: libc::c_uint = 1 << 1;
+const HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
+const HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4;
+const HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8;
+const HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2;
+
+const HOEDOWN_EXTENSIONS: libc::c_uint =
     HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES |
     HOEDOWN_EXT_FENCED_CODE | HOEDOWN_EXT_AUTOLINK |
     HOEDOWN_EXT_STRIKETHROUGH | HOEDOWN_EXT_SUPERSCRIPT |
index e9d65b0a40cf2135c55f1c169582c179bdc21006..497bbd3a1cd6bef5a552b5980bb59cc953530660 100644 (file)
@@ -1471,6 +1471,8 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
             (_, &clean::StructItem(..)) => Greater,
             (&clean::EnumItem(..), _) => Less,
             (_, &clean::EnumItem(..)) => Greater,
+            (&clean::ConstantItem(..), _) => Less,
+            (_, &clean::ConstantItem(..)) => Greater,
             (&clean::StaticItem(..), _) => Less,
             (_, &clean::StaticItem(..)) => Greater,
             (&clean::ForeignFunctionItem(..), _) => Less,
@@ -1507,6 +1509,7 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
                 clean::FunctionItem(..)        => ("functions", "Functions"),
                 clean::TypedefItem(..)         => ("types", "Type Definitions"),
                 clean::StaticItem(..)          => ("statics", "Statics"),
+                clean::ConstantItem(..)        => ("constants", "Constants"),
                 clean::TraitItem(..)           => ("traits", "Traits"),
                 clean::ImplItem(..)            => ("impls", "Implementations"),
                 clean::ViewItemItem(..)        => ("reexports", "Reexports"),
@@ -1526,28 +1529,28 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
                         id = short, name = name));
         }
 
-        match myitem.inner {
-            clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => {
-                struct Initializer<'a>(&'a str, Item<'a>);
-                impl<'a> fmt::Show for Initializer<'a> {
-                    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-                        let Initializer(s, item) = *self;
-                        if s.len() == 0 { return Ok(()); }
-                        try!(write!(f, "<code> = </code>"));
-                        if s.contains("\n") {
-                            match item.href() {
-                                Some(url) => {
-                                    write!(f, "<a href='{}'>[definition]</a>",
-                                           url)
-                                }
-                                None => Ok(()),
-                            }
-                        } else {
-                            write!(f, "<code>{}</code>", s.as_slice())
+        struct Initializer<'a>(&'a str, Item<'a>);
+        impl<'a> fmt::Show for Initializer<'a> {
+            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+                let Initializer(s, item) = *self;
+                if s.len() == 0 { return Ok(()); }
+                try!(write!(f, "<code> = </code>"));
+                if s.contains("\n") {
+                    match item.href() {
+                        Some(url) => {
+                            write!(f, "<a href='{}'>[definition]</a>",
+                                   url)
                         }
+                        None => Ok(()),
                     }
+                } else {
+                    write!(f, "<code>{}</code>", s.as_slice())
                 }
+            }
+        }
 
+        match myitem.inner {
+            clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => {
                 try!(write!(w, "
                     <tr>
                         <td>{}<code>{}static {}{}: {}</code>{}</td>
@@ -1562,6 +1565,20 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                 Initializer(s.expr.as_slice(), Item { cx: cx, item: myitem }),
                 Markdown(blank(myitem.doc_value()))));
             }
+            clean::ConstantItem(ref s) => {
+                try!(write!(w, "
+                    <tr>
+                        <td>{}<code>{}const {}: {}</code>{}</td>
+                        <td class='docblock'>{}&nbsp;</td>
+                    </tr>
+                ",
+                ConciseStability(&myitem.stability),
+                VisSpace(myitem.visibility),
+                *myitem.name.get_ref(),
+                s.type_,
+                Initializer(s.expr.as_slice(), Item { cx: cx, item: myitem }),
+                Markdown(blank(myitem.doc_value()))));
+            }
 
             clean::ViewItemItem(ref item) => {
                 match item.inner {
index 6992a9665929586b553d9d851ef7af72a01f0155..7c6f7ed3fe23014151ad32712b00f4ed4495382e 100644 (file)
                          "ffi",
                          "ffs",
                          "macro",
-                         "primitive"];
+                         "primitive",
+                         "associatedtype",
+                         "constant"];
 
         function itemTypeFromName(typename) {
             for (var i = 0; i < itemTypes.length; ++i) {
index 3c66a7c785094ff35440126918ef46f3079c3fea..1a9dd226f87df6ffdec9b6321d10f603204b6ca3 100644 (file)
@@ -134,7 +134,8 @@ fn fold_item(&mut self, i: Item) -> Option<Item> {
             clean::StructItem(..) | clean::EnumItem(..) |
             clean::TraitItem(..) | clean::FunctionItem(..) |
             clean::VariantItem(..) | clean::MethodItem(..) |
-            clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) => {
+            clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) |
+            clean::ConstantItem(..) => {
                 if ast_util::is_local(i.def_id) &&
                    !self.exported_items.contains(&i.def_id.node) {
                     return None;
index d4a1a35790936d5106287982557a84f2864bbac7..6456f4acd30c3e69119232163798dfd7f8b2c23d 100644 (file)
@@ -308,6 +308,19 @@ pub fn visit_item(&mut self, item: &ast::Item,
                 };
                 om.statics.push(s);
             },
+            ast::ItemConst(ref ty, ref exp) => {
+                let s = Constant {
+                    type_: ty.clone(),
+                    expr: exp.clone(),
+                    id: item.id,
+                    name: name,
+                    attrs: item.attrs.clone(),
+                    whence: item.span,
+                    vis: item.vis,
+                    stab: self.stability(item.id),
+                };
+                om.constants.push(s);
+            },
             ast::ItemTrait(ref gen, _, ref b, ref items) => {
                 let t = Trait {
                     name: name,