From dba2d8afd553bd18806c4f674b0d6bcd925cb78e Mon Sep 17 00:00:00 2001 From: Connor Brewster Date: Mon, 11 Jan 2016 12:26:57 -0700 Subject: [PATCH 1/3] Added option for tighter punctuation in types. fixes #489 --- src/config.rs | 9 +++++++++ src/types.rs | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7e1172e1b9ad..63fb4f760efb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -73,6 +73,13 @@ configuration_option_enum! { Density: CompressedIfEmpty, } +configuration_option_enum! { TypeDensity: + // No spaces around "=" and "+" + Compressed, + // Spaces around " = " and " + " + WhiteSpace, +} + impl Density { pub fn to_list_tactic(self) -> ListTactic { match self { @@ -278,6 +285,8 @@ create_config! { fn_args_density: Density, Density::Tall, "Argument density in functions"; fn_args_layout: StructLitStyle, StructLitStyle::Visual, "Layout of function arguments"; fn_arg_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on function arguments"; + type_punctuation_density: TypeDensity, TypeDensity::WhiteSpace, + "Determines if '+' or '=' are wrapped in spaces in the punctuation of types"; // Should we at least try to put the where clause on the same line as the rest of the // function decl? where_density: Density, Density::CompressedIfEmpty, "Density of a where clause"; diff --git a/src/types.rs b/src/types.rs index 82a335c30124..5500cdecfc0a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -21,6 +21,7 @@ use lists::{format_item_list, itemize_list, format_fn_args}; use rewrite::{Rewrite, RewriteContext}; use utils::{extra_offset, span_after, format_mutability, wrap_str}; use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple}; +use config::TypeDensity; // Does not wrap on simple segments. pub fn rewrite_path(context: &RewriteContext, @@ -424,7 +425,12 @@ impl Rewrite for ast::TyParam { result.push_str(&bounds); } if let Some(ref def) = self.default { - result.push_str(" = "); + let eq_str = if context.config.type_punctuation_density == TypeDensity::Compressed { + "=" + } else { + " = " + }; + result.push_str(eq_str); let budget = try_opt!(width.checked_sub(result.len())); let rewrite = try_opt!(def.rewrite(context, budget, offset + result.len())); result.push_str(&rewrite); @@ -467,8 +473,15 @@ impl Rewrite for ast::Ty { ast::TyObjectSum(ref ty, ref bounds) => { let ty_str = try_opt!(ty.rewrite(context, width, offset)); let overhead = ty_str.len() + 3; - Some(format!("{} + {}", + let plus_str = if context.config.type_punctuation_density == + TypeDensity::Compressed { + "+" + } else { + " + " + }; + Some(format!("{}{}{}", ty_str, + plus_str, try_opt!(bounds.rewrite(context, try_opt!(width.checked_sub(overhead)), offset + overhead)))) From 479b69266be59ad1723d986b32c0d156f7772b9f Mon Sep 17 00:00:00 2001 From: Connor Brewster Date: Tue, 12 Jan 2016 13:42:53 -0700 Subject: [PATCH 2/3] Changed TypeDensity::WhiteSpace to TypeDensity::Wide Changed eq_str and plus_str assignments to use a match --- src/config.rs | 4 ++-- src/types.rs | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index 63fb4f760efb..b2d6cad25b6c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -77,7 +77,7 @@ configuration_option_enum! { TypeDensity: // No spaces around "=" and "+" Compressed, // Spaces around " = " and " + " - WhiteSpace, + Wide, } impl Density { @@ -285,7 +285,7 @@ create_config! { fn_args_density: Density, Density::Tall, "Argument density in functions"; fn_args_layout: StructLitStyle, StructLitStyle::Visual, "Layout of function arguments"; fn_arg_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on function arguments"; - type_punctuation_density: TypeDensity, TypeDensity::WhiteSpace, + type_punctuation_density: TypeDensity, TypeDensity::Wide, "Determines if '+' or '=' are wrapped in spaces in the punctuation of types"; // Should we at least try to put the where clause on the same line as the rest of the // function decl? diff --git a/src/types.rs b/src/types.rs index 5500cdecfc0a..32ae46894145 100644 --- a/src/types.rs +++ b/src/types.rs @@ -425,10 +425,10 @@ impl Rewrite for ast::TyParam { result.push_str(&bounds); } if let Some(ref def) = self.default { - let eq_str = if context.config.type_punctuation_density == TypeDensity::Compressed { - "=" - } else { - " = " + + let eq_str = match context.config.type_punctuation_density { + TypeDensity::Compressed => "=", + TypeDensity::Wide => " = ", }; result.push_str(eq_str); let budget = try_opt!(width.checked_sub(result.len())); @@ -473,11 +473,9 @@ impl Rewrite for ast::Ty { ast::TyObjectSum(ref ty, ref bounds) => { let ty_str = try_opt!(ty.rewrite(context, width, offset)); let overhead = ty_str.len() + 3; - let plus_str = if context.config.type_punctuation_density == - TypeDensity::Compressed { - "+" - } else { - " + " + let plus_str = match context.config.type_punctuation_density { + TypeDensity::Compressed => "+", + TypeDensity::Wide => " + ", }; Some(format!("{}{}{}", ty_str, From 7f8b9bd35682bbe793412342b8d5dfa25cce7869 Mon Sep 17 00:00:00 2001 From: Connor Brewster Date: Tue, 12 Jan 2016 13:51:32 -0700 Subject: [PATCH 3/3] Added test case --- tests/source/type-punctuation.rs | 5 +++++ tests/target/type-punctuation.rs | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 tests/source/type-punctuation.rs create mode 100644 tests/target/type-punctuation.rs diff --git a/tests/source/type-punctuation.rs b/tests/source/type-punctuation.rs new file mode 100644 index 000000000000..29c2f5a4e3bf --- /dev/null +++ b/tests/source/type-punctuation.rs @@ -0,0 +1,5 @@ +// rustfmt-type_punctuation_density: Compressed + +fn Foo + Foo>() { + let i = 6; +} diff --git a/tests/target/type-punctuation.rs b/tests/target/type-punctuation.rs new file mode 100644 index 000000000000..2e5725b3db99 --- /dev/null +++ b/tests/target/type-punctuation.rs @@ -0,0 +1,5 @@ +// rustfmt-type_punctuation_density: Compressed + +fn Foo+Foo>() { + let i = 6; +}