Skip to content

Commit 65920fe

Browse files
authored
Merge pull request rust-lang#20801 from ChayimFriedman2/fix-insert-use
minor: Small fixes for import insertion
2 parents 31ffd29 + a958b84 commit 65920fe

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

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

Lines changed: 12 additions & 6 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,17 +171,26 @@ 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);
181178
mb = match file_granularity {
182179
ImportGranularityGuess::Unknown => mb,
183180
ImportGranularityGuess::Item => None,
184181
ImportGranularityGuess::Module => Some(MergeBehavior::Module),
185-
ImportGranularityGuess::ModuleOrItem => mb.and(Some(MergeBehavior::Module)),
182+
// We use the user's setting to infer if this is module or item.
183+
ImportGranularityGuess::ModuleOrItem => match mb {
184+
Some(MergeBehavior::Module) | None => mb,
185+
// There isn't really a way to decide between module or item here, so we just pick one.
186+
// FIXME: Maybe it is possible to infer based on semantic analysis?
187+
Some(MergeBehavior::One | MergeBehavior::Crate) => Some(MergeBehavior::Module),
188+
},
186189
ImportGranularityGuess::Crate => Some(MergeBehavior::Crate),
187-
ImportGranularityGuess::CrateOrModule => mb.or(Some(MergeBehavior::Crate)),
190+
ImportGranularityGuess::CrateOrModule => match mb {
191+
Some(MergeBehavior::Crate | MergeBehavior::Module) => mb,
192+
Some(MergeBehavior::One) | None => Some(MergeBehavior::Crate),
193+
},
188194
ImportGranularityGuess::One => Some(MergeBehavior::One),
189195
};
190196
}

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)