Skip to content

Commit

Permalink
rustdoc: make Method/WhereClause wrappers use usize for indents
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietMisdreavus committed Oct 17, 2016
1 parent 43abad4 commit 61cc870
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
25 changes: 13 additions & 12 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ pub struct UnsafetySpace(pub hir::Unsafety);
#[derive(Copy, Clone)]
pub struct ConstnessSpace(pub hir::Constness);
/// 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
#[derive(Copy, Clone)]
pub struct MutableSpace(pub clean::Mutability);
/// Similar to VisSpace, but used for mutability
#[derive(Copy, Clone)]
pub struct RawMutableSpace(pub clean::Mutability);
/// 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.
pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
/// Wrapper struct for emitting a comma-separated list of items
Expand Down Expand Up @@ -157,7 +157,7 @@ impl fmt::Display for clean::Generics {

impl<'a> fmt::Display for WhereClause<'a> {
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() {
return Ok(());
}
Expand Down Expand Up @@ -207,14 +207,14 @@ impl<'a> fmt::Display for WhereClause<'a> {
if !f.alternate() {
clause.push_str("</span>");
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
//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'");
repeat("&nbsp;").take(8).collect::<String>()
} else {
repeat("&nbsp;").take(pad.len() + 6).collect::<String>()
repeat("&nbsp;").take(pad + 6).collect::<String>()
};
clause = clause.replace("<br>", &format!("<br>{}", padding));
} else {
Expand Down Expand Up @@ -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)?;
plain.push_str(&format!("{:#}", i.for_));

let pad = repeat(" ").take(plain.len() + 1).collect::<String>();
fmt::Display::fmt(&WhereClause(&i.generics, pad), f)?;
fmt::Display::fmt(&WhereClause(&i.generics, plain.len() + 1), f)?;
Ok(())
}

Expand Down Expand Up @@ -903,19 +902,21 @@ impl<'a> fmt::Display for Method<'a> {

let mut output: String;
let plain: String;
let pad = repeat(" ").take(indent).collect::<String>();
if arrow.is_empty() {
output = format!("({})", args);
plain = format!("{}({})", indent.replace("&nbsp;", " "), args_plain);
plain = format!("{}({})", pad, args_plain);
} else {
output = format!("({args})<br>{arrow}", args = args, arrow = arrow);
plain = format!("{indent}({args}){arrow}",
indent = indent.replace("&nbsp;", " "),
plain = format!("{pad}({args}){arrow}",
pad = pad,
args = args_plain,
arrow = arrow_plain);
}

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);
} else {
output = output.replace("<br>", "");
Expand Down
47 changes: 19 additions & 28 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1967,14 +1967,13 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
UnstableFeatures::Allow => f.constness,
_ => hir::Constness::NotConst
};
let prefix = format!("{}{}{}{:#}fn {}{:#}",
let indent = format!("{}{}{}{:#}fn {}{:#}",
VisSpace(&it.visibility),
ConstnessSpace(vis_constness),
UnsafetySpace(f.unsafety),
AbiSpace(f.abi),
it.name.as_ref().unwrap(),
f.generics);
let indent = repeat("&nbsp;").take(prefix.len()).collect::<String>();
f.generics).len();
write!(w, "<pre class='rust fn'>{vis}{constness}{unsafety}{abi}fn \
{name}{generics}{decl}{where_clause}</pre>",
vis = VisSpace(&it.visibility),
Expand All @@ -1983,8 +1982,8 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
abi = AbiSpace(f.abi),
name = it.name.as_ref().unwrap(),
generics = f.generics,
where_clause = WhereClause(&f.generics, " ".to_string()),
decl = Method(&f.decl, &indent))?;
where_clause = WhereClause(&f.generics, 2),
decl = Method(&f.decl, indent))?;
document(w, cx, it)
}

Expand All @@ -2009,17 +2008,15 @@ 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
write!(w, "<pre class='rust trait'>{}{}trait {}{}{}{} ",
VisSpace(&it.visibility),
UnsafetySpace(t.unsafety),
it.name.as_ref().unwrap(),
t.generics,
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 consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
Expand Down Expand Up @@ -2272,16 +2269,15 @@ fn render_assoc_item(w: &mut fmt::Formatter,
AbiSpace(abi),
name,
*g);
let mut indent = repeat("&nbsp;").take(prefix.len()).collect::<String>();
let mut indent = prefix.len();
let where_indent = if parent == ItemType::Trait {
indent += "&nbsp;&nbsp;&nbsp;&nbsp;";
" ".to_string()
indent += 4;
8
} else if parent == ItemType::Impl {
" ".to_string()
2
} else {
let prefix = prefix + &format!("{:#}", Method(d, &indent));
let prefix = prefix.lines().last().unwrap();
repeat(" ").take(prefix.len() + 1).collect::<String>()
let prefix = prefix + &format!("{:#}", Method(d, indent));
prefix.lines().last().unwrap().len() + 1
};
write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}",
Expand All @@ -2291,7 +2287,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
href = href,
name = name,
generics = *g,
decl = Method(d, &indent),
decl = Method(d, indent),
where_clause = WhereClause(g, where_indent))
}
match item.inner {
Expand Down Expand Up @@ -2402,7 +2398,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
let padding = format!("{}enum {}{:#} ",
VisSpace(&it.visibility),
it.name.as_ref().unwrap(),
e.generics);
e.generics).len();
write!(w, "{}enum {}{}{}",
VisSpace(&it.visibility),
it.name.as_ref().unwrap(),
Expand Down Expand Up @@ -2558,8 +2554,7 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
match ty {
doctree::Plain => {
if let Some(g) = g {
let pad = repeat(" ").take(plain.len() + 1).collect::<String>();
write!(w, "{}", WhereClause(g, pad))?
write!(w, "{}", WhereClause(g, plain.len() + 1))?
}
let mut has_visible_fields = false;
write!(w, " {{")?;
Expand Down Expand Up @@ -2609,16 +2604,14 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
write!(w, ")")?;
plain.push_str(")");
if let Some(g) = g {
let pad = repeat(" ").take(plain.len() + 1).collect::<String>();
write!(w, "{}", WhereClause(g, pad))?
write!(w, "{}", WhereClause(g, plain.len() + 1))?
}
write!(w, ";")?;
}
doctree::Unit => {
// Needed for PhantomData.
if let Some(g) = g {
let pad = repeat(" ").take(plain.len() + 1).collect::<String>();
write!(w, "{}", WhereClause(g, pad))?
write!(w, "{}", WhereClause(g, plain.len() + 1))?
}
write!(w, ";")?;
}
Expand All @@ -2643,8 +2636,7 @@ fn render_union(w: &mut fmt::Formatter, it: &clean::Item,
if let Some(g) = g {
write!(w, "{}", g)?;
plain.push_str(&format!("{:#}", g));
let pad = repeat(" ").take(plain.len() + 1).collect::<String>();
write!(w, "{}", WhereClause(g, pad))?;
write!(w, "{}", WhereClause(g, plain.len() + 1))?;
}

write!(w, " {{\n{}", tab)?;
Expand Down Expand Up @@ -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,
t: &clean::Typedef) -> fmt::Result {
let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics);
let indent = repeat(" ").take(indent.len()).collect::<String>();
let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics).len();
write!(w, "<pre class='rust typedef'>type {}{}{where_clause} = {type_};</pre>",
it.name.as_ref().unwrap(),
t.generics,
Expand Down

0 comments on commit 61cc870

Please sign in to comment.