Skip to content

Commit 1a78991

Browse files
Adjust the tests
1 parent d0a782e commit 1a78991

File tree

3 files changed

+150
-61
lines changed

3 files changed

+150
-61
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 89 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -100,88 +100,122 @@ mod tests {
100100
use super::*;
101101
use crate::helpers::{
102102
check_assist_with_imports_locator, check_assist_with_imports_locator_not_applicable,
103+
TestImportsLocator,
103104
};
104-
use hir::Name;
105105

106-
#[derive(Clone)]
107-
struct TestImportsLocator<'a> {
108-
import_path: &'a [Name],
109-
}
106+
#[test]
107+
fn applicable_when_found_an_import() {
108+
check_assist_with_imports_locator(
109+
auto_import,
110+
TestImportsLocator::new,
111+
r"
112+
PubStruct<|>
110113
111-
impl<'a> TestImportsLocator<'a> {
112-
fn new(import_path: &'a [Name]) -> Self {
113-
TestImportsLocator { import_path }
114-
}
115-
}
114+
pub mod PubMod {
115+
pub struct PubStruct;
116+
}
117+
",
118+
r"
119+
use PubMod::PubStruct;
116120
117-
impl<'a> ImportsLocator for TestImportsLocator<'a> {
118-
fn find_imports(
119-
&mut self,
120-
_: hir::InFile<&ast::NameRef>,
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-
}])
121+
PubStruct<|>
122+
123+
pub mod PubMod {
124+
pub struct PubStruct;
130125
}
131-
}
126+
",
127+
);
132128
}
133129

134130
#[test]
135-
fn applicable_when_found_an_import() {
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);
131+
fn applicable_when_found_multiple_imports() {
138132
check_assist_with_imports_locator(
139133
auto_import,
140-
&mut imports_locator,
141-
"
142-
fn main() {
134+
TestImportsLocator::new,
135+
r"
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;
143158
}
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<|>
144175
145-
Debug<|>",
146-
&format!(
147-
"
148-
use {};
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-
),
176+
pub mod PubMod {
177+
pub struct PubStruct;
178+
}
179+
",
160180
);
161181
}
162182

163183
#[test]
164-
fn not_applicable_when_no_imports_found() {
165-
let mut imports_locator = TestImportsLocator::new(&[]);
184+
fn not_applicable_for_types_with_private_paths() {
166185
check_assist_with_imports_locator_not_applicable(
167186
auto_import,
168-
&mut imports_locator,
169-
"
170-
fn main() {
187+
TestImportsLocator::new,
188+
r"
189+
PrivateStruct<|>
190+
191+
pub mod PubMod {
192+
struct PrivateStruct;
171193
}
194+
",
195+
);
196+
}
172197

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<|>",
174205
);
175206
}
176207

177208
#[test]
178209
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);
181210
check_assist_with_imports_locator_not_applicable(
182211
auto_import,
183-
&mut imports_locator,
184-
"use Debug<|>;",
212+
TestImportsLocator::new,
213+
r"
214+
use PubStruct<|>;
215+
216+
pub mod PubMod {
217+
pub struct PubStruct;
218+
}",
185219
);
186220
}
187221
}

