From 1077a100a16e1ce6e33db6ca42c3f93b2ab9620f Mon Sep 17 00:00:00 2001 From: Flier Lu Date: Wed, 17 May 2017 18:32:18 +0800 Subject: [PATCH 1/3] reorder imports in group --- Configurations.md | 37 ++++++++++++++++++- src/config.rs | 1 + src/visitor.rs | 28 +++++++++++++- .../configs-reorder_imports_in_group-false.rs | 13 +++++++ .../configs-reorder_imports_in_group-true.rs | 13 +++++++ .../configs-reorder_imports_in_group-false.rs | 13 +++++++ .../configs-reorder_imports_in_group-true.rs | 13 +++++++ 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 tests/source/configs-reorder_imports_in_group-false.rs create mode 100644 tests/source/configs-reorder_imports_in_group-true.rs create mode 100644 tests/target/configs-reorder_imports_in_group-false.rs create mode 100644 tests/target/configs-reorder_imports_in_group-true.rs diff --git a/Configurations.md b/Configurations.md index d415047f7eb..1811c3aa5c6 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1007,7 +1007,42 @@ use lorem; use sit; ``` -See also [`reorder_imported_names`](#reorder_imported_names). +See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group). + +## `reorder_imports_in_group` + +Reorder import statements in group + +- **Default value**: `false` +- **Possible values**: `true`, `false` + +**Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`. + +#### `false`: + +```rust +use std::mem; +use std::io; + +use lorem; +use ipsum; +use dolor; +use sit; +``` + +#### `true`: + +```rust +use std::io; +use std::mem; + +use dolor; +use ipsum; +use lorem; +use sit; +``` + +See also [`reorder_imports`](#reorder_imports). ## `single_line_if_else_max_width` diff --git a/src/config.rs b/src/config.rs index 632170e6812..e8ecfa0f8e5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -387,6 +387,7 @@ create_config! { chain_indent: IndentStyle, IndentStyle::Block, "Indentation of chain"; chain_one_line_max: usize, 60, "Maximum length of a chain to fit on a single line"; reorder_imports: bool, false, "Reorder import statements alphabetically"; + reorder_imports_in_group: bool, false, "Reorder import statements in group"; reorder_imported_names: bool, false, "Reorder lists of names in import statements alphabetically"; single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \ diff --git a/src/visitor.rs b/src/visitor.rs index 556b9912697..2321eee272a 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::cmp; + use syntax::{ast, ptr, visit}; use syntax::codemap::{self, CodeMap, Span, BytePos}; use syntax::parse::ParseSess; @@ -31,6 +33,19 @@ fn is_use_item(item: &ast::Item) -> bool { } } +fn item_bound(item: &ast::Item) -> Span { + item.attrs + .iter() + .map(|attr| attr.span) + .fold(item.span, |bound, span| { + Span { + lo: cmp::min(bound.lo, span.lo), + hi: cmp::max(bound.hi, span.hi), + expn_id: span.expn_id, + } + }) +} + pub struct FmtVisitor<'a> { pub parse_session: &'a ParseSess, pub codemap: &'a CodeMap, @@ -510,9 +525,20 @@ impl<'a> FmtVisitor<'a> { // to be potentially reordered within `format_imports`. Otherwise, just format the // next item for output. if self.config.reorder_imports && is_use_item(&*items_left[0]) { + let reorder_imports_in_group = self.config.reorder_imports_in_group; + let mut last = self.codemap.lookup_line_range(item_bound(&items_left[0])); let use_item_length = items_left .iter() - .take_while(|ppi| is_use_item(&***ppi)) + .take_while(|ppi| { + is_use_item(&***ppi) && + (!reorder_imports_in_group || + { + let current = self.codemap.lookup_line_range(item_bound(&ppi)); + let in_same_group = current.lo < last.hi + 2; + last = current; + in_same_group + }) + }) .count(); let (use_items, rest) = items_left.split_at(use_item_length); self.format_imports(use_items); diff --git a/tests/source/configs-reorder_imports_in_group-false.rs b/tests/source/configs-reorder_imports_in_group-false.rs new file mode 100644 index 00000000000..87711bb142b --- /dev/null +++ b/tests/source/configs-reorder_imports_in_group-false.rs @@ -0,0 +1,13 @@ +// rustfmt-reorder_imports: true +// rustfmt-reorder_imports_in_group: false +// Reorder imports in group + +/// This comment should stay with `use std::mem;` +use std::mem; +use std::io; + +use lorem; +/// This comment should stay with `use ipsum;` +use ipsum; +use dolor; +use sit; diff --git a/tests/source/configs-reorder_imports_in_group-true.rs b/tests/source/configs-reorder_imports_in_group-true.rs new file mode 100644 index 00000000000..b5690b89cc0 --- /dev/null +++ b/tests/source/configs-reorder_imports_in_group-true.rs @@ -0,0 +1,13 @@ +// rustfmt-reorder_imports: true +// rustfmt-reorder_imports_in_group: true +// Reorder imports in group + +/// This comment should stay with `use std::mem;` +use std::mem; +use std::io; + +use lorem; +/// This comment should stay with `use ipsum;` +use ipsum; +use dolor; +use sit; diff --git a/tests/target/configs-reorder_imports_in_group-false.rs b/tests/target/configs-reorder_imports_in_group-false.rs new file mode 100644 index 00000000000..42778d91dd8 --- /dev/null +++ b/tests/target/configs-reorder_imports_in_group-false.rs @@ -0,0 +1,13 @@ +// rustfmt-reorder_imports: true +// rustfmt-reorder_imports_in_group: false +// Reorder imports in group + +use dolor; +/// This comment should stay with `use ipsum;` +use ipsum; + +use lorem; +use sit; +use std::io; +/// This comment should stay with `use std::mem;` +use std::mem; diff --git a/tests/target/configs-reorder_imports_in_group-true.rs b/tests/target/configs-reorder_imports_in_group-true.rs new file mode 100644 index 00000000000..c5e353662b5 --- /dev/null +++ b/tests/target/configs-reorder_imports_in_group-true.rs @@ -0,0 +1,13 @@ +// rustfmt-reorder_imports: true +// rustfmt-reorder_imports_in_group: true +// Reorder imports in group + +use std::io; +/// This comment should stay with `use std::mem;` +use std::mem; + +use dolor; +/// This comment should stay with `use ipsum;` +use ipsum; +use lorem; +use sit; From 8ca699ce26bab2ec9a2ce5f8ab31581c6deb12bc Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 18 May 2017 08:03:47 -0400 Subject: [PATCH 2/3] config: fix `suffices` -> `suffixes` typo Fixes #1477. --- Configurations.md | 2 +- src/config.rs | 2 +- src/patterns.rs | 2 +- tests/source/configs-condense_wildcard_suffices-false.rs | 4 ++-- tests/source/configs-condense_wildcard_suffices-true.rs | 4 ++-- tests/source/pattern-condense-wildcards.rs | 2 +- tests/target/configs-condense_wildcard_suffices-false.rs | 4 ++-- tests/target/configs-condense_wildcard_suffices-true.rs | 4 ++-- tests/target/pattern-condense-wildcards.rs | 2 +- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Configurations.md b/Configurations.md index 1811c3aa5c6..1d8774b6c55 100644 --- a/Configurations.md +++ b/Configurations.md @@ -166,7 +166,7 @@ Maximum length of comments. No effect unless`wrap_comments = true`. See also [`wrap_comments`](#wrap_comments). -## `condense_wildcard_suffices` +## `condense_wildcard_suffixes` Replace strings of _ wildcards by a single .. in tuple patterns diff --git a/src/config.rs b/src/config.rs index e8ecfa0f8e5..fa12f16a3dd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -421,6 +421,6 @@ create_config! { 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"; - condense_wildcard_suffices: bool, false, "Replace strings of _ wildcards by a single .. in \ + condense_wildcard_suffixes: bool, false, "Replace strings of _ wildcards by a single .. in \ tuple patterns" } diff --git a/src/patterns.rs b/src/patterns.rs index a1628888622..2b65604e6a8 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -295,7 +295,7 @@ fn rewrite_tuple_pat(pats: &[ptr::P], // Condense wildcard string suffix into a single .. let wildcard_suffix_len = count_wildcard_suffix_len(&items); - let list = if context.config.condense_wildcard_suffices && wildcard_suffix_len >= 2 { + let list = if context.config.condense_wildcard_suffixes && wildcard_suffix_len >= 2 { let new_item_count = 1 + pats.len() - wildcard_suffix_len; items[new_item_count - 1].item = Some("..".to_owned()); diff --git a/tests/source/configs-condense_wildcard_suffices-false.rs b/tests/source/configs-condense_wildcard_suffices-false.rs index e4ad1f2f3cc..3b967f35a8e 100644 --- a/tests/source/configs-condense_wildcard_suffices-false.rs +++ b/tests/source/configs-condense_wildcard_suffices-false.rs @@ -1,5 +1,5 @@ -// rustfmt-condense_wildcard_suffices: false -// Condense wildcard suffices +// rustfmt-condense_wildcard_suffixes: false +// Condense wildcard suffixes fn main() { let (lorem, ipsum, _, _) = (1, 2, 3, 4); diff --git a/tests/source/configs-condense_wildcard_suffices-true.rs b/tests/source/configs-condense_wildcard_suffices-true.rs index 2c8457a88d3..3798a6b9902 100644 --- a/tests/source/configs-condense_wildcard_suffices-true.rs +++ b/tests/source/configs-condense_wildcard_suffices-true.rs @@ -1,5 +1,5 @@ -// rustfmt-condense_wildcard_suffices: true -// Condense wildcard suffices +// rustfmt-condense_wildcard_suffixes: true +// Condense wildcard suffixes fn main() { let (lorem, ipsum, _, _) = (1, 2, 3, 4); diff --git a/tests/source/pattern-condense-wildcards.rs b/tests/source/pattern-condense-wildcards.rs index 5f84aea0a6b..244e935639a 100644 --- a/tests/source/pattern-condense-wildcards.rs +++ b/tests/source/pattern-condense-wildcards.rs @@ -1,5 +1,5 @@ // rustfmt-normalize_comments: true -// rustfmt-condense_wildcard_suffices: true +// rustfmt-condense_wildcard_suffixes: true fn main() { match x { diff --git a/tests/target/configs-condense_wildcard_suffices-false.rs b/tests/target/configs-condense_wildcard_suffices-false.rs index e4ad1f2f3cc..3b967f35a8e 100644 --- a/tests/target/configs-condense_wildcard_suffices-false.rs +++ b/tests/target/configs-condense_wildcard_suffices-false.rs @@ -1,5 +1,5 @@ -// rustfmt-condense_wildcard_suffices: false -// Condense wildcard suffices +// rustfmt-condense_wildcard_suffixes: false +// Condense wildcard suffixes fn main() { let (lorem, ipsum, _, _) = (1, 2, 3, 4); diff --git a/tests/target/configs-condense_wildcard_suffices-true.rs b/tests/target/configs-condense_wildcard_suffices-true.rs index 03bd0c4c6c8..4f880abe80e 100644 --- a/tests/target/configs-condense_wildcard_suffices-true.rs +++ b/tests/target/configs-condense_wildcard_suffices-true.rs @@ -1,5 +1,5 @@ -// rustfmt-condense_wildcard_suffices: true -// Condense wildcard suffices +// rustfmt-condense_wildcard_suffixes: true +// Condense wildcard suffixes fn main() { let (lorem, ipsum, ..) = (1, 2, 3, 4); diff --git a/tests/target/pattern-condense-wildcards.rs b/tests/target/pattern-condense-wildcards.rs index ebf89c582ff..acc41b73e18 100644 --- a/tests/target/pattern-condense-wildcards.rs +++ b/tests/target/pattern-condense-wildcards.rs @@ -1,5 +1,5 @@ // rustfmt-normalize_comments: true -// rustfmt-condense_wildcard_suffices: true +// rustfmt-condense_wildcard_suffixes: true fn main() { match x { From 59cefa988a67440f72b79e42f633af8703c50ae7 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 18 May 2017 08:17:09 -0400 Subject: [PATCH 3/3] Configurations: fix typos in example signatures --- Configurations.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Configurations.md b/Configurations.md index 1811c3aa5c6..dd9462d9758 100644 --- a/Configurations.md +++ b/Configurations.md @@ -259,10 +259,10 @@ trait Lorem { // body } - fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur, + fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur, adipiscing: Adipiscing, elit: Elit); - fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur, + fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur, adipiscing: Adipiscing, elit: Elit) { // body } @@ -279,7 +279,7 @@ trait Lorem { // body } - fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur, + fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur, adipiscing: Adipiscing, elit: Elit); fn lorem(ipsum: Ipsum,