This commit is contained in:
Michael Killough 2017-05-19 16:10:27 +07:00
commit bce1f309b7
14 changed files with 132 additions and 18 deletions

View file

@ -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
@ -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,
@ -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`

View file

@ -449,6 +449,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 \
@ -482,7 +483,7 @@ 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"
}

View file

@ -298,7 +298,7 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
// 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());

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -1,5 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-condense_wildcard_suffices: true
// rustfmt-condense_wildcard_suffixes: true
fn main() {
match x {

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -1,5 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-condense_wildcard_suffices: true
// rustfmt-condense_wildcard_suffixes: true
fn main() {
match x {