rustdoc: make Method/WhereClause wrappers use usize for indents

This commit is contained in:
QuietMisdreavus
2016-10-17 09:55:18 -05:00
parent 43abad4583
commit 61cc8700df
2 changed files with 32 additions and 40 deletions

View File

@@ -42,7 +42,7 @@ pub struct UnsafetySpace(pub hir::Unsafety);
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct ConstnessSpace(pub hir::Constness); pub struct ConstnessSpace(pub hir::Constness);
/// Wrapper struct for properly emitting a method declaration. /// Wrapper struct for properly emitting a method declaration.
pub struct Method<'a>(pub &'a clean::FnDecl, pub &'a str); pub struct Method<'a>(pub &'a clean::FnDecl, pub usize);
/// Similar to VisSpace, but used for mutability /// Similar to VisSpace, but used for mutability
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct MutableSpace(pub clean::Mutability); pub struct MutableSpace(pub clean::Mutability);
@@ -50,7 +50,7 @@ pub struct MutableSpace(pub clean::Mutability);
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct RawMutableSpace(pub clean::Mutability); pub struct RawMutableSpace(pub clean::Mutability);
/// Wrapper struct for emitting a where clause from Generics. /// Wrapper struct for emitting a where clause from Generics.
pub struct WhereClause<'a>(pub &'a clean::Generics, pub String); pub struct WhereClause<'a>(pub &'a clean::Generics, pub usize);
/// Wrapper struct for emitting type parameter bounds. /// Wrapper struct for emitting type parameter bounds.
pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]); pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
/// Wrapper struct for emitting a comma-separated list of items /// Wrapper struct for emitting a comma-separated list of items
@@ -157,7 +157,7 @@ impl fmt::Display for clean::Generics {
impl<'a> fmt::Display for WhereClause<'a> { impl<'a> fmt::Display for WhereClause<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &WhereClause(gens, ref pad) = self; let &WhereClause(gens, pad) = self;
if gens.where_predicates.is_empty() { if gens.where_predicates.is_empty() {
return Ok(()); return Ok(());
} }
@@ -207,14 +207,14 @@ impl<'a> fmt::Display for WhereClause<'a> {
if !f.alternate() { if !f.alternate() {
clause.push_str("</span>"); clause.push_str("</span>");
let plain = format!("{:#}", self); let plain = format!("{:#}", self);
if plain.len() + pad.len() > 80 { if plain.len() > 80 {
//break it onto its own line regardless, but make sure method impls and trait //break it onto its own line regardless, but make sure method impls and trait
//blocks keep their fixed padding (2 and 9, respectively) //blocks keep their fixed padding (2 and 9, respectively)
let padding = if pad.len() > 10 { let padding = if pad > 10 {
clause = clause.replace("class='where'", "class='where fmt-newline'"); clause = clause.replace("class='where'", "class='where fmt-newline'");
repeat("&nbsp;").take(8).collect::<String>() repeat("&nbsp;").take(8).collect::<String>()
} else { } else {
repeat("&nbsp;").take(pad.len() + 6).collect::<String>() repeat("&nbsp;").take(pad + 6).collect::<String>()
}; };
clause = clause.replace("<br>", &format!("<br>{}", padding)); clause = clause.replace("<br>", &format!("<br>{}", padding));
} else { } else {
@@ -773,8 +773,7 @@ fn fmt_impl(i: &clean::Impl, f: &mut fmt::Formatter, link_trait: bool) -> fmt::R
fmt::Display::fmt(&i.for_, f)?; fmt::Display::fmt(&i.for_, f)?;
plain.push_str(&format!("{:#}", i.for_)); plain.push_str(&format!("{:#}", i.for_));
let pad = repeat(" ").take(plain.len() + 1).collect::<String>(); fmt::Display::fmt(&WhereClause(&i.generics, plain.len() + 1), f)?;
fmt::Display::fmt(&WhereClause(&i.generics, pad), f)?;
Ok(()) Ok(())
} }
@@ -903,19 +902,21 @@ impl<'a> fmt::Display for Method<'a> {
let mut output: String; let mut output: String;
let plain: String; let plain: String;
let pad = repeat(" ").take(indent).collect::<String>();
if arrow.is_empty() { if arrow.is_empty() {
output = format!("({})", args); output = format!("({})", args);
plain = format!("{}({})", indent.replace("&nbsp;", " "), args_plain); plain = format!("{}({})", pad, args_plain);
} else { } else {
output = format!("({args})<br>{arrow}", args = args, arrow = arrow); output = format!("({args})<br>{arrow}", args = args, arrow = arrow);
plain = format!("{indent}({args}){arrow}", plain = format!("{pad}({args}){arrow}",
indent = indent.replace("&nbsp;", " "), pad = pad,
args = args_plain, args = args_plain,
arrow = arrow_plain); arrow = arrow_plain);
} }
if plain.len() > 80 { if plain.len() > 80 {
let pad = format!("<br>{}", indent); let pad = repeat("&nbsp;").take(indent).collect::<String>();
let pad = format!("<br>{}", pad);
output = output.replace("<br>", &pad); output = output.replace("<br>", &pad);
} else { } else {
output = output.replace("<br>", ""); output = output.replace("<br>", "");

View File

@@ -1967,14 +1967,13 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
UnstableFeatures::Allow => f.constness, UnstableFeatures::Allow => f.constness,
_ => hir::Constness::NotConst _ => hir::Constness::NotConst
}; };
let prefix = format!("{}{}{}{:#}fn {}{:#}", let indent = format!("{}{}{}{:#}fn {}{:#}",
VisSpace(&it.visibility), VisSpace(&it.visibility),
ConstnessSpace(vis_constness), ConstnessSpace(vis_constness),
UnsafetySpace(f.unsafety), UnsafetySpace(f.unsafety),
AbiSpace(f.abi), AbiSpace(f.abi),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
f.generics); f.generics).len();
let indent = repeat("&nbsp;").take(prefix.len()).collect::<String>();
write!(w, "<pre class='rust fn'>{vis}{constness}{unsafety}{abi}fn \ write!(w, "<pre class='rust fn'>{vis}{constness}{unsafety}{abi}fn \
{name}{generics}{decl}{where_clause}</pre>", {name}{generics}{decl}{where_clause}</pre>",
vis = VisSpace(&it.visibility), vis = VisSpace(&it.visibility),
@@ -1983,8 +1982,8 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
abi = AbiSpace(f.abi), abi = AbiSpace(f.abi),
name = it.name.as_ref().unwrap(), name = it.name.as_ref().unwrap(),
generics = f.generics, generics = f.generics,
where_clause = WhereClause(&f.generics, " ".to_string()), where_clause = WhereClause(&f.generics, 2),
decl = Method(&f.decl, &indent))?; decl = Method(&f.decl, indent))?;
document(w, cx, it) document(w, cx, it)
} }
@@ -2009,9 +2008,6 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
} }
} }
// Where clauses in traits are indented nine spaces, per rustdoc.css
let indent = " ".to_string();
// Output the trait definition // Output the trait definition
write!(w, "<pre class='rust trait'>{}{}trait {}{}{}{} ", write!(w, "<pre class='rust trait'>{}{}trait {}{}{}{} ",
VisSpace(&it.visibility), VisSpace(&it.visibility),
@@ -2019,7 +2015,8 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
t.generics, t.generics,
bounds, bounds,
WhereClause(&t.generics, indent))?; // Where clauses in traits are indented nine spaces, per rustdoc.css
WhereClause(&t.generics, 9))?;
let types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>(); let types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>();
let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>(); let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
@@ -2272,16 +2269,15 @@ fn render_assoc_item(w: &mut fmt::Formatter,
AbiSpace(abi), AbiSpace(abi),
name, name,
*g); *g);
let mut indent = repeat("&nbsp;").take(prefix.len()).collect::<String>(); let mut indent = prefix.len();
let where_indent = if parent == ItemType::Trait { let where_indent = if parent == ItemType::Trait {
indent += "&nbsp;&nbsp;&nbsp;&nbsp;"; indent += 4;
" ".to_string() 8
} else if parent == ItemType::Impl { } else if parent == ItemType::Impl {
" ".to_string() 2
} else { } else {
let prefix = prefix + &format!("{:#}", Method(d, &indent)); let prefix = prefix + &format!("{:#}", Method(d, indent));
let prefix = prefix.lines().last().unwrap(); prefix.lines().last().unwrap().len() + 1
repeat(" ").take(prefix.len() + 1).collect::<String>()
}; };
write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\ write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}", {generics}{decl}{where_clause}",
@@ -2291,7 +2287,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
href = href, href = href,
name = name, name = name,
generics = *g, generics = *g,
decl = Method(d, &indent), decl = Method(d, indent),
where_clause = WhereClause(g, where_indent)) where_clause = WhereClause(g, where_indent))
} }
match item.inner { match item.inner {
@@ -2402,7 +2398,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
let padding = format!("{}enum {}{:#} ", let padding = format!("{}enum {}{:#} ",
VisSpace(&it.visibility), VisSpace(&it.visibility),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
e.generics); e.generics).len();
write!(w, "{}enum {}{}{}", write!(w, "{}enum {}{}{}",
VisSpace(&it.visibility), VisSpace(&it.visibility),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
@@ -2558,8 +2554,7 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
match ty { match ty {
doctree::Plain => { doctree::Plain => {
if let Some(g) = g { if let Some(g) = g {
let pad = repeat(" ").take(plain.len() + 1).collect::<String>(); write!(w, "{}", WhereClause(g, plain.len() + 1))?
write!(w, "{}", WhereClause(g, pad))?
} }
let mut has_visible_fields = false; let mut has_visible_fields = false;
write!(w, " {{")?; write!(w, " {{")?;
@@ -2609,16 +2604,14 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
write!(w, ")")?; write!(w, ")")?;
plain.push_str(")"); plain.push_str(")");
if let Some(g) = g { if let Some(g) = g {
let pad = repeat(" ").take(plain.len() + 1).collect::<String>(); write!(w, "{}", WhereClause(g, plain.len() + 1))?
write!(w, "{}", WhereClause(g, pad))?
} }
write!(w, ";")?; write!(w, ";")?;
} }
doctree::Unit => { doctree::Unit => {
// Needed for PhantomData. // Needed for PhantomData.
if let Some(g) = g { if let Some(g) = g {
let pad = repeat(" ").take(plain.len() + 1).collect::<String>(); write!(w, "{}", WhereClause(g, plain.len() + 1))?
write!(w, "{}", WhereClause(g, pad))?
} }
write!(w, ";")?; write!(w, ";")?;
} }
@@ -2643,8 +2636,7 @@ fn render_union(w: &mut fmt::Formatter, it: &clean::Item,
if let Some(g) = g { if let Some(g) = g {
write!(w, "{}", g)?; write!(w, "{}", g)?;
plain.push_str(&format!("{:#}", g)); plain.push_str(&format!("{:#}", g));
let pad = repeat(" ").take(plain.len() + 1).collect::<String>(); write!(w, "{}", WhereClause(g, plain.len() + 1))?;
write!(w, "{}", WhereClause(g, pad))?;
} }
write!(w, " {{\n{}", tab)?; write!(w, " {{\n{}", tab)?;
@@ -2945,8 +2937,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
t: &clean::Typedef) -> fmt::Result { t: &clean::Typedef) -> fmt::Result {
let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics); let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics).len();
let indent = repeat(" ").take(indent.len()).collect::<String>();
write!(w, "<pre class='rust typedef'>type {}{}{where_clause} = {type_};</pre>", write!(w, "<pre class='rust typedef'>type {}{}{where_clause} = {type_};</pre>",
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
t.generics, t.generics,