From 2034002413a62fb999d9372231905393ed5c0383 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 3 May 2020 03:24:55 +0800 Subject: Support auto-import in macro --- crates/ra_assists/src/handlers/auto_import.rs | 42 +++++++++++++++++----- .../handlers/replace_qualified_name_with_use.rs | 2 +- 2 files changed, 35 insertions(+), 9 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 99682e023..db6c4d2fa 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -45,15 +45,12 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option { return None; } + let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; let mut group = ctx.add_assist_group(auto_import_assets.get_import_group_message()); for import in proposed_imports { group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| { - edit.target(auto_import_assets.syntax_under_caret.text_range()); - insert_use_statement( - &auto_import_assets.syntax_under_caret, - &import, - edit.text_edit_builder(), - ); + edit.target(range); + insert_use_statement(&auto_import_assets.syntax_under_caret, &import, edit); }); } group.finish() @@ -68,10 +65,10 @@ struct AutoImportAssets { impl AutoImportAssets { fn new(ctx: &AssistCtx) -> Option { - if let Some(path_under_caret) = ctx.find_node_at_offset::() { + if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::() { Self::for_regular_path(path_under_caret, &ctx) } else { - Self::for_method_call(ctx.find_node_at_offset()?, &ctx) + Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx) } } @@ -305,6 +302,35 @@ mod tests { ); } + #[test] + fn applicable_when_found_an_import_in_macros() { + check_assist( + auto_import, + r" + macro_rules! foo { + ($i:ident) => { fn foo(a: $i) {} } + } + foo!(Pub<|>Struct); + + pub mod PubMod { + pub struct PubStruct; + } + ", + r" + use PubMod::PubStruct; + + macro_rules! foo { + ($i:ident) => { fn foo(a: $i) {} } + } + foo!(Pub<|>Struct); + + pub mod PubMod { + pub struct PubStruct; + } + ", + ); + } + #[test] fn auto_imports_are_merged() { check_assist( diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index 918e8dd8d..ff2463c77 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs @@ -38,7 +38,7 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option "Replace qualified path with use", |edit| { let path_to_import = hir_path.mod_path().clone(); - insert_use_statement(path.syntax(), &path_to_import, edit.text_edit_builder()); + insert_use_statement(path.syntax(), &path_to_import, edit); if let Some(last) = path.segment() { // Here we are assuming the assist will provide a correct use statement -- cgit v1.2.3