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))
.collect::<Option<Vec<_>>>()?;
let type_str = if context.config.spaces_within_angle_brackets() && !type_list.is_empty() {
format!("::< {} >", type_list.join(", "))
} else {
format!("::<{}>", type_list.join(", "))
};
let type_str =
if context.config.spaces_within_parens_and_brackets() && !type_list.is_empty() {
format!("::< {} >", type_list.join(", "))
} else {
format!("::<{}>", type_list.join(", "))
};
(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_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_within_angle_brackets: bool, false, false,
"Put spaces within non-empty generic arguments";
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";
spaces_within_parens_and_brackets: bool, false, false,
"Put spaces within non-empty parentheses or brackets";
use_try_shorthand: bool, false, false, "Replace uses of the try! macro by the ? shorthand";
write_mode: WriteMode, WriteMode::Overwrite, false,
"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)
}
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 {
("[", "]")
@ -409,7 +409,7 @@ pub fn rewrite_array<'a, I>(
where
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 // "[ "
} else {
1 // "["
@ -439,7 +439,7 @@ where
).collect::<Vec<_>>();
if items.is_empty() {
if context.config.spaces_within_square_brackets() {
if context.config.spaces_within_parens_and_brackets() {
return Some("[ ]".to_string());
} else {
return Some("[]".to_string());
@ -501,7 +501,7 @@ where
let result = if context.config.indent_style() == IndentStyle::Visual
|| 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)
} else {
format!("[{}]", list_str)
@ -1801,7 +1801,7 @@ where
T: Rewrite + Spanned + ToExpr + 'a,
{
// 2 = `( `, 1 = `(`
let paren_overhead = if context.config.spaces_within_parens() {
let paren_overhead = if context.config.spaces_within_parens_and_brackets() {
2
} else {
1
@ -2098,7 +2098,7 @@ pub fn wrap_args_with_parens(
|| (context.inside_macro && !args_str.contains('\n')
&& 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)
} else {
format!("({})", args_str)
@ -2141,11 +2141,12 @@ fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) ->
.offset_left(paren_overhead)
.and_then(|s| s.sub_width(paren_overhead))?;
let paren_wrapper = |s: &str| if context.config.spaces_within_parens() && !s.is_empty() {
format!("( {} )", s)
} else {
format!("({})", s)
};
let paren_wrapper =
|s: &str| if context.config.spaces_within_parens_and_brackets() && !s.is_empty() {
format!("( {} )", s)
} else {
format!("({})", s)
};
let subexpr_str = subexpr.rewrite(context, sub_shape)?;
debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str);
@ -2167,7 +2168,7 @@ fn rewrite_index(
) -> Option<String> {
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 {
("[", "]")
@ -2436,7 +2437,7 @@ where
.unwrap()
.rewrite(context, nested_shape)
.map(|s| {
if context.config.spaces_within_parens() {
if context.config.spaces_within_parens_and_brackets() {
format!("( {}, )", s)
} else {
format!("({},)", s)
@ -2476,7 +2477,7 @@ where
};
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))
} else {
Some(format!("({})", list_str))

View file

@ -1882,7 +1882,9 @@ fn rewrite_fn_base(
} else {
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(' ')
}
@ -1943,7 +1945,7 @@ fn rewrite_fn_base(
if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() {
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(' ')
}
// 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)
.to_string(context.config)
)
} else if context.config.spaces_within_angle_brackets() {
} else if context.config.spaces_within_parens_and_brackets() {
format!("< {} >", list_str)
} else {
format!("<{}>", list_str)

View file

@ -219,7 +219,7 @@ pub fn rewrite_macro(
let mac_shape = shape.offset_left(macro_name.len())?;
// Handle special case: `vec![expr; expr]`
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 {
("[", "]")

View file

@ -109,7 +109,7 @@ impl Rewrite for Pat {
let pats = pats?;
// 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(", "))
} else {
format!("[{}]", pats.join(", "))

View file

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

View file

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