]> git.lizzy.rs Git - rust.git/commitdiff
Format source codes
authortopecongiro <seuchida@gmail.com>
Tue, 23 May 2017 13:13:29 +0000 (22:13 +0900)
committertopecongiro <seuchida@gmail.com>
Tue, 23 May 2017 13:13:29 +0000 (22:13 +0900)
src/comment.rs
src/expr.rs
src/items.rs
src/types.rs
tests/target/chains.rs
tests/target/configs-fn_call_style-block.rs
tests/target/file-lines-1.rs
tests/target/file-lines-3.rs

index 22fa241677f2b92096871b56efb8f8f733e2d6cf..9591ffc4427f8010c0153816e4990fac9fd35024 100644 (file)
@@ -158,15 +158,14 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option<
             // `*` in `/*`.
             let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
             if let Some(fnw) = first_non_whitespace {
-                    if l.as_bytes()[fnw] == '*' as u8 && fnw > 0 {
-                        &l[fnw - 1..]
-                    } else {
-                        &l[fnw..]
-                    }
+                if l.as_bytes()[fnw] == '*' as u8 && fnw > 0 {
+                    &l[fnw - 1..]
                 } else {
-                    ""
+                    &l[fnw..]
                 }
-                .trim_right()
+            } else {
+                ""
+            }.trim_right()
         })
         .collect();
     Some(lines.join(&format!("\n{}", offset.to_string(config))))
