Apply the same transformation to every types

This commit is contained in:
Guillaume Gomez
2017-02-28 00:27:19 +01:00
parent 5ac7a035af
commit d06f72d062
3 changed files with 65 additions and 38 deletions

View File

@@ -90,6 +90,16 @@ impl<'a, T: fmt::Display> fmt::Display for CommaSep<'a, T> {
} }
} }
impl<'a, T: fmt::Debug> fmt::Debug for CommaSep<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, item) in self.0.iter().enumerate() {
if i != 0 { write!(f, ", ")?; }
fmt::Debug::fmt(item, f)?;
}
Ok(())
}
}
impl<'a> fmt::Display for TyParamBounds<'a> { impl<'a> fmt::Display for TyParamBounds<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &TyParamBounds(bounds) = self; let &TyParamBounds(bounds) = self;
@@ -165,7 +175,7 @@ impl<'a> fmt::Display for WhereClause<'a> {
if f.alternate() { if f.alternate() {
clause.push_str(" where "); clause.push_str(" where ");
} else { } else {
clause.push_str(" <span class='where fmt-newline'>where "); clause.push_str(" <span class=\"where fmt-newline\">where ");
} }
for (i, pred) in gens.where_predicates.iter().enumerate() { for (i, pred) in gens.where_predicates.iter().enumerate() {
if i > 0 { if i > 0 {
@@ -449,8 +459,8 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
} else { } else {
root.push_str(&seg.name); root.push_str(&seg.name);
root.push_str("/"); root.push_str("/");
write!(w, "<a class='mod' write!(w, "<a class=\"mod\"
href='{}index.html'>{}</a>::", href=\"{}index.html\">{}</a>::",
root, root,
seg.name)?; seg.name)?;
} }
@@ -491,7 +501,7 @@ fn primitive_link(f: &mut fmt::Formatter,
Some(&def_id) if def_id.is_local() => { Some(&def_id) if def_id.is_local() => {
let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len()); let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
let len = if len == 0 {0} else {len - 1}; let len = if len == 0 {0} else {len - 1};
write!(f, "<a class='primitive' href='{}primitive.{}.html'>", write!(f, "<a class=\"primitive\" href=\"{}primitive.{}.html\">",
repeat("../").take(len).collect::<String>(), repeat("../").take(len).collect::<String>(),
prim.to_url_str())?; prim.to_url_str())?;
needs_termination = true; needs_termination = true;
@@ -508,7 +518,7 @@ fn primitive_link(f: &mut fmt::Formatter,
(.., render::Unknown) => None, (.., render::Unknown) => None,
}; };
if let Some((cname, root)) = loc { if let Some((cname, root)) = loc {
write!(f, "<a class='primitive' href='{}{}/primitive.{}.html'>", write!(f, "<a class=\"primitive\" href=\"{}{}/primitive.{}.html\">",
root, root,
cname, cname,
prim.to_url_str())?; prim.to_url_str())?;
@@ -550,7 +560,7 @@ impl<'a> fmt::Display for HRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match href(self.did) { match href(self.did) {
Some((url, shortty, fqp)) => if !f.alternate() { Some((url, shortty, fqp)) => if !f.alternate() {
write!(f, "<a class='{}' href='{}' title='{} {}'>{}</a>", write!(f, "<a class=\"{}\" href=\"{}\" title=\"{} {}\">{}</a>",
shortty, url, shortty, fqp.join("::"), self.text) shortty, url, shortty, fqp.join("::"), self.text)
} else { } else {
write!(f, "{}", self.text) write!(f, "{}", self.text)
@@ -599,13 +609,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
fmt::Display::fmt(one, f)?; fmt::Display::fmt(one, f)?;
primitive_link(f, PrimitiveType::Tuple, ",)") primitive_link(f, PrimitiveType::Tuple, ",)")
} }
&[ref one] => write!(f, "({},)", one), &[ref one] => write!(f, "({:?},)", one),
many if is_not_debug => { many if is_not_debug => {
primitive_link(f, PrimitiveType::Tuple, "(")?; primitive_link(f, PrimitiveType::Tuple, "(")?;
fmt::Display::fmt(&CommaSep(&many), f)?; fmt::Display::fmt(&CommaSep(&many), f)?;
primitive_link(f, PrimitiveType::Tuple, ")") primitive_link(f, PrimitiveType::Tuple, ")")
} }
many => write!(f, "({})", &CommaSep(&many)), many => write!(f, "({:?})", &CommaSep(&many)),
} }
} }
clean::Vector(ref t) if is_not_debug => { clean::Vector(ref t) if is_not_debug => {
@@ -613,7 +623,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
fmt::Display::fmt(t, f)?; fmt::Display::fmt(t, f)?;
primitive_link(f, PrimitiveType::Slice, &format!("]")) primitive_link(f, PrimitiveType::Slice, &format!("]"))
} }
clean::Vector(ref t) => write!(f, "[{}]", t), clean::Vector(ref t) => write!(f, "[{:?}]", t),
clean::FixedVector(ref t, ref s) if is_not_debug => { clean::FixedVector(ref t, ref s) if is_not_debug => {
primitive_link(f, PrimitiveType::Array, "[")?; primitive_link(f, PrimitiveType::Array, "[")?;
fmt::Display::fmt(t, f)?; fmt::Display::fmt(t, f)?;
@@ -627,9 +637,9 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
} }
clean::FixedVector(ref t, ref s) => { clean::FixedVector(ref t, ref s) => {
if f.alternate() { if f.alternate() {
write!(f, "[{}; {}]", t, s) write!(f, "[{:?}; {}]", t, s)
} else { } else {
write!(f, "[{}; {}]", t, Escape(s)) write!(f, "[{:?}; {}]", t, Escape(s))
} }
} }
clean::Never => f.write_str("!"), clean::Never => f.write_str("!"),
@@ -646,9 +656,9 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
} }
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => { clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
if f.alternate() { if f.alternate() {
write!(f, "*{}{:#}", RawMutableSpace(m), t) write!(f, "*{}{:#?}", RawMutableSpace(m), t)
} else { } else {
write!(f, "*{}{}", RawMutableSpace(m), t) write!(f, "*{}{:?}", RawMutableSpace(m), t)
} }
} }
_ if is_not_debug => { _ if is_not_debug => {
@@ -657,7 +667,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
fmt::Display::fmt(t, f) fmt::Display::fmt(t, f)
} }
_ => { _ => {
write!(f, "*{}{}", RawMutableSpace(m), t) write!(f, "*{}{:?}", RawMutableSpace(m), t)
} }
} }
} }
@@ -681,9 +691,9 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
} }
clean::Generic(_) => { clean::Generic(_) => {
if f.alternate() { if f.alternate() {
write!(f, "&{}{}[{:#}]", lt, m, **bt) write!(f, "&{}{}[{:#?}]", lt, m, **bt)
} else { } else {
write!(f, "&{}{}[{}]", lt, m, **bt) write!(f, "&{}{}[{:?}]", lt, m, **bt)
} }
} }
_ if is_not_debug => { _ if is_not_debug => {
@@ -700,9 +710,9 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
} }
_ => { _ => {
if f.alternate() { if f.alternate() {
write!(f, "&{}{}[{:#}]", lt, m, **bt) write!(f, "&{}{}[{:#?}]", lt, m, **bt)
} else { } else {
write!(f, "&{}{}[{}]", lt, m, **bt) write!(f, "&{}{}[{:?}]", lt, m, **bt)
} }
} }
} }
@@ -765,12 +775,16 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
} }
clean::QPath { ref name, ref self_type, ref trait_ } => { clean::QPath { ref name, ref self_type, ref trait_ } => {
if f.alternate() { if f.alternate() {
if is_not_debug {
write!(f, "<{:#} as {:#}>::{}", self_type, trait_, name) write!(f, "<{:#} as {:#}>::{}", self_type, trait_, name)
} else {
write!(f, "<{:#?} as {:#?}>::{}", self_type, trait_, name)
}
} else { } else {
if is_not_debug { if is_not_debug {
write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name) write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name)
} else { } else {
write!(f, "<{} as {}>::{}", self_type, trait_, name) write!(f, "<{:?} as {:?}>::{}", self_type, trait_, name)
} }
} }
} }

