]> git.lizzy.rs Git - rust.git/commitdiff
Format source codes
authortopecongiro <seuchida@gmail.com>
Sat, 17 Jun 2017 07:56:54 +0000 (16:56 +0900)
committertopecongiro <seuchida@gmail.com>
Sat, 17 Jun 2017 07:56:54 +0000 (16:56 +0900)
src/chains.rs
src/comment.rs
src/expr.rs
src/items.rs
src/lists.rs
src/macros.rs
src/missed_spans.rs
src/types.rs
src/visitor.rs

index ed5278e81fc3f1bedf7af245f570c3f81c08e63c..9b27c92c270a6306d7e0bca1117ca9b7324bf32d 100644 (file)
@@ -183,10 +183,9 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
 
     // Total of all items excluding the last.
     let last_non_try_index = rewrites.len() - (1 + trailing_try_num);
-    let almost_total = rewrites[..last_non_try_index].iter().fold(
-        0,
-        |a, b| a + first_line_width(b),
-    ) + parent_rewrite.len();
+    let almost_total = rewrites[..last_non_try_index]
+        .iter()
+        .fold(0, |a, b| a + first_line_width(b)) + parent_rewrite.len();
     let one_line_len = rewrites.iter().fold(0, |a, r| a + first_line_width(r)) +
         parent_rewrite.len();
 
index ce4b45009a2a5805e50520ade4009617cbf4058f..d8189df363cbd0597f040f52a0ce1c192bf5cf63 100644 (file)
@@ -45,10 +45,9 @@ pub enum CommentStyle<'a> {
 
 fn custom_opener(s: &str) -> &str {
     s.lines().next().map_or("", |first_line| {
-        first_line.find(' ').map_or(
-            first_line,
-            |space_index| &first_line[0..space_index + 1],
-        )
+        first_line
+            .find(' ')
+            .map_or(first_line, |space_index| &first_line[0..space_index + 1])
     })
 }
 
index d22c3f21a779270406b3f0f15c1ef45c77e9b0ed..b69e8bbd42eaf4b6b5472b7322c494098429139f 100644 (file)
@@ -1077,10 +1077,8 @@ fn rewrite_single_line(
 
             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;
@@ -1298,19 +1296,17 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
             let between_kwd_else_block = mk_sp(
                 self.block.span.hi,
-                context.codemap.span_before(
-                    mk_sp(self.block.span.hi, else_block.span.lo),
-                    "else",
-                ),
+                context
+                    .codemap
+                    .span_before(mk_sp(self.block.span.hi, else_block.span.lo), "else"),
             );
             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",
-                ),
+                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);
@@ -1328,10 +1324,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 write!(
                     &mut result,
                     "{}else{}",
-                    between_kwd_else_block_comment.as_ref().map_or(
-                        between_sep,
-                        |s| &**s,
-                    ),
+                    between_kwd_else_block_comment
+                        .as_ref()
+                        .map_or(between_sep, |s| &**s),
                     after_else_comment.as_ref().map_or(after_sep, |s| &**s)
                 ).ok()
             );
