diff options
author | Lukas Tobias Wirth <[email protected]> | 2021-05-18 18:49:15 +0100 |
---|---|---|
committer | Lukas Tobias Wirth <[email protected]> | 2021-05-18 18:53:20 +0100 |
commit | 64f7072c255bd97a58b8344d0beeae281b8f7e13 (patch) | |
tree | a0e0b102c4880ff5c6b9c58a266479b513186acb /crates/ide_db/src/helpers/insert_use.rs | |
parent | e3d0d89d7e3e253234271008df9324ec1faf1746 (diff) |
MergeBehavior -> ImportGranularity
Diffstat (limited to 'crates/ide_db/src/helpers/insert_use.rs')
-rw-r--r-- | crates/ide_db/src/helpers/insert_use.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index 55cdc4da3..a4eb31815 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs | |||
@@ -15,9 +15,32 @@ use crate::{ | |||
15 | 15 | ||
16 | pub use hir::PrefixKind; | 16 | pub use hir::PrefixKind; |
17 | 17 | ||
18 | /// How imports should be grouped into use statements. | ||
19 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
20 | pub enum ImportGranularity { | ||
21 | /// Do not change the granularity of any imports and preserve the original structure written by the developer. | ||
22 | Preserve, | ||
23 | /// Merge imports from the same crate into a single use statement. | ||
24 | Crate, | ||
25 | /// Merge imports from the same module into a single use statement. | ||
26 | Module, | ||
27 | /// Flatten imports so that each has its own use statement. | ||
28 | Item, | ||
29 | } | ||
30 | |||
31 | impl ImportGranularity { | ||
32 | pub fn merge_behavior(self) -> Option<MergeBehavior> { | ||
33 | match self { | ||
34 | ImportGranularity::Crate => Some(MergeBehavior::Crate), | ||
35 | ImportGranularity::Module => Some(MergeBehavior::Module), | ||
36 | ImportGranularity::Preserve | ImportGranularity::Item => None, | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
18 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 41 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
19 | pub struct InsertUseConfig { | 42 | pub struct InsertUseConfig { |
20 | pub merge: Option<MergeBehavior>, | 43 | pub granularity: ImportGranularity, |
21 | pub prefix_kind: PrefixKind, | 44 | pub prefix_kind: PrefixKind, |
22 | pub group: bool, | 45 | pub group: bool, |
23 | } | 46 | } |
@@ -73,7 +96,7 @@ pub fn insert_use<'a>(scope: &ImportScope, path: ast::Path, cfg: InsertUseConfig | |||
73 | let use_item = | 96 | let use_item = |
74 | make::use_(None, make::use_tree(path.clone(), None, None, false)).clone_for_update(); | 97 | make::use_(None, make::use_tree(path.clone(), None, None, false)).clone_for_update(); |
75 | // merge into existing imports if possible | 98 | // merge into existing imports if possible |
76 | if let Some(mb) = cfg.merge { | 99 | if let Some(mb) = cfg.granularity.merge_behavior() { |
77 | for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) { | 100 | for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) { |
78 | if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) { | 101 | if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) { |
79 | ted::replace(existing_use.syntax(), merged.syntax()); | 102 | ted::replace(existing_use.syntax(), merged.syntax()); |