1mod config;
4mod item;
5mod context;
6mod patterns;
7mod generated_lint_completions;
8#[cfg(test)]
9mod test_utils;
10mod render;
11
12mod completions;
13
14use completions::flyimport::position_for_import;
15use ide_db::{
16 base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase,
17};
18use text_edit::TextEdit;
19
20use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
21
22pub use crate::{
23 config::CompletionConfig,
24 item::{CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, InsertTextFormat},
25};
26
27pub fn completions(
103 db: &RootDatabase,
104 config: &CompletionConfig,
105 position: FilePosition,
106) -> Option<Completions> {
107 let ctx = CompletionContext::new(db, position, config)?;
108
109 if ctx.no_completion_required() {
110 return None;
112 }
113
114 let mut acc = Completions::default();
115 completions::attribute::complete_attribute(&mut acc, &ctx);
116 completions::fn_param::complete_fn_param(&mut acc, &ctx);
117 completions::keyword::complete_expr_keyword(&mut acc, &ctx);
118 completions::keyword::complete_use_tree_keyword(&mut acc, &ctx);
119 completions::snippet::complete_expr_snippet(&mut acc, &ctx);
120 completions::snippet::complete_item_snippet(&mut acc, &ctx);
121 completions::qualified_path::complete_qualified_path(&mut acc, &ctx);
122 completions::unqualified_path::complete_unqualified_path(&mut acc, &ctx);
123 completions::dot::complete_dot(&mut acc, &ctx);
124 completions::record::complete_record(&mut acc, &ctx);
125 completions::pattern::complete_pattern(&mut acc, &ctx);
126 completions::postfix::complete_postfix(&mut acc, &ctx);
127 completions::macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
128 completions::trait_impl::complete_trait_impl(&mut acc, &ctx);
129 completions::mod_::complete_mod(&mut acc, &ctx);
130 completions::flyimport::import_on_the_fly(&mut acc, &ctx);
131
132 Some(acc)
133}
134
135pub fn resolve_completion_edits(
137 db: &RootDatabase,
138 config: &CompletionConfig,
139 position: FilePosition,
140 full_import_path: &str,
141 imported_name: String,
142 import_for_trait_assoc_item: bool,
143) -> Option<Vec<TextEdit>> {
144 let ctx = CompletionContext::new(db, position, config)?;
145 let position_for_import = position_for_import(&ctx, None)?;
146 let import_scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?;
147
148 let current_module = ctx.sema.scope(position_for_import).module()?;
149 let current_crate = current_module.krate();
150
151 let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name)
152 .filter_map(|candidate| {
153 let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
154 current_module.find_use_path(db, item)
155 })
156 .find(|mod_path| mod_path.to_string() == full_import_path)?;
157
158 ImportEdit { import_path, import_scope, import_for_trait_assoc_item }
159 .to_text_edit(config.insert_use.merge)
160 .map(|edit| vec![edit])
161}
162
163#[cfg(test)]
164mod tests {
165 use crate::test_utils::{self, TEST_CONFIG};
166
167 struct DetailAndDocumentation<'a> {
168 detail: &'a str,
169 documentation: &'a str,
170 }
171
172 fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) {
173 let (db, position) = test_utils::position(ra_fixture);
174 let config = TEST_CONFIG;
175 let completions: Vec<_> = crate::completions(&db, &config, position).unwrap().into();
176 for item in completions {
177 if item.detail() == Some(expected.detail) {
178 let opt = item.documentation();
179 let doc = opt.as_ref().map(|it| it.as_str());
180 assert_eq!(doc, Some(expected.documentation));
181 return;
182 }
183 }
184 panic!("completion detail not found: {}", expected.detail)
185 }
186
187 fn check_no_completion(ra_fixture: &str) {
188 let (db, position) = test_utils::position(ra_fixture);
189 let config = TEST_CONFIG;
190
191 let completions: Option<Vec<String>> = crate::completions(&db, &config, position)
192 .and_then(|completions| {
193 let completions: Vec<_> = completions.into();
194 if completions.is_empty() {
195 None
196 } else {
197 Some(completions)
198 }
199 })
200 .map(|completions| {
201 completions.into_iter().map(|completion| format!("{:?}", completion)).collect()
202 });
203
204 assert_eq!(completions, None, "Completions were generated, but weren't expected");
206 }
207
208 #[test]
209 fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
210 check_detail_and_documentation(
211 r#"
212macro_rules! bar {
213 () => {
214 struct Bar;
215 impl Bar {
216 #[doc = "Do the foo"]
217 fn foo(&self) {}
218 }
219 }
220}
221
222bar!();
223
224fn foo() {
225 let bar = Bar;
226 bar.fo$0;
227}
228"#,
229 DetailAndDocumentation { detail: "-> ()", documentation: "Do the foo" },
230 );
231 }
232
233 #[test]
234 fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
235 check_detail_and_documentation(
236 r#"
237macro_rules! bar {
238 () => {
239 struct Bar;
240 impl Bar {
241 /// Do the foo
242 fn foo(&self) {}
243 }
244 }
245}
246
247bar!();
248
249fn foo() {
250 let bar = Bar;
251 bar.fo$0;
252}
253"#,
254 DetailAndDocumentation { detail: "-> ()", documentation: " Do the foo" },
255 );
256 }
257
258 #[test]
259 fn test_no_completions_required() {
260 check_no_completion(r#"fn foo() { for i i$0 }"#);
262 check_detail_and_documentation(
264 r#"
265/// Do the foo
266fn foo() -> &'static str { "foo" }
267
268fn bar() {
269 for c in fo$0
270}
271"#,
272 DetailAndDocumentation { detail: "-> &str", documentation: "Do the foo" },
273 );
274 }
275}