From 4565d417900c8383c75711728df4ffcfdceee76f Mon Sep 17 00:00:00 2001 From: Josh Chase Date: Mon, 18 Jun 2018 12:50:13 -0400 Subject: [PATCH 1/4] Add test for #2794 --- tests/config/issue-2794.toml | 3 +++ tests/source/issue-2794.rs | 3 +++ tests/target/issue-2794.rs | 8 ++++++++ 3 files changed, 14 insertions(+) create mode 100644 tests/config/issue-2794.toml create mode 100644 tests/source/issue-2794.rs create mode 100644 tests/target/issue-2794.rs diff --git a/tests/config/issue-2794.toml b/tests/config/issue-2794.toml new file mode 100644 index 00000000000..f8a9fb51a79 --- /dev/null +++ b/tests/config/issue-2794.toml @@ -0,0 +1,3 @@ +indent_style = "Block" +imports_indent = "Block" +imports_layout = "Vertical" diff --git a/tests/source/issue-2794.rs b/tests/source/issue-2794.rs new file mode 100644 index 00000000000..051458b14d1 --- /dev/null +++ b/tests/source/issue-2794.rs @@ -0,0 +1,3 @@ +use std::{ + env, fs, io::{Read, Write}, +}; diff --git a/tests/target/issue-2794.rs b/tests/target/issue-2794.rs new file mode 100644 index 00000000000..a2c01247948 --- /dev/null +++ b/tests/target/issue-2794.rs @@ -0,0 +1,8 @@ +use std::{ + env, + fs, + io::{ + Read, + Write, + }, +}; From ea540651b72c4816f4f0b4fb9286ef1f96d846df Mon Sep 17 00:00:00 2001 From: Josh Chase Date: Tue, 19 Jun 2018 09:30:50 -0400 Subject: [PATCH 2/4] Switch to comments for rustfmt config --- tests/config/issue-2794.toml | 3 --- tests/source/issue-2794.rs | 4 ++++ tests/target/issue-2794.rs | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) delete mode 100644 tests/config/issue-2794.toml diff --git a/tests/config/issue-2794.toml b/tests/config/issue-2794.toml deleted file mode 100644 index f8a9fb51a79..00000000000 --- a/tests/config/issue-2794.toml +++ /dev/null @@ -1,3 +0,0 @@ -indent_style = "Block" -imports_indent = "Block" -imports_layout = "Vertical" diff --git a/tests/source/issue-2794.rs b/tests/source/issue-2794.rs index 051458b14d1..c3f9c0412a4 100644 --- a/tests/source/issue-2794.rs +++ b/tests/source/issue-2794.rs @@ -1,3 +1,7 @@ +// rustfmt-indent_style: Block +// rustfmt-imports_indent: Block +// rustfmt-imports_layout: Vertical + use std::{ env, fs, io::{Read, Write}, }; diff --git a/tests/target/issue-2794.rs b/tests/target/issue-2794.rs index a2c01247948..951c0af206d 100644 --- a/tests/target/issue-2794.rs +++ b/tests/target/issue-2794.rs @@ -1,3 +1,7 @@ +// rustfmt-indent_style: Block +// rustfmt-imports_indent: Block +// rustfmt-imports_layout: Vertical + use std::{ env, fs, From 2077855e000210be5c89f4d7e17e495122686b28 Mon Sep 17 00:00:00 2001 From: Josh Chase Date: Tue, 19 Jun 2018 11:28:00 -0400 Subject: [PATCH 3/4] Remove NestedImport tactic --- src/config/lists.rs | 2 -- src/imports.rs | 16 ++++++---------- src/lists.rs | 5 ++--- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/config/lists.rs b/src/config/lists.rs index a9846a1cc74..04406e8d566 100644 --- a/src/config/lists.rs +++ b/src/config/lists.rs @@ -19,8 +19,6 @@ pub enum DefinitiveListTactic { Vertical, Horizontal, Mixed, - /// Tactic for nested import. - NestedImport, /// Special case tactic for `format!()`, `write!()` style macros. SpecialMacro(usize), } diff --git a/src/imports.rs b/src/imports.rs index a1cf6a83f07..4740921c525 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -706,16 +706,12 @@ fn rewrite_nested_use_tree( shape.width.saturating_sub(2) }; - let tactic = if has_nested_list { - DefinitiveListTactic::NestedImport - } else { - definitive_tactic( - &list_items, - context.config.imports_layout(), - Separator::Comma, - remaining_width, - ) - }; + let tactic = definitive_tactic( + &list_items, + context.config.imports_layout(), + Separator::Comma, + remaining_width, + ); let ends_with_newline = context.config.imports_indent() == IndentStyle::Block && tactic != DefinitiveListTactic::Horizontal; diff --git a/src/lists.rs b/src/lists.rs index 6e867918864..5562602133b 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -282,14 +282,13 @@ where result.push('\n'); result.push_str(indent_str); } - DefinitiveListTactic::Mixed | DefinitiveListTactic::NestedImport => { + DefinitiveListTactic::Mixed => { let total_width = total_item_width(item) + item_sep_len; // 1 is space between separator and item. if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width) || prev_item_had_post_comment - || (tactic == DefinitiveListTactic::NestedImport - && (prev_item_is_nested_import || (!first && inner_item.contains("::")))) + || (prev_item_is_nested_import || (!first && inner_item.contains("::"))) { result.push('\n'); result.push_str(indent_str); From ee5ff2d9e8f2fcf7ef8ceefc5de78e5439ed7a21 Mon Sep 17 00:00:00 2001 From: Josh Chase Date: Tue, 19 Jun 2018 11:28:58 -0400 Subject: [PATCH 4/4] Add flag to the ListFormatting struct for nested imports --- src/attr.rs | 1 + src/closures.rs | 1 + src/expr.rs | 2 ++ src/imports.rs | 1 + src/items.rs | 4 ++++ src/lists.rs | 6 +++++- src/macros.rs | 1 + src/matches.rs | 1 + src/overflow.rs | 1 + src/reorder.rs | 1 + src/types.rs | 1 + src/vertical.rs | 1 + 12 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/attr.rs b/src/attr.rs index 28e3e24ea09..efbd4872aae 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -247,6 +247,7 @@ impl Rewrite for ast::MetaItem { shape: item_shape, ends_with_newline: false, preserve_newline: false, + nested: false, config: context.config, }; let item_str = write_list(&item_vec, &fmt)?; diff --git a/src/closures.rs b/src/closures.rs index 3d0a52de0dc..257b8729f45 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -260,6 +260,7 @@ fn rewrite_closure_fn_decl( shape: arg_shape, ends_with_newline: false, preserve_newline: true, + nested: false, config: context.config, }; let list_str = write_list(&item_vec, &fmt)?; diff --git a/src/expr.rs b/src/expr.rs index 112383e7e4c..0c2716a2dba 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1338,6 +1338,7 @@ pub fn rewrite_multiple_patterns( shape, ends_with_newline: false, preserve_newline: false, + nested: false, config: context.config, }; write_list(&items, &fmt) @@ -1902,6 +1903,7 @@ where shape, ends_with_newline: false, preserve_newline: false, + nested: false, config: context.config, }; let list_str = write_list(&item_vec, &fmt)?; diff --git a/src/imports.rs b/src/imports.rs index 4740921c525..7e45bb55f0d 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -727,6 +727,7 @@ fn rewrite_nested_use_tree( shape: nested_shape, ends_with_newline, preserve_newline: true, + nested: has_nested_list, config: context.config, }; diff --git a/src/items.rs b/src/items.rs index 38311abcba0..843d5839de9 100644 --- a/src/items.rs +++ b/src/items.rs @@ -533,6 +533,7 @@ impl<'a> FmtVisitor<'a> { shape, ends_with_newline: true, preserve_newline: true, + nested: false, config: self.config, }; @@ -2307,6 +2308,7 @@ fn rewrite_args( shape: Shape::legacy(budget, indent), ends_with_newline: tactic.ends_with_newline(context.config.indent_style()), preserve_newline: true, + nested: false, config: context.config, }; @@ -2494,6 +2496,7 @@ fn rewrite_where_clause_rfc_style( shape: clause_shape, ends_with_newline: true, preserve_newline: true, + nested: false, config: context.config, }; let preds_str = write_list(&items.collect::>(), &fmt)?; @@ -2607,6 +2610,7 @@ fn rewrite_where_clause( shape: Shape::legacy(budget, offset), ends_with_newline: tactic.ends_with_newline(context.config.indent_style()), preserve_newline: true, + nested: false, config: context.config, }; let preds_str = write_list(&item_vec, &fmt)?; diff --git a/src/lists.rs b/src/lists.rs index 5562602133b..97dd093b4f9 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -34,6 +34,8 @@ pub struct ListFormatting<'a> { pub ends_with_newline: bool, // Remove newlines between list elements for expressions. pub preserve_newline: bool, + // Nested import lists get some special handling for the "Mixed" list type + pub nested: bool, pub config: &'a Config, } @@ -288,7 +290,8 @@ where // 1 is space between separator and item. if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width) || prev_item_had_post_comment - || (prev_item_is_nested_import || (!first && inner_item.contains("::"))) + || (formatting.nested + && (prev_item_is_nested_import || (!first && inner_item.contains("::")))) { result.push('\n'); result.push_str(indent_str); @@ -824,6 +827,7 @@ pub fn struct_lit_formatting<'a>( shape, ends_with_newline, preserve_newline: true, + nested: false, config: context.config, } } diff --git a/src/macros.rs b/src/macros.rs index 9053aaebd90..801e04baad0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -399,6 +399,7 @@ pub fn rewrite_macro_def( shape: arm_shape, ends_with_newline: true, preserve_newline: true, + nested: false, config: context.config, }; diff --git a/src/matches.rs b/src/matches.rs index 9551dc36002..c179efc3963 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -224,6 +224,7 @@ fn rewrite_match_arms( shape: arm_shape, ends_with_newline: true, preserve_newline: true, + nested: false, config: context.config, }; diff --git a/src/overflow.rs b/src/overflow.rs index 62675b4002c..f91ef38e2ee 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -388,6 +388,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> { _ => false, }, preserve_newline: false, + nested: false, config: self.context.config, }; diff --git a/src/reorder.rs b/src/reorder.rs index 027b64e8317..ed6df1227ef 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -77,6 +77,7 @@ fn wrap_reorderable_items( shape, ends_with_newline: true, preserve_newline: false, + nested: false, config: context.config, }; diff --git a/src/types.rs b/src/types.rs index 46d48e72763..44fdbee6efd 100644 --- a/src/types.rs +++ b/src/types.rs @@ -366,6 +366,7 @@ where shape: list_shape, ends_with_newline: tactic.ends_with_newline(context.config.indent_style()), preserve_newline: true, + nested: false, config: context.config, }; diff --git a/src/vertical.rs b/src/vertical.rs index 1595b22c5c4..a8a8ee6cec3 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -252,6 +252,7 @@ fn rewrite_aligned_items_inner( shape: item_shape, ends_with_newline: true, preserve_newline: true, + nested: false, config: context.config, }; write_list(&items, &fmt)