Skip to content

Commit

Permalink
rustdoc: Implement constant documentation
Browse files Browse the repository at this point in the history
At the same time, migrate statics to constants.
  • Loading branch information
alexcrichton committed Oct 9, 2014
1 parent 1bfe450 commit 01d58fe
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 46 deletions.
25 changes: 25 additions & 0 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -300,6 +300,7 @@ pub enum ItemEnum {
ModuleItem(Module),
TypedefItem(Typedef),
StaticItem(Static),
ConstantItem(Constant),
TraitItem(Trait),
ImplItem(Impl),
/// `use` and `extern crate`
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions src/librustdoc/doctree.rs
Expand Up @@ -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>,
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions src/librustdoc/flock.rs
Expand Up @@ -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")]
Expand All @@ -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")]
Expand All @@ -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"))]
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/html/item_type.rs
Expand Up @@ -39,6 +39,7 @@ pub enum ItemType {
Macro = 15,
Primitive = 16,
AssociatedType = 17,
Constant = 18,
}

impl ItemType {
Expand All @@ -62,6 +63,7 @@ impl ItemType {
Macro => "macro",
Primitive => "primitive",
AssociatedType => "associatedtype",
Constant => "constant",
}
}
}
Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions src/librustdoc/html/markdown.rs
Expand Up @@ -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 |
Expand Down
53 changes: 35 additions & 18 deletions src/librustdoc/html/render.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand All @@ -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>
Expand All @@ -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'>{}&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 {
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/static/main.js
Expand Up @@ -569,7 +569,9 @@
"ffi",
"ffs",
"macro",
"primitive"];
"primitive",
"associatedtype",
"constant"];

function itemTypeFromName(typename) {
for (var i = 0; i < itemTypes.length; ++i) {
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/passes.rs
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/librustdoc/visit_ast.rs
Expand Up @@ -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,
Expand Down

0 comments on commit 01d58fe

Please sign in to comment.