From 6f3c329500fc0e1f67d454864f658e72a52183b3 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 1 Sep 2015 18:38:12 +1200 Subject: [PATCH] Support non-indented where clauses --- src/config.rs | 2 ++ src/items.rs | 9 +++++++-- tests/config/small_tabs.toml | 1 + tests/source/fn-custom-2.rs | 9 +++++++++ tests/target/fn-custom-2.rs | 13 +++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index fdfca063172..03b522be366 100644 --- a/src/config.rs +++ b/src/config.rs @@ -91,6 +91,7 @@ create_config! { fn_args_paren_newline: bool, fn_args_layout: Density, fn_arg_indent: BlockIndentStyle, + where_indent: BlockIndentStyle, // Visual will be treated like Tabbed generics_indent: BlockIndentStyle, struct_trailing_comma: SeparatorTactic, struct_lit_trailing_comma: SeparatorTactic, @@ -118,6 +119,7 @@ impl Default for Config { fn_args_paren_newline: true, fn_args_layout: Density::Tall, fn_arg_indent: BlockIndentStyle::Visual, + where_indent: BlockIndentStyle::Tabbed, generics_indent: BlockIndentStyle::Visual, struct_trailing_comma: SeparatorTactic::Vertical, struct_lit_trailing_comma: SeparatorTactic::Vertical, diff --git a/src/items.rs b/src/items.rs index ae440826bdf..94083e81c11 100644 --- a/src/items.rs +++ b/src/items.rs @@ -800,9 +800,14 @@ impl<'a> FmtVisitor<'a> { return String::new(); } + let extra_indent = match self.config.where_indent { + BlockIndentStyle::Inherit => 0, + BlockIndentStyle::Tabbed | BlockIndentStyle::Visual => config.tab_spaces, + }; + let context = self.get_context(); // 6 = "where ".len() - let offset = indent + config.tab_spaces + 6; + let offset = indent + extra_indent + 6; let budget = self.config.ideal_width + self.config.leeway - offset; let span_start = span_for_where_pred(&where_clause.predicates[0]).lo; let items = itemize_list(self.codemap, @@ -828,7 +833,7 @@ impl<'a> FmtVisitor<'a> { }; format!("\n{}where {}", - make_indent(indent + config.tab_spaces), + make_indent(indent + extra_indent), write_list(&items.collect::>(), &fmt)) } diff --git a/tests/config/small_tabs.toml b/tests/config/small_tabs.toml index e621547a010..3d6f01d7375 100644 --- a/tests/config/small_tabs.toml +++ b/tests/config/small_tabs.toml @@ -8,6 +8,7 @@ fn_return_indent = "WithArgs" fn_args_paren_newline = true fn_args_layout = "Tall" fn_arg_indent = "Visual" +where_indent = "Tabbed" generics_indent = "Visual" struct_trailing_comma = "Vertical" struct_lit_trailing_comma = "Vertical" diff --git a/tests/source/fn-custom-2.rs b/tests/source/fn-custom-2.rs index 1b7e6ad78ad..6c9238e8087 100644 --- a/tests/source/fn-custom-2.rs +++ b/tests/source/fn-custom-2.rs @@ -1,5 +1,6 @@ // rustfmt-fn_arg_indent: Inherit // rustfmt-generics_indent: Tabbed +// rustfmt-where_indent: Inherit // Test different indents. fn foo(a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee) { @@ -10,6 +11,10 @@ fn bar<'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb, TTTTTTTTTTTTT, UUUUUUUUUUUUUUUUUUUU: WW bar(); } +fn baz() where X: TTTTTTTT { + baz(); +} + impl Foo { fn foo(self, a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee) { foo(); @@ -18,6 +23,10 @@ impl Foo { fn bar<'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb, TTTTTTTTTTTTT, UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW>(a: Aaaaaaaaaaaaaaa) { bar(); } + + fn baz() where X: TTTTTTTT { + baz(); + } } struct Foo { diff --git a/tests/target/fn-custom-2.rs b/tests/target/fn-custom-2.rs index 1bb7ef401ec..e5c8af9a38b 100644 --- a/tests/target/fn-custom-2.rs +++ b/tests/target/fn-custom-2.rs @@ -1,5 +1,6 @@ // rustfmt-fn_arg_indent: Inherit // rustfmt-generics_indent: Tabbed +// rustfmt-where_indent: Inherit // Test different indents. fn foo(a: Aaaaaaaaaaaaaaa, @@ -17,6 +18,12 @@ fn bar<'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb, bar(); } +fn baz() +where X: TTTTTTTT +{ + baz(); +} + impl Foo { fn foo(self, a: Aaaaaaaaaaaaaaa, @@ -33,6 +40,12 @@ impl Foo { (a: Aaaaaaaaaaaaaaa) { bar(); } + + fn baz() + where X: TTTTTTTT + { + baz(); + } } struct Foo