diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/handlers/auto_import.rs | 112 |
1 files changed, 92 insertions, 20 deletions
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 27d96b941..a25f0650d 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -1,10 +1,11 @@ | |||
1 | use ra_ide_db::imports_locator::ImportsLocator; | 1 | use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; |
2 | use ra_syntax::ast::{self, AstNode}; | 2 | use ra_syntax::ast::{self, AstNode}; |
3 | 3 | ||
4 | use crate::{ | 4 | use crate::{ |
5 | assist_ctx::{Assist, AssistCtx}, | 5 | assist_ctx::{Assist, AssistCtx}, |
6 | insert_use_statement, AssistId, | 6 | insert_use_statement, AssistId, |
7 | }; | 7 | }; |
8 | use hir::{db::HirDatabase, Adt, ModPath, Module, ModuleDef, PathResolution, SourceAnalyzer}; | ||
8 | use std::collections::BTreeSet; | 9 | use std::collections::BTreeSet; |
9 | 10 | ||
10 | // Assist: auto_import | 11 | // Assist: auto_import |
@@ -44,29 +45,13 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> { | |||
44 | let source_analyzer = ctx.source_analyzer(&position, None); | 45 | let source_analyzer = ctx.source_analyzer(&position, None); |
45 | let module_with_name_to_import = source_analyzer.module()?; | 46 | let module_with_name_to_import = source_analyzer.module()?; |
46 | 47 | ||
47 | let name_ref_to_import = | 48 | let import_candidate = ImportCandidate::new(&path_under_caret, &source_analyzer, ctx.db)?; |
48 | path_under_caret.syntax().descendants().find_map(ast::NameRef::cast)?; | 49 | let proposed_imports = import_candidate.search_for_imports(ctx.db, module_with_name_to_import); |
49 | if source_analyzer | ||
50 | .resolve_path(ctx.db, &name_ref_to_import.syntax().ancestors().find_map(ast::Path::cast)?) | ||
51 | .is_some() | ||
52 | { | ||
53 | return None; | ||
54 | } | ||
55 | |||
56 | let name_to_import = name_ref_to_import.syntax().to_string(); | ||
57 | let proposed_imports = ImportsLocator::new(ctx.db) | ||
58 | .find_imports(&name_to_import) | ||
59 | .into_iter() | ||
60 | .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def)) | ||
61 | .filter(|use_path| !use_path.segments.is_empty()) | ||
62 | .take(20) | ||
63 | .collect::<BTreeSet<_>>(); | ||
64 | |||
65 | if proposed_imports.is_empty() { | 50 | if proposed_imports.is_empty() { |
66 | return None; | 51 | return None; |
67 | } | 52 | } |
68 | 53 | ||
69 | let mut group = ctx.add_assist_group(format!("Import {}", name_to_import)); | 54 | let mut group = ctx.add_assist_group(format!("Import {}", import_candidate.get_search_query())); |
70 | for import in proposed_imports { | 55 | for import in proposed_imports { |
71 | group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| { | 56 | group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| { |
72 | edit.target(path_under_caret.syntax().text_range()); | 57 | edit.target(path_under_caret.syntax().text_range()); |
@@ -81,6 +66,92 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> { | |||
81 | group.finish() | 66 | group.finish() |
82 | } | 67 | } |
83 | 68 | ||
69 | #[derive(Debug)] | ||
70 | // TODO kb rustdocs | ||
71 | enum ImportCandidate { | ||
72 | UnqualifiedName(ast::NameRef), | ||
73 | QualifierStart(ast::NameRef), | ||
74 | TraitFunction(Adt, ast::PathSegment), | ||
75 | } | ||
76 | |||
77 | impl ImportCandidate { | ||
78 | // TODO kb refactor this mess | ||
79 | fn new( | ||
80 | path_under_caret: &ast::Path, | ||
81 | source_analyzer: &SourceAnalyzer, | ||
82 | db: &impl HirDatabase, | ||
83 | ) -> Option<Self> { | ||
84 | if source_analyzer.resolve_path(db, path_under_caret).is_some() { | ||
85 | return None; | ||
86 | } | ||
87 | |||
88 | let segment = path_under_caret.segment()?; | ||
89 | if let Some(qualifier) = path_under_caret.qualifier() { | ||
90 | let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; | ||
91 | let qualifier_start_path = | ||
92 | qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; | ||
93 | if let Some(qualifier_start_resolution) = | ||
94 | source_analyzer.resolve_path(db, &qualifier_start_path) | ||
95 | { | ||
96 | let qualifier_resolution = if &qualifier_start_path == path_under_caret { | ||
97 | qualifier_start_resolution | ||
98 | } else { | ||
99 | source_analyzer.resolve_path(db, &qualifier)? | ||
100 | }; | ||
101 | if let PathResolution::Def(ModuleDef::Adt(function_callee)) = qualifier_resolution { | ||
102 | Some(ImportCandidate::TraitFunction(function_callee, segment)) | ||
103 | } else { | ||
104 | None | ||
105 | } | ||
106 | } else { | ||
107 | Some(ImportCandidate::QualifierStart(qualifier_start)) | ||
108 | } | ||
109 | } else { | ||
110 | if source_analyzer.resolve_path(db, path_under_caret).is_none() { | ||
111 | Some(ImportCandidate::UnqualifiedName( | ||
112 | segment.syntax().descendants().find_map(ast::NameRef::cast)?, | ||
113 | )) | ||
114 | } else { | ||
115 | None | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | |||
120 | fn get_search_query(&self) -> String { | ||
121 | match self { | ||
122 | ImportCandidate::UnqualifiedName(name_ref) | ||
123 | | ImportCandidate::QualifierStart(name_ref) => name_ref.syntax().to_string(), | ||
124 | ImportCandidate::TraitFunction(_, trait_function) => { | ||
125 | trait_function.syntax().to_string() | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | |||
130 | fn search_for_imports( | ||
131 | &self, | ||
132 | db: &RootDatabase, | ||
133 | module_with_name_to_import: Module, | ||
134 | ) -> BTreeSet<ModPath> { | ||
135 | ImportsLocator::new(db) | ||
136 | .find_imports(&self.get_search_query()) | ||
137 | .into_iter() | ||
138 | .filter_map(|module_def| match self { | ||
139 | ImportCandidate::TraitFunction(function_callee, _) => { | ||
140 | if let ModuleDef::Function(function) = module_def { | ||
141 | dbg!(function); | ||
142 | todo!() | ||
143 | } else { | ||
144 | None | ||
145 | } | ||
146 | } | ||
147 | _ => module_with_name_to_import.find_use_path(db, module_def), | ||
148 | }) | ||
149 | .filter(|use_path| !use_path.segments.is_empty()) | ||
150 | .take(20) | ||
151 | .collect::<BTreeSet<_>>() | ||
152 | } | ||
153 | } | ||
154 | |||
84 | #[cfg(test)] | 155 | #[cfg(test)] |
85 | mod tests { | 156 | mod tests { |
86 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; | 157 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; |
@@ -381,6 +452,7 @@ mod tests { | |||
381 | } | 452 | } |
382 | 453 | ||
383 | #[test] | 454 | #[test] |
455 | #[ignore] // TODO kb | ||
384 | fn trait_method() { | 456 | fn trait_method() { |
385 | check_assist( | 457 | check_assist( |
386 | auto_import, | 458 | auto_import, |