diff options
Diffstat (limited to 'crates/completion/src/lib.rs')
-rw-r--r-- | crates/completion/src/lib.rs | 285 |
1 files changed, 0 insertions, 285 deletions
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs deleted file mode 100644 index ee1b822e7..000000000 --- a/crates/completion/src/lib.rs +++ /dev/null | |||
@@ -1,285 +0,0 @@ | |||
1 | //! `completions` crate provides utilities for generating completions of user input. | ||
2 | |||
3 | mod config; | ||
4 | mod item; | ||
5 | mod context; | ||
6 | mod patterns; | ||
7 | mod generated_lint_completions; | ||
8 | #[cfg(test)] | ||
9 | mod test_utils; | ||
10 | mod render; | ||
11 | |||
12 | mod completions; | ||
13 | |||
14 | use ide_db::{ | ||
15 | base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase, | ||
16 | }; | ||
17 | use syntax::AstNode; | ||
18 | use text_edit::TextEdit; | ||
19 | |||
20 | use crate::{completions::Completions, context::CompletionContext, item::CompletionKind}; | ||
21 | |||
22 | pub use crate::{ | ||
23 | config::CompletionConfig, | ||
24 | item::{CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, InsertTextFormat}, | ||
25 | }; | ||
26 | |||
27 | //FIXME: split the following feature into fine-grained features. | ||
28 | |||
29 | // Feature: Magic Completions | ||
30 | // | ||
31 | // In addition to usual reference completion, rust-analyzer provides some ✨magic✨ | ||
32 | // completions as well: | ||
33 | // | ||
34 | // Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor | ||
35 | // is placed at the appropriate position. Even though `if` is easy to type, you | ||
36 | // still want to complete it, to get ` { }` for free! `return` is inserted with a | ||
37 | // space or `;` depending on the return type of the function. | ||
38 | // | ||
39 | // When completing a function call, `()` are automatically inserted. If a function | ||
40 | // takes arguments, the cursor is positioned inside the parenthesis. | ||
41 | // | ||
42 | // There are postfix completions, which can be triggered by typing something like | ||
43 | // `foo().if`. The word after `.` determines postfix completion. Possible variants are: | ||
44 | // | ||
45 | // - `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result` | ||
46 | // - `expr.match` -> `match expr {}` | ||
47 | // - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result` | ||
48 | // - `expr.ref` -> `&expr` | ||
49 | // - `expr.refm` -> `&mut expr` | ||
50 | // - `expr.let` -> `let $0 = expr;` | ||
51 | // - `expr.letm` -> `let mut $0 = expr;` | ||
52 | // - `expr.not` -> `!expr` | ||
53 | // - `expr.dbg` -> `dbg!(expr)` | ||
54 | // - `expr.dbgr` -> `dbg!(&expr)` | ||
55 | // - `expr.call` -> `(expr)` | ||
56 | // | ||
57 | // There also snippet completions: | ||
58 | // | ||
59 | // .Expressions | ||
60 | // - `pd` -> `eprintln!(" = {:?}", );` | ||
61 | // - `ppd` -> `eprintln!(" = {:#?}", );` | ||
62 | // | ||
63 | // .Items | ||
64 | // - `tfn` -> `#[test] fn feature(){}` | ||
65 | // - `tmod` -> | ||
66 | // ```rust | ||
67 | // #[cfg(test)] | ||
68 | // mod tests { | ||
69 | // use super::*; | ||
70 | // | ||
71 | // #[test] | ||
72 | // fn test_name() {} | ||
73 | // } | ||
74 | // ``` | ||
75 | // | ||
76 | // And the auto import completions, enabled with the `rust-analyzer.completion.autoimport.enable` setting and the corresponding LSP client capabilities. | ||
77 | // Those are the additional completion options with automatic `use` import and options from all project importable items, | ||
78 | // fuzzy matched agains the completion imput. | ||
79 | |||
80 | /// Main entry point for completion. We run completion as a two-phase process. | ||
81 | /// | ||
82 | /// First, we look at the position and collect a so-called `CompletionContext. | ||
83 | /// This is a somewhat messy process, because, during completion, syntax tree is | ||
84 | /// incomplete and can look really weird. | ||
85 | /// | ||
86 | /// Once the context is collected, we run a series of completion routines which | ||
87 | /// look at the context and produce completion items. One subtlety about this | ||
88 | /// phase is that completion engine should not filter by the substring which is | ||
89 | /// already present, it should give all possible variants for the identifier at | ||
90 | /// the caret. In other words, for | ||
91 | /// | ||
92 | /// ```no_run | ||
93 | /// fn f() { | ||
94 | /// let foo = 92; | ||
95 | /// let _ = bar$0 | ||
96 | /// } | ||
97 | /// ``` | ||
98 | /// | ||
99 | /// `foo` *should* be present among the completion variants. Filtering by | ||
100 | /// identifier prefix/fuzzy match should be done higher in the stack, together | ||
101 | /// with ordering of completions (currently this is done by the client). | ||
102 | pub 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 | // No work required here. | ||
111 | 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 | |||
135 | /// Resolves additional completion data at the position given. | ||
136 | pub fn resolve_completion_edits( | ||
137 | db: &RootDatabase, | ||
138 | config: &CompletionConfig, | ||
139 | position: FilePosition, | ||
140 | full_import_path: &str, | ||
141 | imported_name: String, | ||
142 | ) -> Option<Vec<TextEdit>> { | ||
143 | let ctx = CompletionContext::new(db, position, config)?; | ||
144 | let anchor = ctx.name_ref_syntax.as_ref()?; | ||
145 | let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; | ||
146 | |||
147 | let current_module = ctx.sema.scope(anchor.syntax()).module()?; | ||
148 | let current_crate = current_module.krate(); | ||
149 | |||
150 | let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name) | ||
151 | .filter_map(|candidate| { | ||
152 | let item: hir::ItemInNs = candidate.either(Into::into, Into::into); | ||
153 | current_module.find_use_path(db, item) | ||
154 | }) | ||
155 | .find(|mod_path| mod_path.to_string() == full_import_path)?; | ||
156 | |||
157 | ImportEdit { import_path, import_scope } | ||
158 | .to_text_edit(config.insert_use.merge) | ||
159 | .map(|edit| vec![edit]) | ||
160 | } | ||
161 | |||
162 | #[cfg(test)] | ||
163 | mod tests { | ||
164 | use crate::test_utils::{self, TEST_CONFIG}; | ||
165 | |||
166 | struct DetailAndDocumentation<'a> { | ||
167 | detail: &'a str, | ||
168 | documentation: &'a str, | ||
169 | } | ||
170 | |||
171 | fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) { | ||
172 | let (db, position) = test_utils::position(ra_fixture); | ||
173 | let config = TEST_CONFIG; | ||
174 | let completions: Vec<_> = crate::completions(&db, &config, position).unwrap().into(); | ||
175 | for item in completions { | ||
176 | if item.detail() == Some(expected.detail) { | ||
177 | let opt = item.documentation(); | ||
178 | let doc = opt.as_ref().map(|it| it.as_str()); | ||
179 | assert_eq!(doc, Some(expected.documentation)); | ||
180 | return; | ||
181 | } | ||
182 | } | ||
183 | panic!("completion detail not found: {}", expected.detail) | ||
184 | } | ||
185 | |||
186 | fn check_no_completion(ra_fixture: &str) { | ||
187 | let (db, position) = test_utils::position(ra_fixture); | ||
188 | let config = TEST_CONFIG; | ||
189 | |||
190 | let completions: Option<Vec<String>> = crate::completions(&db, &config, position) | ||
191 | .and_then(|completions| { | ||
192 | let completions: Vec<_> = completions.into(); | ||
193 | if completions.is_empty() { | ||
194 | None | ||
195 | } else { | ||
196 | Some(completions) | ||
197 | } | ||
198 | }) | ||
199 | .map(|completions| { | ||
200 | completions.into_iter().map(|completion| format!("{:?}", completion)).collect() | ||
201 | }); | ||
202 | |||
203 | // `assert_eq` instead of `assert!(completions.is_none())` to get the list of completions if test will panic. | ||
204 | assert_eq!(completions, None, "Completions were generated, but weren't expected"); | ||
205 | } | ||
206 | |||
207 | #[test] | ||
208 | fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() { | ||
209 | check_detail_and_documentation( | ||
210 | r#" | ||
211 | //- /lib.rs | ||
212 | macro_rules! bar { | ||
213 | () => { | ||
214 | struct Bar; | ||
215 | impl Bar { | ||
216 | #[doc = "Do the foo"] | ||
217 | fn foo(&self) {} | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | bar!(); | ||
223 | |||
224 | fn foo() { | ||
225 | let bar = Bar; | ||
226 | bar.fo$0; | ||
227 | } | ||
228 | "#, | ||
229 | DetailAndDocumentation { detail: "fn foo(&self)", 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#" | ||
237 | //- /lib.rs | ||
238 | macro_rules! bar { | ||
239 | () => { | ||
240 | struct Bar; | ||
241 | impl Bar { | ||
242 | /// Do the foo | ||
243 | fn foo(&self) {} | ||
244 | } | ||
245 | } | ||
246 | } | ||
247 | |||
248 | bar!(); | ||
249 | |||
250 | fn foo() { | ||
251 | let bar = Bar; | ||
252 | bar.fo$0; | ||
253 | } | ||
254 | "#, | ||
255 | DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" }, | ||
256 | ); | ||
257 | } | ||
258 | |||
259 | #[test] | ||
260 | fn test_no_completions_required() { | ||
261 | // There must be no hint for 'in' keyword. | ||
262 | check_no_completion( | ||
263 | r#" | ||
264 | fn foo() { | ||
265 | for i i$0 | ||
266 | } | ||
267 | "#, | ||
268 | ); | ||
269 | // After 'in' keyword hints may be spawned. | ||
270 | check_detail_and_documentation( | ||
271 | r#" | ||
272 | /// Do the foo | ||
273 | fn foo() -> &'static str { "foo" } | ||
274 | |||
275 | fn bar() { | ||
276 | for c in fo$0 | ||
277 | } | ||
278 | "#, | ||
279 | DetailAndDocumentation { | ||
280 | detail: "fn foo() -> &'static str", | ||
281 | documentation: "Do the foo", | ||
282 | }, | ||
283 | ); | ||
284 | } | ||
285 | } | ||