index ad0d654175de754dfe63e31398fac441ed214962..885577b304ef30a4e97da34fd935c46a781556ef 100644 (file)
@@ -316,8 +316,7 @@ pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
     let lhs_budget = try_opt!(context
                                   .config
                                   .max_width()
-                                  .checked_sub(shape.used_width() + prefix.len() +
-                                               infix.len()));
+                                  .checked_sub(shape.used_width() + prefix.len() + infix.len()));
     let rhs_shape = match context.config.control_style() {
         Style::Default => {
             try_opt!(shape.sub_width(suffix.len() + prefix.len())).visual_indent(prefix.len())
@@ -853,8 +852,8 @@ fn rewrite_single_line(&self,
 
             let new_width = try_opt!(new_width.checked_sub(if_str.len()));
             let else_expr = &else_node.stmts[0];
-            let else_str = try_opt!(else_expr.rewrite(context,
-                                                      Shape::legacy(new_width, Indent::empty())));
+            let else_str =
+                try_opt!(else_expr.rewrite(context, Shape::legacy(new_width, Indent::empty())));
 
             if if_str.contains('\n') || else_str.contains('\n') {
                 return None;
@@ -953,14 +952,13 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         };
 
         // for event in event
-        let between_kwd_cond =
-            mk_sp(context.codemap.span_after(self.span, self.keyword.trim()),
-                  self.pat
-                      .map_or(cond_span.lo, |p| if self.matcher.is_empty() {
-                p.span.lo
-            } else {
-                context.codemap.span_before(self.span, self.matcher.trim())
-            }));
+        let between_kwd_cond = mk_sp(context.codemap.span_after(self.span, self.keyword.trim()),
+                                     self.pat
+                                         .map_or(cond_span.lo, |p| if self.matcher.is_empty() {
+            p.span.lo
+        } else {
+            context.codemap.span_before(self.span, self.matcher.trim())
+        }));
 
         let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
 
@@ -1042,9 +1040,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             let between_kwd_else_block_comment =
                 extract_comment(between_kwd_else_block, context, shape);
 
-            let after_else = mk_sp(context.codemap.span_after(mk_sp(self.block.span.hi,
-                                                                    else_block.span.lo),
-                                                              "else"),
+            let after_else = mk_sp(context
+                                       .codemap
+                                       .span_after(mk_sp(self.block.span.hi, else_block.span.lo),
+                                                   "else"),
                                    else_block.span.lo);
             let after_else_comment = extract_comment(after_else, context, shape);
 
index f98cf9507e1c72151e16b03dc81a93c2d480f9fa..bddb07263b874acdf92d73a59689b794636d82ae 100644 (file)
@@ -1062,11 +1062,11 @@ fn format_tuple_struct(context: &RewriteContext,
                          |field| field.rewrite(context, Shape::legacy(item_budget, item_indent)),
                          context.codemap.span_after(span, "("),
                          span.hi);
-        let body_budget = try_opt!(context
-                                       .config
-                                       .max_width()
-                                       .checked_sub(offset.block_only().width() + result.len() +
-                                                    3));
+        let body_budget =
+            try_opt!(context
+                         .config
+                         .max_width()
+                         .checked_sub(offset.block_only().width() + result.len() + 3));
         let body = try_opt!(list_helper(items,
                                         // TODO budget is wrong in block case
                                         Shape::legacy(body_budget, item_indent),
@@ -1126,8 +1126,7 @@ pub fn rewrite_type_alias(context: &RewriteContext,
 
     let generics_indent = indent + result.len();
     let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
-    let shape = try_opt!(Shape::indented(generics_indent, context.config)
-                             .sub_width(" =".len()));
+    let shape = try_opt!(Shape::indented(generics_indent, context.config).sub_width(" =".len()));
     let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
 
     result.push_str(&generics_str);
@@ -1310,28 +1309,30 @@ pub fn rewrite_associated_type(ident: ast::Ident,
                                -> Option<String> {
     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(joiner.to_string()))
-                                         .collect::<Option<String>>());
-            if bounds.len() > 0 {
-                format!(": {}", bound_str)
-            } else {
-                String::new()
-            }
+    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(joiner.to_string()))
+                         .collect::<Option<String>>());
+        if bounds.len() > 0 {
+            format!(": {}", bound_str)
         } else {
             String::new()
-        };
+        }
+    } else {
+        String::new()
+    };
 
     if let Some(ty) = ty_opt {
         let ty_str = try_opt!(ty.rewrite(context,
@@ -1931,8 +1932,7 @@ fn compute_budgets_for_args(context: &RewriteContext,
             let multi_line_budget = try_opt!(context
                                                  .config
                                                  .max_width()
-                                                 .checked_sub(indent.width() + result.len() +
-                                                              4));
+                                                 .checked_sub(indent.width() + result.len() + 4));
 
             return Some((one_line_budget, multi_line_budget, indent + result.len() + 1));
         }
index 8f99be11d1a8b0608fdd99902293fe56120b95ab..688d6719441610cce02b2fe22d3078a28b92c3be 100644 (file)
@@ -364,12 +364,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let colon = type_bound_colon(context);
 
                 if !bound_lifetimes.is_empty() {
-                    let lifetime_str: String =
-                        try_opt!(bound_lifetimes
-                                     .iter()
-                                     .map(|lt| lt.rewrite(context, shape))
-                                     .intersperse(Some(", ".to_string()))
-                                     .collect());
+                    let lifetime_str: String = try_opt!(bound_lifetimes
+                                                            .iter()
+                                                            .map(|lt| lt.rewrite(context, shape))
+                                                            .intersperse(Some(", ".to_string()))
+                                                            .collect());
 
                     let joiner = match context.config.type_punctuation_density() {
                         TypeDensity::Compressed => "+",
@@ -378,17 +377,13 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     // 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));
-                    let bounds_str: String =
-                        try_opt!(bounds
-                                     .iter()
-                                     .map(|ty_bound| {
-                                              ty_bound.rewrite(context,
-                                                               Shape::legacy(budget,
-                                                                             shape.indent +
-                                                                             used_width))
-                                          })
-                                     .intersperse(Some(joiner.to_string()))
-                                     .collect());
+                    let bounds_str: String = try_opt!(bounds
+                                                          .iter()
+                                                          .map(|ty_bound| {
+                        ty_bound.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
+                    })
+                                                          .intersperse(Some(joiner.to_string()))
+                                                          .collect());
 
                     if context.config.spaces_within_angle_brackets() && lifetime_str.len() > 0 {
                         format!("for< {} > {}{}{}",
@@ -406,17 +401,13 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     };
                     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()
-                                     .map(|ty_bound| {
-                                              ty_bound.rewrite(context,
-                                                               Shape::legacy(budget,
-                                                                             shape.indent +
-                                                                             used_width))
-                                          })
-                                     .intersperse(Some(joiner.to_string()))
-                                     .collect());
+                    let bounds_str: String = try_opt!(bounds
+                                                          .iter()
+                                                          .map(|ty_bound| {
+                        ty_bound.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
+                    })
+                                                          .intersperse(Some(joiner.to_string()))
+                                                          .collect());
 
                     format!("{}{}{}", type_str, colon, bounds_str)
                 }
@@ -532,12 +523,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 TypeDensity::Compressed => "+",
                 TypeDensity::Wide => " + ",
             };
-            let bounds: String =
-                try_opt!(self.bounds
-                             .iter()
-                             .map(|ty_bound| ty_bound.rewrite(context, shape))
-                             .intersperse(Some(joiner.to_string()))
-                             .collect());
+            let bounds: String = try_opt!(self.bounds
+                                              .iter()
+                                              .map(|ty_bound| ty_bound.rewrite(context, shape))
+                                              .intersperse(Some(joiner.to_string()))
+                                              .collect());
 
             result.push_str(&bounds);
         }
@@ -612,8 +602,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                              let lt_budget = try_opt!(shape.width.checked_sub(2 + mut_len));
                              let lt_str = try_opt!(lifetime.rewrite(context,
                                                                     Shape::legacy(lt_budget,
-                                                                                  shape.indent +
-                                                                                  2 +
+                                                                                  shape.indent + 2 +
                                                                                   mut_len)));
                              let lt_len = lt_str.len();
                              let budget = try_opt!(shape.width.checked_sub(2 + mut_len + lt_len));
index b8e47607b8329f3a4e07a43ff3a2e8dddceceb1f..80b024829912fd786336d20f11b69d5881f5e47c 100644 (file)
@@ -66,38 +66,34 @@ fn floaters() {
     };
 
     let x = Foo {
-            field1: val1,
-            field2: val2,
-        }
-        .method_call()
+        field1: val1,
+        field2: val2,
+    }.method_call()
         .method_call();
 
     let y = if cond {
-            val1
-        } else {
-            val2
-        }
-        .method_call();
+        val1
+    } else {
+        val2
+    }.method_call();
 
     {
         match x {
             PushParam => {
                 // params are 1-indexed
                 stack.push(mparams[match cur.to_digit(10) {
-                                   Some(d) => d as usize - 1,
-                                   None => return Err("bad param number".to_owned()),
-                               }]
-                               .clone());
+                    Some(d) => d as usize - 1,
+                    None => return Err("bad param number".to_owned()),
+                }].clone());
             }
         }
     }
 
     if cond {
-            some();
-        } else {
-            none();
-        }
-        .bar()
+        some();
+    } else {
+        none();
+    }.bar()
         .baz();
 
     Foo { x: val }
