Combine spaces_within_parens and spaces_within_brackets

This commit is contained in:
topecongiro 2017-11-14 23:42:31 +09:00
parent 94e22bb334
commit bc543cce0b
8 changed files with 41 additions and 38 deletions

View file

@ -485,11 +485,12 @@ fn rewrite_method_call(
.map(|ty| ty.rewrite(context, shape)) .map(|ty| ty.rewrite(context, shape))
.collect::<Option<Vec<_>>>()?; .collect::<Option<Vec<_>>>()?;
let type_str = if context.config.spaces_within_angle_brackets() && !type_list.is_empty() { let type_str =
format!("::< {} >", type_list.join(", ")) if context.config.spaces_within_parens_and_brackets() && !type_list.is_empty() {
} else { format!("::< {} >", type_list.join(", "))
format!("::<{}>", type_list.join(", ")) } else {
}; format!("::<{}>", type_list.join(", "))
};
(types.last().unwrap().span.hi(), type_str) (types.last().unwrap().span.hi(), type_str)
}; };

View file

@ -616,11 +616,8 @@ create_config! {
space_before_colon: bool, false, false, "Leave a space before the colon"; space_before_colon: bool, false, false, "Leave a space before the colon";
space_after_colon: bool, true, false, "Leave a space after the colon"; space_after_colon: bool, true, false, "Leave a space after the colon";
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ... range operators"; spaces_around_ranges: bool, false, false, "Put spaces around the .. and ... range operators";
spaces_within_angle_brackets: bool, false, false, spaces_within_parens_and_brackets: bool, false, false,
"Put spaces within non-empty generic arguments"; "Put spaces within non-empty parentheses or brackets";
spaces_within_square_brackets: bool, false, false,
"Put spaces within non-empty square brackets";
spaces_within_parens: bool, false, false, "Put spaces within non-empty parentheses";
use_try_shorthand: bool, false, false, "Replace uses of the try! macro by the ? shorthand"; use_try_shorthand: bool, false, false, "Replace uses of the try! macro by the ? shorthand";
write_mode: WriteMode, WriteMode::Overwrite, false, write_mode: WriteMode, WriteMode::Overwrite, false,
"What Write Mode to use when none is supplied: \ "What Write Mode to use when none is supplied: \

View file

@ -202,7 +202,7 @@ pub fn format_expr(
rewrite_index(&**expr, &**index, context, shape) rewrite_index(&**expr, &**index, context, shape)
} }
ast::ExprKind::Repeat(ref expr, ref repeats) => { ast::ExprKind::Repeat(ref expr, ref repeats) => {
let (lbr, rbr) = if context.config.spaces_within_square_brackets() { let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
("[ ", " ]") ("[ ", " ]")
} else { } else {
("[", "]") ("[", "]")
@ -409,7 +409,7 @@ pub fn rewrite_array<'a, I>(
where where
I: Iterator<Item = &'a ast::Expr>, I: Iterator<Item = &'a ast::Expr>,
{ {
let bracket_size = if context.config.spaces_within_square_brackets() { let bracket_size = if context.config.spaces_within_parens_and_brackets() {
2 // "[ " 2 // "[ "
} else { } else {
1 // "[" 1 // "["
@ -439,7 +439,7 @@ where
).collect::<Vec<_>>(); ).collect::<Vec<_>>();
if items.is_empty() { if items.is_empty() {
if context.config.spaces_within_square_brackets() { if context.config.spaces_within_parens_and_brackets() {
return Some("[ ]".to_string()); return Some("[ ]".to_string());
} else { } else {
return Some("[]".to_string()); return Some("[]".to_string());
@ -501,7 +501,7 @@ where
let result = if context.config.indent_style() == IndentStyle::Visual let result = if context.config.indent_style() == IndentStyle::Visual
|| tactic == DefinitiveListTactic::Horizontal || tactic == DefinitiveListTactic::Horizontal
{ {
if context.config.spaces_within_square_brackets() && !list_str.is_empty() { if context.config.spaces_within_parens_and_brackets() && !list_str.is_empty() {
format!("[ {} ]", list_str) format!("[ {} ]", list_str)
} else { } else {
format!("[{}]", list_str) format!("[{}]", list_str)
@ -1801,7 +1801,7 @@ where
T: Rewrite + Spanned + ToExpr + 'a, T: Rewrite + Spanned + ToExpr + 'a,
{ {
// 2 = `( `, 1 = `(` // 2 = `( `, 1 = `(`
let paren_overhead = if context.config.spaces_within_parens() { let paren_overhead = if context.config.spaces_within_parens_and_brackets() {
2 2
} else { } else {
1 1
@ -2098,7 +2098,7 @@ pub fn wrap_args_with_parens(
|| (context.inside_macro && !args_str.contains('\n') || (context.inside_macro && !args_str.contains('\n')
&& args_str.len() + paren_overhead(context) <= shape.width) || is_extendable && args_str.len() + paren_overhead(context) <= shape.width) || is_extendable
{ {
if context.config.spaces_within_parens() && !args_str.is_empty() { if context.config.spaces_within_parens_and_brackets() && !args_str.is_empty() {
format!("( {} )", args_str) format!("( {} )", args_str)
} else { } else {
format!("({})", args_str) format!("({})", args_str)
@ -2141,11 +2141,12 @@ fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) ->
.offset_left(paren_overhead) .offset_left(paren_overhead)
.and_then(|s| s.sub_width(paren_overhead))?; .and_then(|s| s.sub_width(paren_overhead))?;
let paren_wrapper = |s: &str| if context.config.spaces_within_parens() && !s.is_empty() { let paren_wrapper =
format!("( {} )", s) |s: &str| if context.config.spaces_within_parens_and_brackets() && !s.is_empty() {
} else { format!("( {} )", s)
format!("({})", s) } else {
}; format!("({})", s)
};
let subexpr_str = subexpr.rewrite(context, sub_shape)?; let subexpr_str = subexpr.rewrite(context, sub_shape)?;
debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str); debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str);
@ -2167,7 +2168,7 @@ fn rewrite_index(
) -> Option<String> { ) -> Option<String> {
let expr_str = expr.rewrite(context, shape)?; let expr_str = expr.rewrite(context, shape)?;
let (lbr, rbr) = if context.config.spaces_within_square_brackets() { let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
("[ ", " ]") ("[ ", " ]")
} else { } else {
("[", "]") ("[", "]")
@ -2436,7 +2437,7 @@ where
.unwrap() .unwrap()
.rewrite(context, nested_shape) .rewrite(context, nested_shape)
.map(|s| { .map(|s| {
if context.config.spaces_within_parens() { if context.config.spaces_within_parens_and_brackets() {
format!("( {}, )", s) format!("( {}, )", s)
} else { } else {
format!("({},)", s) format!("({},)", s)
@ -2476,7 +2477,7 @@ where
}; };
let list_str = write_list(&item_vec, &fmt)?; let list_str = write_list(&item_vec, &fmt)?;
if context.config.spaces_within_parens() && !list_str.is_empty() { if context.config.spaces_within_parens_and_brackets() && !list_str.is_empty() {
Some(format!("( {} )", list_str)) Some(format!("( {} )", list_str))
} else { } else {
Some(format!("({})", list_str)) Some(format!("({})", list_str))

View file

@ -1882,7 +1882,9 @@ fn rewrite_fn_base(
} else { } else {
result.push('('); result.push('(');
} }
if context.config.spaces_within_parens() && !fd.inputs.is_empty() && result.ends_with('(') { if context.config.spaces_within_parens_and_brackets() && !fd.inputs.is_empty()
&& result.ends_with('(')
{
result.push(' ') result.push(' ')
} }
@ -1943,7 +1945,7 @@ fn rewrite_fn_base(
if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() { if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() {
result.push('\n'); result.push('\n');
} }
if context.config.spaces_within_parens() && !fd.inputs.is_empty() { if context.config.spaces_within_parens_and_brackets() && !fd.inputs.is_empty() {
result.push(' ') result.push(' ')
} }
// If the last line of args contains comment, we cannot put the closing paren // If the last line of args contains comment, we cannot put the closing paren
@ -2522,7 +2524,7 @@ pub fn wrap_generics_with_angle_brackets(
.block_unindent(context.config) .block_unindent(context.config)
.to_string(context.config) .to_string(context.config)
) )
} else if context.config.spaces_within_angle_brackets() { } else if context.config.spaces_within_parens_and_brackets() {
format!("< {} >", list_str) format!("< {} >", list_str)
} else { } else {
format!("<{}>", list_str) format!("<{}>", list_str)

View file

@ -219,7 +219,7 @@ pub fn rewrite_macro(
let mac_shape = shape.offset_left(macro_name.len())?; let mac_shape = shape.offset_left(macro_name.len())?;
// Handle special case: `vec![expr; expr]` // Handle special case: `vec![expr; expr]`
if vec_with_semi { if vec_with_semi {
let (lbr, rbr) = if context.config.spaces_within_square_brackets() { let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
("[ ", " ]") ("[ ", " ]")
} else { } else {
("[", "]") ("[", "]")

View file

@ -109,7 +109,7 @@ impl Rewrite for Pat {
let pats = pats?; let pats = pats?;
// Unwrap all the sub-strings and join them with commas. // Unwrap all the sub-strings and join them with commas.
let result = if context.config.spaces_within_square_brackets() { let result = if context.config.spaces_within_parens_and_brackets() {
format!("[ {} ]", pats.join(", ")) format!("[ {} ]", pats.join(", "))
} else { } else {
format!("[{}]", pats.join(", ")) format!("[{}]", pats.join(", "))

View file

@ -54,7 +54,7 @@ pub fn rewrite_path(
if let Some(qself) = qself { if let Some(qself) = qself {
result.push('<'); result.push('<');
if context.config.spaces_within_angle_brackets() { if context.config.spaces_within_parens_and_brackets() {
result.push_str(" ") result.push_str(" ")
} }
@ -81,7 +81,7 @@ pub fn rewrite_path(
)?; )?;
} }
if context.config.spaces_within_angle_brackets() { if context.config.spaces_within_parens_and_brackets() {
result.push_str(" ") result.push_str(" ")
} }
@ -434,7 +434,9 @@ impl Rewrite for ast::WherePredicate {
.collect::<Option<Vec<_>>>()?; .collect::<Option<Vec<_>>>()?;
let bounds_str = join_bounds(context, ty_shape, &bounds); let bounds_str = join_bounds(context, ty_shape, &bounds);
if context.config.spaces_within_angle_brackets() && !lifetime_str.is_empty() { if context.config.spaces_within_parens_and_brackets()
&& !lifetime_str.is_empty()
{
format!( format!(
"for< {} > {}{}{}", "for< {} > {}{}{}",
lifetime_str, lifetime_str,
@ -600,7 +602,7 @@ impl Rewrite for ast::PolyTraitRef {
.rewrite(context, shape.offset_left(extra_offset)?)?; .rewrite(context, shape.offset_left(extra_offset)?)?;
Some( Some(
if context.config.spaces_within_angle_brackets() && !lifetime_str.is_empty() { if context.config.spaces_within_parens_and_brackets() && !lifetime_str.is_empty() {
format!("for< {} > {}", lifetime_str, path_str) format!("for< {} > {}", lifetime_str, path_str)
} else { } else {
format!("for<{}> {}", lifetime_str, path_str) format!("for<{}> {}", lifetime_str, path_str)
@ -671,7 +673,7 @@ impl Rewrite for ast::Ty {
let budget = shape.width.checked_sub(2)?; let budget = shape.width.checked_sub(2)?;
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1)) ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
.map(|ty_str| { .map(|ty_str| {
if context.config.spaces_within_parens() { if context.config.spaces_within_parens_and_brackets() {
format!("( {} )", ty_str) format!("( {} )", ty_str)
} else { } else {
format!("({})", ty_str) format!("({})", ty_str)
@ -679,14 +681,14 @@ impl Rewrite for ast::Ty {
}) })
} }
ast::TyKind::Slice(ref ty) => { ast::TyKind::Slice(ref ty) => {
let budget = if context.config.spaces_within_square_brackets() { let budget = if context.config.spaces_within_parens_and_brackets() {
shape.width.checked_sub(4)? shape.width.checked_sub(4)?
} else { } else {
shape.width.checked_sub(2)? shape.width.checked_sub(2)?
}; };
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1)) ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
.map(|ty_str| { .map(|ty_str| {
if context.config.spaces_within_square_brackets() { if context.config.spaces_within_parens_and_brackets() {
format!("[ {} ]", ty_str) format!("[ {} ]", ty_str)
} else { } else {
format!("[{}]", ty_str) format!("[{}]", ty_str)
@ -703,7 +705,7 @@ impl Rewrite for ast::Ty {
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape) rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
} }
ast::TyKind::Array(ref ty, ref repeats) => { ast::TyKind::Array(ref ty, ref repeats) => {
let use_spaces = context.config.spaces_within_square_brackets(); let use_spaces = context.config.spaces_within_parens_and_brackets();
let lbr = if use_spaces { "[ " } else { "[" }; let lbr = if use_spaces { "[ " } else { "[" };
let rbr = if use_spaces { " ]" } else { "]" }; let rbr = if use_spaces { " ]" } else { "]" };
rewrite_pair( rewrite_pair(

View file

@ -441,7 +441,7 @@ pub fn colon_spaces(before: bool, after: bool) -> &'static str {
#[inline] #[inline]
pub fn paren_overhead(context: &RewriteContext) -> usize { pub fn paren_overhead(context: &RewriteContext) -> usize {
if context.config.spaces_within_parens() { if context.config.spaces_within_parens_and_brackets() {
4 4
} else { } else {
2 2