mirror of
https://github.com/rust-lang/rust
synced 2024-10-03 23:35:18 +00:00
Add ImportGranularity::Guess
This commit is contained in:
parent
b8a99692d1
commit
5fd9f6c7b9
|
@ -18,6 +18,8 @@
|
||||||
/// How imports should be grouped into use statements.
|
/// How imports should be grouped into use statements.
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum ImportGranularity {
|
pub enum ImportGranularity {
|
||||||
|
/// Try to guess the granularity of imports on a per module basis by observing the existing imports.
|
||||||
|
Guess,
|
||||||
/// Do not change the granularity of any imports and preserve the original structure written by the developer.
|
/// Do not change the granularity of any imports and preserve the original structure written by the developer.
|
||||||
Preserve,
|
Preserve,
|
||||||
/// Merge imports from the same crate into a single use statement.
|
/// Merge imports from the same crate into a single use statement.
|
||||||
|
@ -28,16 +30,6 @@ pub enum ImportGranularity {
|
||||||
Item,
|
Item,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ImportGranularity {
|
|
||||||
pub fn merge_behavior(self) -> Option<MergeBehavior> {
|
|
||||||
match self {
|
|
||||||
ImportGranularity::Crate => Some(MergeBehavior::Crate),
|
|
||||||
ImportGranularity::Module => Some(MergeBehavior::Module),
|
|
||||||
ImportGranularity::Preserve | ImportGranularity::Item => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub struct InsertUseConfig {
|
pub struct InsertUseConfig {
|
||||||
pub granularity: ImportGranularity,
|
pub granularity: ImportGranularity,
|
||||||
|
@ -118,10 +110,10 @@ fn guess_merge_behavior_from_scope(&self) -> Option<MergeBehavior> {
|
||||||
pub fn insert_use<'a>(scope: &ImportScope, path: ast::Path, cfg: InsertUseConfig) {
|
pub fn insert_use<'a>(scope: &ImportScope, path: ast::Path, cfg: InsertUseConfig) {
|
||||||
let _p = profile::span("insert_use");
|
let _p = profile::span("insert_use");
|
||||||
let mb = match cfg.granularity {
|
let mb = match cfg.granularity {
|
||||||
ImportGranularity::Preserve => scope.guess_merge_behavior_from_scope(),
|
ImportGranularity::Guess => scope.guess_merge_behavior_from_scope(),
|
||||||
ImportGranularity::Crate => Some(MergeBehavior::Crate),
|
ImportGranularity::Crate => Some(MergeBehavior::Crate),
|
||||||
ImportGranularity::Module => Some(MergeBehavior::Module),
|
ImportGranularity::Module => Some(MergeBehavior::Module),
|
||||||
ImportGranularity::Item => None,
|
ImportGranularity::Item | ImportGranularity::Preserve => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let use_item =
|
let use_item =
|
||||||
|
|
|
@ -36,7 +36,7 @@ struct ConfigData {
|
||||||
/// The strategy to use when inserting new imports or merging imports.
|
/// The strategy to use when inserting new imports or merging imports.
|
||||||
assist_importGranularity |
|
assist_importGranularity |
|
||||||
assist_importMergeBehavior |
|
assist_importMergeBehavior |
|
||||||
assist_importMergeBehaviour: ImportGranularityDef = "\"preserve\"",
|
assist_importMergeBehaviour: ImportGranularityDef = "\"guess\"",
|
||||||
/// The path structure for newly inserted paths to use.
|
/// The path structure for newly inserted paths to use.
|
||||||
assist_importPrefix: ImportPrefixDef = "\"plain\"",
|
assist_importPrefix: ImportPrefixDef = "\"plain\"",
|
||||||
/// Group inserted imports by the [following order](https://rust-analyzer.github.io/manual.html#auto-import). Groups are separated by newlines.
|
/// Group inserted imports by the [following order](https://rust-analyzer.github.io/manual.html#auto-import). Groups are separated by newlines.
|
||||||
|
@ -610,6 +610,7 @@ pub fn inlay_hints(&self) -> InlayHintsConfig {
|
||||||
fn insert_use_config(&self) -> InsertUseConfig {
|
fn insert_use_config(&self) -> InsertUseConfig {
|
||||||
InsertUseConfig {
|
InsertUseConfig {
|
||||||
granularity: match self.data.assist_importGranularity {
|
granularity: match self.data.assist_importGranularity {
|
||||||
|
ImportGranularityDef::Guess => ImportGranularity::Guess,
|
||||||
ImportGranularityDef::Preserve => ImportGranularity::Preserve,
|
ImportGranularityDef::Preserve => ImportGranularity::Preserve,
|
||||||
ImportGranularityDef::Item => ImportGranularity::Item,
|
ImportGranularityDef::Item => ImportGranularity::Item,
|
||||||
ImportGranularityDef::Crate => ImportGranularity::Crate,
|
ImportGranularityDef::Crate => ImportGranularity::Crate,
|
||||||
|
@ -719,9 +720,10 @@ enum ManifestOrProjectJson {
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
enum ImportGranularityDef {
|
enum ImportGranularityDef {
|
||||||
|
Preserve,
|
||||||
|
Guess,
|
||||||
#[serde(alias = "none")]
|
#[serde(alias = "none")]
|
||||||
Item,
|
Item,
|
||||||
Preserve,
|
|
||||||
#[serde(alias = "full")]
|
#[serde(alias = "full")]
|
||||||
Crate,
|
Crate,
|
||||||
#[serde(alias = "last")]
|
#[serde(alias = "last")]
|
||||||
|
|
|
@ -387,15 +387,17 @@
|
||||||
"$generated-start": false,
|
"$generated-start": false,
|
||||||
"rust-analyzer.assist.importGranularity": {
|
"rust-analyzer.assist.importGranularity": {
|
||||||
"markdownDescription": "How imports should be grouped into use statements.",
|
"markdownDescription": "How imports should be grouped into use statements.",
|
||||||
"default": "preserve",
|
"default": "guess",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": [
|
"enum": [
|
||||||
|
"guess",
|
||||||
"preserve",
|
"preserve",
|
||||||
"crate",
|
"crate",
|
||||||
"module",
|
"module",
|
||||||
"item"
|
"item"
|
||||||
],
|
],
|
||||||
"enumDescriptions": [
|
"enumDescriptions": [
|
||||||
|
"Try to guess the granularity of imports on a per module basis by observing the existing imports.",
|
||||||
"Do not change the granularity of any imports and preserve the original structure written by the developer.",
|
"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 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.",
|
"Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
|
||||||
|
|
Loading…
Reference in a new issue