Add three new options for spaces

This commit is contained in:
est31 2016-09-17 01:44:51 +02:00
parent 4418fab4f2
commit efd3e5c091
10 changed files with 134 additions and 17 deletions

View File

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

View File

@ -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) => ":",
}
}

View File

@ -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<String> {
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);

View File

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

View File

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

View File

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

View File

@ -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)]);
}
}
}

View File

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

View File

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

View File

@ -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)]);
}
}
}