@@ -108,21 +104,19 @@ fn floaters() {
         .quux();
 
     Foo {
-            y: i_am_multi_line,
-            z: ok,
-        }
-        .baz(|| {
-                 force();
-                 multiline();
-             })
+        y: i_am_multi_line,
+        z: ok,
+    }.baz(|| {
+              force();
+              multiline();
+          })
         .quux();
 
     a +
     match x {
-            true => "yay!",
-            false => "boo!",
-        }
-        .bar()
+        true => "yay!",
+        false => "boo!",
+    }.bar()
 }
 
 fn is_replaced_content() -> bool {
index cd19d8bb838c3d4988f9b32a1b1398a9be94000a..8149269158c2fa89110952654d6466c9a1e8dbfb 100644 (file)
@@ -37,8 +37,7 @@ fn issue1420() {
         # Getting started
         ...
     "#,
-    )
-        .running(waltz)
+    ).running(waltz)
 }
 
 // #1563
index 250b25e6ea3a7182ec45860da0af7b028b28f0db..a43f314a2420c554b419dbef365c0f81f41966a6 100644 (file)
@@ -2,10 +2,9 @@
 
 fn floaters() {
     let x = Foo {
-            field1: val1,
-            field2: val2,
-        }
-        .method_call()
+        field1: val1,
+        field2: val2,
+    }.method_call()
         .method_call();
 
     let y = if cond {
index 6652d9c92cd21568908499a0cc66b04e5713eb61..754c7c879b014e6e510c46f5e58b78e967243eb1 100644 (file)
@@ -3,10 +3,9 @@
 
 fn floaters() {
     let x = Foo {
-            field1: val1,
-            field2: val2,
-        }
-        .method_call()
+        field1: val1,
+        field2: val2,
+    }.method_call()
         .method_call();
 
     let y = if cond { val1 } else { val2 }.method_call();