Skip to content

Commit

Permalink
Rename to imports_granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
goffrie committed Jan 9, 2021
1 parent 28c08e4 commit 46dba84
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 47 deletions.
6 changes: 3 additions & 3 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ pub enum Foo {}
pub enum Foo {}
```

## `imports_merge_style`
## `imports_granularity`

Merge together related imports based on their paths.

Expand Down Expand Up @@ -1751,7 +1751,7 @@ use qux::{h, i};

## `merge_imports`

This option is deprecated. Use `imports_merge_style = "Crate"` instead.
This option is deprecated. Use `imports_granularity = "Crate"` instead.

- **Default value**: `false`
- **Possible values**: `true`, `false`
Expand Down Expand Up @@ -2602,7 +2602,7 @@ Enable unstable features on stable and beta channels (unstable features are avai

For example:
```bash
rustfmt src/lib.rs --config unstable_features=true imports_merge_style=Crate
rustfmt src/lib.rs --config unstable_features=true imports_granularity=Crate
```

## `use_field_init_shorthand`
Expand Down
45 changes: 23 additions & 22 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ create_config! {
// Imports
imports_indent: IndentStyle, IndentStyle::Block, false, "Indent of imports";
imports_layout: ListTactic, ListTactic::Mixed, false, "Item layout inside a import block";
imports_merge_style: ImportMergeStyle, ImportMergeStyle::Preserve, false, "Merge imports";
imports_granularity: ImportGranularity, ImportGranularity::Preserve, false,
"Merge or split imports to the provided granularity";
group_imports: GroupImportsTactic, GroupImportsTactic::Preserve, false,
"Controls the strategy for how imports are grouped together";
merge_imports: bool, false, false, "(deprecated: use imports_merge_style instead)";
merge_imports: bool, false, false, "(deprecated: use imports_granularity instead)";

// Ordering
reorder_imports: bool, true, true, "Reorder import and extern crate statements alphabetically";
Expand Down Expand Up @@ -447,9 +448,9 @@ mod test {
single_line_if_else_max_width: usize, 50, true, "Maximum line length for single \
line if-else expressions. A value of zero means always break if-else expressions.";
// merge_imports deprecation
imports_merge_style: ImportMergeStyle, ImportMergeStyle::Preserve, false,
imports_granularity: ImportGranularity, ImportGranularity::Preserve, false,
"Merge imports";
merge_imports: bool, false, false, "(deprecated: use imports_merge_style instead)";
merge_imports: bool, false, false, "(deprecated: use imports_granularity instead)";

unstable_features: bool, false, true,
"Enables unstable features on stable and beta channels \
Expand Down Expand Up @@ -601,7 +602,7 @@ fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
imports_merge_style = "Preserve"
imports_granularity = "Preserve"
group_imports = "Preserve"
reorder_imports = true
reorder_modules = true
Expand Down Expand Up @@ -722,13 +723,13 @@ ignore = []
}
let toml = r#"
unstable_features = true
imports_merge_style = "Crate"
imports_granularity = "Crate"
"#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert_eq!(config.was_set().unstable_features(), true);
assert_eq!(config.was_set().imports_merge_style(), true);
assert_eq!(config.was_set().imports_granularity(), true);
assert_eq!(config.unstable_features(), true);
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Crate);
assert_eq!(config.imports_granularity(), ImportGranularity::Crate);
}

#[test]
Expand All @@ -738,9 +739,9 @@ ignore = []
return;
}
let config =
Config::from_toml("imports_merge_style = \"Crate\"", Path::new("")).unwrap();
assert_eq!(config.was_set().imports_merge_style(), false);
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Preserve);
Config::from_toml("imports_granularity = \"Crate\"", Path::new("")).unwrap();
assert_eq!(config.was_set().imports_granularity(), false);
assert_eq!(config.imports_granularity(), ImportGranularity::Preserve);
}

#[test]
Expand Down Expand Up @@ -785,12 +786,12 @@ ignore = []
}
let mut config = Config::default();
assert_eq!(config.unstable_features(), false);
config.override_value("imports_merge_style", "Crate");
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Preserve);
config.override_value("imports_granularity", "Crate");
assert_eq!(config.imports_granularity(), ImportGranularity::Preserve);
config.override_value("unstable_features", "true");
assert_eq!(config.unstable_features(), true);
config.override_value("imports_merge_style", "Crate");
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Crate);
config.override_value("imports_granularity", "Crate");
assert_eq!(config.imports_granularity(), ImportGranularity::Crate);
}

#[test]
Expand Down Expand Up @@ -1055,18 +1056,18 @@ ignore = []
merge_imports = true
"#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Crate);
assert_eq!(config.imports_granularity(), ImportGranularity::Crate);
}

#[test]
fn test_both_set() {
let toml = r#"
unstable_features = true
merge_imports = true
imports_merge_style = "Preserve"
imports_granularity = "Preserve"
"#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Preserve);
assert_eq!(config.imports_granularity(), ImportGranularity::Preserve);
}

#[test]
Expand All @@ -1076,20 +1077,20 @@ ignore = []
merge_imports = true
"#;
let mut config = Config::from_toml(toml, Path::new("")).unwrap();
config.override_value("imports_merge_style", "Preserve");
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Preserve);
config.override_value("imports_granularity", "Preserve");
assert_eq!(config.imports_granularity(), ImportGranularity::Preserve);
}

#[test]
fn test_old_overridden() {
let toml = r#"
unstable_features = true
imports_merge_style = "Module"
imports_granularity = "Module"
"#;
let mut config = Config::from_toml(toml, Path::new("")).unwrap();
config.override_value("merge_imports", "true");
// no effect: the new option always takes precedence
assert_eq!(config.imports_merge_style(), ImportMergeStyle::Module);
assert_eq!(config.imports_granularity(), ImportGranularity::Module);
}
}
}
10 changes: 5 additions & 5 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,13 @@ macro_rules! create_config {
if self.was_set().merge_imports() {
eprintln!(
"Warning: the `merge_imports` option is deprecated. \
Use `imports_merge_style=Crate` instead"
Use `imports_granularity=Crate` instead"
);
if !self.was_set().imports_merge_style() {
self.imports_merge_style.2 = if self.merge_imports() {
ImportMergeStyle::Crate
if !self.was_set().imports_granularity() {
self.imports_granularity.2 = if self.merge_imports() {
ImportGranularity::Crate
} else {
ImportMergeStyle::Preserve
ImportGranularity::Preserve
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub enum GroupImportsTactic {

#[config_type]
/// How to merge imports.
pub enum ImportMergeStyle {
pub enum ImportGranularity {
/// Do not merge imports.
Preserve,
/// Use one `use` statement per crate.
Expand Down
10 changes: 5 additions & 5 deletions src/formatting/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::cmp::{Ord, Ordering};
use rustc_ast::ast;
use rustc_span::{symbol::sym, Span};

use crate::config::{Config, GroupImportsTactic, ImportMergeStyle};
use crate::config::{Config, GroupImportsTactic, ImportGranularity};
use crate::formatting::imports::UseSegment;
use crate::formatting::modules::{get_mod_inner_attrs, FileModMap};
use crate::formatting::{
Expand Down Expand Up @@ -228,14 +228,14 @@ fn rewrite_reorderable_or_regroupable_items(
for (item, list_item) in normalized_items.iter_mut().zip(list_items) {
item.list_item = Some(list_item.clone());
}
match context.config.imports_merge_style() {
ImportMergeStyle::Crate => {
match context.config.imports_granularity() {
ImportGranularity::Crate => {
normalized_items = merge_use_trees(normalized_items, SharedPrefix::Crate)
}
ImportMergeStyle::Module => {
ImportGranularity::Module => {
normalized_items = merge_use_trees(normalized_items, SharedPrefix::Module)
}
ImportMergeStyle::Preserve => {}
ImportGranularity::Preserve => {}
}

let mut regrouped_items = match context.config.group_imports() {
Expand Down
2 changes: 1 addition & 1 deletion src/rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct Opt {
/// Set options from command line.
///
/// Set configuration options via command line by specifying a list of key-value pairs
/// separated by commas (e.g., rustfmt --config=max_width=100,imports_merge_style=Crate).
/// separated by commas (e.g., rustfmt --config=max_width=100,imports_granularity=Crate).
/// These settings precedes any other settings specified in configuration files.
#[structopt(long = "config")]
inline_config: Option<Vec<InlineConfig>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// rustfmt-group_imports: StdExternalCrate
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate
use chrono::Utc;
use super::update::convert_publish_payload;

Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs/imports_layout/merge_mixed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// rustfmt-imports_indent: Block
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate
// rustfmt-imports_layout: Mixed

use std::{fmt, io, str};
Expand Down
2 changes: 1 addition & 1 deletion tests/source/imports_merge_style_crate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate

use a::{c,d,b};
use a::{d, e, b, a, f};
Expand Down
2 changes: 1 addition & 1 deletion tests/source/imports_merge_style_module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-imports_merge_style: Module
// rustfmt-imports_granularity: Module

use a::{b::c, d::e};
use a::{f, g::{h, i}};
Expand Down
2 changes: 1 addition & 1 deletion tests/source/issue-3750.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate

pub mod foo {
pub mod bar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// rustfmt-group_imports: StdExternalCrate
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate
use alloc::{alloc::Layout, vec::Vec};
use core::f32;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs/imports_layout/merge_mixed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// rustfmt-imports_indent: Block
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate
// rustfmt-imports_layout: Mixed

use std::{fmt, io, str, str::FromStr};
2 changes: 1 addition & 1 deletion tests/target/imports_merge_style_crate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate

use a::{a, b, c, d, e, f, g};

Expand Down
2 changes: 1 addition & 1 deletion tests/target/imports_merge_style_module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-imports_merge_style: Module
// rustfmt-imports_granularity: Module

use a::b::c;
use a::d::e;
Expand Down
2 changes: 1 addition & 1 deletion tests/target/issue-3750.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-imports_merge_style: Crate
// rustfmt-imports_granularity: Crate

pub mod foo {
pub mod bar {
Expand Down

0 comments on commit 46dba84

Please sign in to comment.