Merge pull request #1471 from regexident/type_punctuation_density

Made `type_punctuation_density` apply too all `+` in types
This commit is contained in:
Nick Cameron 2017-05-02 11:17:35 +12:00 committed by GitHub
commit 5071c41cf8
4 changed files with 85 additions and 9 deletions

View File

@ -21,7 +21,7 @@
use comment::{FindUncommented, contains_comment};
use visitor::FmtVisitor;
use rewrite::{Rewrite, RewriteContext};
use config::{Config, IndentStyle, Density, ReturnIndent, BraceStyle, Style};
use config::{Config, IndentStyle, Density, ReturnIndent, BraceStyle, Style, TypeDensity};
use itertools::Itertools;
use syntax::{ast, abi, codemap, ptr, symbol};
@ -1299,13 +1299,17 @@ pub fn rewrite_associated_type(ident: ast::Ident,
let prefix = format!("type {}", ident);
let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt {
let joiner = match context.config.type_punctuation_density {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
let bounds: &[_] = ty_param_bounds;
let bound_str = try_opt!(bounds
.iter()
.map(|ty_bound| {
ty_bound.rewrite(context, Shape::legacy(context.config.max_width, indent))
})
.intersperse(Some(" + ".to_string()))
.intersperse(Some(joiner.to_string()))
.collect::<Option<String>>());
if bounds.len() > 0 {
format!(": {}", bound_str)
@ -2015,11 +2019,14 @@ fn rewrite_trait_bounds(context: &RewriteContext,
if bounds.is_empty() {
return Some(String::new());
}
let joiner = match context.config.type_punctuation_density {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
let bound_str = try_opt!(bounds
.iter()
.map(|ty_bound| ty_bound.rewrite(&context, shape))
.intersperse(Some(" + ".to_string()))
.intersperse(Some(joiner.to_string()))
.collect::<Option<String>>());
let mut result = String::new();

View File

@ -370,6 +370,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
.intersperse(Some(", ".to_string()))
.collect());
let joiner = match context.config.type_punctuation_density {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
// 6 = "for<> ".len()
let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6;
let budget = try_opt!(shape.width.checked_sub(used_width));
@ -379,7 +383,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
Shape::legacy(budget,
shape.indent + used_width))
})
.intersperse(Some(" + ".to_string()))
.intersperse(Some(joiner.to_string()))
.collect());
if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
@ -392,6 +396,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
format!("for<{}> {}{}{}", lifetime_str, type_str, colon, bounds_str)
}
} else {
let joiner = match context.config.type_punctuation_density {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
let used_width = type_str.len() + colon.len();
let budget = try_opt!(shape.width.checked_sub(used_width));
let bounds_str: String = try_opt!(bounds.iter()
@ -400,7 +408,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
Shape::legacy(budget,
shape.indent + used_width))
})
.intersperse(Some(" + ".to_string()))
.intersperse(Some(joiner.to_string()))
.collect());
format!("{}{}{}", type_str, colon, bounds_str)
@ -456,7 +464,11 @@ fn rewrite_bounded_lifetime<'b, I>(lt: &ast::Lifetime,
.map(|b| b.rewrite(context, shape))
.collect());
let colon = type_bound_colon(context);
let result = format!("{}{}{}", result, colon, appendix.join(" + "));
let joiner = match context.config.type_punctuation_density {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
let result = format!("{}{}{}", result, colon, appendix.join(joiner));
wrap_str(result, context.config.max_width, shape)
}
}
@ -509,12 +521,15 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
if context.config.space_after_bound_colon {
result.push_str(" ");
}
let joiner = match context.config.type_punctuation_density {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
let bounds: String =
try_opt!(self.bounds
.iter()
.map(|ty_bound| ty_bound.rewrite(context, shape))
.intersperse(Some(" + ".to_string()))
.intersperse(Some(joiner.to_string()))
.collect());
result.push_str(&bounds);

View File

@ -1,5 +1,32 @@
// rustfmt-type_punctuation_density: Compressed
struct Foo<T: Eq + Clone, U>
where U: Eq + Clone {
// body
}
trait Foo<'a, T = usize>
where T: 'a + Eq + Clone
{
type Bar: Eq + Clone;
}
trait Foo: Eq + Clone {
// body
}
impl<T> Foo<'a> for Bar
where for<'a> T: 'a + Eq + Clone
{
// body
}
fn foo<'a, 'b, 'c>()
where 'a: 'b + 'c
{
// body
}
fn Foo<T = Foo, Output = Expr<'tcx> + Foo>() {
let i = 6;
}

View File

@ -1,5 +1,32 @@
// rustfmt-type_punctuation_density: Compressed
struct Foo<T: Eq+Clone, U>
where U: Eq+Clone {
// body
}
trait Foo<'a, T=usize>
where T: 'a+Eq+Clone
{
type Bar: Eq+Clone;
}
trait Foo: Eq+Clone {
// body
}
impl<T> Foo<'a> for Bar
where for<'a> T: 'a+Eq+Clone
{
// body
}
fn foo<'a, 'b, 'c>()
where 'a: 'b+'c
{
// body
}
fn Foo<T=Foo, Output=Expr<'tcx>+Foo>() {
let i = 6;
}