crates/ra_assists/src/lib.rs

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,59 @@ mod assists {
226226

227227
#[cfg(test)]
228228
mod helpers {
229-
use ra_db::{fixture::WithFixture, FileRange};
229+
use hir::db::DefDatabase;
230+
use ra_db::{fixture::WithFixture, FileId, FileRange};
230231
use ra_syntax::TextRange;
231232
use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
232233

233234
use crate::{test_db::TestDB, Assist, AssistCtx, ImportsLocator};
235+
use std::sync::Arc;
236+
237+
pub(crate) struct TestImportsLocator {
238+
db: Arc<TestDB>,
239+
test_file_id: FileId,
240+
}
241+
242+
impl TestImportsLocator {
243+
pub(crate) fn new(db: Arc<TestDB>, test_file_id: FileId) -> Self {
244+
TestImportsLocator { db, test_file_id }
245+
}
246+
}
247+
248+
impl ImportsLocator for TestImportsLocator {
249+
fn find_imports(&mut self, name_to_import: &str) -> Vec<hir::ModuleDef> {
250+
let crate_def_map = self.db.crate_def_map(self.db.test_crate());
251+
let mut findings = vec![];
252+
253+
let mut module_ids_to_process =
254+
crate_def_map.modules_for_file(self.test_file_id).collect::<Vec<_>>();
255+
256+
while !module_ids_to_process.is_empty() {
257+
let mut more_ids_to_process = vec![];
258+
for local_module_id in module_ids_to_process.drain(..) {
259+
for (name, namespace_data) in
260+
crate_def_map[local_module_id].scope.entries_without_primitives()
261+
{
262+
let found_a_match = &name.to_string() == name_to_import;
263+
vec![namespace_data.types, namespace_data.values]
264+
.into_iter()
265+
.filter_map(std::convert::identity)
266+
.for_each(|(module_def_id, _)| {
267+
if found_a_match {
268+
findings.push(module_def_id.into());
269+
}
270+
if let hir::ModuleDefId::ModuleId(module_id) = module_def_id {
271+
more_ids_to_process.push(module_id.local_id);
272+
}
273+
});
274+
}
275+
}
276+
module_ids_to_process = more_ids_to_process;
277+
}
278+
279+
findings
280+
}
281+
}
234282

235283
pub(crate) fn check_assist(
236284
assist: fn(AssistCtx<TestDB>) -> Option<Assist>,
@@ -262,16 +310,19 @@ mod helpers {
262310

263311
pub(crate) fn check_assist_with_imports_locator<F: ImportsLocator>(
264312
assist: fn(AssistCtx<TestDB>, &mut F) -> Option<Assist>,
265-
imports_locator: &mut F,
313+
imports_locator_provider: fn(db: Arc<TestDB>, file_id: FileId) -> F,
266314
before: &str,
267315
after: &str,
268316
) {
269317
let (before_cursor_pos, before) = extract_offset(before);
270318
let (db, file_id) = TestDB::with_single_file(&before);
319+
let db = Arc::new(db);
320+
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
271321
let frange =
272322
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
273-
let assist = AssistCtx::with_ctx(&db, frange, true, |ctx| assist(ctx, imports_locator))
274-
.expect("code action is not applicable");
323+
let assist =
324+
AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator))
325+
.expect("code action is not applicable");
275326
let action = match assist {
276327
Assist::Unresolved { .. } => unreachable!(),
277328
Assist::Resolved { assist } => assist.get_first_action(),
@@ -364,14 +415,17 @@ mod helpers {
364415

365416
pub(crate) fn check_assist_with_imports_locator_not_applicable<F: ImportsLocator>(
366417
assist: fn(AssistCtx<TestDB>, &mut F) -> Option<Assist>,
367-
imports_locator: &mut F,
418+
imports_locator_provider: fn(db: Arc<TestDB>, file_id: FileId) -> F,
368419
before: &str,
369420
) {
370421
let (before_cursor_pos, before) = extract_offset(before);
371422
let (db, file_id) = TestDB::with_single_file(&before);
423+
let db = Arc::new(db);
424+
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
372425
let frange =
373426
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
374-
let assist = AssistCtx::with_ctx(&db, frange, true, |ctx| assist(ctx, imports_locator));
427+
let assist =
428+
AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator));
375429
assert!(assist.is_none());
376430
}
377431

crates/ra_hir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub use hir_def::{
5656
nameres::ModuleSource,
5757
path::{ModPath, Path, PathKind},
5858
type_ref::Mutability,
59+
ModuleDefId,
5960
};
6061
pub use hir_expand::{
6162
name::{AsName, Name},

0 commit comments

Comments
 (0)