Skip to content

Commit d221ff4

Browse files
Auto import macros
1 parent f9494f1 commit d221ff4

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

crates/ra_assists/src/ast_transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'a> QualifyPaths<'a> {
129129
let resolution = self.source_scope.resolve_hir_path(&hir_path?)?;
130130
match resolution {
131131
PathResolution::Def(def) => {
132-
let found_path = from.find_use_path(self.source_scope.db, def)?;
132+
let found_path = from.find_use_path(self.source_scope.db, def.into())?;
133133
let mut path = path_to_ast(found_path);
134134

135135
let type_args = p

crates/ra_assists/src/handlers/auto_import.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hir::{
44
AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait,
55
Type,
66
};
7-
use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase};
7+
use ra_ide_db::{defs::Definition, imports_locator::ImportsLocator, RootDatabase};
88
use ra_prof::profile;
99
use ra_syntax::{
1010
ast::{self, AstNode},
@@ -127,14 +127,16 @@ impl AutoImportAssets {
127127
ImportsLocator::new(db)
128128
.find_imports(&self.get_search_query())
129129
.into_iter()
130-
.filter_map(|module_def| match &self.import_candidate {
130+
.filter_map(|definition| match &self.import_candidate {
131131
ImportCandidate::TraitAssocItem(assoc_item_type, _) => {
132-
let located_assoc_item = match module_def {
133-
ModuleDef::Function(located_function) => located_function
134-
.as_assoc_item(db)
135-
.map(|assoc| assoc.container(db))
136-
.and_then(Self::assoc_to_trait),
137-
ModuleDef::Const(located_const) => located_const
132+
let located_assoc_item = match definition {
133+
Definition::ModuleDef(ModuleDef::Function(located_function)) => {
134+
located_function
135+
.as_assoc_item(db)
136+
.map(|assoc| assoc.container(db))
137+
.and_then(Self::assoc_to_trait)
138+
}
139+
Definition::ModuleDef(ModuleDef::Const(located_const)) => located_const
138140
.as_assoc_item(db)
139141
.map(|assoc| assoc.container(db))
140142
.and_then(Self::assoc_to_trait),
@@ -152,11 +154,13 @@ impl AutoImportAssets {
152154
None,
153155
|_, assoc| Self::assoc_to_trait(assoc.container(db)),
154156
)
155-
.map(ModuleDef::from)
157+
.map(|located_trait| ModuleDef::from(located_trait).into())
156158
}
157159
ImportCandidate::TraitMethod(function_callee, _) => {
158160
let located_assoc_item =
159-
if let ModuleDef::Function(located_function) = module_def {
161+
if let Definition::ModuleDef(ModuleDef::Function(located_function)) =
162+
definition
163+
{
160164
located_function
161165
.as_assoc_item(db)
162166
.map(|assoc| assoc.container(db))
@@ -178,11 +182,15 @@ impl AutoImportAssets {
178182
Self::assoc_to_trait(function.as_assoc_item(db)?.container(db))
179183
},
180184
)
181-
.map(ModuleDef::from)
185+
.map(|located_trait| ModuleDef::from(located_trait).into())
182186
}
183-
_ => Some(module_def),
187+
_ => match definition {
188+
Definition::ModuleDef(module_def) => Some(module_def.into()),
189+
Definition::Macro(macro_def) => Some(macro_def.into()),
190+
_ => None,
191+
},
184192
})
185-
.filter_map(|module_def| self.module_with_name_to_import.find_use_path(db, module_def))
193+
.filter_map(|item| self.module_with_name_to_import.find_use_path(db, item))
186194
.filter(|use_path| !use_path.segments.is_empty())
187195
.take(20)
188196
.collect::<BTreeSet<_>>()

crates/ra_assists/src/handlers/fill_match_arms.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::iter;
44

5-
use hir::{Adt, HasSource, Semantics};
5+
use hir::{Adt, HasSource, ModuleDef, Semantics};
66
use itertools::Itertools;
77
use ra_ide_db::RootDatabase;
88

@@ -154,7 +154,8 @@ fn resolve_tuple_of_enum_def(
154154
}
155155

156156
fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> {
157-
let path = crate::ast_transform::path_to_ast(module.find_use_path(db, var.into())?);
157+
let path =
158+
crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var).into())?);
158159

159160
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
160161
let pat: ast::Pat = match var.source(db).value.kind() {

crates/ra_hir/src/code_model.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ impl ModuleDef {
139139
}
140140
}
141141

142+
impl From<ModuleDef> for ItemInNs {
143+
fn from(module_def: ModuleDef) -> Self {
144+
match module_def {
145+
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
146+
ItemInNs::Values(module_def.into())
147+
}
148+
_ => ItemInNs::Types(module_def.into()),
149+
}
150+
}
151+
}
152+
142153
pub use hir_def::{
143154
attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc,
144155
};
@@ -275,19 +286,9 @@ impl Module {
275286
pub fn find_use_path(
276287
self,
277288
db: &dyn HirDatabase,
278-
item: ModuleDef,
289+
item: ItemInNs,
279290
) -> Option<hir_def::path::ModPath> {
280-
// FIXME expose namespace choice
281-
hir_def::find_path::find_path(db.upcast(), determine_item_namespace(item), self.into())
282-
}
283-
}
284-
285-
fn determine_item_namespace(module_def: ModuleDef) -> ItemInNs {
286-
match module_def {
287-
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
288-
ItemInNs::Values(module_def.into())
289-
}
290-
_ => ItemInNs::Types(module_def.into()),
291+
hir_def::find_path::find_path(db.upcast(), item, self.into())
291292
}
292293
}
293294

@@ -759,6 +760,12 @@ impl MacroDef {
759760
}
760761
}
761762

763+
impl From<MacroDef> for ItemInNs {
764+
fn from(macro_def: MacroDef) -> Self {
765+
ItemInNs::Macros(macro_def.into())
766+
}
767+
}
768+
762769
/// Invariant: `inner.as_assoc_item(db).is_some()`
763770
/// We do not actively enforce this invariant.
764771
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

crates/ra_ide_db/src/imports_locator.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This module contains an import search funcionality that is provided to the ra_assists module.
22
//! Later, this should be moved away to a separate crate that is accessible from the ra_assists module.
33
4-
use hir::{ModuleDef, Semantics};
4+
use hir::Semantics;
55
use ra_prof::profile;
66
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
77

@@ -20,7 +20,7 @@ impl<'a> ImportsLocator<'a> {
2020
Self { sema: Semantics::new(db) }
2121
}
2222

23-
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> {
23+
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Definition> {
2424
let _p = profile("search_for_imports");
2525
let db = self.sema.db;
2626

@@ -42,10 +42,6 @@ impl<'a> ImportsLocator<'a> {
4242
.into_iter()
4343
.chain(lib_results.into_iter())
4444
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
45-
.filter_map(|name_definition_to_import| match name_definition_to_import {
46-
Definition::ModuleDef(module_def) => Some(module_def),
47-
_ => None,
48-
})
4945
.collect()
5046
}
5147

0 commit comments

Comments
 (0)