View File

@@ -144,12 +144,12 @@ impl<U: Write> Writer for U {
-> io::Result<()> { -> io::Result<()> {
match klass { match klass {
Class::None => write!(self, "{}", text), Class::None => write!(self, "{}", text),
klass => write!(self, "<span class='{}'>{}</span>", klass.rustdoc_class(), text), klass => write!(self, "<span class=\"{}\">{}</span>", klass.rustdoc_class(), text),
} }
} }
fn enter_span(&mut self, klass: Class) -> io::Result<()> { fn enter_span(&mut self, klass: Class) -> io::Result<()> {
write!(self, "<span class='{}'>", klass.rustdoc_class()) write!(self, "<span class=\"{}\">", klass.rustdoc_class())
} }
fn exit_span(&mut self) -> io::Result<()> { fn exit_span(&mut self) -> io::Result<()> {
@@ -363,7 +363,7 @@ fn write_header(class: Option<&str>,
if let Some(id) = id { if let Some(id) = id {
write!(out, "id='{}' ", id)?; write!(out, "id='{}' ", id)?;
} }
write!(out, "class='rust {}'>\n", class.unwrap_or("")) write!(out, "class=\"rust {}\">\n", class.unwrap_or(""))
} }
fn write_footer(out: &mut Write) -> io::Result<()> { fn write_footer(out: &mut Write) -> io::Result<()> {

View File

@@ -1547,7 +1547,7 @@ impl<'a> fmt::Display for Item<'a> {
component)?; component)?;
} }
} }
write!(fmt, "<a class='{}' href=''>{}</a>", write!(fmt, "<a class=\"{}\" href=''>{}</a>",
self.item.type_(), self.item.name.as_ref().unwrap())?; self.item.type_(), self.item.name.as_ref().unwrap())?;
write!(fmt, "</span>")?; // in-band write!(fmt, "</span>")?; // in-band
@@ -1667,8 +1667,20 @@ fn md_render_assoc_item(item: &clean::Item) -> String {
} }
} }
fn get_doc_value(item: &clean::Item) -> Option<&str> {
let x = item.doc_value();
if x.is_none() {
match item.inner {
clean::AssociatedConstItem(_, _) => Some(""),
_ => None,
}
} else {
x
}
}
fn document_full(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::Result { fn document_full(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::Result {
if let Some(s) = item.doc_value() { if let Some(s) = get_doc_value(item) {
write!(w, "<div class='docblock'>{}</div>", write!(w, "<div class='docblock'>{}</div>",
Markdown(&format!("{}{}", md_render_assoc_item(item), s)))?; Markdown(&format!("{}{}", md_render_assoc_item(item), s)))?;
} }
@@ -1831,7 +1843,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
let doc_value = myitem.doc_value().unwrap_or(""); let doc_value = myitem.doc_value().unwrap_or("");
write!(w, " write!(w, "
<tr class='{stab} module-item'> <tr class='{stab} module-item'>
<td><a class='{class}' href='{href}' <td><a class=\"{class}\" href=\"{href}\"
title='{title_type} {title}'>{name}</a>{unsafety_flag}</td> title='{title_type} {title}'>{name}</a>{unsafety_flag}</td>
<td class='docblock-short'> <td class='docblock-short'>
{stab_docs} {docs} {stab_docs} {docs}
@@ -2228,12 +2240,13 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink) -> String {
fn assoc_const(w: &mut fmt::Formatter, fn assoc_const(w: &mut fmt::Formatter,
it: &clean::Item, it: &clean::Item,
_ty: &clean::Type, ty: &clean::Type,
_default: Option<&String>, _default: Option<&String>,
link: AssocItemLink) -> fmt::Result { link: AssocItemLink) -> fmt::Result {
write!(w, "const <a href='{}' class='constant'><b>{}</b></a>", write!(w, "const <a href='{}' class=\"constant\"><b>{}</b></a>: {}",
naive_assoc_href(it, link), naive_assoc_href(it, link),
it.name.as_ref().unwrap())?; it.name.as_ref().unwrap(),
ty)?;
Ok(()) Ok(())
} }
@@ -2241,7 +2254,7 @@ fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
bounds: &Vec<clean::TyParamBound>, bounds: &Vec<clean::TyParamBound>,
default: Option<&clean::Type>, default: Option<&clean::Type>,
link: AssocItemLink) -> fmt::Result { link: AssocItemLink) -> fmt::Result {
write!(w, "type <a href='{}' class='type'>{}</a>", write!(w, "type <a href='{}' class=\"type\">{}</a>",
naive_assoc_href(it, link), naive_assoc_href(it, link),
it.name.as_ref().unwrap())?; it.name.as_ref().unwrap())?;
if !bounds.is_empty() { if !bounds.is_empty() {
@@ -2384,7 +2397,7 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
let ns_id = derive_id(format!("{}.{}", let ns_id = derive_id(format!("{}.{}",
field.name.as_ref().unwrap(), field.name.as_ref().unwrap(),
ItemType::StructField.name_space())); ItemType::StructField.name_space()));
write!(w, "<span id='{id}' class='{item_type}'> write!(w, "<span id='{id}' class=\"{item_type}\">
<span id='{ns_id}' class='invisible'> <span id='{ns_id}' class='invisible'>
<code>{name}: {ty}</code> <code>{name}: {ty}</code>
</span></span>", </span></span>",
@@ -2426,7 +2439,7 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if fields.peek().is_some() { if fields.peek().is_some() {
write!(w, "<h2 class='fields'>Fields</h2>")?; write!(w, "<h2 class='fields'>Fields</h2>")?;
for (field, ty) in fields { for (field, ty) in fields {
write!(w, "<span id='{shortty}.{name}' class='{shortty}'><code>{name}: {ty}</code> write!(w, "<span id='{shortty}.{name}' class=\"{shortty}\"><code>{name}: {ty}</code>
</span>", </span>",
shortty = ItemType::StructField, shortty = ItemType::StructField,
name = field.name.as_ref().unwrap(), name = field.name.as_ref().unwrap(),
@@ -2911,7 +2924,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
if render_method_item { if render_method_item {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space())); let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class='{}'>", id, item_type)?; write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<span id='{}' class='invisible'>", ns_id)?; write!(w, "<span id='{}' class='invisible'>", ns_id)?;
write!(w, "<code>")?; write!(w, "<code>")?;
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl)?; render_assoc_item(w, item, link.anchor(&id), ItemType::Impl)?;
@@ -2923,7 +2936,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
clean::TypedefItem(ref tydef, _) => { clean::TypedefItem(ref tydef, _) => {
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name)); let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space())); let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class='{}'>", id, item_type)?; write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?; write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?; assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?; write!(w, "</code></span></h4>\n")?;
@@ -2931,7 +2944,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
clean::AssociatedConstItem(ref ty, ref default) => { clean::AssociatedConstItem(ref ty, ref default) => {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space())); let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class='{}'>", id, item_type)?; write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?; write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?; assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?; write!(w, "</code></span></h4>\n")?;
@@ -2939,7 +2952,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
clean::ConstantItem(ref c) => { clean::ConstantItem(ref c) => {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space())); let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class='{}'>", id, item_type)?; write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?; write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
assoc_const(w, item, &c.type_, Some(&c.expr), link.anchor(&id))?; assoc_const(w, item, &c.type_, Some(&c.expr), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?; write!(w, "</code></span></h4>\n")?;
@@ -2947,7 +2960,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
clean::AssociatedTypeItem(ref bounds, ref default) => { clean::AssociatedTypeItem(ref bounds, ref default) => {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space())); let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class='{}'>", id, item_type)?; write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?; write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?; assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?; write!(w, "</code></span></h4>\n")?;
@@ -2965,7 +2978,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
// We need the stability of the item from the trait // We need the stability of the item from the trait
// because impls can't have a stability. // because impls can't have a stability.
document_stability(w, cx, it)?; document_stability(w, cx, it)?;
if item.doc_value().is_some() { if get_doc_value(item).is_some() {
document_full(w, item)?; document_full(w, item)?;
} else { } else {
// In case the item isn't documented, // In case the item isn't documented,