Add two new whitespace options (#1109)

* An option to leave a space before the colon in a type annotation

* An option to leave a space before the colon in a trait or lifetime bound
This commit is contained in:
Nathan Ridge 2016-08-02 21:07:56 -04:00
parent e76cb6a907
commit 4b999a99c0
7 changed files with 64 additions and 7 deletions

View File

@ -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";

View File

@ -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<String> {
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<ast::Expr>>,
context: &RewriteContext)
-> Option<String> {
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()));

View File

@ -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

View File

@ -0,0 +1,4 @@
// rustfmt-space_before_bound: true
trait Trait {}
fn f<'a, 'b: 'a, T: Trait>() {}

View File

@ -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,
}

View File

@ -0,0 +1,4 @@
// rustfmt-space_before_bound: true
trait Trait {}
fn f<'a, 'b : 'a, T : Trait>() {}

View File

@ -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,
}