]> git.lizzy.rs Git - rust.git/commitdiff
Made `type_punctuation_density` apply too all `+` in types
authorVincent Esche <regexident@gmail.com>
Mon, 24 Apr 2017 17:59:21 +0000 (19:59 +0200)
committerVincent Esche <regexident@gmail.com>
Mon, 1 May 2017 23:03:46 +0000 (01:03 +0200)
src/items.rs
src/types.rs
tests/source/type-punctuation.rs
tests/target/type-punctuation.rs

index 66040362d52e2894995e67147684db8b7cfd0034..7a4aa7813052c40ab1fb37c4105017b89dd09d12 100644 (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();
index c0a40720fc3ced9a53742022ff4c5206a90481c7..ca98401db143186c881ed27261cc03ca4cfa21ef 100644 (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);
index 29c2f5a4e3bfe2939967b21a7750d9263a289ea4..0980e4a3732c363c4a5a6055076633c365cecd9a 100644 (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;
 }
index 2e5725b3db99c6b692158fe8dec8ff07dbeeaff7..e7021eca84fb8a9b5edd6233aef2c4f9ad77bea7 100644 (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;
 }