Skip to content

Commit

Permalink
Change None -> Preserve for group_imports config
Browse files Browse the repository at this point in the history
Also reword configuration to be less specific.
  • Loading branch information
MattX committed Oct 4, 2020
1 parent 4f4acf7 commit ee577df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
22 changes: 11 additions & 11 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2010,20 +2010,15 @@ use sit;

## `group_imports`

Discard existing import groups, and create three groups for:
1. `std`, `core` and `alloc`,
2. external crates,
3. `self`, `super` and `crate` imports.

Within each group, imports are sorted as with `reorder_imports`.

This has no effect is `reorder_imports` is `false`.
Controls the strategy for how imports are grouped together.

- **Default value**: `None`
- **Possible values**: `None`, `StdExternalCrate`
- **Default value**: `Preserve`
- **Possible values**: `Preserve`, `StdExternalCrate`
- **Stable**: No

#### `None` (default):
#### `Preserve` (default):

Preserve the source file's import groups.

```rust
use super::update::convert_publish_payload;
Expand All @@ -2044,6 +2039,11 @@ use core::f32;

#### `StdExternalCrate`:

Discard existing import groups, and create three groups for:
1. `std`, `core` and `alloc`,
2. external crates,
3. `self`, `super` and `crate` imports.

```rust
use alloc::alloc::Layout;
use core::f32;
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ create_config! {
imports_indent: IndentStyle, IndentStyle::Block, false, "Indent of imports";
imports_layout: ListTactic, ListTactic::Mixed, false, "Item layout inside a import block";
merge_imports: bool, false, false, "Merge imports";
group_imports: GroupImportsTactic, GroupImportsTactic::None, false,
group_imports: GroupImportsTactic, GroupImportsTactic::Preserve, false,
"Reorganize import groups";

// Ordering
Expand Down Expand Up @@ -595,7 +595,7 @@ where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
merge_imports = false
group_imports = "None"
group_imports = "Preserve"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
Expand Down
2 changes: 1 addition & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Density {
/// Configuration for import groups, i.e. sets of imports separated by newlines.
pub enum GroupImportsTactic {
/// Keep groups as they are.
None,
Preserve,
/// Discard existing groups, and create new groups for
/// 1. `std` / `core` / `alloc` imports
/// 2. other imports
Expand Down
14 changes: 6 additions & 8 deletions src/formatting/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn rewrite_reorderable_item(

/// Rewrite a list of items with reordering and/or regrouping. Every item
/// in `items` must have the same `ast::ItemKind`. Whether reordering, regrouping,
/// or both ore done is determined from the `context`.
/// or both are done is determined from the `context`.
fn rewrite_reorderable_or_regroupable_items(
context: &RewriteContext<'_>,
reorderable_items: &[&ast::Item],
Expand Down Expand Up @@ -230,11 +230,9 @@ fn rewrite_reorderable_or_regroupable_items(
normalized_items = merge_use_trees(normalized_items);
}

let mut regrouped_items = if context.config.group_imports() != GroupImportsTactic::None
{
group_imports(normalized_items)
} else {
vec![normalized_items]
let mut regrouped_items = match context.config.group_imports() {
GroupImportsTactic::Preserve => vec![normalized_items],
GroupImportsTactic::StdExternalCrate => group_imports(normalized_items),
};

if context.config.reorder_imports() {
Expand Down Expand Up @@ -363,14 +361,14 @@ impl ReorderableItemKind {
ReorderableItemKind::ExternCrate
| ReorderableItemKind::Mod
| ReorderableItemKind::Other => false,
ReorderableItemKind::Use => config.group_imports() != GroupImportsTactic::None,
ReorderableItemKind::Use => config.group_imports() != GroupImportsTactic::Preserve,
}
}

fn in_group(self, config: &Config) -> bool {
match self {
ReorderableItemKind::ExternCrate | ReorderableItemKind::Mod => true,
ReorderableItemKind::Use => config.group_imports() == GroupImportsTactic::None,
ReorderableItemKind::Use => config.group_imports() == GroupImportsTactic::Preserve,
ReorderableItemKind::Other => false,
}
}
Expand Down

0 comments on commit ee577df

Please sign in to comment.