aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-02-02 13:06:14 +0000
committerGitHub <[email protected]>2020-02-02 13:06:14 +0000
commite24829909adeccf4b40ab362d48cab4c5f1178dc (patch)
tree2cb1799491dd5a0f408ad749fbe27c565b0cfa7f /crates/ra_assists/src/assists
parentdce7dc44be948bb6b73b79ce284ec2eb83811ae8 (diff)
parent2ee94e3e24a8cda1594eadba9c64a553ec046818 (diff)
Merge #2982
2982: Merge imports when auto importing r=flodiebold a=SomeoneToIgnore Co-authored-by: Kirill Bulatov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r--crates/ra_assists/src/assists/add_import.rs10
-rw-r--r--crates/ra_assists/src/assists/auto_import.rs41
2 files changed, 36 insertions, 15 deletions
diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs
index bf6cfe865..fc038df78 100644
--- a/crates/ra_assists/src/assists/add_import.rs
+++ b/crates/ra_assists/src/assists/add_import.rs
@@ -1,4 +1,4 @@
1use hir::{self, db::HirDatabase}; 1use hir::{self, db::HirDatabase, ModPath};
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, NameOwner}, 3 ast::{self, NameOwner},
4 AstNode, Direction, SmolStr, 4 AstNode, Direction, SmolStr,
@@ -20,10 +20,10 @@ pub fn auto_import_text_edit(
20 position: &SyntaxNode, 20 position: &SyntaxNode,
21 // The statement to use as anchor (last resort) 21 // The statement to use as anchor (last resort)
22 anchor: &SyntaxNode, 22 anchor: &SyntaxNode,
23 // The path to import as a sequence of strings 23 path_to_import: &ModPath,
24 target: &[SmolStr],
25 edit: &mut TextEditBuilder, 24 edit: &mut TextEditBuilder,
26) { 25) {
26 let target = path_to_import.to_string().split("::").map(SmolStr::new).collect::<Vec<_>>();
27 let container = position.ancestors().find_map(|n| { 27 let container = position.ancestors().find_map(|n| {
28 if let Some(module) = ast::Module::cast(n.clone()) { 28 if let Some(module) = ast::Module::cast(n.clone()) {
29 return module.item_list().map(|it| it.syntax().clone()); 29 return module.item_list().map(|it| it.syntax().clone());
@@ -32,8 +32,8 @@ pub fn auto_import_text_edit(
32 }); 32 });
33 33
34 if let Some(container) = container { 34 if let Some(container) = container {
35 let action = best_action_for_target(container, anchor.clone(), target); 35 let action = best_action_for_target(container, anchor.clone(), &target);
36 make_assist(&action, target, edit); 36 make_assist(&action, &target, edit);
37 } 37 }
38} 38}
39 39
diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs
index 8c483e2da..2629f00e6 100644
--- a/crates/ra_assists/src/assists/auto_import.rs
+++ b/crates/ra_assists/src/assists/auto_import.rs
@@ -1,7 +1,6 @@
1use hir::db::HirDatabase; 1use hir::{db::HirDatabase, ModPath};
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, AstNode}, 3 ast::{self, AstNode},
4 SmolStr,
5 SyntaxKind::USE_ITEM, 4 SyntaxKind::USE_ITEM,
6 SyntaxNode, 5 SyntaxNode,
7}; 6};
@@ -58,7 +57,6 @@ pub(crate) fn auto_import<F: ImportsLocator>(
58 .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def)) 57 .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def))
59 .filter(|use_path| !use_path.segments.is_empty()) 58 .filter(|use_path| !use_path.segments.is_empty())
60 .take(20) 59 .take(20)
61 .map(|import| import.to_string())
62 .collect::<std::collections::BTreeSet<_>>(); 60 .collect::<std::collections::BTreeSet<_>>();
63 if proposed_imports.is_empty() { 61 if proposed_imports.is_empty() {
64 return None; 62 return None;
@@ -76,15 +74,10 @@ pub(crate) fn auto_import<F: ImportsLocator>(
76 ) 74 )
77} 75}
78 76
79fn import_to_action(import: String, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder { 77fn import_to_action(import: ModPath, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder {
80 let mut action_builder = ActionBuilder::default(); 78 let mut action_builder = ActionBuilder::default();
81 action_builder.label(format!("Import `{}`", &import)); 79 action_builder.label(format!("Import `{}`", &import));
82 auto_import_text_edit( 80 auto_import_text_edit(position, anchor, &import, action_builder.text_edit_builder());
83 position,
84 anchor,
85 &[SmolStr::new(import)],
86 action_builder.text_edit_builder(),
87 );
88 action_builder 81 action_builder
89} 82}
90 83
@@ -121,6 +114,34 @@ mod tests {
121 } 114 }
122 115
123 #[test] 116 #[test]
117 fn auto_imports_are_merged() {
118 check_assist_with_imports_locator(
119 auto_import,
120 TestImportsLocator::new,
121 r"
122 use PubMod::PubStruct1;
123
124 PubStruct2<|>
125
126 pub mod PubMod {
127 pub struct PubStruct1;
128 pub struct PubStruct2;
129 }
130 ",
131 r"
132 use PubMod::{PubStruct2, PubStruct1};
133
134 PubStruct2<|>
135
136 pub mod PubMod {
137 pub struct PubStruct1;
138 pub struct PubStruct2;
139 }
140 ",
141 );
142 }
143
144 #[test]
124 fn applicable_when_found_multiple_imports() { 145 fn applicable_when_found_multiple_imports() {
125 check_assist_with_imports_locator( 146 check_assist_with_imports_locator(
126 auto_import, 147 auto_import,