diff --git a/src/config.rs b/src/config.rs index 9cd15bc0e33..71adc24c09f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -408,6 +408,9 @@ create_config! { match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm"; closure_block_indent_threshold: isize, 5, "How many lines a closure must have before it is \ 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_before_bound: bool, false, "Leave a space before the colon in a trait or lifetime bound"; 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/items.rs b/src/items.rs index a0e5626b882..ce44d1be839 100644 --- a/src/items.rs +++ b/src/items.rs @@ -45,13 +45,17 @@ impl Rewrite for ast::Local { let mut infix = String::new(); if let Some(ref ty) = self.ty { - // 2 = ": ".len() + let separator = if context.config.space_before_type_annotation { + " : " + } else { + ": " + }; + let indent = offset + last_line_width(&result) + separator.len(); // 1 = ; - let indent = offset + last_line_width(&result) + 2; let budget = try_opt!(width.checked_sub(indent.width() + 1)); let rewrite = try_opt!(ty.rewrite(context, budget, indent)); - infix.push_str(": "); + infix.push_str(separator); infix.push_str(&rewrite); } @@ -998,6 +1002,14 @@ pub fn rewrite_type_alias(context: &RewriteContext, Some(result) } +fn type_annotation_spacing(config: &Config) -> &str { + if config.space_before_type_annotation { + " " + } else { + "" + } +} + impl Rewrite for ast::StructField { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option { if contains_skip(&self.attrs) { @@ -1014,8 +1026,9 @@ impl Rewrite for ast::StructField { attr_str.push_str(&offset.to_string(context.config)); } + let type_annotation_spacing = type_annotation_spacing(context.config); let result = match name { - Some(name) => format!("{}{}{}: ", attr_str, vis, name), + Some(name) => format!("{}{}{}{}: ", attr_str, vis, name, type_annotation_spacing), None => format!("{}{}", attr_str, vis), }; @@ -1034,11 +1047,13 @@ pub fn rewrite_static(prefix: &str, expr_opt: Option<&ptr::P>, context: &RewriteContext) -> Option { - let prefix = format!("{}{} {}{}: ", + let type_annotation_spacing = type_annotation_spacing(context.config); + let prefix = format!("{}{} {}{}{}: ", format_visibility(vis), prefix, format_mutability(mutability), - ident); + ident, + type_annotation_spacing); // 2 = " =".len() let ty_str = try_opt!(ty.rewrite(context, context.config.max_width - context.block_indent.width() - @@ -1117,6 +1132,9 @@ impl Rewrite for ast::Arg { let mut result = try_opt!(self.pat.rewrite(context, width, offset)); if self.ty.node != ast::TyKind::Infer { + if context.config.space_before_type_annotation { + result.push_str(" "); + } 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())); diff --git a/src/types.rs b/src/types.rs index 8fa24df0caa..598ac3b6ac8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -406,7 +406,12 @@ 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 result = format!("{}: {}", result, appendix.join(" + ")); + let bound_spacing = if context.config.space_before_bound { + " " + } else { + "" + }; + let result = format!("{}{}: {}", result, bound_spacing, appendix.join(" + ")); wrap_str(result, context.config.max_width, width, offset) } } @@ -449,6 +454,9 @@ impl Rewrite for ast::TyParam { let mut result = String::with_capacity(128); result.push_str(&self.ident.to_string()); if !self.bounds.is_empty() { + if context.config.space_before_bound { + result.push_str(" "); + } result.push_str(": "); let bounds: String = try_opt!(self.bounds diff --git a/tests/source/space-before-bound.rs b/tests/source/space-before-bound.rs new file mode 100644 index 00000000000..cb4cf8aafd6 --- /dev/null +++ b/tests/source/space-before-bound.rs @@ -0,0 +1,4 @@ +// rustfmt-space_before_bound: true + +trait Trait {} +fn f<'a, 'b: 'a, T: Trait>() {} diff --git a/tests/source/space-before-type-annotation.rs b/tests/source/space-before-type-annotation.rs new file mode 100644 index 00000000000..c1b0248d8aa --- /dev/null +++ b/tests/source/space-before-type-annotation.rs @@ -0,0 +1,10 @@ +// rustfmt-space_before_type_annotation: true + +static staticVar: i32 = 42; +const constVar: i32 = 42; +fn foo(paramVar: i32) { + let localVar: i32 = 42; +} +struct S { + fieldVar: i32, +} diff --git a/tests/target/space-before-bound.rs b/tests/target/space-before-bound.rs new file mode 100644 index 00000000000..7e1ef870315 --- /dev/null +++ b/tests/target/space-before-bound.rs @@ -0,0 +1,4 @@ +// rustfmt-space_before_bound: true + +trait Trait {} +fn f<'a, 'b : 'a, T : Trait>() {} diff --git a/tests/target/space-before-type-annotation.rs b/tests/target/space-before-type-annotation.rs new file mode 100644 index 00000000000..8bd16499b14 --- /dev/null +++ b/tests/target/space-before-type-annotation.rs @@ -0,0 +1,10 @@ +// rustfmt-space_before_type_annotation: true + +static staticVar : i32 = 42; +const constVar : i32 = 42; +fn foo(paramVar : i32) { + let localVar : i32 = 42; +} +struct S { + fieldVar : i32, +}