diff options
| author | topecongiro <seuchida@gmail.com> | 2017-05-23 22:13:29 +0900 |
|---|---|---|
| committer | topecongiro <seuchida@gmail.com> | 2017-05-23 22:13:29 +0900 |
| commit | ec33121aafb081f33c78f062492cd61b77cef565 (patch) | |
| tree | fa9434080f8c98ff428ab4fc2276a68a34c23e25 /src | |
| parent | cb4a6a24191f1a5535a6694b2310be5ac9d60bfe (diff) | |
Format source codes
Diffstat (limited to 'src')
| -rw-r--r-- | src/comment.rs | 13 | ||||
| -rw-r--r-- | src/expr.rs | 29 | ||||
| -rw-r--r-- | src/items.rs | 58 | ||||
| -rw-r--r-- | src/types.rs | 61 |
4 files changed, 74 insertions, 87 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, 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 @@ impl<'a> ControlFlow<'a> { 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 @@ impl<'a> Rewrite for ControlFlow<'a> { }; // 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 @@ impl<'a> Rewrite for ControlFlow<'a> { 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<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)); } 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 @@ impl Rewrite for ast::WherePredicate { 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 @@ impl Rewrite for ast::WherePredicate { // 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 @@ impl Rewrite for ast::WherePredicate { }; 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 @@ impl Rewrite for ast::TyParam { 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 @@ impl Rewrite for ast::Ty { 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)); |
