From ec33121aafb081f33c78f062492cd61b77cef565 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 23 May 2017 22:13:29 +0900 Subject: [PATCH] Format source codes --- src/comment.rs | 13 ++--- src/expr.rs | 29 +++++----- src/items.rs | 58 ++++++++++---------- src/types.rs | 61 +++++++++------------ tests/target/chains.rs | 52 ++++++++---------- tests/target/configs-fn_call_style-block.rs | 3 +- tests/target/file-lines-1.rs | 7 +-- tests/target/file-lines-3.rs | 7 +-- 8 files changed, 104 insertions(+), 126 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index 22fa241677f..9591ffc4427 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -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)))) diff --git a/src/expr.rs b/src/expr.rs index ad0d654175d..885577b304e 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -316,8 +316,7 @@ pub fn rewrite_pair(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 { }; // 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 { 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); diff --git a/src/items.rs b/src/items.rs index f98cf9507e1..bddb07263b8 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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 { 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::>()); - 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::>()); + 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)); } diff --git a/src/types.rs b/src/types.rs index 8f99be11d1a..688d6719441 100644 --- a/src/types.rs +++ b/src/types.rs @@ -364,12 +364,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { 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 { // 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 { }; 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 { 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 { 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)); diff --git a/tests/target/chains.rs b/tests/target/chains.rs index b8e47607b83..80b02482991 100644 --- a/tests/target/chains.rs +++ b/tests/target/chains.rs @@ -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 { diff --git a/tests/target/configs-fn_call_style-block.rs b/tests/target/configs-fn_call_style-block.rs index cd19d8bb838..8149269158c 100644 --- a/tests/target/configs-fn_call_style-block.rs +++ b/tests/target/configs-fn_call_style-block.rs @@ -37,8 +37,7 @@ fn issue1420() { # Getting started ... "#, - ) - .running(waltz) + ).running(waltz) } // #1563 diff --git a/tests/target/file-lines-1.rs b/tests/target/file-lines-1.rs index 250b25e6ea3..a43f314a242 100644 --- a/tests/target/file-lines-1.rs +++ b/tests/target/file-lines-1.rs @@ -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 { diff --git a/tests/target/file-lines-3.rs b/tests/target/file-lines-3.rs index 6652d9c92cd..754c7c879b0 100644 --- a/tests/target/file-lines-3.rs +++ b/tests/target/file-lines-3.rs @@ -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(); -- 2.44.0