aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2019-12-24 00:19:09 +0000
committerKirill Bulatov <[email protected]>2020-01-26 22:16:29 +0000
commit316795e074dff8f627f8c70c85d236420ecfb3a6 (patch)
treeda6e266139563ef314d0563a01723ae2264609d2 /crates/ra_ide/src
parentd1330a4a65f0113c687716a5a679239af4df9c11 (diff)
Initial auto import action implementation
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/assists.rs7
-rw-r--r--crates/ra_ide/src/imports_locator.rs97
-rw-r--r--crates/ra_ide/src/lib.rs1
3 files changed, 102 insertions, 3 deletions
diff --git a/crates/ra_ide/src/assists.rs b/crates/ra_ide/src/assists.rs
index a936900da..c43c45c65 100644
--- a/crates/ra_ide/src/assists.rs
+++ b/crates/ra_ide/src/assists.rs
@@ -2,8 +2,9 @@
2 2
3use ra_db::{FilePosition, FileRange}; 3use ra_db::{FilePosition, FileRange};
4 4
5use crate::{db::RootDatabase, FileId, SourceChange, SourceFileEdit}; 5use crate::{
6 6 db::RootDatabase, imports_locator::ImportsLocatorIde, FileId, SourceChange, SourceFileEdit,
7};
7use either::Either; 8use either::Either;
8pub use ra_assists::AssistId; 9pub use ra_assists::AssistId;
9use ra_assists::{AssistAction, AssistLabel}; 10use ra_assists::{AssistAction, AssistLabel};
@@ -16,7 +17,7 @@ pub struct Assist {
16} 17}
17 18
18pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> { 19pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> {
19 ra_assists::assists(db, frange) 20 ra_assists::assists_with_imports_locator(db, frange, ImportsLocatorIde::new(db))
20 .into_iter() 21 .into_iter()
21 .map(|assist| { 22 .map(|assist| {
22 let file_id = frange.file_id; 23 let file_id = frange.file_id;
diff --git a/crates/ra_ide/src/imports_locator.rs b/crates/ra_ide/src/imports_locator.rs
new file mode 100644
index 000000000..23391ac3b
--- /dev/null
+++ b/crates/ra_ide/src/imports_locator.rs
@@ -0,0 +1,97 @@
1//! This module contains an import search funcionality that is provided to the ra_assists module.
2//! Later, this should be moved away to a separate crate that is accessible from the ra_assists module.
3
4use crate::{
5 db::RootDatabase,
6 references::{classify_name, classify_name_ref, NameDefinition, NameKind},
7 symbol_index::{self, FileSymbol},
8 Query,
9};
10use ast::NameRef;
11use hir::{db::HirDatabase, InFile, ModPath, Module, SourceBinder};
12use itertools::Itertools;
13use ra_assists::ImportsLocator;
14use ra_prof::profile;
15use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
16
17pub(crate) struct ImportsLocatorIde<'a> {
18 source_binder: SourceBinder<'a, RootDatabase>,
19}
20
21impl<'a> ImportsLocatorIde<'a> {
22 pub(crate) fn new(db: &'a RootDatabase) -> Self {
23 Self { source_binder: SourceBinder::new(db) }
24 }
25
26 fn search_for_imports(
27 &mut self,
28 name_to_import: &ast::NameRef,
29 module_with_name_to_import: Module,
30 ) -> Vec<ModPath> {
31 let _p = profile("search_for_imports");
32 let db = self.source_binder.db;
33 let name_to_import = name_to_import.text();
34
35 let project_results = {
36 let mut query = Query::new(name_to_import.to_string());
37 query.exact();
38 query.limit(10);
39 symbol_index::world_symbols(db, query)
40 };
41 let lib_results = {
42 let mut query = Query::new(name_to_import.to_string());
43 query.libs();
44 query.exact();
45 query.limit(10);
46 symbol_index::world_symbols(db, query)
47 };
48
49 project_results
50 .into_iter()
51 .chain(lib_results.into_iter())
52 .filter_map(|import_candidate| self.get_name_definition(db, &import_candidate))
53 .filter_map(|name_definition_to_import| {
54 if let NameKind::Def(module_def) = name_definition_to_import.kind {
55 module_with_name_to_import.find_use_path(db, module_def)
56 } else {
57 None
58 }
59 })
60 .filter(|use_path| !use_path.segments.is_empty())
61 .unique()
62 .collect()
63 }
64
65 fn get_name_definition(
66 &mut self,
67 db: &impl HirDatabase,
68 import_candidate: &FileSymbol,
69 ) -> Option<NameDefinition> {
70 let _p = profile("get_name_definition");
71 let file_id = import_candidate.file_id.into();
72 let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?);
73 let candidate_name_node = if candidate_node.kind() != NAME {
74 candidate_node.children().find(|it| it.kind() == NAME)?
75 } else {
76 candidate_node
77 };
78 classify_name(
79 &mut self.source_binder,
80 hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? },
81 )
82 }
83}
84
85impl<'a> ImportsLocator<'a> for ImportsLocatorIde<'a> {
86 fn find_imports(
87 &mut self,
88 name_to_import: InFile<&NameRef>,
89 module_with_name_to_import: Module,
90 ) -> Option<Vec<ModPath>> {
91 if classify_name_ref(&mut self.source_binder, name_to_import).is_none() {
92 Some(self.search_for_imports(name_to_import.value, module_with_name_to_import))
93 } else {
94 None
95 }
96 }
97}
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 62fe6d2a9..03ad6b2c1 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -30,6 +30,7 @@ mod syntax_highlighting;
30mod parent_module; 30mod parent_module;
31mod references; 31mod references;
32mod impls; 32mod impls;
33mod imports_locator;
33mod assists; 34mod assists;
34mod diagnostics; 35mod diagnostics;
35mod syntax_tree; 36mod syntax_tree;