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

Commit 23f01ed

Browse files
authored
Merge pull request rust-lang#1188 from Rantanen/master
Add support for spaces_within_angle_brackets
2 parents 171d917 + d68d43c commit 23f01ed

File tree

6 files changed

+152
-5
lines changed

6 files changed

+152
-5
lines changed

src/chains.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,13 @@ fn rewrite_method_call(method_name: ast::Ident,
416416
.map(|ty| ty.rewrite(context, width, offset))
417417
.collect());
418418

419-
(types.last().unwrap().span.hi, format!("::<{}>", type_list.join(", ")))
419+
let type_str = if context.config.spaces_within_angle_brackets && type_list.len() > 0 {
420+
format!("::< {} >", type_list.join(", "))
421+
} else {
422+
format!("::<{}>", type_list.join(", "))
423+
};
424+
425+
(types.last().unwrap().span.hi, type_str)
420426
};
421427

422428
let callee_str = format!(".{}{}", method_name, type_str);

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ create_config! {
416416
space_after_bound_colon: bool, true,
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";
419+
spaces_within_angle_brackets: bool, false, "Put spaces within non-empty generic arguments";
419420
spaces_within_parens: bool, false, "Put spaces within non-empty parentheses";
420421
use_try_shorthand: bool, false, "Replace uses of the try! macro by the ? shorthand";
421422
write_mode: WriteMode, WriteMode::Replace,

src/items.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,11 @@ fn rewrite_generics(context: &RewriteContext,
18001800
span.hi);
18011801
let list_str = try_opt!(format_item_list(items, h_budget, offset, context.config));
18021802

1803-
Some(format!("<{}>", list_str))
1803+
Some(if context.config.spaces_within_angle_brackets {
1804+
format!("< {} >", list_str)
1805+
} else {
1806+
format!("<{}>", list_str)
1807+
})
18041808
}
18051809

18061810
fn rewrite_trait_bounds(context: &RewriteContext,

src/types.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub fn rewrite_path(context: &RewriteContext,
4545

4646
if let Some(qself) = qself {
4747
result.push('<');
48+
if context.config.spaces_within_angle_brackets {
49+
result.push_str(" ")
50+
}
51+
4852
let fmt_ty = try_opt!(qself.ty.rewrite(context, width, offset));
4953
result.push_str(&fmt_ty);
5054

@@ -68,6 +72,10 @@ pub fn rewrite_path(context: &RewriteContext,
6872
offset + extra_offset));
6973
}
7074

75+
if context.config.spaces_within_angle_brackets {
76+
result.push_str(" ")
77+
}
78+
7179
result.push_str(">::");
7280
span_lo = qself.ty.span.hi + BytePos(1);
7381
}
@@ -212,7 +220,11 @@ fn rewrite_segment(expr_context: bool,
212220
// Update position of last bracket.
213221
*span_lo = next_span_lo;
214222

215-
format!("{}<{}>", separator, list_str)
223+
if context.config.spaces_within_angle_brackets && list_str.len() > 0 {
224+
format!("{}< {} >", separator, list_str)
225+
} else {
226+
format!("{}<{}>", separator, list_str)
227+
}
216228
}
217229
ast::PathParameters::Parenthesized(ref data) => {
218230
let output = match data.output {
@@ -350,7 +362,11 @@ impl Rewrite for ast::WherePredicate {
350362
.intersperse(Some(" + ".to_string()))
351363
.collect());
352364

353-
format!("for<{}> {}: {}", lifetime_str, type_str, bounds_str)
365+
if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
366+
format!("for< {} > {}: {}", lifetime_str, type_str, bounds_str)
367+
} else {
368+
format!("for<{}> {}: {}", lifetime_str, type_str, bounds_str)
369+
}
354370
} else {
355371
// 2 = ": ".len()
356372
let used_width = type_str.len() + 2;
@@ -513,7 +529,11 @@ impl Rewrite for ast::PolyTraitRef {
513529
let path_str = try_opt!(self.trait_ref
514530
.rewrite(context, max_path_width, offset + extra_offset));
515531

516-
Some(format!("for<{}> {}", lifetime_str, path_str))
532+
Some(if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
533+
format!("for< {} > {}", lifetime_str, path_str)
534+
} else {
535+
format!("for<{}> {}", lifetime_str, path_str)
536+
})
517537
} else {
518538
self.trait_ref.rewrite(context, width, offset)
519539
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// rustfmt-spaces_within_angle_brackets: true
2+
3+
struct Foo<T> {
4+
i: T,
5+
}
6+
7+
struct Bar<T, E> {
8+
i: T,
9+
e: E,
10+
}
11+
12+
struct Foo<'a> {
13+
i: &'a str,
14+
}
15+
16+
enum E<T> {
17+
T(T),
18+
}
19+
20+
enum E<T, S> {
21+
T(T),
22+
S(S),
23+
}
24+
25+
fn foo<T>(a: T) {
26+
foo::<u32>(10);
27+
}
28+
29+
fn foo<T, E>(a: T, b: E) {
30+
foo::<u32, str>(10, "bar");
31+
}
32+
33+
fn foo<T: Send, E: Send>(a: T, b: E) {
34+
35+
foo::<u32, str>(10, "bar");
36+
37+
let opt: Option<u32>;
38+
let res: Result<u32, String>;
39+
}
40+
41+
fn foo<'a>(a: &'a str) {
42+
foo("foo");
43+
}
44+
45+
fn foo<'a, 'b>(a: &'a str, b: &'b str) {
46+
foo("foo", "bar");
47+
}
48+
49+
impl Foo {
50+
fn bar() {
51+
<Foo as Foo>::bar();
52+
}
53+
}
54+
55+
trait MyTrait<A, D> {}
56+
impl<A: Send, D: Send> MyTrait<A, D> for Foo {}
57+
58+
fn foo() where for<'a> u32: 'a {}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// rustfmt-spaces_within_angle_brackets: true
2+
3+
struct Foo< T > {
4+
i: T,
5+
}
6+
7+
struct Bar< T, E > {
8+
i: T,
9+
e: E,
10+
}
11+
12+
struct Foo< 'a > {
13+
i: &'a str,
14+
}
15+
16+
enum E< T > {
17+
T(T),
18+
}
19+
20+
enum E< T, S > {
21+
T(T),
22+
S(S),
23+
}
24+
25+
fn foo< T >(a: T) {
26+
foo::< u32 >(10);
27+
}
28+
29+
fn foo< T, E >(a: T, b: E) {
30+
foo::< u32, str >(10, "bar");
31+
}
32+
33+
fn foo< T: Send, E: Send >(a: T, b: E) {
34+
35+
foo::< u32, str >(10, "bar");
36+
37+
let opt: Option< u32 >;
38+
let res: Result< u32, String >;
39+
}
40+
41+
fn foo< 'a >(a: &'a str) {
42+
foo("foo");
43+
}
44+
45+
fn foo< 'a, 'b >(a: &'a str, b: &'b str) {
46+
foo("foo", "bar");
47+
}
48+
49+
impl Foo {
50+
fn bar() {
51+
< Foo as Foo >::bar();
52+
}
53+
}
54+
55+
trait MyTrait< A, D > {}
56+
impl< A: Send, D: Send > MyTrait< A, D > for Foo {}
57+
58+
fn foo() where for< 'a > u32: 'a {}

0 commit comments

Comments
 (0)