diff options
| author | Mikko Rantanen <jubjub@jubjubnest.net> | 2016-10-13 04:34:08 +0300 |
|---|---|---|
| committer | Nick Cameron <nrc@ncameron.org> | 2016-10-13 14:34:08 +1300 |
| commit | 7be202fa3c53ab1d4e208245653fde033ec0131c (patch) | |
| tree | 00683c92e043cee446aab7b103ac42bde18ecf17 /src | |
| parent | 724f75eaa5d5eb72baabab7bc3ec3b8ed0d52815 (diff) | |
Add support for spaces_within_parens config (#1187)
* Add support for spaces_within_parens config * Changes based on review comments
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 1 | ||||
| -rw-r--r-- | src/expr.rs | 25 | ||||
| -rw-r--r-- | src/items.rs | 19 | ||||
| -rw-r--r-- | src/patterns.rs | 14 | ||||
| -rw-r--r-- | src/types.rs | 13 |
5 files changed, 64 insertions, 8 deletions
diff --git a/src/config.rs b/src/config.rs index f93a6e115a5..ad14f81a819 100644 --- a/src/config.rs +++ b/src/config.rs @@ -416,6 +416,7 @@ create_config! { space_after_bound_colon: bool, true, "Leave a space after the colon in a trait or lifetime bound"; spaces_around_ranges: bool, false, "Put spaces around the .. and ... range operators"; + spaces_within_parens: bool, false, "Put spaces within non-empty parentheses"; use_try_shorthand: bool, false, "Replace uses of the try! macro by the ? shorthand"; write_mode: WriteMode, WriteMode::Replace, "What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage"; diff --git a/src/expr.rs b/src/expr.rs index ce1eb5dc053..e1d74a3cc79 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1536,7 +1536,11 @@ fn rewrite_call_inner<R>(context: &RewriteContext, None => return Err(Ordering::Less), }; - Ok(format!("{}({})", callee_str, list_str)) + Ok(if context.config.spaces_within_parens && list_str.len() > 0 { + format!("{}( {} )", callee_str, list_str) + } else { + format!("{}({})", callee_str, list_str) + }) } fn rewrite_paren(context: &RewriteContext, @@ -1549,7 +1553,12 @@ fn rewrite_paren(context: &RewriteContext, // paren on the same line as the subexpr. let subexpr_str = subexpr.rewrite(context, try_opt!(width.checked_sub(2)), offset + 1); debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str); - subexpr_str.map(|s| format!("({})", s)) + + subexpr_str.map(|s| if context.config.spaces_within_parens && s.len() > 0 { + format!("( {} )", s) + } else { + format!("({})", s) + }) } fn rewrite_struct_lit<'a>(context: &RewriteContext, @@ -1749,7 +1758,11 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext, return items.next() .unwrap() .rewrite(&aligned, budget, indent) - .map(|s| format!("({},)", s)); + .map(|s| if context.config.spaces_within_parens { + format!("( {}, )", s) + } else { + format!("({},)", s) + }); } let list_lo = context.codemap.span_after(span, "("); @@ -1764,7 +1777,11 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext, span.hi - BytePos(1)); let list_str = try_opt!(format_item_list(items, budget, indent, context.config)); - Some(format!("({})", list_str)) + if context.config.spaces_within_parens && list_str.len() > 0 { + Some(format!("( {} )", list_str)) + } else { + Some(format!("({})", list_str)) + } } fn rewrite_binary_op(context: &RewriteContext, diff --git a/src/items.rs b/src/items.rs index 7e18f0a2f4d..c3e58cba835 100644 --- a/src/items.rs +++ b/src/items.rs @@ -961,7 +961,17 @@ fn format_tuple_struct(context: &RewriteContext, context.codemap.span_after(span, "("), span.hi); let body = try_opt!(format_item_list(items, item_budget, item_indent, context.config)); + + if context.config.spaces_within_parens && body.len() > 0 { + result.push(' '); + } + result.push_str(&body); + + if context.config.spaces_within_parens && body.len() > 0 { + result.push(' '); + } + result.push(')'); if !where_clause_str.is_empty() && !where_clause_str.contains('\n') && @@ -1386,12 +1396,18 @@ fn rewrite_fn_base(context: &RewriteContext, result.push_str(&arg_indent.to_string(context.config)); arg_indent = arg_indent + 1; // extra space for `(` result.push('('); + if context.config.spaces_within_parens && fd.inputs.len() > 0 { + result.push(' ') + } } else { result.push_str("(\n"); result.push_str(&arg_indent.to_string(context.config)); } } else { result.push('('); + if context.config.spaces_within_parens && fd.inputs.len() > 0 { + result.push(' ') + } } if multi_line_ret_str { @@ -1432,6 +1448,9 @@ fn rewrite_fn_base(context: &RewriteContext, result.push(')'); } else { result.push_str(&arg_str); + if context.config.spaces_within_parens && fd.inputs.len() > 0 { + result.push(' ') + } result.push(')'); } diff --git a/src/patterns.rs b/src/patterns.rs index 1aaa28b853a..149844cd196 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -234,10 +234,20 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>], let list = try_opt!(format_item_list(items, width, offset, context.config)); match path_str { - Some(path_str) => Some(format!("{}({})", path_str, list)), + Some(path_str) => { + Some(if context.config.spaces_within_parens { + format!("{}( {} )", path_str, list) + } else { + format!("{}({})", path_str, list) + }) + } None => { let comma = if add_comma { "," } else { "" }; - Some(format!("({}{})", list, comma)) + Some(if context.config.spaces_within_parens { + format!("( {}{} )", list, comma) + } else { + format!("({}{})", list, comma) + }) } } } diff --git a/src/types.rs b/src/types.rs index a718750db95..624f17f6be8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -311,7 +311,11 @@ fn format_function_type<'a, I>(inputs: I, String::new() }; - Some(format!("({}){}{}", list_str, infix, output)) + Some(if context.config.spaces_within_parens { + format!("( {} ){}{}", list_str, infix, output) + } else { + format!("({}){}{}", list_str, infix, output) + }) } impl Rewrite for ast::WherePredicate { @@ -575,7 +579,12 @@ impl Rewrite for ast::Ty { // comments. ast::TyKind::Paren(ref ty) => { let budget = try_opt!(width.checked_sub(2)); - ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("({})", ty_str)) + ty.rewrite(context, budget, offset + 1) + .map(|ty_str| if context.config.spaces_within_parens { + format!("( {} )", ty_str) + } else { + format!("({})", ty_str) + }) } ast::TyKind::Vec(ref ty) => { let budget = try_opt!(width.checked_sub(2)); |
