From d221ff4f9e0e270939a2944a2471bc3f027a674b Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 23 Mar 2020 00:19:55 +0200 Subject: Auto import macros --- crates/ra_assists/src/handlers/auto_import.rs | 34 ++++++++++++++--------- crates/ra_assists/src/handlers/fill_match_arms.rs | 5 ++-- 2 files changed, 24 insertions(+), 15 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 bb280f633..49d8c4c3d 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -4,7 +4,7 @@ use hir::{ AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, Type, }; -use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; +use ra_ide_db::{defs::Definition, imports_locator::ImportsLocator, RootDatabase}; use ra_prof::profile; use ra_syntax::{ ast::{self, AstNode}, @@ -127,14 +127,16 @@ impl AutoImportAssets { ImportsLocator::new(db) .find_imports(&self.get_search_query()) .into_iter() - .filter_map(|module_def| match &self.import_candidate { + .filter_map(|definition| match &self.import_candidate { ImportCandidate::TraitAssocItem(assoc_item_type, _) => { - let located_assoc_item = match module_def { - ModuleDef::Function(located_function) => located_function - .as_assoc_item(db) - .map(|assoc| assoc.container(db)) - .and_then(Self::assoc_to_trait), - ModuleDef::Const(located_const) => located_const + let located_assoc_item = match definition { + Definition::ModuleDef(ModuleDef::Function(located_function)) => { + located_function + .as_assoc_item(db) + .map(|assoc| assoc.container(db)) + .and_then(Self::assoc_to_trait) + } + Definition::ModuleDef(ModuleDef::Const(located_const)) => located_const .as_assoc_item(db) .map(|assoc| assoc.container(db)) .and_then(Self::assoc_to_trait), @@ -152,11 +154,13 @@ impl AutoImportAssets { None, |_, assoc| Self::assoc_to_trait(assoc.container(db)), ) - .map(ModuleDef::from) + .map(|located_trait| ModuleDef::from(located_trait).into()) } ImportCandidate::TraitMethod(function_callee, _) => { let located_assoc_item = - if let ModuleDef::Function(located_function) = module_def { + if let Definition::ModuleDef(ModuleDef::Function(located_function)) = + definition + { located_function .as_assoc_item(db) .map(|assoc| assoc.container(db)) @@ -178,11 +182,15 @@ impl AutoImportAssets { Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) }, ) - .map(ModuleDef::from) + .map(|located_trait| ModuleDef::from(located_trait).into()) } - _ => Some(module_def), + _ => match definition { + Definition::ModuleDef(module_def) => Some(module_def.into()), + Definition::Macro(macro_def) => Some(macro_def.into()), + _ => None, + }, }) - .filter_map(|module_def| self.module_with_name_to_import.find_use_path(db, module_def)) + .filter_map(|item| self.module_with_name_to_import.find_use_path(db, item)) .filter(|use_path| !use_path.segments.is_empty()) .take(20) .collect::>() diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index 7463b2af7..869942b12 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs @@ -2,7 +2,7 @@ use std::iter; -use hir::{Adt, HasSource, Semantics}; +use hir::{Adt, HasSource, ModuleDef, Semantics}; use itertools::Itertools; use ra_ide_db::RootDatabase; @@ -154,7 +154,8 @@ fn resolve_tuple_of_enum_def( } fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option { - let path = crate::ast_transform::path_to_ast(module.find_use_path(db, var.into())?); + let path = + crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var).into())?); // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though let pat: ast::Pat = match var.source(db).value.kind() { -- cgit v1.2.3 From 944f28fe5bf2b8e4316cc67bf5f824333fc4f180 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 23 Mar 2020 13:34:56 +0200 Subject: Use more generic public api --- crates/ra_assists/src/handlers/auto_import.rs | 44 ++++++++++++----------- crates/ra_assists/src/handlers/fill_match_arms.rs | 3 +- 2 files changed, 24 insertions(+), 23 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 49d8c4c3d..443eeaaf0 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -4,7 +4,7 @@ use hir::{ AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, Type, }; -use ra_ide_db::{defs::Definition, imports_locator::ImportsLocator, RootDatabase}; +use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; use ra_prof::profile; use ra_syntax::{ ast::{self, AstNode}, @@ -17,6 +17,7 @@ use crate::{ utils::insert_use_statement, AssistId, }; +use either::Either; // Assist: auto_import // @@ -127,16 +128,14 @@ impl AutoImportAssets { ImportsLocator::new(db) .find_imports(&self.get_search_query()) .into_iter() - .filter_map(|definition| match &self.import_candidate { + .filter_map(|candidate| match &self.import_candidate { ImportCandidate::TraitAssocItem(assoc_item_type, _) => { - let located_assoc_item = match definition { - Definition::ModuleDef(ModuleDef::Function(located_function)) => { - located_function - .as_assoc_item(db) - .map(|assoc| assoc.container(db)) - .and_then(Self::assoc_to_trait) - } - Definition::ModuleDef(ModuleDef::Const(located_const)) => located_const + let located_assoc_item = match candidate { + Either::Left(ModuleDef::Function(located_function)) => located_function + .as_assoc_item(db) + .map(|assoc| assoc.container(db)) + .and_then(Self::assoc_to_trait), + Either::Left(ModuleDef::Const(located_const)) => located_const .as_assoc_item(db) .map(|assoc| assoc.container(db)) .and_then(Self::assoc_to_trait), @@ -154,13 +153,12 @@ impl AutoImportAssets { None, |_, assoc| Self::assoc_to_trait(assoc.container(db)), ) - .map(|located_trait| ModuleDef::from(located_trait).into()) + .map(ModuleDef::from) + .map(Either::Left) } ImportCandidate::TraitMethod(function_callee, _) => { let located_assoc_item = - if let Definition::ModuleDef(ModuleDef::Function(located_function)) = - definition - { + if let Either::Left(ModuleDef::Function(located_function)) = candidate { located_function .as_assoc_item(db) .map(|assoc| assoc.container(db)) @@ -182,15 +180,19 @@ impl AutoImportAssets { Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) }, ) - .map(|located_trait| ModuleDef::from(located_trait).into()) + .map(ModuleDef::from) + .map(Either::Left) + } + _ => Some(candidate), + }) + .filter_map(|candidate| match candidate { + Either::Left(module_def) => { + self.module_with_name_to_import.find_use_path(db, module_def) + } + Either::Right(macro_def) => { + self.module_with_name_to_import.find_use_path(db, macro_def) } - _ => match definition { - Definition::ModuleDef(module_def) => Some(module_def.into()), - Definition::Macro(macro_def) => Some(macro_def.into()), - _ => None, - }, }) - .filter_map(|item| self.module_with_name_to_import.find_use_path(db, item)) .filter(|use_path| !use_path.segments.is_empty()) .take(20) .collect::>() diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index 869942b12..88b4c8926 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs @@ -154,8 +154,7 @@ fn resolve_tuple_of_enum_def( } fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option { - let path = - crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var).into())?); + let path = crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var))?); // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though let pat: ast::Pat = match var.source(db).value.kind() { -- cgit v1.2.3 From dd3b64124b086cf68c3f8b1e838601b5770a9795 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 23 Mar 2020 23:23:26 +0200 Subject: Add a test --- crates/ra_assists/src/handlers/auto_import.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (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 443eeaaf0..99682e023 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -59,6 +59,7 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option { group.finish() } +#[derive(Debug)] struct AutoImportAssets { import_candidate: ImportCandidate, module_with_name_to_import: Module, @@ -449,6 +450,30 @@ mod tests { ); } + #[test] + fn macro_import() { + check_assist( + auto_import, + r" + //- /lib.rs crate:crate_with_macro + #[macro_export] + macro_rules! foo { + () => () + } + + //- /main.rs crate:main deps:crate_with_macro + fn main() { + foo<|> + }", + r"use crate_with_macro::foo; + +fn main() { + foo<|> +} +", + ); + } + #[test] fn auto_import_target() { check_assist_target( -- cgit v1.2.3