Skip to content

Commit

Permalink
Add spaces_within_square_brackets config option. (#1191)
Browse files Browse the repository at this point in the history
* Add spaces_within_square_brackets config option.

Enabling the config enforces spaces within various array/slice brackets.

* Fixed budget-calculations for [] spacing
  • Loading branch information
Rantanen authored and nrc committed Oct 17, 2016
1 parent 23f01ed commit 4b1c669
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Expand Up @@ -417,6 +417,7 @@ create_config! {
"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_angle_brackets: bool, false, "Put spaces within non-empty generic arguments";
spaces_within_square_brackets: bool, false, "Put spaces within non-empty square brackets";
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,
Expand Down
27 changes: 20 additions & 7 deletions src/expr.rs
Expand Up @@ -206,10 +206,16 @@ fn format_expr(expr: &ast::Expr,
rewrite_pair(&**expr, &**ty, "", ": ", "", context, width, offset)
}
ast::ExprKind::Index(ref expr, ref index) => {
rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset)
let use_spaces = context.config.spaces_within_square_brackets;
let lbr = if use_spaces { "[ " } else { "[" };
let rbr = if use_spaces { " ]" } else { "]" };
rewrite_pair(&**expr, &**index, "", lbr, rbr, context, width, offset)
}
ast::ExprKind::Repeat(ref expr, ref repeats) => {
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
let use_spaces = context.config.spaces_within_square_brackets;
let lbr = if use_spaces { "[ " } else { "[" };
let rbr = if use_spaces { " ]" } else { "]" };
rewrite_pair(&**expr, &**repeats, lbr, "; ", rbr, context, width, offset)
}
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
let delim = match limits {
Expand Down Expand Up @@ -303,11 +309,14 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
-> Option<String>
where I: Iterator<Item = &'a ast::Expr>
{
// 1 = [
let offset = offset + 1;
let bracket_size = if context.config.spaces_within_square_brackets {
2 // "[ "
} else {
1 // "["
};
let offset = offset + bracket_size;
let inner_context = &RewriteContext { block_indent: offset, ..*context };
// 2 for brackets
let max_item_width = try_opt!(width.checked_sub(2));
let max_item_width = try_opt!(width.checked_sub(bracket_size * 2));
let items = itemize_list(context.codemap,
expr_iter,
"]",
Expand Down Expand Up @@ -339,7 +348,11 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
};
let list_str = try_opt!(write_list(&items, &fmt));

Some(format!("[{}]", list_str))
Some(if context.config.spaces_within_square_brackets && list_str.len() > 0 {
format!("[ {} ]", list_str)
} else {
format!("[{}]", list_str)
})
}

// This functions is pretty messy because of the rules around closures and blocks:
Expand Down
6 changes: 5 additions & 1 deletion src/patterns.rs
Expand Up @@ -94,7 +94,11 @@ impl Rewrite for Pat {
let pats = try_opt!(pats);

// Unwrap all the sub-strings and join them with commas.
let result = format!("[{}]", pats.join(", "));
let result = if context.config.spaces_within_square_brackets {
format!("[ {} ]", pats.join(", "))
} else {
format!("[{}]", pats.join(", "))
};
wrap_str(result, context.config.max_width, width, offset)
}
PatKind::Struct(ref path, ref fields, elipses) => {
Expand Down
18 changes: 15 additions & 3 deletions src/types.rs
Expand Up @@ -607,8 +607,17 @@ impl Rewrite for ast::Ty {
})
}
ast::TyKind::Vec(ref ty) => {
let budget = try_opt!(width.checked_sub(2));
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str))
let budget = if context.config.spaces_within_square_brackets {
try_opt!(width.checked_sub(4))
} else {
try_opt!(width.checked_sub(2))
};
ty.rewrite(context, budget, offset + 1)
.map(|ty_str| if context.config.spaces_within_square_brackets {
format!("[ {} ]", ty_str)
} else {
format!("[{}]", ty_str)
})
}
ast::TyKind::Tup(ref items) => {
rewrite_tuple(context,
Expand All @@ -622,7 +631,10 @@ impl Rewrite for ast::Ty {
rewrite_path(context, false, q_self.as_ref(), path, width, offset)
}
ast::TyKind::FixedLengthVec(ref ty, ref repeats) => {
rewrite_pair(&**ty, &**repeats, "[", "; ", "]", context, width, offset)
let use_spaces = context.config.spaces_within_square_brackets;
let lbr = if use_spaces { "[ " } else { "[" };
let rbr = if use_spaces { " ]" } else { "]" };
rewrite_pair(&**ty, &**repeats, lbr, "; ", rbr, context, width, offset)
}
ast::TyKind::Infer => {
if width >= 1 {
Expand Down
28 changes: 28 additions & 0 deletions tests/source/spaces-within-square-brackets.rs
@@ -0,0 +1,28 @@
// rustfmt-spaces_within_square_brackets: true

fn main() {

let arr: [i32; 5] = [1, 2, 3, 4, 5];
let arr: [i32; 500] = [0; 500];

let v = vec![1, 2, 3];
assert_eq!(arr, [1, 2, 3]);

let i = arr[0];

let slice = &arr[1..2];

let line100_________________________________________________________________________ = [1, 2];
let line101__________________________________________________________________________ = [1, 2];
let line102___________________________________________________________________________ = [1, 2];
let line103____________________________________________________________________________ = [1, 2];
let line104_____________________________________________________________________________ = [1, 2];

let line100_____________________________________________________________________ = vec![1, 2];
let line101______________________________________________________________________ = vec![1, 2];
let line102_______________________________________________________________________ = vec![1, 2];
let line103________________________________________________________________________ = vec![1, 2];
let line104_________________________________________________________________________ = vec![1, 2];
}

fn f(slice: &[i32]) {}
36 changes: 36 additions & 0 deletions tests/target/spaces-within-square-brackets.rs
@@ -0,0 +1,36 @@
// rustfmt-spaces_within_square_brackets: true

fn main() {

let arr: [ i32; 5 ] = [ 1, 2, 3, 4, 5 ];
let arr: [ i32; 500 ] = [ 0; 500 ];

let v = vec![ 1, 2, 3 ];
assert_eq!(arr, [ 1, 2, 3 ]);

let i = arr[ 0 ];

let slice = &arr[ 1..2 ];

let line100_________________________________________________________________________ = [ 1, 2 ];
let line101__________________________________________________________________________ = [ 1,
2 ];
let line102___________________________________________________________________________ = [ 1,
2 ];
let line103____________________________________________________________________________ = [ 1,
2 ];
let line104_____________________________________________________________________________ =
[ 1, 2 ];

let line100_____________________________________________________________________ = vec![ 1, 2 ];
let line101______________________________________________________________________ = vec![ 1,
2 ];
let line102_______________________________________________________________________ = vec![ 1,
2 ];
let line103________________________________________________________________________ = vec![ 1,
2 ];
let line104_________________________________________________________________________ =
vec![ 1, 2 ];
}

fn f(slice: &[ i32 ]) {}

0 comments on commit 4b1c669

Please sign in to comment.