Skip to content

Commit 944f28f

Browse files
Use more generic public api
1 parent d5e11b3 commit 944f28f

File tree

9 files changed

+58
-47
lines changed

9 files changed

+58
-47
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_assists/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ format-buf = "1.0.0"
1212
join_to_string = "0.1.3"
1313
rustc-hash = "1.1.0"
1414
itertools = "0.9.0"
15+
either = "1.5.3"
1516

1617
ra_syntax = { path = "../ra_syntax" }
1718
ra_text_edit = { path = "../ra_text_edit" }

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.into())?;
132+
let found_path = from.find_use_path(self.source_scope.db, def)?;
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: 23 additions & 21 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::{defs::Definition, imports_locator::ImportsLocator, RootDatabase};
7+
use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase};
88
use ra_prof::profile;
99
use ra_syntax::{
1010
ast::{self, AstNode},
@@ -17,6 +17,7 @@ use crate::{
1717
utils::insert_use_statement,
1818
AssistId,
1919
};
20+
use either::Either;
2021

2122
// Assist: auto_import
2223
//
@@ -127,16 +128,14 @@ impl AutoImportAssets {
127128
ImportsLocator::new(db)
128129
.find_imports(&self.get_search_query())
129130
.into_iter()
130-
.filter_map(|definition| match &self.import_candidate {
131+
.filter_map(|candidate| match &self.import_candidate {
131132
ImportCandidate::TraitAssocItem(assoc_item_type, _) => {
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
133+
let located_assoc_item = match candidate {
134+
Either::Left(ModuleDef::Function(located_function)) => located_function
135+
.as_assoc_item(db)
136+
.map(|assoc| assoc.container(db))
137+
.and_then(Self::assoc_to_trait),
138+
Either::Left(ModuleDef::Const(located_const)) => located_const
140139
.as_assoc_item(db)
141140
.map(|assoc| assoc.container(db))
142141
.and_then(Self::assoc_to_trait),
@@ -154,13 +153,12 @@ impl AutoImportAssets {
154153
None,
155154
|_, assoc| Self::assoc_to_trait(assoc.container(db)),
156155
)
157-
.map(|located_trait| ModuleDef::from(located_trait).into())
156+
.map(ModuleDef::from)
157+
.map(Either::Left)
158158
}
159159
ImportCandidate::TraitMethod(function_callee, _) => {
160160
let located_assoc_item =
161-
if let Definition::ModuleDef(ModuleDef::Function(located_function)) =
162-
definition
163-
{
161+
if let Either::Left(ModuleDef::Function(located_function)) = candidate {
164162
located_function
165163
.as_assoc_item(db)
166164
.map(|assoc| assoc.container(db))
@@ -182,15 +180,19 @@ impl AutoImportAssets {
182180
Self::assoc_to_trait(function.as_assoc_item(db)?.container(db))
183181
},
184182
)
185-
.map(|located_trait| ModuleDef::from(located_trait).into())
183+
.map(ModuleDef::from)
184+
.map(Either::Left)
185+
}
186+
_ => Some(candidate),
187+
})
188+
.filter_map(|candidate| match candidate {
189+
Either::Left(module_def) => {
190+
self.module_with_name_to_import.find_use_path(db, module_def)
191+
}
192+
Either::Right(macro_def) => {
193+
self.module_with_name_to_import.find_use_path(db, macro_def)
186194
}
187-
_ => match definition {
188-
Definition::ModuleDef(module_def) => Some(module_def.into()),
189-
Definition::Macro(macro_def) => Some(macro_def.into()),
190-
_ => None,
191-
},
192195
})
193-
.filter_map(|item| self.module_with_name_to_import.find_use_path(db, item))
194196
.filter(|use_path| !use_path.segments.is_empty())
195197
.take(20)
196198
.collect::<BTreeSet<_>>()

crates/ra_assists/src/handlers/fill_match_arms.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ 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 =
158-
crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var).into())?);
157+
let path = crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var))?);
159158

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

crates/ra_hir/src/code_model.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,6 @@ impl ModuleDef {
143143
}
144144
}
145145

