Skip to content

Commit

Permalink
Squash the various 'reorder imports' option into one
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Apr 7, 2018
1 parent 4fe98e5 commit 8ae87c3
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 139 deletions.
117 changes: 2 additions & 115 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A possible content of `rustfmt.toml` or `.rustfmt.toml` might look like this:

```toml
indent_style = "Block"
reorder_imported_names = true
reorder_imports = false
```

Each configuration option is either stable or unstable.
Expand Down Expand Up @@ -1237,31 +1237,10 @@ fn dolor() -> usize {}
fn adipiscing() -> usize {}
```

## `reorder_imported_names`

Reorder lists of names in import statements alphabetically

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No

#### `false` (default):

```rust
use super::{lorem, ipsum, dolor, sit};
```

#### `true`:

```rust
use super::{dolor, ipsum, lorem, sit};
```

See also [`reorder_imports`](#reorder_imports).

## `reorder_imports`

Reorder import statements alphabetically
Reorder import and extern crate statements alphabetically

- **Default value**: `false`
- **Possible values**: `true`, `false`
Expand All @@ -1285,98 +1264,6 @@ use lorem;
use sit;
```

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`
- **Stable**: No

**Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.

#### `true` (default):

```rust
use std::io;
use std::mem;

use dolor;
use ipsum;
use lorem;
use sit;
```

#### `false`:


```rust
use dolor;
use ipsum;
use lorem;
use sit;
use std::io;
use std::mem;
```

See also [`reorder_imports`](#reorder_imports).

## `reorder_extern_crates`

Reorder `extern crate` statements alphabetically

- **Default value**: `true`
- **Possible values**: `true`, `false`
- **Stable**: No

#### `true` (default):

```rust
extern crate dolor;
extern crate ipsum;
extern crate lorem;
extern crate sit;
```

#### `false`:

```rust
extern crate lorem;
extern crate ipsum;

extern crate dolor;
extern crate sit;
```

See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).

## `reorder_extern_crates_in_group`

Reorder `extern crate` statements in group

- **Default value**: `true`
- **Possible values**: `true`, `false`
- **Stable**: No

#### `false` (default):

This value has no influence beyond the effect of the [`reorder_extern_crates`](#reorder_extern_crates) option. Set [`reorder_extern_crates`](#reorder_extern_crates) to `false` if you do not want `extern crate` groups to be collapsed and ordered.

#### `true`:

**Note:** This only takes effect when [`reorder_extern_crates`](#reorder_extern_crates) is set to `true`.

```rust
extern crate a;
extern crate b;

extern crate dolor;
extern crate ipsum;
extern crate lorem;
extern crate sit;
```

## `reorder_modules`

Expand Down
9 changes: 2 additions & 7 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,9 @@ create_config! {
imports_layout: ListTactic, ListTactic::Mixed, false, "Item layout inside a import block";

// Ordering
reorder_extern_crates: bool, true, false, "Reorder extern crate statements alphabetically";
reorder_extern_crates_in_group: bool, true, false, "Reorder extern crate statements in group";
reorder_imports: bool, true, false, "Reorder import statements alphabetically";
reorder_imports_in_group: bool, true, false, "Reorder import statements in group";
reorder_imported_names: bool, true, false,
"Reorder lists of names in import statements alphabetically";
reorder_modules: bool, true, false, "Reorder module statemtents alphabetically in group";
reorder_impl_items: bool, false, false, "Reorder impl items";
reorder_imports: bool, true, false, "Reorder import and extern crate statements alphabetically";
reorder_modules: bool, true, false, "Reorder module statements alphabetically in group";

// Spaces around punctuation
binop_separator: SeparatorPlace, SeparatorPlace::Front, false,
Expand Down
1 change: 1 addition & 0 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ fn rewrite_nested_use_tree(
);
(tactic, remaining_width)
};

let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
&& tactic != DefinitiveListTactic::Horizontal;
let fmt = ListFormatting {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ use std::time::Duration;
use syntax::ast;
pub use syntax::codemap::FileName;
use syntax::codemap::{CodeMap, FilePathMapping};
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
use syntax::errors::{DiagnosticBuilder, Handler};
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
use syntax::parse::{self, ParseSess};

use checkstyle::{output_footer, output_header};
Expand Down
12 changes: 6 additions & 6 deletions src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,18 @@ impl ReorderableItemKind {

pub fn is_reorderable(&self, config: &Config) -> bool {
match *self {
ReorderableItemKind::ExternCrate => config.reorder_extern_crates(),
ReorderableItemKind::ExternCrate => config.reorder_imports(),
ReorderableItemKind::Mod => config.reorder_modules(),
ReorderableItemKind::Use => config.reorder_imports(),
ReorderableItemKind::Other => false,
}
}

pub fn in_group(&self, config: &Config) -> bool {
pub fn in_group(&self) -> bool {
match *self {
ReorderableItemKind::ExternCrate => config.reorder_extern_crates_in_group(),
ReorderableItemKind::Mod => config.reorder_modules(),
ReorderableItemKind::Use => config.reorder_imports_in_group(),
ReorderableItemKind::ExternCrate => false,
ReorderableItemKind::Mod => true,
ReorderableItemKind::Use => true,
ReorderableItemKind::Other => false,
}
}
Expand Down Expand Up @@ -268,7 +268,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let item_kind = ReorderableItemKind::from(items[0]);
if item_kind.is_reorderable(self.config) {
let visited_items_num =
self.walk_reorderable_items(items, item_kind, item_kind.in_group(self.config));
self.walk_reorderable_items(items, item_kind, item_kind.in_group());
let (_, rest) = items.split_at(visited_items_num);
items = rest;
} else {
Expand Down
13 changes: 5 additions & 8 deletions tests/target/extern.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// rustfmt-normalize_comments: true

extern crate foo;
extern crate foo as bar;

extern crate bar;
extern crate chrono;
extern crate dotenv;
extern crate futures;

extern crate bar;
extern crate foo;

// #2315
extern crate foo;
extern crate foo as bar;
extern crate futures;
extern crate proc_macro;
// #2315
extern crate proc_macro2;

extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/import-fencepost-length.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
use aaaaaaaaaaaaaaa::{bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, dddddddd};
use aaaaaaaaaaaaaaa::{bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc,
ddddddddd};
use aaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
2 changes: 1 addition & 1 deletion tests/target/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ use foo::{baz, qux as bar};

// With absolute paths
use foo;
use Foo;
use foo::Bar;
use foo::{Bar, Baz};
use Foo;
use {Bar, Baz};

// Root globs
Expand Down

0 comments on commit 8ae87c3

Please sign in to comment.