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