146-
impl From<ModuleDef> for ItemInNs {
147-
fn from(module_def: ModuleDef) -> Self {
148-
match module_def {
149-
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
150-
ItemInNs::Values(module_def.into())
151-
}
152-
_ => ItemInNs::Types(module_def.into()),
153-
}
154-
}
155-
}
156-
157146
pub use hir_def::{
158147
attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc,
159148
};
@@ -290,9 +279,9 @@ impl Module {
290279
pub fn find_use_path(
291280
self,
292281
db: &dyn DefDatabase,
293-
item: ItemInNs,
282+
item: impl Into<ItemInNs>,
294283
) -> Option<hir_def::path::ModPath> {
295-
hir_def::find_path::find_path(db, item, self.into())
284+
hir_def::find_path::find_path(db, item.into(), self.into())
296285
}
297286
}
298287

@@ -764,12 +753,6 @@ impl MacroDef {
764753
}
765754
}
766755

767-
impl From<MacroDef> for ItemInNs {
768-
fn from(macro_def: MacroDef) -> Self {
769-
ItemInNs::Macros(macro_def.into())
770-
}
771-
}
772-
773756
/// Invariant: `inner.as_assoc_item(db).is_some()`
774757
/// We do not actively enforce this invariant.
775758
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

crates/ra_hir/src/from_id.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use hir_def::{
99
};
1010

1111
use crate::{
12-
Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, Local, ModuleDef, StructField,
13-
VariantDef,
12+
code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, Local,
13+
MacroDef, ModuleDef, StructField, VariantDef,
1414
};
1515

1616
macro_rules! from_id {
@@ -228,3 +228,20 @@ impl From<(DefWithBodyId, PatId)> for Local {
228228
Local { parent, pat_id }
229229
}
230230
}
231+
232+
impl From<MacroDef> for ItemInNs {
233+
fn from(macro_def: MacroDef) -> Self {
234+
ItemInNs::Macros(macro_def.into())
235+
}
236+
}
237+
238+
impl From<ModuleDef> for ItemInNs {
239+
fn from(module_def: ModuleDef) -> Self {
240+
match module_def {
241+
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
242+
ItemInNs::Values(module_def.into())
243+
}
244+
_ => ItemInNs::Types(module_def.into()),
245+
}
246+
}
247+
}

crates/ra_ide_db/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fst = { version = "0.4", default-features = false }
1717
rustc-hash = "1.1.0"
1818
superslice = "1.0.0"
1919
once_cell = "1.3.1"
20+
either = "1.5.3"
2021

2122
ra_syntax = { path = "../ra_syntax" }
2223
ra_text_edit = { path = "../ra_text_edit" }

crates/ra_ide_db/src/imports_locator.rs

Lines changed: 8 additions & 2 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::Semantics;
4+
use hir::{MacroDef, ModuleDef, Semantics};
55
use ra_prof::profile;
66
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
77

@@ -10,6 +10,7 @@ use crate::{
1010
symbol_index::{self, FileSymbol, Query},
1111
RootDatabase,
1212
};
13+
use either::Either;
1314

1415
pub struct ImportsLocator<'a> {
1516
sema: Semantics<'a, RootDatabase>,
@@ -20,7 +21,7 @@ impl<'a> ImportsLocator<'a> {
2021
Self { sema: Semantics::new(db) }
2122
}
2223

23-
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Definition> {
24+
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Either<ModuleDef, MacroDef>> {
2425
let _p = profile("search_for_imports");
2526
let db = self.sema.db;
2627

@@ -42,6 +43,11 @@ impl<'a> ImportsLocator<'a> {
4243
.into_iter()
4344
.chain(lib_results.into_iter())
4445
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
46+
.filter_map(|name_definition_to_import| match name_definition_to_import {
47+
Definition::ModuleDef(module_def) => Some(Either::Left(module_def)),
48+
Definition::Macro(macro_def) => Some(Either::Right(macro_def)),
49+
_ => None,
50+
})
4551
.collect()
4652
}
4753

0 commit comments

Comments
 (0)