@@ -1478,10 +1473,9 @@ fn rewrite_match(
 
     let arm_indent_str = arm_shape.indent.to_string(context.config);
 
-    let open_brace_pos = context.codemap.span_after(
-        mk_sp(cond.span.hi, arm_start_pos(&arms[0])),
-        "{",
-    );
+    let open_brace_pos = context
+        .codemap
+        .span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), "{");
 
     for (i, arm) in arms.iter().enumerate() {
         // Make sure we get the stuff between arms.
@@ -2051,10 +2045,8 @@ pub fn rewrite_call_inner<'a, T>(
 
 fn need_block_indent(s: &str, shape: Shape) -> bool {
     s.lines().skip(1).any(|s| {
-        s.find(|c| !char::is_whitespace(c)).map_or(
-            false,
-            |w| w + 1 < shape.indent.width(),
-        )
+        s.find(|c| !char::is_whitespace(c))
+            .map_or(false, |w| w + 1 < shape.indent.width())
     })
 }
 
@@ -2271,10 +2263,8 @@ fn can_be_overflowed<'a, T>(context: &RewriteContext, args: &[&T]) -> bool
 where
     T: Rewrite + Spanned + ToExpr + 'a,
 {
-    args.last().map_or(
-        false,
-        |x| x.can_be_overflowed(context, args.len()),
-    )
+    args.last()
+        .map_or(false, |x| x.can_be_overflowed(context, args.len()))
 }
 
 pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len: usize) -> bool {
@@ -2541,10 +2531,9 @@ fn rewrite_field(context: &RewriteContext, field: &ast::Field, shape: Shape) ->
             Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),
             None => {
                 let expr_offset = shape.indent.block_indent(context.config);
-                let expr = field.expr.rewrite(
-                    context,
-                    Shape::indented(expr_offset, context.config),
-                );
+                let expr = field
+                    .expr
+                    .rewrite(context, Shape::indented(expr_offset, context.config));
                 expr.map(|s| {
                     format!(
                         "{}{}:\n{}{}",
@@ -2729,7 +2718,7 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
             0
         };
     // 1 = space between operator and rhs.
-    let orig_shape = try_opt!(shape.block_indent(0).offset_left(last_line_width + 1));
+    let orig_shape = try_opt!(shape.offset_left(last_line_width + 1));
     let rhs = match ex.node {
         ast::ExprKind::Mac(ref mac) => {
             match rewrite_macro(mac, None, context, orig_shape, MacroPosition::Expression) {
index 9a6bb08d3b6d1cad4d0999c7099c0da7225cb7f1..40d57ac45d7a9e8c42efc71dfcb0ec7c53ba70d5 100644 (file)
@@ -488,10 +488,12 @@ fn format_variant(&self, field: &ast::Variant) -> Option<String> {
 
         let context = self.get_context();
         let indent = self.block_indent;
-        let mut result = try_opt!(field.node.attrs.rewrite(
-            &context,
-            Shape::indented(indent, self.config),
-        ));
+        let mut result = try_opt!(
+            field
+                .node
+                .attrs
+                .rewrite(&context, Shape::indented(indent, self.config))
+        );
         if !result.is_empty() {
             let shape = Shape {
                 width: context.config.max_width(),
@@ -1436,10 +1438,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
         let name = self.ident;
         let vis = format_visibility(&self.vis);
-        let mut attr_str = try_opt!(self.attrs.rewrite(
-            context,
-            Shape::indented(shape.indent, context.config),
-        ));
+        let mut attr_str = try_opt!(
+            self.attrs
+                .rewrite(context, Shape::indented(shape.indent, context.config))
+        );
         // Try format missing comments after attributes
         let missing_comment = if !self.attrs.is_empty() {
             rewrite_missing_comment_on_field(
@@ -1470,10 +1472,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
         let type_offset = shape.indent.block_indent(context.config);
         let rewrite_type_in_next_line = || {
-            self.ty.rewrite(
-                context,
-                Shape::indented(type_offset, context.config),
-            )
+            self.ty
+                .rewrite(context, Shape::indented(type_offset, context.config))
         };
 
         let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();
@@ -1670,10 +1670,10 @@ fn is_empty_infer(context: &RewriteContext, ty: &ast::Ty) -> bool {
 impl Rewrite for ast::Arg {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         if is_named_arg(self) {
-            let mut result = try_opt!(self.pat.rewrite(
-                context,
-                Shape::legacy(shape.width, shape.indent),
-            ));
+            let mut result = try_opt!(
+                self.pat
+                    .rewrite(context, Shape::legacy(shape.width, shape.indent))
+            );
 
             if !is_empty_infer(context, &*self.ty) {
                 if context.config.space_before_type_annotation() {
@@ -1872,17 +1872,17 @@ fn rewrite_fn_base(
     let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
     result.push_str(&generics_str);
 
-    let snuggle_angle_bracket = generics_str.lines().last().map_or(
-        false,
-        |l| l.trim_left().len() == 1,
-    );
+    let snuggle_angle_bracket = generics_str
+        .lines()
+        .last()
+        .map_or(false, |l| l.trim_left().len() == 1);
 
     // Note that the width and indent don't really matter, we'll re-layout the
     // return type later anyway.
-    let ret_str = try_opt!(fd.output.rewrite(
-        &context,
-        Shape::indented(indent, context.config),
-    ));
+    let ret_str = try_opt!(
+        fd.output
+            .rewrite(&context, Shape::indented(indent, context.config))
+    );
 
     let multi_line_ret_str = ret_str.contains('\n');
     let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
@@ -1942,10 +1942,10 @@ fn rewrite_fn_base(
     }
 
     // A conservative estimation, to goal is to be over all parens in generics
-    let args_start = generics.ty_params.last().map_or(
-        span.lo,
-        |tp| end_typaram(tp),
-    );
+    let args_start = generics
+        .ty_params
+        .last()
+        .map_or(span.lo, |tp| end_typaram(tp));
     let args_span = mk_sp(
         context.codemap.span_after(mk_sp(args_start, span.hi), "("),
         span_for_return(&fd.output).lo,
@@ -1987,10 +1987,10 @@ fn rewrite_fn_base(
         }
         // If the last line of args contains comment, we cannot put the closing paren
         // on the same line.
-        if arg_str.lines().last().map_or(
-            false,
-            |last_line| last_line.contains("//"),
-        )
+        if arg_str
+            .lines()
+            .last()
+            .map_or(false, |last_line| last_line.contains("//"))
         {
             args_last_line_contains_comment = true;
             result.push('\n');
@@ -2048,10 +2048,10 @@ fn rewrite_fn_base(
         if multi_line_ret_str || ret_should_indent {
             // Now that we know the proper indent and width, we need to
             // re-layout the return type.
-            let ret_str = try_opt!(fd.output.rewrite(
-                context,
-                Shape::indented(ret_indent, context.config),
-            ));
+            let ret_str = try_opt!(
+                fd.output
+                    .rewrite(context, Shape::indented(ret_indent, context.config))
+            );
             result.push_str(&ret_str);
         } else {
             result.push_str(&ret_str);
@@ -2063,14 +2063,12 @@ fn rewrite_fn_base(
             let snippet_hi = span.hi;
             let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
             // Try to preserve the layout of the original snippet.
-            let original_starts_with_newline = snippet.find(|c| c != ' ').map_or(
-                false,
-                |i| snippet[i..].starts_with('\n'),
-            );
-            let original_ends_with_newline = snippet.rfind(|c| c != ' ').map_or(
-                false,
-                |i| snippet[i..].ends_with('\n'),
-            );
+            let original_starts_with_newline = snippet
+                .find(|c| c != ' ')
+                .map_or(false, |i| snippet[i..].starts_with('\n'));
+            let original_ends_with_newline = snippet
+                .rfind(|c| c != ' ')
+                .map_or(false, |i| snippet[i..].ends_with('\n'));
             let snippet = snippet.trim();
             if !snippet.is_empty() {
                 result.push(if original_starts_with_newline {
index da412211233772a91589be54ea85075a38336331..d176ef01f9b57a033dbed7f348a85343a186c40e 100644 (file)
@@ -125,17 +125,15 @@ pub struct ListItem {
 impl ListItem {
     pub fn is_multiline(&self) -> bool {
         self.item.as_ref().map_or(false, |s| s.contains('\n')) || self.pre_comment.is_some() ||
-            self.post_comment.as_ref().map_or(
-                false,
-                |s| s.contains('\n'),
-            )
+            self.post_comment
+                .as_ref()
+                .map_or(false, |s| s.contains('\n'))
     }
 
     pub fn has_line_pre_comment(&self) -> bool {
-        self.pre_comment.as_ref().map_or(
-            false,
-            |comment| comment.starts_with("//"),
-        )
+        self.pre_comment
+            .as_ref()
+            .map_or(false, |comment| comment.starts_with("//"))
     }
 
     pub fn from_str<S: Into<String>>(s: S) -> ListItem {
index 88bfb96c5d6a806d9b816bcf5ac72e15e689774b..419dea87a330ea33bc8a568b33a5b52cfd55022b 100644 (file)
@@ -219,10 +219,9 @@ pub fn rewrite_macro(
                 let rewrite = try_opt!(rewrite_array(
                     expr_vec.iter().map(|x| &**x),
                     mk_sp(
-                        context.codemap.span_after(
-                            mac.span,
-                            original_style.opener(),
-                        ),
+                        context
+                            .codemap
+                            .span_after(mac.span, original_style.opener()),
                         mac.span.hi - BytePos(1),
                     ),
                     context,
index 45e4dc19daef3bbfd7df3b6c969080e461db74c5..fb0e298bbbf42a60c0963f0e963a9b4e9f9a6f4c 100644 (file)
@@ -146,11 +146,9 @@ fn replace_chars(string: &str) -> String {
                 let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count();
 
                 if rewrite_next_comment &&
-                    !self.config.file_lines().intersects_range(
-                        file_name,
-                        cur_line,
-                        cur_line + subslice_num_lines,
-                    )
+                    !self.config
+                        .file_lines()
+                        .intersects_range(file_name, cur_line, cur_line + subslice_num_lines)
                 {
                     rewrite_next_comment = false;
                 }
index 723317a1040317e94270be838d0ecda2b5c6b462..39b35de20b2130905f1cd533b6bef0f651734e8a 100644 (file)
@@ -172,10 +172,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     TypeDensity::Compressed => format!("{}=", binding.ident),
                 };
                 let budget = try_opt!(shape.width.checked_sub(result.len()));
-                let rewrite = try_opt!(binding.ty.rewrite(
-                    context,
-                    Shape::legacy(budget, shape.indent + result.len()),
-                ));
+                let rewrite = try_opt!(
+                    binding
+                        .ty
+                        .rewrite(context, Shape::legacy(budget, shape.indent + result.len()))
+                );
                 result.push_str(&rewrite);
                 Some(result)
             }
@@ -448,10 +449,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 // 3 = " = ".len()
                 let used_width = 3 + lhs_ty_str.len();
                 let budget = try_opt!(shape.width.checked_sub(used_width));
-                let rhs_ty_str = try_opt!(rhs_ty.rewrite(
-                    context,
-                    Shape::legacy(budget, shape.indent + used_width),
-                ));
+                let rhs_ty_str = try_opt!(
+                    rhs_ty.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
+                );
                 format!("{} = {}", lhs_ty_str, rhs_ty_str)
             }
         };
@@ -651,10 +651,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                         format!(
                             "&{}{}",
                             mut_str,
-                            try_opt!(mt.ty.rewrite(
-                                context,
-                                Shape::legacy(budget, shape.indent + 1 + mut_len),
-                            ))
+                            try_opt!(
+                                mt.ty.rewrite(
+                                    context,
+                                    Shape::legacy(budget, shape.indent + 1 + mut_len),
+                                )
+                            )
                         )
                     }
                 })
index b8225aaed1c12407474c34f6de6dece4fe13e9e8..d20b4e971b54a5ab77b31109a37c8ada8989e13e 100644 (file)
@@ -290,10 +290,9 @@ pub fn visit_item(&mut self, item: &ast::Item) {
             ast::ItemKind::Impl(..) => {
                 self.format_missing_with_indent(source!(self, item.span).lo);
                 let snippet = self.get_context().snippet(item.span);
-                let where_span_end =
-                    snippet
-                        .find_uncommented("{")
-                        .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
+                let where_span_end = snippet
+                    .find_uncommented("{")
+                    .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
                 if let Some(impl_str) = format_impl(
                     &self.get_context(),
                     item,