aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r--crates/ra_assists/src/assists/auto_import.rs144
1 files changed, 89 insertions, 55 deletions
diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs
index d196f6c5c..295fdf2e2 100644
--- a/crates/ra_assists/src/assists/auto_import.rs
+++ b/crates/ra_assists/src/assists/auto_import.rs
@@ -100,88 +100,122 @@ mod tests {
100 use super::*; 100 use super::*;
101 use crate::helpers::{ 101 use crate::helpers::{
102 check_assist_with_imports_locator, check_assist_with_imports_locator_not_applicable, 102 check_assist_with_imports_locator, check_assist_with_imports_locator_not_applicable,
103 TestImportsLocator,
103 }; 104 };
104 use hir::Name;
105 105
106 #[derive(Clone)] 106 #[test]
107 struct TestImportsLocator<'a> { 107 fn applicable_when_found_an_import() {
108 import_path: &'a [Name], 108 check_assist_with_imports_locator(
109 } 109 auto_import,
110 TestImportsLocator::new,
111 r"
112 PubStruct<|>
110 113
111 impl<'a> TestImportsLocator<'a> { 114 pub mod PubMod {
112 fn new(import_path: &'a [Name]) -> Self { 115 pub struct PubStruct;
113 TestImportsLocator { import_path } 116 }
114 } 117 ",
115 } 118 r"
119 use PubMod::PubStruct;
116 120
117 impl<'a> ImportsLocator for TestImportsLocator<'a> { 121 PubStruct<|>
118 fn find_imports( 122
119 &mut self, 123 pub mod PubMod {
120 _: hir::InFile<&ast::NameRef>, 124 pub struct PubStruct;
121 _: hir::Module,
122 ) -> Option<Vec<hir::ModPath>> {
123 if self.import_path.is_empty() {
124 None
125 } else {
126 Some(vec![hir::ModPath {
127 kind: hir::PathKind::Plain,
128 segments: self.import_path.to_owned(),
129 }])
130 } 125 }
131 } 126 ",
127 );
132 } 128 }
133 129
134 #[test] 130 #[test]
135 fn applicable_when_found_an_import() { 131 fn applicable_when_found_multiple_imports() {
136 let import_path = &[hir::name::known::std, hir::name::known::ops, hir::name::known::Debug];
137 let mut imports_locator = TestImportsLocator::new(import_path);
138 check_assist_with_imports_locator( 132 check_assist_with_imports_locator(
139 auto_import, 133 auto_import,
140 &mut imports_locator, 134 TestImportsLocator::new,
141 " 135 r"
142 fn main() { 136 PubStruct<|>
137
138 pub mod PubMod1 {
139 pub struct PubStruct;
140 }
141 pub mod PubMod2 {
142 pub struct PubStruct;
143 }
144 pub mod PubMod3 {
145 pub struct PubStruct;
146 }
147 ",
148 r"
149 use PubMod1::PubStruct;
150
151 PubStruct<|>
152
153 pub mod PubMod1 {
154 pub struct PubStruct;
155 }
156 pub mod PubMod2 {
157 pub struct PubStruct;
143 } 158 }
159 pub mod PubMod3 {
160 pub struct PubStruct;
161 }
162 ",
163 );
164 }
165
166 #[test]
167 fn not_applicable_for_already_imported_types() {
168 check_assist_with_imports_locator_not_applicable(
169 auto_import,
170 TestImportsLocator::new,
171 r"
172 use PubMod::PubStruct;
173
174 PubStruct<|>
144 175
145 Debug<|>", 176 pub mod PubMod {
146 &format!( 177 pub struct PubStruct;
147 " 178 }
148 use {}; 179 ",
149
150 fn main() {{
151 }}
152
153 Debug<|>",
154 import_path
155 .into_iter()
156 .map(|name| name.to_string())
157 .collect::<Vec<String>>()
158 .join("::")
159 ),
160 ); 180 );
161 } 181 }
162 182
163 #[test] 183 #[test]
164 fn not_applicable_when_no_imports_found() { 184 fn not_applicable_for_types_with_private_paths() {
165 let mut imports_locator = TestImportsLocator::new(&[]);
166 check_assist_with_imports_locator_not_applicable( 185 check_assist_with_imports_locator_not_applicable(
167 auto_import, 186 auto_import,
168 &mut imports_locator, 187 TestImportsLocator::new,
169 " 188 r"
170 fn main() { 189 PrivateStruct<|>
190
191 pub mod PubMod {
192 struct PrivateStruct;
171 } 193 }
194 ",
195 );
196 }
172 197
173 Debug<|>", 198 #[test]
199 fn not_applicable_when_no_imports_found() {
200 check_assist_with_imports_locator_not_applicable(
201 auto_import,
202 TestImportsLocator::new,
203 "
204 PubStruct<|>",
174 ); 205 );
175 } 206 }
176 207
177 #[test] 208 #[test]
178 fn not_applicable_in_import_statements() { 209 fn not_applicable_in_import_statements() {
179 let import_path = &[hir::name::known::std, hir::name::known::ops, hir::name::known::Debug];
180 let mut imports_locator = TestImportsLocator::new(import_path);
181 check_assist_with_imports_locator_not_applicable( 210 check_assist_with_imports_locator_not_applicable(
182 auto_import, 211 auto_import,
183 &mut imports_locator, 212 TestImportsLocator::new,
184 "use Debug<|>;", 213 r"
214 use PubStruct<|>;
215
216 pub mod PubMod {
217 pub struct PubStruct;
218 }",
185 ); 219 );
186 } 220 }
187} 221}