From 622d040ba5192bc17f0724a8d970a779002802e1 Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Thu, 31 Dec 2020 17:53:00 -0800 Subject: [PATCH] Rename to imports_granularity --- Configurations.md | 6 +-- src/config.rs | 45 ++++++++++--------- src/config/config_type.rs | 10 ++--- src/config/options.rs | 2 +- src/formatting/reorder.rs | 10 ++--- src/rustfmt/main.rs | 2 +- .../StdExternalCrate-merge_imports.rs | 2 +- .../configs/imports_layout/merge_mixed.rs | 2 +- ..._crate.rs => imports_granularity_crate.rs} | 2 +- ...odule.rs => imports_granularity_module.rs} | 2 +- tests/source/issue-3750.rs | 2 +- .../StdExternalCrate-merge_imports.rs | 2 +- .../configs/imports_layout/merge_mixed.rs | 2 +- ..._crate.rs => imports_granularity_crate.rs} | 2 +- ...odule.rs => imports_granularity_module.rs} | 2 +- tests/target/issue-3750.rs | 2 +- 16 files changed, 48 insertions(+), 47 deletions(-) rename tests/source/{imports_merge_style_crate.rs => imports_granularity_crate.rs} (93%) rename tests/source/{imports_merge_style_module.rs => imports_granularity_module.rs} (86%) rename tests/target/{imports_merge_style_crate.rs => imports_granularity_crate.rs} (91%) rename tests/target/{imports_merge_style_module.rs => imports_granularity_module.rs} (86%) diff --git a/Configurations.md b/Configurations.md index e8d8b187903..4833a6ecbc4 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1705,7 +1705,7 @@ pub enum Foo {} pub enum Foo {} ``` -## `imports_merge_style` +## `imports_granularity` Merge together related imports based on their paths. @@ -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` @@ -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` diff --git a/src/config.rs b/src/config.rs index bc4e293dae6..8d2bbe7305f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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"; @@ -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 \ @@ -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 @@ -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] @@ -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] @@ -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] @@ -1055,7 +1056,7 @@ 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] @@ -1063,10 +1064,10 @@ ignore = [] 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] @@ -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); } } } diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 1b903d6b770..75d3521bcc3 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -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 }; } } diff --git a/src/config/options.rs b/src/config/options.rs index ae5eafa2dcf..f09481898c7 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -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. diff --git a/src/formatting/reorder.rs b/src/formatting/reorder.rs index 0a01a670729..9d1777d82df 100644 --- a/src/formatting/reorder.rs +++ b/src/formatting/reorder.rs @@ -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::{ @@ -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() { diff --git a/src/rustfmt/main.rs b/src/rustfmt/main.rs index ea2f705b7fb..571eb072b2c 100644 --- a/src/rustfmt/main.rs +++ b/src/rustfmt/main.rs @@ -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>, diff --git a/tests/source/configs/group_imports/StdExternalCrate-merge_imports.rs b/tests/source/configs/group_imports/StdExternalCrate-merge_imports.rs index a3c7be06a0d..ea7f6280a64 100644 --- a/tests/source/configs/group_imports/StdExternalCrate-merge_imports.rs +++ b/tests/source/configs/group_imports/StdExternalCrate-merge_imports.rs @@ -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; diff --git a/tests/source/configs/imports_layout/merge_mixed.rs b/tests/source/configs/imports_layout/merge_mixed.rs index 48e22764470..477c4aa1684 100644 --- a/tests/source/configs/imports_layout/merge_mixed.rs +++ b/tests/source/configs/imports_layout/merge_mixed.rs @@ -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}; diff --git a/tests/source/imports_merge_style_crate.rs b/tests/source/imports_granularity_crate.rs similarity index 93% rename from tests/source/imports_merge_style_crate.rs rename to tests/source/imports_granularity_crate.rs index 51552a0bfb5..d16681b01b5 100644 --- a/tests/source/imports_merge_style_crate.rs +++ b/tests/source/imports_granularity_crate.rs @@ -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}; diff --git a/tests/source/imports_merge_style_module.rs b/tests/source/imports_granularity_module.rs similarity index 86% rename from tests/source/imports_merge_style_module.rs rename to tests/source/imports_granularity_module.rs index a02bf718003..5a4fad5872b 100644 --- a/tests/source/imports_merge_style_module.rs +++ b/tests/source/imports_granularity_module.rs @@ -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}}; diff --git a/tests/source/issue-3750.rs b/tests/source/issue-3750.rs index c5f0556eeef..1189a99d2b6 100644 --- a/tests/source/issue-3750.rs +++ b/tests/source/issue-3750.rs @@ -1,4 +1,4 @@ -// rustfmt-imports_merge_style: Crate +// rustfmt-imports_granularity: Crate pub mod foo { pub mod bar { diff --git a/tests/target/configs/group_imports/StdExternalCrate-merge_imports.rs b/tests/target/configs/group_imports/StdExternalCrate-merge_imports.rs index 60333c5806c..5e4064dd811 100644 --- a/tests/target/configs/group_imports/StdExternalCrate-merge_imports.rs +++ b/tests/target/configs/group_imports/StdExternalCrate-merge_imports.rs @@ -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; diff --git a/tests/target/configs/imports_layout/merge_mixed.rs b/tests/target/configs/imports_layout/merge_mixed.rs index c429c3e972b..bc0da92fffb 100644 --- a/tests/target/configs/imports_layout/merge_mixed.rs +++ b/tests/target/configs/imports_layout/merge_mixed.rs @@ -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}; diff --git a/tests/target/imports_merge_style_crate.rs b/tests/target/imports_granularity_crate.rs similarity index 91% rename from tests/target/imports_merge_style_crate.rs rename to tests/target/imports_granularity_crate.rs index 1268d8522bd..d75906d30f1 100644 --- a/tests/target/imports_merge_style_crate.rs +++ b/tests/target/imports_granularity_crate.rs @@ -1,4 +1,4 @@ -// rustfmt-imports_merge_style: Crate +// rustfmt-imports_granularity: Crate use a::{a, b, c, d, e, f, g}; diff --git a/tests/target/imports_merge_style_module.rs b/tests/target/imports_granularity_module.rs similarity index 86% rename from tests/target/imports_merge_style_module.rs rename to tests/target/imports_granularity_module.rs index c1e4a2cfebb..9c1387c466a 100644 --- a/tests/target/imports_merge_style_module.rs +++ b/tests/target/imports_granularity_module.rs @@ -1,4 +1,4 @@ -// rustfmt-imports_merge_style: Module +// rustfmt-imports_granularity: Module use a::b::c; use a::d::e; diff --git a/tests/target/issue-3750.rs b/tests/target/issue-3750.rs index f9ddfa5baff..6875f8d3897 100644 --- a/tests/target/issue-3750.rs +++ b/tests/target/issue-3750.rs @@ -1,4 +1,4 @@ -// rustfmt-imports_merge_style: Crate +// rustfmt-imports_granularity: Crate pub mod foo { pub mod bar {