Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4b1c669

Browse files
Rantanennrc
authored andcommitted
Add spaces_within_square_brackets config option. (rust-lang#1191)
* Add spaces_within_square_brackets config option. Enabling the config enforces spaces within various array/slice brackets. * Fixed budget-calculations for [] spacing
1 parent 23f01ed commit 4b1c669

File tree

6 files changed

+105
-11
lines changed

6 files changed

+105
-11
lines changed

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ create_config! {
417417
"Leave a space after the colon in a trait or lifetime bound";
418418
spaces_around_ranges: bool, false, "Put spaces around the .. and ... range operators";
419419
spaces_within_angle_brackets: bool, false, "Put spaces within non-empty generic arguments";
420+
spaces_within_square_brackets: bool, false, "Put spaces within non-empty square brackets";
420421
spaces_within_parens: bool, false, "Put spaces within non-empty parentheses";
421422
use_try_shorthand: bool, false, "Replace uses of the try! macro by the ? shorthand";
422423
write_mode: WriteMode, WriteMode::Replace,

src/expr.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,16 @@ fn format_expr(expr: &ast::Expr,
206206
rewrite_pair(&**expr, &**ty, "", ": ", "", context, width, offset)
207207
}
208208
ast::ExprKind::Index(ref expr, ref index) => {
209-
rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset)
209+
let use_spaces = context.config.spaces_within_square_brackets;
210+
let lbr = if use_spaces { "[ " } else { "[" };
211+
let rbr = if use_spaces { " ]" } else { "]" };
212+
rewrite_pair(&**expr, &**index, "", lbr, rbr, context, width, offset)
210213
}
211214
ast::ExprKind::Repeat(ref expr, ref repeats) => {
212-
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
215+
let use_spaces = context.config.spaces_within_square_brackets;
216+
let lbr = if use_spaces { "[ " } else { "[" };
217+
let rbr = if use_spaces { " ]" } else { "]" };
218+
rewrite_pair(&**expr, &**repeats, lbr, "; ", rbr, context, width, offset)
213219
}
214220
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
215221
let delim = match limits {
@@ -303,11 +309,14 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
303309
-> Option<String>
304310
where I: Iterator<Item = &'a ast::Expr>
305311
{
306-
// 1 = [
307-
let offset = offset + 1;
312+
let bracket_size = if context.config.spaces_within_square_brackets {
313+
2 // "[ "
314+
} else {
315+
1 // "["
316+
};
317+
let offset = offset + bracket_size;
308318
let inner_context = &RewriteContext { block_indent: offset, ..*context };
309-
// 2 for brackets
310-
let max_item_width = try_opt!(width.checked_sub(2));
319+
let max_item_width = try_opt!(width.checked_sub(bracket_size * 2));
311320
let items = itemize_list(context.codemap,
312321
expr_iter,
313322
"]",
@@ -339,7 +348,11 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
339348
};
340349
let list_str = try_opt!(write_list(&items, &fmt));
341350

342-
Some(format!("[{}]", list_str))
351+
Some(if context.config.spaces_within_square_brackets && list_str.len() > 0 {
352+
format!("[ {} ]", list_str)
353+
} else {
354+
format!("[{}]", list_str)
355+
})
343356
}
344357

345358
// This functions is pretty messy because of the rules around closures and blocks:

src/patterns.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ impl Rewrite for Pat {
9494
let pats = try_opt!(pats);
9595

9696
// Unwrap all the sub-strings and join them with commas.
97-
let result = format!("[{}]", pats.join(", "));
97+
let result = if context.config.spaces_within_square_brackets {
98+
format!("[ {} ]", pats.join(", "))
99+
} else {
100+
format!("[{}]", pats.join(", "))
101+
};
98102
wrap_str(result, context.config.max_width, width, offset)
99103
}
100104
PatKind::Struct(ref path, ref fields, elipses) => {

src/types.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,17 @@ impl Rewrite for ast::Ty {
607607
})
608608
}
609609
ast::TyKind::Vec(ref ty) => {
610-
let budget = try_opt!(width.checked_sub(2));
611-
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str))
610+
let budget = if context.config.spaces_within_square_brackets {
611+
try_opt!(width.checked_sub(4))
612+
} else {
613+
try_opt!(width.checked_sub(2))
614+
};
615+
ty.rewrite(context, budget, offset + 1)
616+
.map(|ty_str| if context.config.spaces_within_square_brackets {
617+
format!("[ {} ]", ty_str)
618+
} else {
619+
format!("[{}]", ty_str)
620+
})
612621
}
613622
ast::TyKind::Tup(ref items) => {
614623
rewrite_tuple(context,
@@ -622,7 +631,10 @@ impl Rewrite for ast::Ty {
622631
rewrite_path(context, false, q_self.as_ref(), path, width, offset)
623632
}
624633
ast::TyKind::FixedLengthVec(ref ty, ref repeats) => {
625-
rewrite_pair(&**ty, &**repeats, "[", "; ", "]", context, width, offset)
634+
let use_spaces = context.config.spaces_within_square_brackets;
635+
let lbr = if use_spaces { "[ " } else { "[" };
636+
let rbr = if use_spaces { " ]" } else { "]" };
637+
rewrite_pair(&**ty, &**repeats, lbr, "; ", rbr, context, width, offset)
626638
}
627639
ast::TyKind::Infer => {
628640
if width >= 1 {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// rustfmt-spaces_within_square_brackets: true
2+
3+
fn main() {
4+
5+
let arr: [i32; 5] = [1, 2, 3, 4, 5];
6+
let arr: [i32; 500] = [0; 500];
7+
8+
let v = vec![1, 2, 3];
9+
assert_eq!(arr, [1, 2, 3]);
10+
11+
let i = arr[0];
12+
13+
let slice = &arr[1..2];
14+
15+
let line100_________________________________________________________________________ = [1, 2];
16+
let line101__________________________________________________________________________ = [1, 2];
17+
let line102___________________________________________________________________________ = [1, 2];
18+
let line103____________________________________________________________________________ = [1, 2];
19+
let line104_____________________________________________________________________________ = [1, 2];
20+
21+
let line100_____________________________________________________________________ = vec![1, 2];
22+
let line101______________________________________________________________________ = vec![1, 2];
23+
let line102_______________________________________________________________________ = vec![1, 2];
24+
let line103________________________________________________________________________ = vec![1, 2];
25+
let line104_________________________________________________________________________ = vec![1, 2];
26+
}
27+
28+
fn f(slice: &[i32]) {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// rustfmt-spaces_within_square_brackets: true
2+
3+
fn main() {
4+
5+
let arr: [ i32; 5 ] = [ 1, 2, 3, 4, 5 ];
6+
let arr: [ i32; 500 ] = [ 0; 500 ];
7+
8+
let v = vec![ 1, 2, 3 ];
9+
assert_eq!(arr, [ 1, 2, 3 ]);
10+
11+
let i = arr[ 0 ];
12+
13+
let slice = &arr[ 1..2 ];
14+
15+
let line100_________________________________________________________________________ = [ 1, 2 ];
16+
let line101__________________________________________________________________________ = [ 1,
17+
2 ];
18+
let line102___________________________________________________________________________ = [ 1,
19+
2 ];
20+
let line103____________________________________________________________________________ = [ 1,
21+
2 ];
22+
let line104_____________________________________________________________________________ =
23+
[ 1, 2 ];
24+
25+
let line100_____________________________________________________________________ = vec![ 1, 2 ];
26+
let line101______________________________________________________________________ = vec![ 1,
27+
2 ];
28+
let line102_______________________________________________________________________ = vec![ 1,
29+
2 ];
30+
let line103________________________________________________________________________ = vec![ 1,
31+
2 ];
32+
let line104_________________________________________________________________________ =
33+
vec![ 1, 2 ];
34+
}
35+
36+
fn f(slice: &[ i32 ]) {}

0 commit comments

Comments
 (0)