From efd3e5c0914f0c8d401a504be4116bf947296218 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 17 Sep 2016 01:44:51 +0200 Subject: [PATCH] Add three new options for spaces --- src/config.rs | 5 ++++ src/expr.rs | 30 ++++++++++++++----- src/items.rs | 30 ++++++++++++++----- src/types.rs | 18 +++++++++-- .../space-not-after-type-annotation-colon.rs | 14 +++++++++ tests/source/space-not-before-bound-colon.rs | 5 ++++ tests/source/spaces-around-ranges.rs | 15 ++++++++++ .../space-not-after-type-annotation-colon.rs | 14 +++++++++ tests/target/space-not-before-bound-colon.rs | 5 ++++ tests/target/spaces-around-ranges.rs | 15 ++++++++++ 10 files changed, 134 insertions(+), 17 deletions(-) create mode 100644 tests/source/space-not-after-type-annotation-colon.rs create mode 100644 tests/source/space-not-before-bound-colon.rs create mode 100644 tests/source/spaces-around-ranges.rs create mode 100644 tests/target/space-not-after-type-annotation-colon.rs create mode 100644 tests/target/space-not-before-bound-colon.rs create mode 100644 tests/target/spaces-around-ranges.rs diff --git a/src/config.rs b/src/config.rs index 71adc24c09f..f93a6e115a5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -410,7 +410,12 @@ create_config! { block indented. -1 means never use block indent."; space_before_type_annotation: bool, false, "Leave a space before the colon in a type annotation"; + space_after_type_annotation_colon: bool, true, + "Leave a space after the colon in a type annotation"; space_before_bound: bool, false, "Leave a space before the colon in a trait or lifetime bound"; + space_after_bound_colon: bool, true, + "Leave a space after the colon in a trait or lifetime bound"; + spaces_around_ranges: bool, false, "Put spaces around the .. and ... range operators"; use_try_shorthand: bool, false, "Replace uses of the try! macro by the ? shorthand"; write_mode: WriteMode, WriteMode::Replace, "What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage"; diff --git a/src/expr.rs b/src/expr.rs index bece2ef91fe..11e2ca88145 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -219,13 +219,28 @@ fn format_expr(expr: &ast::Expr, match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) { (Some(ref lhs), Some(ref rhs)) => { - rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset) + let sp_delim = if context.config.spaces_around_ranges { + format!(" {} ", delim) + } else { + delim.into() + }; + rewrite_pair(&**lhs, &**rhs, "", &sp_delim, "", context, width, offset) } (None, Some(ref rhs)) => { - rewrite_unary_prefix(context, delim, &**rhs, width, offset) + let sp_delim = if context.config.spaces_around_ranges { + format!("{} ", delim) + } else { + delim.into() + }; + rewrite_unary_prefix(context, &sp_delim, &**rhs, width, offset) } (Some(ref lhs), None) => { - rewrite_unary_suffix(context, delim, &**lhs, width, offset) + let sp_delim = if context.config.spaces_around_ranges { + format!(" {}", delim) + } else { + delim.into() + }; + rewrite_unary_suffix(context, &sp_delim, &**lhs, width, offset) } (None, None) => wrap_str(delim.into(), context.config.max_width, width, offset), } @@ -1672,10 +1687,11 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, } pub fn type_annotation_separator(config: &Config) -> &str { - if config.space_before_type_annotation { - " : " - } else { - ": " + match (config.space_before_type_annotation, config.space_after_type_annotation_colon) { + (true, true) => " : ", + (true, false) => " :", + (false, true) => ": ", + (false, false) => ":", } } diff --git a/src/items.rs b/src/items.rs index 1071a4017d4..b381ba8b7b2 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1049,12 +1049,17 @@ pub fn rewrite_type_alias(context: &RewriteContext, Some(result) } -fn type_annotation_spacing(config: &Config) -> &str { - if config.space_before_type_annotation { +fn type_annotation_spacing(config: &Config) -> (&str, &str) { + (if config.space_before_type_annotation { " " } else { "" - } + }, + if config.space_after_type_annotation_colon { + " " + } else { + "" + }) } impl Rewrite for ast::StructField { @@ -1075,7 +1080,14 @@ impl Rewrite for ast::StructField { let type_annotation_spacing = type_annotation_spacing(context.config); let result = match name { - Some(name) => format!("{}{}{}{}: ", attr_str, vis, name, type_annotation_spacing), + Some(name) => { + format!("{}{}{}{}:{}", + attr_str, + vis, + name, + type_annotation_spacing.0, + type_annotation_spacing.1) + } None => format!("{}{}", attr_str, vis), }; @@ -1095,12 +1107,13 @@ pub fn rewrite_static(prefix: &str, context: &RewriteContext) -> Option { let type_annotation_spacing = type_annotation_spacing(context.config); - let prefix = format!("{}{} {}{}{}: ", + let prefix = format!("{}{} {}{}{}:{}", format_visibility(vis), prefix, format_mutability(mutability), ident, - type_annotation_spacing); + type_annotation_spacing.0, + type_annotation_spacing.1); // 2 = " =".len() let ty_str = try_opt!(ty.rewrite(context, context.config.max_width - context.block_indent.width() - @@ -1175,7 +1188,10 @@ impl Rewrite for ast::Arg { if context.config.space_before_type_annotation { result.push_str(" "); } - result.push_str(": "); + result.push_str(":"); + if context.config.space_after_type_annotation_colon { + result.push_str(" "); + } let max_width = try_opt!(width.checked_sub(result.len())); let ty_str = try_opt!(self.ty.rewrite(context, max_width, offset + result.len())); result.push_str(&ty_str); diff --git a/src/types.rs b/src/types.rs index cb310e13c33..a718750db95 100644 --- a/src/types.rs +++ b/src/types.rs @@ -405,12 +405,21 @@ fn rewrite_bounded_lifetime<'b, I>(lt: &ast::Lifetime, let appendix: Vec<_> = try_opt!(bounds.into_iter() .map(|b| b.rewrite(context, width, offset)) .collect()); - let bound_spacing = if context.config.space_before_bound { + let bound_spacing_before = if context.config.space_before_bound { " " } else { "" }; - let result = format!("{}{}: {}", result, bound_spacing, appendix.join(" + ")); + let bound_spacing_after = if context.config.space_after_bound_colon { + " " + } else { + "" + }; + let result = format!("{}{}:{}{}", + result, + bound_spacing_before, + bound_spacing_after, + appendix.join(" + ")); wrap_str(result, context.config.max_width, width, offset) } } @@ -456,7 +465,10 @@ impl Rewrite for ast::TyParam { if context.config.space_before_bound { result.push_str(" "); } - result.push_str(": "); + result.push_str(":"); + if context.config.space_after_bound_colon { + result.push_str(" "); + } let bounds: String = try_opt!(self.bounds .iter() diff --git a/tests/source/space-not-after-type-annotation-colon.rs b/tests/source/space-not-after-type-annotation-colon.rs new file mode 100644 index 00000000000..86f829c8353 --- /dev/null +++ b/tests/source/space-not-after-type-annotation-colon.rs @@ -0,0 +1,14 @@ +// rustfmt-space_before_type_annotation: true +// rustfmt-space_after_type_annotation_colon: false + +static staticVar: i32 = 42; +const constVar: i32 = 42; +fn foo(paramVar: i32) { + let localVar: i32 = 42; +} +struct S { + fieldVar: i32, +} +fn f() { + S { fieldVar: 42 } +} diff --git a/tests/source/space-not-before-bound-colon.rs b/tests/source/space-not-before-bound-colon.rs new file mode 100644 index 00000000000..4ec569f7902 --- /dev/null +++ b/tests/source/space-not-before-bound-colon.rs @@ -0,0 +1,5 @@ +// rustfmt-space_before_bound: true +// rustfmt-space_after_bound_colon: false + +trait Trait {} +fn f<'a, 'b: 'a, T: Trait>() {} diff --git a/tests/source/spaces-around-ranges.rs b/tests/source/spaces-around-ranges.rs new file mode 100644 index 00000000000..188f8f9074f --- /dev/null +++ b/tests/source/spaces-around-ranges.rs @@ -0,0 +1,15 @@ +// rustfmt-spaces_around_ranges: true + +fn bar(v: &[u8]) {} + +fn foo() { + let a = vec![0; 20]; + for j in 0...20 { + for i in 0..3 { + bar(a[i..j]); + bar(a[i..]); + bar(a[..j]); + bar(a[...(j + 1)]); + } + } +} diff --git a/tests/target/space-not-after-type-annotation-colon.rs b/tests/target/space-not-after-type-annotation-colon.rs new file mode 100644 index 00000000000..3bdfa57fb73 --- /dev/null +++ b/tests/target/space-not-after-type-annotation-colon.rs @@ -0,0 +1,14 @@ +// rustfmt-space_before_type_annotation: true +// rustfmt-space_after_type_annotation_colon: false + +static staticVar :i32 = 42; +const constVar :i32 = 42; +fn foo(paramVar :i32) { + let localVar :i32 = 42; +} +struct S { + fieldVar :i32, +} +fn f() { + S { fieldVar :42 } +} diff --git a/tests/target/space-not-before-bound-colon.rs b/tests/target/space-not-before-bound-colon.rs new file mode 100644 index 00000000000..ef48eca1114 --- /dev/null +++ b/tests/target/space-not-before-bound-colon.rs @@ -0,0 +1,5 @@ +// rustfmt-space_before_bound: true +// rustfmt-space_after_bound_colon: false + +trait Trait {} +fn f<'a, 'b :'a, T :Trait>() {} diff --git a/tests/target/spaces-around-ranges.rs b/tests/target/spaces-around-ranges.rs new file mode 100644 index 00000000000..7b280f1439d --- /dev/null +++ b/tests/target/spaces-around-ranges.rs @@ -0,0 +1,15 @@ +// rustfmt-spaces_around_ranges: true + +fn bar(v: &[u8]) {} + +fn foo() { + let a = vec![0; 20]; + for j in 0 ... 20 { + for i in 0 .. 3 { + bar(a[i .. j]); + bar(a[i ..]); + bar(a[.. j]); + bar(a[... (j + 1)]); + } + } +}