diff --git a/src/items.rs b/src/items.rs index 66040362d52..7a4aa781305 100644 --- a/src/items.rs +++ b/src/items.rs @@ -21,7 +21,7 @@ use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, type_annota 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::>()); 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::>()); let mut result = String::new(); diff --git a/src/types.rs b/src/types.rs index c0a40720fc3..ca98401db14 100644 --- a/src/types.rs +++ b/src/types.rs @@ -370,6 +370,10 @@ impl Rewrite for ast::WherePredicate { .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 @@ impl Rewrite for ast::WherePredicate { 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 @@ impl Rewrite for ast::WherePredicate { 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 @@ impl Rewrite for ast::WherePredicate { 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 @@ impl Rewrite for ast::TyParam { 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); diff --git a/tests/source/type-punctuation.rs b/tests/source/type-punctuation.rs index 29c2f5a4e3b..0980e4a3732 100644 --- a/tests/source/type-punctuation.rs +++ b/tests/source/type-punctuation.rs @@ -1,5 +1,32 @@ // rustfmt-type_punctuation_density: Compressed +struct Foo + 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 Foo<'a> for Bar + where for<'a> T: 'a + Eq + Clone +{ + // body +} + +fn foo<'a, 'b, 'c>() + where 'a: 'b + 'c +{ + // body +} + fn Foo + Foo>() { let i = 6; } diff --git a/tests/target/type-punctuation.rs b/tests/target/type-punctuation.rs index 2e5725b3db9..e7021eca84f 100644 --- a/tests/target/type-punctuation.rs +++ b/tests/target/type-punctuation.rs @@ -1,5 +1,32 @@ // rustfmt-type_punctuation_density: Compressed +struct Foo + 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 Foo<'a> for Bar + where for<'a> T: 'a+Eq+Clone +{ + // body +} + +fn foo<'a, 'b, 'c>() + where 'a: 'b+'c +{ + // body +} + fn Foo+Foo>() { let i = 6; }