aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ide_assists/src/tests.rs7
-rw-r--r--crates/ide_completion/src/test_utils.rs7
-rw-r--r--crates/ide_db/src/helpers/insert_use.rs27
-rw-r--r--crates/ide_db/src/helpers/insert_use/tests.rs16
-rw-r--r--crates/rust-analyzer/src/config.rs25
-rw-r--r--crates/rust-analyzer/src/integrated_benchmarks.rs9
-rw-r--r--crates/rust-analyzer/src/to_proto.rs4
-rw-r--r--editors/code/package.json18
8 files changed, 75 insertions, 38 deletions
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs
index 0d3969c36..bb1ca0b3d 100644
--- a/crates/ide_assists/src/tests.rs
+++ b/crates/ide_assists/src/tests.rs
@@ -4,7 +4,10 @@ use expect_test::expect;
4use hir::Semantics; 4use hir::Semantics;
5use ide_db::{ 5use ide_db::{
6 base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}, 6 base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt},
7 helpers::{insert_use::InsertUseConfig, merge_imports::MergeBehavior, SnippetCap}, 7 helpers::{
8 insert_use::{ImportGranularity, InsertUseConfig},
9 SnippetCap,
10 },
8 source_change::FileSystemEdit, 11 source_change::FileSystemEdit,
9 RootDatabase, 12 RootDatabase,
10}; 13};
@@ -21,7 +24,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
21 snippet_cap: SnippetCap::new(true), 24 snippet_cap: SnippetCap::new(true),
22 allowed: None, 25 allowed: None,
23 insert_use: InsertUseConfig { 26 insert_use: InsertUseConfig {
24 merge: Some(MergeBehavior::Crate), 27 granularity: ImportGranularity::Crate,
25 prefix_kind: hir::PrefixKind::Plain, 28 prefix_kind: hir::PrefixKind::Plain,
26 group: true, 29 group: true,
27 }, 30 },
diff --git a/crates/ide_completion/src/test_utils.rs b/crates/ide_completion/src/test_utils.rs
index 939fb2d47..b150a5c3f 100644
--- a/crates/ide_completion/src/test_utils.rs
+++ b/crates/ide_completion/src/test_utils.rs
@@ -3,7 +3,10 @@
3use hir::{PrefixKind, Semantics}; 3use hir::{PrefixKind, Semantics};
4use ide_db::{ 4use ide_db::{
5 base_db::{fixture::ChangeFixture, FileLoader, FilePosition}, 5 base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
6 helpers::{insert_use::InsertUseConfig, merge_imports::MergeBehavior, SnippetCap}, 6 helpers::{
7 insert_use::{ImportGranularity, InsertUseConfig},
8 SnippetCap,
9 },
7 RootDatabase, 10 RootDatabase,
8}; 11};
9use itertools::Itertools; 12use itertools::Itertools;
@@ -20,7 +23,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
20 add_call_argument_snippets: true, 23 add_call_argument_snippets: true,
21 snippet_cap: SnippetCap::new(true), 24 snippet_cap: SnippetCap::new(true),
22 insert_use: InsertUseConfig { 25 insert_use: InsertUseConfig {
23 merge: Some(MergeBehavior::Crate), 26 granularity: ImportGranularity::Crate,
24 prefix_kind: PrefixKind::Plain, 27 prefix_kind: PrefixKind::Plain,
25 group: true, 28 group: true,
26 }, 29 },
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
16pub use hir::PrefixKind; 16pub use hir::PrefixKind;
17 17
18/// How imports should be grouped into use statements.
19#[derive(Copy, Clone, Debug, PartialEq, Eq)]
20pub 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
31impl 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)]
19pub struct InsertUseConfig { 42pub 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());
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs
index 248227d29..f99857a89 100644
--- a/crates/ide_db/src/helpers/insert_use/tests.rs
+++ b/crates/ide_db/src/helpers/insert_use/tests.rs
@@ -21,7 +21,7 @@ use crate::bar::A;
21use self::bar::A; 21use self::bar::A;
22use super::bar::A; 22use super::bar::A;
23use external_crate2::bar::A;", 23use external_crate2::bar::A;",
24 None, 24 ImportGranularity::Item,
25 false, 25 false,
26 false, 26 false,
27 ); 27 );
@@ -36,7 +36,7 @@ fn insert_not_group_empty() {
36 r"use external_crate2::bar::A; 36 r"use external_crate2::bar::A;
37 37
38", 38",
39 None, 39 ImportGranularity::Item,
40 false, 40 false,
41 false, 41 false,
42 ); 42 );
@@ -281,7 +281,7 @@ fn insert_empty_module() {
281 r"{ 281 r"{
282 use foo::bar; 282 use foo::bar;
283}", 283}",
284 None, 284 ImportGranularity::Item,
285 true, 285 true,
286 true, 286 true,
287 ) 287 )
@@ -635,7 +635,7 @@ fn check(
635 path: &str, 635 path: &str,
636 ra_fixture_before: &str, 636 ra_fixture_before: &str,
637 ra_fixture_after: &str, 637 ra_fixture_after: &str,
638 mb: Option<MergeBehavior>, 638 granularity: ImportGranularity,
639 module: bool, 639 module: bool,
640 group: bool, 640 group: bool,
641) { 641) {
@@ -651,21 +651,21 @@ fn check(
651 .find_map(ast::Path::cast) 651 .find_map(ast::Path::cast)
652 .unwrap(); 652 .unwrap();
653 653
654 insert_use(&file, path, InsertUseConfig { merge: mb, prefix_kind: PrefixKind::Plain, group }); 654 insert_use(&file, path, InsertUseConfig { granularity, prefix_kind: PrefixKind::Plain, group });
655 let result = file.as_syntax_node().to_string(); 655 let result = file.as_syntax_node().to_string();
656 assert_eq_text!(ra_fixture_after, &result); 656 assert_eq_text!(ra_fixture_after, &result);
657} 657}
658 658
659fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 659fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
660 check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehavior::Crate), false, true) 660 check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true)
661} 661}
662 662
663fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 663fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
664 check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehavior::Module), false, true) 664 check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true)
665} 665}
666 666
667fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 667fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
668 check(path, ra_fixture_before, ra_fixture_after, None, false, true) 668 check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true)
669} 669}
670 670
671fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { 671fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index a3866c1ba..e72387257 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -12,8 +12,7 @@ use std::{ffi::OsString, iter, path::PathBuf};
12use flycheck::FlycheckConfig; 12use flycheck::FlycheckConfig;
13use ide::{AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig}; 13use ide::{AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig};
14use ide_db::helpers::{ 14use ide_db::helpers::{
15 insert_use::{InsertUseConfig, PrefixKind}, 15 insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
16 merge_imports::MergeBehavior,
17 SnippetCap, 16 SnippetCap,
18}; 17};
19use lsp_types::{ClientCapabilities, MarkupKind}; 18use lsp_types::{ClientCapabilities, MarkupKind};
@@ -35,8 +34,9 @@ use crate::{
35config_data! { 34config_data! {
36 struct ConfigData { 35 struct ConfigData {
37 /// The strategy to use when inserting new imports or merging imports. 36 /// The strategy to use when inserting new imports or merging imports.
37 assist_importGranularity |
38 assist_importMergeBehavior | 38 assist_importMergeBehavior |
39 assist_importMergeBehaviour: MergeBehaviorDef = "\"crate\"", 39 assist_importMergeBehaviour: ImportGranularityDef = "\"preserve\"",
40 /// The path structure for newly inserted paths to use. 40 /// The path structure for newly inserted paths to use.
41 assist_importPrefix: ImportPrefixDef = "\"plain\"", 41 assist_importPrefix: ImportPrefixDef = "\"plain\"",
42 /// Group inserted imports by the [following order](https://rust-analyzer.github.io/manual.html#auto-import). Groups are separated by newlines. 42 /// Group inserted imports by the [following order](https://rust-analyzer.github.io/manual.html#auto-import). Groups are separated by newlines.
@@ -609,10 +609,11 @@ impl Config {
609 } 609 }
610 fn insert_use_config(&self) -> InsertUseConfig { 610 fn insert_use_config(&self) -> InsertUseConfig {
611 InsertUseConfig { 611 InsertUseConfig {
612 merge: match self.data.assist_importMergeBehavior { 612 granularity: match self.data.assist_importGranularity {
613 MergeBehaviorDef::None => None, 613 ImportGranularityDef::Preserve => ImportGranularity::Preserve,
614 MergeBehaviorDef::Crate => Some(MergeBehavior::Crate), 614 ImportGranularityDef::Item => ImportGranularity::Item,
615 MergeBehaviorDef::Module => Some(MergeBehavior::Module), 615 ImportGranularityDef::Crate => ImportGranularity::Crate,
616 ImportGranularityDef::Module => ImportGranularity::Module,
616 }, 617 },
617 prefix_kind: match self.data.assist_importPrefix { 618 prefix_kind: match self.data.assist_importPrefix {
618 ImportPrefixDef::Plain => PrefixKind::Plain, 619 ImportPrefixDef::Plain => PrefixKind::Plain,
@@ -717,8 +718,10 @@ enum ManifestOrProjectJson {
717 718
718#[derive(Deserialize, Debug, Clone)] 719#[derive(Deserialize, Debug, Clone)]
719#[serde(rename_all = "snake_case")] 720#[serde(rename_all = "snake_case")]
720enum MergeBehaviorDef { 721enum ImportGranularityDef {
721 None, 722 #[serde(alias = "none")]
723 Item,
724 Preserve,
722 #[serde(alias = "full")] 725 #[serde(alias = "full")]
723 Crate, 726 Crate,
724 #[serde(alias = "last")] 727 #[serde(alias = "last")]
@@ -737,7 +740,7 @@ macro_rules! _config_data {
737 (struct $name:ident { 740 (struct $name:ident {
738 $( 741 $(
739 $(#[doc=$doc:literal])* 742 $(#[doc=$doc:literal])*
740 $field:ident $(| $alias:ident)?: $ty:ty = $default:expr, 743 $field:ident $(| $alias:ident)*: $ty:ty = $default:expr,
741 )* 744 )*
742 }) => { 745 }) => {
743 #[allow(non_snake_case)] 746 #[allow(non_snake_case)]
@@ -749,7 +752,7 @@ macro_rules! _config_data {
749 $field: get_field( 752 $field: get_field(
750 &mut json, 753 &mut json,
751 stringify!($field), 754 stringify!($field),
752 None$(.or(Some(stringify!($alias))))?, 755 None$(.or(Some(stringify!($alias))))*,
753 $default, 756 $default,
754 ), 757 ),
755 )*} 758 )*}
diff --git a/crates/rust-analyzer/src/integrated_benchmarks.rs b/crates/rust-analyzer/src/integrated_benchmarks.rs
index ba2790acb..cd0b8d559 100644
--- a/crates/rust-analyzer/src/integrated_benchmarks.rs
+++ b/crates/rust-analyzer/src/integrated_benchmarks.rs
@@ -13,7 +13,10 @@
13use std::{convert::TryFrom, sync::Arc}; 13use std::{convert::TryFrom, sync::Arc};
14 14
15use ide::{Change, CompletionConfig, FilePosition, TextSize}; 15use ide::{Change, CompletionConfig, FilePosition, TextSize};
16use ide_db::helpers::{insert_use::InsertUseConfig, merge_imports::MergeBehavior, SnippetCap}; 16use ide_db::helpers::{
17 insert_use::{ImportGranularity, InsertUseConfig},
18 SnippetCap,
19};
17use test_utils::project_root; 20use test_utils::project_root;
18use vfs::{AbsPathBuf, VfsPath}; 21use vfs::{AbsPathBuf, VfsPath};
19 22
@@ -133,7 +136,7 @@ fn integrated_completion_benchmark() {
133 add_call_argument_snippets: true, 136 add_call_argument_snippets: true,
134 snippet_cap: SnippetCap::new(true), 137 snippet_cap: SnippetCap::new(true),
135 insert_use: InsertUseConfig { 138 insert_use: InsertUseConfig {
136 merge: Some(MergeBehavior::Crate), 139 granularity: ImportGranularity::Crate,
137 prefix_kind: hir::PrefixKind::ByCrate, 140 prefix_kind: hir::PrefixKind::ByCrate,
138 group: true, 141 group: true,
139 }, 142 },
@@ -166,7 +169,7 @@ fn integrated_completion_benchmark() {
166 add_call_argument_snippets: true, 169 add_call_argument_snippets: true,
167 snippet_cap: SnippetCap::new(true), 170 snippet_cap: SnippetCap::new(true),
168 insert_use: InsertUseConfig { 171 insert_use: InsertUseConfig {
169 merge: Some(MergeBehavior::Crate), 172 granularity: ImportGranularity::Crate,
170 prefix_kind: hir::PrefixKind::ByCrate, 173 prefix_kind: hir::PrefixKind::ByCrate,
171 group: true, 174 group: true,
172 }, 175 },
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 9dec46c78..64d5f9e2e 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -1145,7 +1145,7 @@ mod tests {
1145 1145
1146 use ide::Analysis; 1146 use ide::Analysis;
1147 use ide_db::helpers::{ 1147 use ide_db::helpers::{
1148 insert_use::{InsertUseConfig, PrefixKind}, 1148 insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
1149 SnippetCap, 1149 SnippetCap,
1150 }; 1150 };
1151 1151
@@ -1177,7 +1177,7 @@ mod tests {
1177 add_call_argument_snippets: true, 1177 add_call_argument_snippets: true,
1178 snippet_cap: SnippetCap::new(true), 1178 snippet_cap: SnippetCap::new(true),
1179 insert_use: InsertUseConfig { 1179 insert_use: InsertUseConfig {
1180 merge: None, 1180 granularity: ImportGranularity::Item,
1181 prefix_kind: PrefixKind::Plain, 1181 prefix_kind: PrefixKind::Plain,
1182 group: true, 1182 group: true,
1183 }, 1183 },
diff --git a/editors/code/package.json b/editors/code/package.json
index 2e67b6775..81179ff9b 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -385,19 +385,21 @@
385 "markdownDescription": "Optional settings passed to the debug engine. Example: `{ \"lldb\": { \"terminal\":\"external\"} }`" 385 "markdownDescription": "Optional settings passed to the debug engine. Example: `{ \"lldb\": { \"terminal\":\"external\"} }`"
386 }, 386 },
387 "$generated-start": false, 387 "$generated-start": false,
388 "rust-analyzer.assist.importMergeBehavior": { 388 "rust-analyzer.assist.importGranularity": {
389 "markdownDescription": "The strategy to use when inserting new imports or merging imports.", 389 "markdownDescription": "How imports should be grouped into use statements.",
390 "default": "crate", 390 "default": "preserve",
391 "type": "string", 391 "type": "string",
392 "enum": [ 392 "enum": [
393 "none", 393 "preserve",
394 "crate", 394 "crate",
395 "module" 395 "module",
396 "item"
396 ], 397 ],
397 "enumDescriptions": [ 398 "enumDescriptions": [
398 "Do not merge imports at all.", 399 "Do not change the granularity of any imports and preserve the original structure written by the developer.",
399 "Merge imports from the same crate into a single `use` statement.", 400 "Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements.",
400 "Merge imports from the same module into a single `use` statement." 401 "Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements.",
402 "Flatten imports so that each has its own use statement."
401 ] 403 ]
402 }, 404 },
403 "rust-analyzer.assist.importPrefix": { 405 "rust-analyzer.assist.importPrefix": {