From 55af4aff5641232e5c08ecf2402b1c795a24e50b Mon Sep 17 00:00:00 2001 From: P1start Date: Sat, 22 Nov 2014 20:48:55 +1300 Subject: [PATCH] Change how rustdoc shows constants and statics to be more similar to other items Fixes #19046. --- src/librustdoc/html/render.rs | 92 +++++++++++++---------------------- 1 file changed, 35 insertions(+), 57 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 5947f305b3c75..3b9c38d18fdbb 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1386,6 +1386,8 @@ impl<'a> fmt::Show for Item<'a> { clean::TypedefItem(ref t) => item_typedef(fmt, self.item, t), clean::MacroItem(ref m) => item_macro(fmt, self.item, m), clean::PrimitiveItem(ref p) => item_primitive(fmt, self.item, p), + clean::StaticItem(ref i) => item_static(fmt, self.item, i), + clean::ConstantItem(ref c) => item_constant(fmt, self.item, c), _ => Ok(()) } } @@ -1411,13 +1413,6 @@ fn full_path(cx: &Context, item: &clean::Item) -> String { return s } -fn blank<'a>(s: Option<&'a str>) -> &'a str { - match s { - Some(s) => s, - None => "" - } -} - fn shorter<'a>(s: Option<&'a str>) -> &'a str { match s { Some(s) => match s.find_str("\n\n") { @@ -1528,57 +1523,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, id = short, name = name)); } - 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, " = ")); - if s.contains("\n") { - match item.href() { - Some(url) => { - write!(f, "[definition]", - url) - } - None => Ok(()), - } - } else { - write!(f, "{}", s.as_slice()) - } - } - } - match myitem.inner { - clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => { - try!(write!(w, " - - {}{}static {}{}: {}{} - {}  - - ", - ConciseStability(&myitem.stability), - VisSpace(myitem.visibility), - MutableSpace(s.mutability), - *myitem.name.as_ref().unwrap(), - s.type_, - Initializer(s.expr.as_slice(), Item { cx: cx, item: myitem }), - Markdown(blank(myitem.doc_value())))); - } - clean::ConstantItem(ref s) => { - try!(write!(w, " - - {}{}const {}: {}{} - {}  - - ", - ConciseStability(&myitem.stability), - VisSpace(myitem.visibility), - *myitem.name.as_ref().unwrap(), - s.type_, - Initializer(s.expr.as_slice(), Item { cx: cx, item: myitem }), - Markdown(blank(myitem.doc_value())))); - } - clean::ViewItemItem(ref item) => { match item.inner { clean::ExternCrate(ref name, ref src, _) => { @@ -1625,6 +1570,39 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, write!(w, "") } +struct Initializer<'a>(&'a str); +impl<'a> fmt::Show for Initializer<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let Initializer(s) = *self; + if s.len() == 0 { return Ok(()); } + try!(write!(f, " = ")); + write!(f, "{}", s.as_slice()) + } +} + +fn item_constant(w: &mut fmt::Formatter, it: &clean::Item, + c: &clean::Constant) -> fmt::Result { + try!(write!(w, "
{vis}const \
+                    {name}: {typ}{init}
", + vis = VisSpace(it.visibility), + name = it.name.as_ref().unwrap().as_slice(), + typ = c.type_, + init = Initializer(c.expr.as_slice()))); + document(w, it) +} + +fn item_static(w: &mut fmt::Formatter, it: &clean::Item, + s: &clean::Static) -> fmt::Result { + try!(write!(w, "
{vis}static {mutability}\
+                    {name}: {typ}{init}
", + vis = VisSpace(it.visibility), + mutability = MutableSpace(s.mutability), + name = it.name.as_ref().unwrap().as_slice(), + typ = s.type_, + init = Initializer(s.expr.as_slice()))); + document(w, it) +} + fn item_function(w: &mut fmt::Formatter, it: &clean::Item, f: &clean::Function) -> fmt::Result { try!(write!(w, "
{vis}{fn_style}fn \