From b048fe6b356934e7e3990037738b3dfe322cce0d Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 16 Jun 2017 18:56:32 +0900 Subject: [PATCH] Format source codes --- src/chains.rs | 11 ++++---- src/comment.rs | 7 ++--- src/expr.rs | 70 +++++++++++++++++++++----------------------------- src/items.rs | 52 ++++++++++++++++++------------------- src/lists.rs | 21 ++++++++------- src/types.rs | 10 ++------ src/visitor.rs | 6 ++--- 7 files changed, 78 insertions(+), 99 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 25c1c353915..5a4a404bfdb 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -165,9 +165,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - ); let child_shape_iter = Some(first_child_shape).into_iter().chain( - ::std::iter::repeat( - other_child_shape, - ).take(subexpr_list.len() - 1), + ::std::iter::repeat(other_child_shape).take(subexpr_list.len() - 1), ); let iter = subexpr_list.iter().rev().zip(child_shape_iter); let mut rewrites = try_opt!( @@ -178,9 +176,10 @@ 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(); diff --git a/src/comment.rs b/src/comment.rs index d8189df363c..ce4b45009a2 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -45,9 +45,10 @@ 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], + ) }) } diff --git a/src/expr.rs b/src/expr.rs index 092f64fbb3e..6b4669a70e7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -298,9 +298,7 @@ fn format_expr( Some(format!( "{}{}", "do catch ", - try_opt!( - block.rewrite(&context, Shape::legacy(budget, shape.indent)) - ) + try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent))) )) } }; @@ -941,13 +939,9 @@ fn to_control_flow<'a>(expr: &'a ast::Expr, expr_type: ExprType) -> Option Some( ControlFlow::new_loop(block, label, expr.span), ), - ast::ExprKind::While(ref cond, ref block, label) => Some(ControlFlow::new_while( - None, - cond, - block, - label, - expr.span, - )), + ast::ExprKind::While(ref cond, ref block, label) => Some( + ControlFlow::new_while(None, cond, block, label, expr.span), + ), ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => { Some(ControlFlow::new_while( Some(pat), @@ -1302,14 +1296,13 @@ impl<'a> Rewrite for ControlFlow<'a> { } }; - 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", - ), - ); + 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", + ), + ); let between_kwd_else_block_comment = extract_comment(between_kwd_else_block, context, shape); @@ -1434,10 +1427,9 @@ fn rewrite_match_arm_comment( result.push_str(&missed_str[..first_brk]); let missed_str = &missed_str[first_brk..]; // If missed_str had one newline, it starts with it - let first = missed_str.find(|c: char| !c.is_whitespace()).unwrap_or( - missed_str - .len(), - ); + let first = missed_str + .find(|c: char| !c.is_whitespace()) + .unwrap_or(missed_str.len()); if missed_str[..first].chars().filter(|c| c == &'\n').count() >= 2 { // Excessive vertical whitespace before comment should be preserved // FIXME handle vertical whitespace better @@ -2053,20 +2045,16 @@ where Ok(format!( "{}{}", callee_str, - wrap_args_with_parens( - context, - &list_str, - extendable, - args_shape, - nested_shape, - ) + wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape) )) } 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(), + ) }) } @@ -2408,10 +2396,12 @@ fn rewrite_index( let indent = indent.to_string(&context.config); // FIXME this is not right, since we don't take into account that shape.width // might be reduced from max_width by something on the right. - let budget = try_opt!(context.config.max_width().checked_sub( - indent.len() + lbr.len() + - rbr.len(), - )); + let budget = try_opt!( + context + .config + .max_width() + .checked_sub(indent.len() + lbr.len() + rbr.len()) + ); let index_str = try_opt!(index.rewrite(context, Shape::legacy(budget, shape.indent))); Some(format!( "{}\n{}{}{}{}", @@ -2603,12 +2593,10 @@ where // 3 = "(" + ",)" let nested_shape = try_opt!(shape.sub_width(3)).visual_indent(1); return items.next().unwrap().rewrite(context, nested_shape).map( - |s| { - if context.config.spaces_within_parens() { - format!("( {}, )", s) - } else { - format!("({},)", s) - } + |s| if context.config.spaces_within_parens() { + format!("( {}, )", s) + } else { + format!("({},)", s) }, ); } diff --git a/src/items.rs b/src/items.rs index 1eb85771fd6..02a35286117 100644 --- a/src/items.rs +++ b/src/items.rs @@ -803,10 +803,9 @@ fn format_impl_ref_and_type( Style::Legacy => new_line_offset + trait_ref_overhead, Style::Rfc => new_line_offset, }; - result.push_str(&*try_opt!(self_ty.rewrite( - context, - Shape::legacy(budget, type_offset), - ))); + result.push_str(&*try_opt!( + self_ty.rewrite(context, Shape::legacy(budget, type_offset)) + )); Some(result) } else { unreachable!(); @@ -967,8 +966,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) where_density, "{", !has_body, - trait_bound_str.is_empty() && - last_line_width(&generics_str) == 1, + trait_bound_str.is_empty() && last_line_width(&generics_str) == 1, None, )); // If the where clause cannot fit on the same line, @@ -1260,10 +1258,12 @@ fn format_tuple_struct( 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 @@ -1548,8 +1548,7 @@ pub fn rewrite_static( let ty_str = try_opt!(ty.rewrite( context, Shape::legacy( - context.config.max_width() - offset.block_indent - - prefix.len() - 2, + context.config.max_width() - offset.block_indent - prefix.len() - 2, offset.block_only(), ), )); @@ -1613,8 +1612,7 @@ pub fn rewrite_associated_type( let ty_str = try_opt!(ty.rewrite( context, Shape::legacy( - context.config.max_width() - indent.block_indent - - prefix.len() - 2, + context.config.max_width() - indent.block_indent - prefix.len() - 2, indent.block_only(), ), )); @@ -1857,8 +1855,7 @@ fn rewrite_fn_base( 2 }; let shape = try_opt!( - Shape::indented(indent + last_line_width(&result), context.config) - .sub_width(overhead) + Shape::indented(indent + last_line_width(&result), context.config).sub_width(overhead) ); let g_span = mk_sp(span.lo, span_for_return(&fd.output).lo); let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span)); @@ -1979,10 +1976,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'); @@ -2055,13 +2052,14 @@ 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 { diff --git a/src/lists.rs b/src/lists.rs index c770d1bf953..da412211233 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -132,9 +132,10 @@ impl ListItem { } 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: S) -> ListItem { @@ -419,10 +420,9 @@ where } } None => { - post_snippet.find_uncommented(self.terminator).unwrap_or( - post_snippet - .len(), - ) + post_snippet + .find_uncommented(self.terminator) + .unwrap_or(post_snippet.len()) } }; @@ -435,10 +435,9 @@ where let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len()); // From the end of the first line of comments. let test_snippet = &test_snippet[first_newline..]; - let first = test_snippet.find(|c: char| !c.is_whitespace()).unwrap_or( - test_snippet - .len(), - ); + let first = test_snippet + .find(|c: char| !c.is_whitespace()) + .unwrap_or(test_snippet.len()); // From the end of the first line of comments to the next non-whitespace char. let test_snippet = &test_snippet[..first]; diff --git a/src/types.rs b/src/types.rs index 3cdf9096094..723317a1040 100644 --- a/src/types.rs +++ b/src/types.rs @@ -590,10 +590,7 @@ impl Rewrite for ast::PolyTraitRef { let max_path_width = try_opt!(shape.width.checked_sub(extra_offset)); let path_str = try_opt!(self.trait_ref.rewrite( context, - Shape::legacy( - max_path_width, - shape.indent + extra_offset, - ), + Shape::legacy(max_path_width, shape.indent + extra_offset), )); Some(if context.config.spaces_within_angle_brackets() && @@ -645,10 +642,7 @@ impl Rewrite for ast::Ty { mut_str, try_opt!(mt.ty.rewrite( context, - Shape::legacy( - budget, - shape.indent + 2 + mut_len + lt_len, - ), + Shape::legacy(budget, shape.indent + 2 + mut_len + lt_len), )) ) } diff --git a/src/visitor.rs b/src/visitor.rs index 17f48933499..a303e099f7a 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -782,8 +782,8 @@ impl Rewrite for ast::MetaItem { impl Rewrite for ast::Attribute { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - try_opt!(self.meta()).rewrite(context, shape).map(|rw| { - if rw.starts_with("///") { + try_opt!(self.meta()).rewrite(context, shape).map( + |rw| if rw.starts_with("///") { rw } else { let original = context.snippet(self.span); @@ -793,7 +793,7 @@ impl Rewrite for ast::Attribute { format!("#[{}]", rw) } } - }) + ) } }