Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions crates/ide-db/src/imports/insert_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ pub use hir::PrefixKind;
/// How imports should be grouped into use statements.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ImportGranularity {
/// Do not change the granularity of any imports and preserve the original structure written
/// by the developer.
Preserve,
/// Merge imports from the same crate into a single use statement.
Crate,
/// Merge imports from the same module into a single use statement.
Expand Down Expand Up @@ -174,17 +171,26 @@ fn insert_use_with_alias_option(
ImportGranularity::Crate => Some(MergeBehavior::Crate),
ImportGranularity::Module => Some(MergeBehavior::Module),
ImportGranularity::One => Some(MergeBehavior::One),
ImportGranularity::Item | ImportGranularity::Preserve => None,
ImportGranularity::Item => None,
};
if !cfg.enforce_granularity {
let file_granularity = guess_granularity_from_scope(scope);
mb = match file_granularity {
ImportGranularityGuess::Unknown => mb,
ImportGranularityGuess::Item => None,
ImportGranularityGuess::Module => Some(MergeBehavior::Module),
ImportGranularityGuess::ModuleOrItem => mb.and(Some(MergeBehavior::Module)),
// We use the user's setting to infer if this is module or item.
ImportGranularityGuess::ModuleOrItem => match mb {
Some(MergeBehavior::Module) | None => mb,
// There isn't really a way to decide between module or item here, so we just pick one.
// FIXME: Maybe it is possible to infer based on semantic analysis?
Some(MergeBehavior::One | MergeBehavior::Crate) => Some(MergeBehavior::Module),
},
ImportGranularityGuess::Crate => Some(MergeBehavior::Crate),
ImportGranularityGuess::CrateOrModule => mb.or(Some(MergeBehavior::Crate)),
ImportGranularityGuess::CrateOrModule => match mb {
Some(MergeBehavior::Crate | MergeBehavior::Module) => mb,
Some(MergeBehavior::One) | None => Some(MergeBehavior::Crate),
},
ImportGranularityGuess::One => Some(MergeBehavior::One),
};
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl DiagnosticsConfig {
style_lints: true,
snippet_cap: SnippetCap::new(true),
insert_use: InsertUseConfig {
granularity: ImportGranularity::Preserve,
granularity: ImportGranularity::Item,
enforce_granularity: false,
prefix_kind: PrefixKind::Plain,
group: false,
Expand Down
29 changes: 20 additions & 9 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,8 +1944,9 @@ impl Config {
fn insert_use_config(&self, source_root: Option<SourceRootId>) -> InsertUseConfig {
InsertUseConfig {
granularity: match self.imports_granularity_group(source_root) {
ImportGranularityDef::Preserve => ImportGranularity::Preserve,
ImportGranularityDef::Item => ImportGranularity::Item,
ImportGranularityDef::Item | ImportGranularityDef::Preserve => {
ImportGranularity::Item
}
ImportGranularityDef::Crate => ImportGranularity::Crate,
ImportGranularityDef::Module => ImportGranularity::Module,
ImportGranularityDef::One => ImportGranularity::One,
Expand Down Expand Up @@ -3504,13 +3505,23 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
},
"ImportGranularityDef" => set! {
"type": "string",
"enum": ["preserve", "crate", "module", "item", "one"],
"enumDescriptions": [
"Do not change the granularity of any imports and preserve the original structure written by the developer.",
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
"Flatten imports so that each has its own use statement.",
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
"anyOf": [
{
"enum": ["crate", "module", "item", "one"],
"enumDescriptions": [
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
"Flatten imports so that each has its own use statement.",
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
],
},
{
"enum": ["preserve"],
"enumDescriptions": [
"Deprecated - unless `enforceGranularity` is `true`, the style of the current file is preferred over this setting. Behaves like `item`.",
],
"deprecated": true,
}
],
},
"ImportPrefixDef" => set! {
Expand Down
37 changes: 24 additions & 13 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2009,19 +2009,30 @@
"markdownDescription": "How imports should be grouped into use statements.",
"default": "crate",
"type": "string",
"enum": [
"preserve",
"crate",
"module",
"item",
"one"
],
"enumDescriptions": [
"Do not change the granularity of any imports and preserve the original structure written by the developer.",
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
"Flatten imports so that each has its own use statement.",
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
"anyOf": [
{
"enum": [
"crate",
"module",
"item",
"one"
],
"enumDescriptions": [
"Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
"Flatten imports so that each has its own use statement.",
"Merge all imports into a single use statement as long as they have the same visibility and attributes."
]
},
{
"enum": [
"preserve"
],
"enumDescriptions": [
"Deprecated - unless `enforceGranularity` is `true`, the style of the current file is preferred over this setting. Behaves like `item`."
],
"deprecated": true
}
]
}
}
Expand Down