diff options
author | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
---|---|---|
committer | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
commit | 178c3e135a2a249692f7784712492e7884ae0c00 (patch) | |
tree | ac6b769dbf7162150caa0c1624786a4dd79ff3be /crates/ra_ide/src/completion.rs | |
parent | 06ff8e6c760ff05f10e868b5d1f9d79e42fbb49c (diff) | |
parent | c2594daf2974dbd4ce3d9b7ec72481764abaceb5 (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'crates/ra_ide/src/completion.rs')
-rw-r--r-- | crates/ra_ide/src/completion.rs | 211 |
1 files changed, 0 insertions, 211 deletions
diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs deleted file mode 100644 index 9c33a5a43..000000000 --- a/crates/ra_ide/src/completion.rs +++ /dev/null | |||
@@ -1,211 +0,0 @@ | |||
1 | mod completion_config; | ||
2 | mod completion_item; | ||
3 | mod completion_context; | ||
4 | mod presentation; | ||
5 | mod patterns; | ||
6 | #[cfg(test)] | ||
7 | mod test_utils; | ||
8 | |||
9 | mod complete_attribute; | ||
10 | mod complete_dot; | ||
11 | mod complete_record; | ||
12 | mod complete_pattern; | ||
13 | mod complete_fn_param; | ||
14 | mod complete_keyword; | ||
15 | mod complete_snippet; | ||
16 | mod complete_qualified_path; | ||
17 | mod complete_unqualified_path; | ||
18 | mod complete_postfix; | ||
19 | mod complete_macro_in_item_position; | ||
20 | mod complete_trait_impl; | ||
21 | mod unstable_feature_descriptor; | ||
22 | use ra_ide_db::RootDatabase; | ||
23 | |||
24 | use crate::{ | ||
25 | completion::{ | ||
26 | completion_context::CompletionContext, | ||
27 | completion_item::{CompletionKind, Completions}, | ||
28 | }, | ||
29 | FilePosition, | ||
30 | }; | ||
31 | |||
32 | //FIXME: cyclic imports caused by xtask generation, this should be better | ||
33 | use crate::completion::{ | ||
34 | complete_attribute::LintCompletion, unstable_feature_descriptor::UNSTABLE_FEATURE_DESCRIPTOR, | ||
35 | }; | ||
36 | |||
37 | pub use crate::completion::{ | ||
38 | completion_config::CompletionConfig, | ||
39 | completion_item::{CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat}, | ||
40 | }; | ||
41 | |||
42 | //FIXME: split the following feature into fine-grained features. | ||
43 | |||
44 | // Feature: Magic Completions | ||
45 | // | ||
46 | // In addition to usual reference completion, rust-analyzer provides some ✨magic✨ | ||
47 | // completions as well: | ||
48 | // | ||
49 | // Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor | ||
50 | // is placed at the appropriate position. Even though `if` is easy to type, you | ||
51 | // still want to complete it, to get ` { }` for free! `return` is inserted with a | ||
52 | // space or `;` depending on the return type of the function. | ||
53 | // | ||
54 | // When completing a function call, `()` are automatically inserted. If a function | ||
55 | // takes arguments, the cursor is positioned inside the parenthesis. | ||
56 | // | ||
57 | // There are postfix completions, which can be triggered by typing something like | ||
58 | // `foo().if`. The word after `.` determines postfix completion. Possible variants are: | ||
59 | // | ||
60 | // - `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result` | ||
61 | // - `expr.match` -> `match expr {}` | ||
62 | // - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result` | ||
63 | // - `expr.ref` -> `&expr` | ||
64 | // - `expr.refm` -> `&mut expr` | ||
65 | // - `expr.not` -> `!expr` | ||
66 | // - `expr.dbg` -> `dbg!(expr)` | ||
67 | // | ||
68 | // There also snippet completions: | ||
69 | // | ||
70 | // .Expressions | ||
71 | // - `pd` -> `eprintln!(" = {:?}", );` | ||
72 | // - `ppd` -> `eprintln!(" = {:#?}", );` | ||
73 | // | ||
74 | // .Items | ||
75 | // - `tfn` -> `#[test] fn feature(){}` | ||
76 | // - `tmod` -> | ||
77 | // ```rust | ||
78 | // #[cfg(test)] | ||
79 | // mod tests { | ||
80 | // use super::*; | ||
81 | // | ||
82 | // #[test] | ||
83 | // fn test_name() {} | ||
84 | // } | ||
85 | // ``` | ||
86 | |||
87 | /// Main entry point for completion. We run completion as a two-phase process. | ||
88 | /// | ||
89 | /// First, we look at the position and collect a so-called `CompletionContext. | ||
90 | /// This is a somewhat messy process, because, during completion, syntax tree is | ||
91 | /// incomplete and can look really weird. | ||
92 | /// | ||
93 | /// Once the context is collected, we run a series of completion routines which | ||
94 | /// look at the context and produce completion items. One subtlety about this | ||
95 | /// phase is that completion engine should not filter by the substring which is | ||
96 | /// already present, it should give all possible variants for the identifier at | ||
97 | /// the caret. In other words, for | ||
98 | /// | ||
99 | /// ```no-run | ||
100 | /// fn f() { | ||
101 | /// let foo = 92; | ||
102 | /// let _ = bar<|> | ||
103 | /// } | ||
104 | /// ``` | ||
105 | /// | ||
106 | /// `foo` *should* be present among the completion variants. Filtering by | ||
107 | /// identifier prefix/fuzzy match should be done higher in the stack, together | ||
108 | /// with ordering of completions (currently this is done by the client). | ||
109 | pub(crate) fn completions( | ||
110 | db: &RootDatabase, | ||
111 | config: &CompletionConfig, | ||
112 | position: FilePosition, | ||
113 | ) -> Option<Completions> { | ||
114 | let ctx = CompletionContext::new(db, position, config)?; | ||
115 | |||
116 | let mut acc = Completions::default(); | ||
117 | complete_attribute::complete_attribute(&mut acc, &ctx); | ||
118 | complete_fn_param::complete_fn_param(&mut acc, &ctx); | ||
119 | complete_keyword::complete_expr_keyword(&mut acc, &ctx); | ||
120 | complete_keyword::complete_use_tree_keyword(&mut acc, &ctx); | ||
121 | complete_snippet::complete_expr_snippet(&mut acc, &ctx); | ||
122 | complete_snippet::complete_item_snippet(&mut acc, &ctx); | ||
123 | complete_qualified_path::complete_qualified_path(&mut acc, &ctx); | ||
124 | complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx); | ||
125 | complete_dot::complete_dot(&mut acc, &ctx); | ||
126 | complete_record::complete_record(&mut acc, &ctx); | ||
127 | complete_pattern::complete_pattern(&mut acc, &ctx); | ||
128 | complete_postfix::complete_postfix(&mut acc, &ctx); | ||
129 | complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); | ||
130 | complete_trait_impl::complete_trait_impl(&mut acc, &ctx); | ||
131 | |||
132 | Some(acc) | ||
133 | } | ||
134 | |||
135 | #[cfg(test)] | ||
136 | mod tests { | ||
137 | use crate::completion::completion_config::CompletionConfig; | ||
138 | use crate::mock_analysis::analysis_and_position; | ||
139 | |||
140 | struct DetailAndDocumentation<'a> { | ||
141 | detail: &'a str, | ||
142 | documentation: &'a str, | ||
143 | } | ||
144 | |||
145 | fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) { | ||
146 | let (analysis, position) = analysis_and_position(ra_fixture); | ||
147 | let config = CompletionConfig::default(); | ||
148 | let completions = analysis.completions(&config, position).unwrap().unwrap(); | ||
149 | for item in completions { | ||
150 | if item.detail() == Some(expected.detail) { | ||
151 | let opt = item.documentation(); | ||
152 | let doc = opt.as_ref().map(|it| it.as_str()); | ||
153 | assert_eq!(doc, Some(expected.documentation)); | ||
154 | return; | ||
155 | } | ||
156 | } | ||
157 | panic!("completion detail not found: {}", expected.detail) | ||
158 | } | ||
159 | |||
160 | #[test] | ||
161 | fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() { | ||
162 | check_detail_and_documentation( | ||
163 | r#" | ||
164 | //- /lib.rs | ||
165 | macro_rules! bar { | ||
166 | () => { | ||
167 | struct Bar; | ||
168 | impl Bar { | ||
169 | #[doc = "Do the foo"] | ||
170 | fn foo(&self) {} | ||
171 | } | ||
172 | } | ||
173 | } | ||
174 | |||
175 | bar!(); | ||
176 | |||
177 | fn foo() { | ||
178 | let bar = Bar; | ||
179 | bar.fo<|>; | ||
180 | } | ||
181 | "#, | ||
182 | DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" }, | ||
183 | ); | ||
184 | } | ||
185 | |||
186 | #[test] | ||
187 | fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() { | ||
188 | check_detail_and_documentation( | ||
189 | r#" | ||
190 | //- /lib.rs | ||
191 | macro_rules! bar { | ||
192 | () => { | ||
193 | struct Bar; | ||
194 | impl Bar { | ||
195 | /// Do the foo | ||
196 | fn foo(&self) {} | ||
197 | } | ||
198 | } | ||
199 | } | ||
200 | |||
201 | bar!(); | ||
202 | |||
203 | fn foo() { | ||
204 | let bar = Bar; | ||
205 | bar.fo<|>; | ||
206 | } | ||
207 | "#, | ||
208 | DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" }, | ||
209 | ); | ||
210 | } | ||
211 | } | ||