From efd3e5c0914f0c8d401a504be4116bf947296218 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 17 Sep 2016 01:44:51 +0200 Subject: [PATCH 1/2] 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 71adc24c09fe..f93a6e115a5f 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 bece2ef91fe5..11e2ca881457 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 1071a4017d4a..b381ba8b7b28 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 cb310e13c332..a718750db955 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 000000000000..86f829c83538 --- /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 000000000000..4ec569f7902f --- /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 000000000000..188f8f9074f9 --- /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 000000000000..3bdfa57fb739 --- /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 000000000000..ef48eca11149 --- /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 000000000000..7b280f1439d4 --- /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)]); + } + } +} From c6243c950ee3376f562cc2dd08a3d31c9c3818b2 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 17 Sep 2016 03:20:00 +0200 Subject: [PATCH 2/2] Improve comment rewriting with normalize_comments == false Only change multiline comments of the form ```rust /* * Text */ ``` while not affecting comments of the form ```rust /* Text */ ``` when normalize_comments is off. In the first case, we have a known character we can align against, while we don't have one in the second case. Before, we have converted the second form into the first, but this is against the spirit of normalize_comments being turned off. Fixes #956 --- src/comment.rs | 10 ++++++++++ tests/source/comment4.rs | 12 ++++++++++++ tests/target/comment4.rs | 12 ++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/comment.rs b/src/comment.rs index 979d3bef90e2..5a525f1bb23d 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -71,6 +71,16 @@ pub fn rewrite_comment(orig: &str, let indent_str = offset.to_string(config); let line_breaks = s.chars().filter(|&c| c == '\n').count(); + let num_bare_lines = s.lines() + .enumerate() + .map(|(_, line)| line.trim()) + .filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*"))) + .count(); + + if num_bare_lines > 0 && !config.normalize_comments { + return Some(orig.to_owned()); + } + let lines = s.lines() .enumerate() .map(|(i, mut line)| { diff --git a/tests/source/comment4.rs b/tests/source/comment4.rs index 81754f93c4c6..0ed18ca8da12 100644 --- a/tests/source/comment4.rs +++ b/tests/source/comment4.rs @@ -33,3 +33,15 @@ fn test() { /// test123 fn doc_comment() { } + +/* +Regression test for issue #956 + +(some very important text) +*/ + +/* +fn debug_function() { + println!("hello"); +} +// */ diff --git a/tests/target/comment4.rs b/tests/target/comment4.rs index c4d25ca11435..910bade6c88f 100644 --- a/tests/target/comment4.rs +++ b/tests/target/comment4.rs @@ -32,3 +32,15 @@ fn test() { /// test123 fn doc_comment() {} + +/* +Regression test for issue #956 + +(some very important text) +*/ + +/* +fn debug_function() { + println!("hello"); +} +// */