Skip to content

Commit dd3b641

Browse files
Add a test
1 parent 944f28f commit dd3b641

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

crates/ra_assists/src/handlers/auto_import.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
5959
group.finish()
6060
}
6161

62+
#[derive(Debug)]
6263
struct AutoImportAssets {
6364
import_candidate: ImportCandidate,
6465
module_with_name_to_import: Module,
@@ -449,6 +450,30 @@ mod tests {
449450
);
450451
}
451452

453+
#[test]
454+
fn macro_import() {
455+
check_assist(
456+
auto_import,
457+
r"
458+
//- /lib.rs crate:crate_with_macro
459+
#[macro_export]
460+
macro_rules! foo {
461+
() => ()
462+
}
463+
464+
//- /main.rs crate:main deps:crate_with_macro
465+
fn main() {
466+
foo<|>
467+
}",
468+
r"use crate_with_macro::foo;
469+
470+
fn main() {
471+
foo<|>
472+
}
473+
",
474+
);
475+
}
476+
452477
#[test]
453478
fn auto_import_target() {
454479
check_assist_target(

crates/ra_assists/src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ mod helpers {
165165

166166
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
167167
use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
168-
use ra_syntax::TextRange;
169168
use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset};
170169

171170
use crate::{AssistCtx, AssistHandler};
@@ -175,8 +174,7 @@ mod helpers {
175174
let (mut db, file_id) = RootDatabase::with_single_file(text);
176175
// FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`,
177176
// but it looks like this might need specialization? :(
178-
let local_roots = vec![db.file_source_root(file_id)];
179-
db.set_local_roots(Arc::new(local_roots));
177+
db.set_local_roots(Arc::new(vec![db.file_source_root(file_id)]));
180178
(db, file_id)
181179
}
182180

@@ -206,19 +204,32 @@ mod helpers {
206204
}
207205

208206
fn check(assist: AssistHandler, before: &str, expected: ExpectedResult) {
209-
let (range_or_offset, before) = extract_range_or_offset(before);
210-
let range: TextRange = range_or_offset.into();
207+
let (text_without_caret, file_with_caret_id, range_or_offset, db) =
208+
if before.contains("//-") {
209+
let (mut db, position) = RootDatabase::with_position(before);
210+
db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)]));
211+
(
212+
db.file_text(position.file_id).as_ref().to_owned(),
213+
position.file_id,
214+
RangeOrOffset::Offset(position.offset),
215+
db,
216+
)
217+
} else {
218+
let (range_or_offset, text_without_caret) = extract_range_or_offset(before);
219+
let (db, file_id) = with_single_file(&text_without_caret);
220+
(text_without_caret, file_id, range_or_offset, db)
221+
};
222+
223+
let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
211224

212-
let (db, file_id) = with_single_file(&before);
213-
let frange = FileRange { file_id, range };
214225
let sema = Semantics::new(&db);
215226
let assist_ctx = AssistCtx::new(&sema, frange, true);
216227

217228
match (assist(assist_ctx), expected) {
218229
(Some(assist), ExpectedResult::After(after)) => {
219230
let action = assist.0[0].action.clone().unwrap();
220231

221-
let mut actual = action.edit.apply(&before);
232+
let mut actual = action.edit.apply(&text_without_caret);
222233
match action.cursor_position {
223234
None => {
224235
if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset {
@@ -237,7 +248,7 @@ mod helpers {
237248
(Some(assist), ExpectedResult::Target(target)) => {
238249
let action = assist.0[0].action.clone().unwrap();
239250
let range = action.target.expect("expected target on action");
240-
assert_eq_text!(&before[range], target);
251+
assert_eq_text!(&text_without_caret[range], target);
241252
}
242253
(Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"),
243254
(None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => {

0 commit comments

Comments
 (0)