From 0204dcbd98c144b664f29d24ed7f8b31a7fcaf17 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 5 Jun 2018 19:58:21 +0900 Subject: [PATCH 1/3] Update tests --- tests/target/configs/imports_layout/merge_mixed.rs | 3 ++- tests/target/imports.rs | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/target/configs/imports_layout/merge_mixed.rs b/tests/target/configs/imports_layout/merge_mixed.rs index d67979840d0e..415a11211a12 100644 --- a/tests/target/configs/imports_layout/merge_mixed.rs +++ b/tests/target/configs/imports_layout/merge_mixed.rs @@ -3,5 +3,6 @@ // rustfmt-imports_layout: Mixed use std::{ - fmt, io, str::{self, FromStr}, + fmt, io, + str::{self, FromStr}, }; diff --git a/tests/target/imports.rs b/tests/target/imports.rs index 01964fbd2951..92b1a818a7da 100644 --- a/tests/target/imports.rs +++ b/tests/target/imports.rs @@ -82,8 +82,9 @@ use self::unix::{}; use foo::{ a, b, bar::{ - baz, foo::{a, b, cxxxxxxxxxxxxx, yyyyyyyyyyyyyy, zzzzzzzzzzzzzzzz}, qux, xxxxxxxxxxx, - yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz, + baz, + foo::{a, b, cxxxxxxxxxxxxx, yyyyyyyyyyyyyy, zzzzzzzzzzzzzzzz}, + qux, xxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz, }, boo, c, }; From 35fee77d39558845d4c55423af524fe850134a7f Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 5 Jun 2018 19:58:28 +0900 Subject: [PATCH 2/3] Add a test for #2765 --- tests/source/imports.rs | 2 ++ tests/target/imports.rs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/source/imports.rs b/tests/source/imports.rs index 30ce49605f7b..b56e3f1afdb8 100644 --- a/tests/source/imports.rs +++ b/tests/source/imports.rs @@ -83,6 +83,8 @@ use foo::{a, bar::{baz, qux, xxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz, foo:: use fooo::{baar::{foobar::{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz}}, z, bar, bar::*, x, y}; +use exonum::{api::{Api, ApiError}, blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet}, crypto::{Hash, PublicKey}, helpers::Height, node::TransactionSend, storage::{ListProof, MapProof}}; + // nested imports with a single sub-tree. use a::{b::{c::*}}; use a::{b::{c::{}}}; diff --git a/tests/target/imports.rs b/tests/target/imports.rs index 92b1a818a7da..df4d8cdd864d 100644 --- a/tests/target/imports.rs +++ b/tests/target/imports.rs @@ -94,7 +94,18 @@ use fooo::{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz, }, - bar, bar::*, x, y, z, + bar, + bar::*, + x, y, z, +}; + +use exonum::{ + api::{Api, ApiError}, + blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet}, + crypto::{Hash, PublicKey}, + helpers::Height, + node::TransactionSend, + storage::{ListProof, MapProof}, }; // nested imports with a single sub-tree. From 42ab258757167abcc067433aa15945f5c51a1e8e Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 5 Jun 2018 19:58:44 +0900 Subject: [PATCH 3/3] Put each nested import on its own line while putting non-nested imports on the same line as much as possible. --- src/config/lists.rs | 2 ++ src/imports.rs | 16 ++++++++++------ src/lists.rs | 6 +++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/config/lists.rs b/src/config/lists.rs index 04406e8d5669..a9846a1cc748 100644 --- a/src/config/lists.rs +++ b/src/config/lists.rs @@ -19,6 +19,8 @@ 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 4740921c5250..a1cf6a83f07f 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -706,12 +706,16 @@ fn rewrite_nested_use_tree( shape.width.saturating_sub(2) }; - let tactic = definitive_tactic( - &list_items, - context.config.imports_layout(), - Separator::Comma, - remaining_width, - ); + let tactic = if has_nested_list { + DefinitiveListTactic::NestedImport + } else { + 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 24779e1667e3..6e8679188641 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -229,6 +229,7 @@ where let sep_place = SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator); let mut prev_item_had_post_comment = false; + let mut prev_item_is_nested_import = false; let mut line_len = 0; let indent_str = &formatting.shape.indent.to_string(formatting.config); @@ -281,12 +282,14 @@ where result.push('\n'); result.push_str(indent_str); } - DefinitiveListTactic::Mixed => { + DefinitiveListTactic::Mixed | DefinitiveListTactic::NestedImport => { 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("::")))) { result.push('\n'); result.push_str(indent_str); @@ -452,6 +455,7 @@ where } prev_item_had_post_comment = item.post_comment.is_some(); + prev_item_is_nested_import = inner_item.contains("::"); } Some(result)