Skip to content

Commit c13b877

Browse files
Deprecate preserve import granularity option
It didn't do anything (behaved like `item`), as with `enforceGranularity = false` (which is the default), the style of the current file is always preferred, regardless of the setting. We could make it fail when the setting is `preserve` and the current file's style could not be detected, but that makes little sense. It is a bit weird that the default is `crate` but `preserve` falls back to `item`, however that was the previous behavior.
1 parent 8efe156 commit c13b877

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

src/tools/rust-analyzer/crates/ide-db/src/imports/insert_use.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ pub use hir::PrefixKind;
2727
/// How imports should be grouped into use statements.
2828
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2929
pub enum ImportGranularity {
30-
/// Do not change the granularity of any imports and preserve the original structure written
31-
/// by the developer.
32-
Preserve,
3330
/// Merge imports from the same crate into a single use statement.
3431
Crate,
3532
/// Merge imports from the same module into a single use statement.
@@ -174,7 +171,7 @@ fn insert_use_with_alias_option(
174171
ImportGranularity::Crate => Some(MergeBehavior::Crate),
175172
ImportGranularity::Module => Some(MergeBehavior::Module),
176173
ImportGranularity::One => Some(MergeBehavior::One),
177-
ImportGranularity::Item | ImportGranularity::Preserve => None,
174+
ImportGranularity::Item => None,
178175
};
179176
if !cfg.enforce_granularity {
180177
let file_granularity = guess_granularity_from_scope(scope);

src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl DiagnosticsConfig {
253253
style_lints: true,
254254
snippet_cap: SnippetCap::new(true),
255255
insert_use: InsertUseConfig {
256-
granularity: ImportGranularity::Preserve,
256+
granularity: ImportGranularity::Item,
257257
enforce_granularity: false,
258258
prefix_kind: PrefixKind::Plain,
259259
group: false,

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,8 +1944,9 @@ impl Config {
19441944
fn insert_use_config(&self, source_root: Option<SourceRootId>) -> InsertUseConfig {
19451945
InsertUseConfig {
19461946
granularity: match self.imports_granularity_group(source_root) {
1947-
ImportGranularityDef::Preserve => ImportGranularity::Preserve,
1948-
ImportGranularityDef::Item => ImportGranularity::Item,
1947+
ImportGranularityDef::Item | ImportGranularityDef::Preserve => {
1948+
ImportGranularity::Item
1949+
}
19491950
ImportGranularityDef::Crate => ImportGranularity::Crate,
19501951
ImportGranularityDef::Module => ImportGranularity::Module,
19511952
ImportGranularityDef::One => ImportGranularity::One,
@@ -3504,13 +3505,23 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
35043505
},
35053506
"ImportGranularityDef" => set! {
35063507
"type": "string",
3507-
"enum": ["preserve", "crate", "module", "item", "one"],
3508-
"enumDescriptions": [
3509-
"Do not change the granularity of any imports and preserve the original structure written by the developer.",
3510-
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
3511-
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
3512-
"Flatten imports so that each has its own use statement.",
3513-
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
3508+
"anyOf": [
3509+
{
3510+
"enum": ["crate", "module", "item", "one"],
3511+
"enumDescriptions": [
3512+
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
3513+
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
3514+
"Flatten imports so that each has its own use statement.",
3515+
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
3516+
],
3517+
},
3518+
{
3519+
"enum": ["preserve"],
3520+
"enumDescriptions": [
3521+
"Deprecated - unless `enforceGranularity` is `true`, the style of the current file is preferred over this setting. Behaves like `item`.",
3522+
],
3523+
"deprecated": true,
3524+
}
35143525
],
35153526
},
35163527
"ImportPrefixDef" => set! {

src/tools/rust-analyzer/editors/code/package.json

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,19 +2009,30 @@
20092009
"markdownDescription": "How imports should be grouped into use statements.",
20102010
"default": "crate",
20112011
"type": "string",
2012-
"enum": [
2013-
"preserve",
2014-
"crate",
2015-
"module",
2016-
"item",
2017-
"one"
2018-
],
2019-
"enumDescriptions": [
2020-
"Do not change the granularity of any imports and preserve the original structure written by the developer.",
2021-
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
2022-
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
2023-
"Flatten imports so that each has its own use statement.",
2024-
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
2012+
"anyOf": [
2013+
{
2014+
"enum": [
2015+
"crate",
2016+
"module",
2017+
"item",
2018+
"one"
2019+
],
2020+
"enumDescriptions": [
2021+
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
2022+
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
2023+
"Flatten imports so that each has its own use statement.",
2024+
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
2025+
]
2026+
},
2027+
{
2028+
"enum": [
2029+
"preserve"
2030+
],
2031+
"enumDescriptions": [
2032+
"Deprecated - unless `enforceGranularity` is `true`, the style of the current file is preferred over this setting. Behaves like `item`."
2033+
],
2034+
"deprecated": true
2035+
}
20252036
]
20262037
}
20272038
}

0 commit comments

Comments
 (0)