Skip to content

Commit d0a782e

Browse files
Have a better trait interface
1 parent bef5cf0 commit d0a782e

File tree

4 files changed

+45
-64
lines changed

4 files changed

+45
-64
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::db::HirDatabase;
1+
use hir::{db::HirDatabase, AsName};
22
use ra_syntax::{
33
ast::{self, AstNode},
44
SmolStr, SyntaxElement,
@@ -41,23 +41,29 @@ pub(crate) fn auto_import<F: ImportsLocator>(
4141
current_file.syntax().clone()
4242
}
4343
};
44+
let source_analyzer = ctx.source_analyzer(&position, None);
45+
let module_with_name_to_import = source_analyzer.module()?;
46+
let path_to_import = ctx.covering_element().ancestors().find_map(ast::Path::cast)?;
47+
if source_analyzer.resolve_path(ctx.db, &path_to_import).is_some() {
48+
return None;
49+
}
4450

45-
let module_with_name_to_import = ctx.source_analyzer(&position, None).module()?;
46-
let name_to_import = hir::InFile {
47-
file_id: ctx.frange.file_id.into(),
48-
value: &find_applicable_name_ref(ctx.covering_element())?,
49-
};
50-
51-
let proposed_imports =
52-
imports_locator.find_imports(name_to_import, module_with_name_to_import)?;
51+
let name_to_import = &find_applicable_name_ref(ctx.covering_element())?.as_name();
52+
let proposed_imports = imports_locator
53+
.find_imports(&name_to_import.to_string())
54+
.into_iter()
55+
.filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def))
56+
.filter(|use_path| !use_path.segments.is_empty())
57+
.take(20)
58+
.collect::<std::collections::HashSet<_>>();
5359
if proposed_imports.is_empty() {
5460
return None;
5561
}
5662

5763
ctx.add_assist_group(AssistId("auto_import"), "auto import", || {
5864
proposed_imports
5965
.into_iter()
60-
.map(|import| import_to_action(import.to_string(), &position, &path))
66+
.map(|import| import_to_action(import.to_string(), &position, &path_to_import))
6167
.collect()
6268
})
6369
}

crates/ra_assists/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ mod test_db;
1414
pub mod ast_transform;
1515

1616
use either::Either;
17-
use hir::{db::HirDatabase, InFile, ModPath, Module};
17+
use hir::{db::HirDatabase, ModuleDef};
1818
use ra_db::FileRange;
19-
use ra_syntax::{ast::NameRef, TextRange, TextUnit};
19+
use ra_syntax::{TextRange, TextUnit};
2020
use ra_text_edit::TextEdit;
2121

2222
pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
@@ -85,11 +85,7 @@ where
8585
/// accessible from the ra_assists crate.
8686
pub trait ImportsLocator {
8787
/// Finds all imports for the given name and the module that contains this name.
88-
fn find_imports(
89-
&mut self,
90-
name_to_import: InFile<&NameRef>,
91-
module_with_name_to_import: Module,
92-
) -> Option<Vec<ModPath>>;
88+
fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef>;
9389
}
9490

9591
/// Return all the assists applicable at the given position

crates/ra_hir/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub use hir_def::{
5858
type_ref::Mutability,
5959
};
6060
pub use hir_expand::{
61-
name, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin,
61+
name::{AsName, Name},
62+
HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin,
6263
};
6364
pub use hir_ty::{display::HirDisplay, CallableDef};

crates/ra_ide/src/imports_locator.rs

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
44
use crate::{
55
db::RootDatabase,
6-
references::{classify_name, classify_name_ref, NameDefinition, NameKind},
6+
references::{classify_name, NameDefinition, NameKind},
77
symbol_index::{self, FileSymbol},
88
Query,
99
};
10-
use ast::NameRef;
11-
use hir::{db::HirDatabase, InFile, ModPath, Module, SourceBinder};
12-
use itertools::Itertools;
10+
use hir::{db::HirDatabase, ModuleDef, SourceBinder};
1311
use ra_assists::ImportsLocator;
1412
use ra_prof::profile;
1513
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
@@ -23,14 +21,30 @@ impl<'a> ImportsLocatorIde<'a> {
2321
Self { source_binder: SourceBinder::new(db) }
2422
}
2523

26-
fn search_for_imports(
24+
fn get_name_definition(
2725
&mut self,
28-
name_to_import: &ast::NameRef,
29-
module_with_name_to_import: Module,
30-
) -> Vec<ModPath> {
26+
db: &impl HirDatabase,
27+
import_candidate: &FileSymbol,
28+
) -> Option<NameDefinition> {
29+
let _p = profile("get_name_definition");
30+
let file_id = import_candidate.file_id.into();
31+
let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?);
32+
let candidate_name_node = if candidate_node.kind() != NAME {
33+
candidate_node.children().find(|it| it.kind() == NAME)?
34+
} else {
35+
candidate_node
36+
};
37+
classify_name(
38+
&mut self.source_binder,
39+
hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? },
40+
)
41+
}
42+
}
43+
44+
impl<'a> ImportsLocator for ImportsLocatorIde<'a> {
45+
fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> {
3146
let _p = profile("search_for_imports");
3247
let db = self.source_binder.db;
33-
let name_to_import = name_to_import.text();
3448

3549
let project_results = {
3650
let mut query = Query::new(name_to_import.to_string());
@@ -52,47 +66,11 @@ impl<'a> ImportsLocatorIde<'a> {
5266
.filter_map(|import_candidate| self.get_name_definition(db, &import_candidate))
5367
.filter_map(|name_definition_to_import| {
5468
if let NameKind::Def(module_def) = name_definition_to_import.kind {
55-
module_with_name_to_import.find_use_path(db, module_def)
69+
Some(module_def)
5670
} else {
5771
None
5872
}
5973
})
60-
.filter(|use_path| !use_path.segments.is_empty())
61-
.unique()
62-
.take(20)
6374
.collect()
6475
}
65-
66-
fn get_name_definition(
67-
&mut self,
68-
db: &impl HirDatabase,
69-
import_candidate: &FileSymbol,
70-
) -> Option<NameDefinition> {
71-
let _p = profile("get_name_definition");
72-
let file_id = import_candidate.file_id.into();
73-
let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?);
74-
let candidate_name_node = if candidate_node.kind() != NAME {
75-
candidate_node.children().find(|it| it.kind() == NAME)?
76-
} else {
77-
candidate_node
78-
};
79-
classify_name(
80-
&mut self.source_binder,
81-
hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? },
82-
)
83-
}
84-
}
85-
86-
impl<'a> ImportsLocator for ImportsLocatorIde<'a> {
87-
fn find_imports(
88-
&mut self,
89-
name_to_import: InFile<&NameRef>,
90-
module_with_name_to_import: Module,
91-
) -> Option<Vec<ModPath>> {
92-
if classify_name_ref(&mut self.source_binder, name_to_import).is_none() {
93-
Some(self.search_for_imports(name_to_import.value, module_with_name_to_import))
94-
} else {
95-
None
96-
}
97-
}
9876
}

0 commit comments

Comments
 (0)