diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-10-06 17:41:15 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-10-09 09:44:51 -0700 |
| commit | 01d58fe2cbad0872f92571960ca3d8a5b01d0784 (patch) | |
| tree | d4537ac8a64494444ded4aacf645f067ff45dfc1 | |
| parent | 1bfe450a5ed0396c23185fc739d77ac9f79efe15 (diff) | |
| download | rust-01d58fe2cbad0872f92571960ca3d8a5b01d0784.tar.gz rust-01d58fe2cbad0872f92571960ca3d8a5b01d0784.zip | |
rustdoc: Implement constant documentation
At the same time, migrate statics to constants.
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 25 | ||||
| -rw-r--r-- | src/librustdoc/doctree.rs | 13 | ||||
| -rw-r--r-- | src/librustdoc/flock.rs | 32 | ||||
| -rw-r--r-- | src/librustdoc/html/item_type.rs | 3 | ||||
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 20 | ||||
| -rw-r--r-- | src/librustdoc/html/render.rs | 53 | ||||
| -rw-r--r-- | src/librustdoc/html/static/main.js | 4 | ||||
| -rw-r--r-- | src/librustdoc/passes.rs | 3 | ||||
| -rw-r--r-- | src/librustdoc/visit_ast.rs | 13 |
9 files changed, 120 insertions, 46 deletions
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 5c78bb976f1..7e9bb2844a7 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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 @@ impl Clean<Item> for doctree::Module { 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 @@ impl Clean<Item> for doctree::Static { } } +#[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, diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 72964609049..b173f0f16e3 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -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 @@ impl 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 diff --git a/src/librustdoc/flock.rs b/src/librustdoc/flock.rs index d1cc37497dc..ef921a84cfb 100644 --- a/src/librustdoc/flock.rs +++ b/src/librustdoc/flock.rs @@ -38,10 +38,10 @@ mod imp { 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 @@ mod imp { 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 @@ mod imp { 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 @@ mod imp { 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 { diff --git a/src/librustdoc/html/item_type.rs b/src/librustdoc/html/item_type.rs index e18ab0bd14f..0b35f8ddc69 100644 --- a/src/librustdoc/html/item_type.rs +++ b/src/librustdoc/html/item_type.rs @@ -39,6 +39,7 @@ pub enum ItemType { Macro = 15, Primitive = 16, AssociatedType = 17, + Constant = 18, } impl ItemType { @@ -62,6 +63,7 @@ impl ItemType { 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, diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index faf361f0290..a5c6f79ef6b 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -48,16 +48,16 @@ pub struct Markdown<'a>(pub &'a str); /// 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 | diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index e9d65b0a40c..497bbd3a1cd 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1471,6 +1471,8 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, (_, &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 item_module(w: &mut fmt::Formatter, cx: &Context, 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 item_module(w: &mut fmt::Formatter, cx: &Context, 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 item_module(w: &mut fmt::Formatter, cx: &Context, 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'>{} </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 { diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 6992a966592..7c6f7ed3fe2 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -569,7 +569,9 @@ "ffi", "ffs", "macro", - "primitive"]; + "primitive", + "associatedtype", + "constant"]; function itemTypeFromName(typename) { for (var i = 0; i < itemTypes.length; ++i) { diff --git a/src/librustdoc/passes.rs b/src/librustdoc/passes.rs index 3c66a7c7850..1a9dd226f87 100644 --- a/src/librustdoc/passes.rs +++ b/src/librustdoc/passes.rs @@ -134,7 +134,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 ast_util::is_local(i.def_id) && !self.exported_items.contains(&i.def_id.node) { return None; diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index d4a1a357909..6456f4acd30 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -308,6 +308,19 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { }; 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, |
