diff options
Diffstat (limited to 'crates/ra_ide_api/src/completion')
22 files changed, 248 insertions, 300 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 18b2d68d5..4a111aba5 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs | |||
@@ -4,17 +4,10 @@ use crate::completion::{CompletionContext, Completions}; | |||
4 | 4 | ||
5 | /// Complete dot accesses, i.e. fields or methods (currently only fields). | 5 | /// Complete dot accesses, i.e. fields or methods (currently only fields). |
6 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | 6 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { |
7 | let (function, receiver) = match (&ctx.function, ctx.dot_receiver) { | 7 | let receiver_ty = match ctx.dot_receiver.and_then(|it| ctx.analyzer.type_of(ctx.db, it)) { |
8 | (Some(function), Some(receiver)) => (function, receiver), | 8 | Some(it) => it, |
9 | _ => return, | ||
10 | }; | ||
11 | let infer_result = function.infer(ctx.db); | ||
12 | let source_map = function.body_source_map(ctx.db); | ||
13 | let expr = match source_map.node_expr(receiver) { | ||
14 | Some(expr) => expr, | ||
15 | None => return, | 9 | None => return, |
16 | }; | 10 | }; |
17 | let receiver_ty = infer_result[expr].clone(); | ||
18 | if !ctx.is_call { | 11 | if !ctx.is_call { |
19 | complete_fields(acc, ctx, receiver_ty.clone()); | 12 | complete_fields(acc, ctx, receiver_ty.clone()); |
20 | } | 13 | } |
@@ -55,29 +48,41 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty | |||
55 | 48 | ||
56 | #[cfg(test)] | 49 | #[cfg(test)] |
57 | mod tests { | 50 | mod tests { |
58 | use crate::completion::{check_completion, CompletionKind}; | 51 | use crate::completion::{do_completion, CompletionKind, CompletionItem}; |
52 | use insta::assert_debug_snapshot_matches; | ||
59 | 53 | ||
60 | fn check_ref_completion(name: &str, code: &str) { | 54 | fn do_ref_completion(code: &str) -> Vec<CompletionItem> { |
61 | check_completion(name, code, CompletionKind::Reference); | 55 | do_completion(code, CompletionKind::Reference) |
62 | } | 56 | } |
63 | 57 | ||
64 | #[test] | 58 | #[test] |
65 | fn test_struct_field_completion() { | 59 | fn test_struct_field_completion() { |
66 | check_ref_completion( | 60 | assert_debug_snapshot_matches!( |
67 | "struct_field_completion", | 61 | do_ref_completion( |
68 | r" | 62 | r" |
69 | struct A { the_field: u32 } | 63 | struct A { the_field: u32 } |
70 | fn foo(a: A) { | 64 | fn foo(a: A) { |
71 | a.<|> | 65 | a.<|> |
72 | } | 66 | } |
73 | ", | 67 | ", |
68 | ), | ||
69 | @r###"[ | ||
70 | CompletionItem { | ||
71 | label: "the_field", | ||
72 | source_range: [94; 94), | ||
73 | delete: [94; 94), | ||
74 | insert: "the_field", | ||
75 | kind: Field, | ||
76 | detail: "u32" | ||
77 | } | ||
78 | ]"### | ||
74 | ); | 79 | ); |
75 | } | 80 | } |
76 | 81 | ||
77 | #[test] | 82 | #[test] |
78 | fn test_struct_field_completion_self() { | 83 | fn test_struct_field_completion_self() { |
79 | check_ref_completion( | 84 | assert_debug_snapshot_matches!( |
80 | "struct_field_completion_self", | 85 | do_ref_completion( |
81 | r" | 86 | r" |
82 | struct A { | 87 | struct A { |
83 | /// This is the_field | 88 | /// This is the_field |
@@ -89,13 +94,35 @@ mod tests { | |||
89 | } | 94 | } |
90 | } | 95 | } |
91 | ", | 96 | ", |
97 | ), | ||
98 | @r###"[ | ||
99 | CompletionItem { | ||
100 | label: "foo", | ||
101 | source_range: [187; 187), | ||
102 | delete: [187; 187), | ||
103 | insert: "foo()$0", | ||
104 | kind: Method, | ||
105 | detail: "fn foo(self)" | ||
106 | }, | ||
107 | CompletionItem { | ||
108 | label: "the_field", | ||
109 | source_range: [187; 187), | ||
110 | delete: [187; 187), | ||
111 | insert: "the_field", | ||
112 | kind: Field, | ||
113 | detail: "(u32,)", | ||
114 | documentation: Documentation( | ||
115 | "This is the_field" | ||
116 | ) | ||
117 | } | ||
118 | ]"### | ||
92 | ); | 119 | ); |
93 | } | 120 | } |
94 | 121 | ||
95 | #[test] | 122 | #[test] |
96 | fn test_struct_field_completion_autoderef() { | 123 | fn test_struct_field_completion_autoderef() { |
97 | check_ref_completion( | 124 | assert_debug_snapshot_matches!( |
98 | "struct_field_completion_autoderef", | 125 | do_ref_completion( |
99 | r" | 126 | r" |
100 | struct A { the_field: (u32, i32) } | 127 | struct A { the_field: (u32, i32) } |
101 | impl A { | 128 | impl A { |
@@ -104,26 +131,47 @@ mod tests { | |||
104 | } | 131 | } |
105 | } | 132 | } |
106 | ", | 133 | ", |
134 | ), | ||
135 | @r###"[ | ||
136 | CompletionItem { | ||
137 | label: "foo", | ||
138 | source_range: [126; 126), | ||
139 | delete: [126; 126), | ||
140 | insert: "foo()$0", | ||
141 | kind: Method, | ||
142 | detail: "fn foo(&self)" | ||
143 | }, | ||
144 | CompletionItem { | ||
145 | label: "the_field", | ||
146 | source_range: [126; 126), | ||
147 | delete: [126; 126), | ||
148 | insert: "the_field", | ||
149 | kind: Field, | ||
150 | detail: "(u32, i32)" | ||
151 | } | ||
152 | ]"### | ||
107 | ); | 153 | ); |
108 | } | 154 | } |
109 | 155 | ||
110 | #[test] | 156 | #[test] |
111 | fn test_no_struct_field_completion_for_method_call() { | 157 | fn test_no_struct_field_completion_for_method_call() { |
112 | check_ref_completion( | 158 | assert_debug_snapshot_matches!( |
113 | "no_struct_field_completion_for_method_call", | 159 | do_ref_completion( |
114 | r" | 160 | r" |
115 | struct A { the_field: u32 } | 161 | struct A { the_field: u32 } |
116 | fn foo(a: A) { | 162 | fn foo(a: A) { |
117 | a.<|>() | 163 | a.<|>() |
118 | } | 164 | } |
119 | ", | 165 | ", |
166 | ), | ||
167 | @"[]" | ||
120 | ); | 168 | ); |
121 | } | 169 | } |
122 | 170 | ||
123 | #[test] | 171 | #[test] |
124 | fn test_method_completion() { | 172 | fn test_method_completion() { |
125 | check_ref_completion( | 173 | assert_debug_snapshot_matches!( |
126 | "method_completion", | 174 | do_ref_completion( |
127 | r" | 175 | r" |
128 | struct A {} | 176 | struct A {} |
129 | impl A { | 177 | impl A { |
@@ -133,13 +181,24 @@ mod tests { | |||
133 | a.<|> | 181 | a.<|> |
134 | } | 182 | } |
135 | ", | 183 | ", |
184 | ), | ||
185 | @r###"[ | ||
186 | CompletionItem { | ||
187 | label: "the_method", | ||
188 | source_range: [144; 144), | ||
189 | delete: [144; 144), | ||
190 | insert: "the_method()$0", | ||
191 | kind: Method, | ||
192 | detail: "fn the_method(&self)" | ||
193 | } | ||
194 | ]"### | ||
136 | ); | 195 | ); |
137 | } | 196 | } |
138 | 197 | ||
139 | #[test] | 198 | #[test] |
140 | fn test_no_non_self_method() { | 199 | fn test_no_non_self_method() { |
141 | check_ref_completion( | 200 | assert_debug_snapshot_matches!( |
142 | "no_non_self_method", | 201 | do_ref_completion( |
143 | r" | 202 | r" |
144 | struct A {} | 203 | struct A {} |
145 | impl A { | 204 | impl A { |
@@ -149,13 +208,15 @@ mod tests { | |||
149 | a.<|> | 208 | a.<|> |
150 | } | 209 | } |
151 | ", | 210 | ", |
211 | ), | ||
212 | @"[]" | ||
152 | ); | 213 | ); |
153 | } | 214 | } |
154 | 215 | ||
155 | #[test] | 216 | #[test] |
156 | fn test_method_attr_filtering() { | 217 | fn test_method_attr_filtering() { |
157 | check_ref_completion( | 218 | assert_debug_snapshot_matches!( |
158 | "method_attr_filtering", | 219 | do_ref_completion( |
159 | r" | 220 | r" |
160 | struct A {} | 221 | struct A {} |
161 | impl A { | 222 | impl A { |
@@ -169,26 +230,56 @@ mod tests { | |||
169 | a.<|> | 230 | a.<|> |
170 | } | 231 | } |
171 | ", | 232 | ", |
233 | ), | ||
234 | @r###"[ | ||
235 | CompletionItem { | ||
236 | label: "the_method", | ||
237 | source_range: [249; 249), | ||
238 | delete: [249; 249), | ||
239 | insert: "the_method()$0", | ||
240 | kind: Method, | ||
241 | detail: "fn the_method(&self)" | ||
242 | } | ||
243 | ]"### | ||
172 | ); | 244 | ); |
173 | } | 245 | } |
174 | 246 | ||
175 | #[test] | 247 | #[test] |
176 | fn test_tuple_field_completion() { | 248 | fn test_tuple_field_completion() { |
177 | check_ref_completion( | 249 | assert_debug_snapshot_matches!( |
178 | "tuple_field_completion", | 250 | do_ref_completion( |
179 | r" | 251 | r" |
180 | fn foo() { | 252 | fn foo() { |
181 | let b = (0, 3.14); | 253 | let b = (0, 3.14); |
182 | b.<|> | 254 | b.<|> |
183 | } | 255 | } |
184 | ", | 256 | ", |
257 | ), | ||
258 | @r###"[ | ||
259 | CompletionItem { | ||
260 | label: "0", | ||
261 | source_range: [75; 75), | ||
262 | delete: [75; 75), | ||
263 | insert: "0", | ||
264 | kind: Field, | ||
265 | detail: "i32" | ||
266 | }, | ||
267 | CompletionItem { | ||
268 | label: "1", | ||
269 | source_range: [75; 75), | ||
270 | delete: [75; 75), | ||
271 | insert: "1", | ||
272 | kind: Field, | ||
273 | detail: "f64" | ||
274 | } | ||
275 | ]"### | ||
185 | ); | 276 | ); |
186 | } | 277 | } |
187 | 278 | ||
188 | #[test] | 279 | #[test] |
189 | fn test_tuple_field_inference() { | 280 | fn test_tuple_field_inference() { |
190 | check_ref_completion( | 281 | assert_debug_snapshot_matches!( |
191 | "tuple_field_inference", | 282 | do_ref_completion( |
192 | r" | 283 | r" |
193 | pub struct S; | 284 | pub struct S; |
194 | impl S { | 285 | impl S { |
@@ -204,6 +295,41 @@ mod tests { | |||
204 | } | 295 | } |
205 | } | 296 | } |
206 | ", | 297 | ", |
298 | ), | ||
299 | @r###"[ | ||
300 | CompletionItem { | ||
301 | label: "blah", | ||
302 | source_range: [299; 300), | ||
303 | delete: [299; 300), | ||
304 | insert: "blah()$0", | ||
305 | kind: Method, | ||
306 | detail: "pub fn blah(&self)" | ||
307 | } | ||
308 | ]"### | ||
309 | ); | ||
310 | } | ||
311 | |||
312 | #[test] | ||
313 | fn test_completion_works_in_consts() { | ||
314 | assert_debug_snapshot_matches!( | ||
315 | do_ref_completion( | ||
316 | r" | ||
317 | struct A { the_field: u32 } | ||
318 | const X: u32 = { | ||
319 | A { the_field: 92 }.<|> | ||
320 | }; | ||
321 | ", | ||
322 | ), | ||
323 | @r###"[ | ||
324 | CompletionItem { | ||
325 | label: "the_field", | ||
326 | source_range: [106; 106), | ||
327 | delete: [106; 106), | ||
328 | insert: "the_field", | ||
329 | kind: Field, | ||
330 | detail: "u32" | ||
331 | } | ||
332 | ]"### | ||
207 | ); | 333 | ); |
208 | } | 334 | } |
209 | } | 335 | } |
diff --git a/crates/ra_ide_api/src/completion/complete_fn_param.rs b/crates/ra_ide_api/src/completion/complete_fn_param.rs index f87ccdeb9..85ef62f52 100644 --- a/crates/ra_ide_api/src/completion/complete_fn_param.rs +++ b/crates/ra_ide_api/src/completion/complete_fn_param.rs | |||
@@ -54,48 +54,79 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) | |||
54 | 54 | ||
55 | #[cfg(test)] | 55 | #[cfg(test)] |
56 | mod tests { | 56 | mod tests { |
57 | use crate::completion::{check_completion, CompletionKind}; | 57 | use crate::completion::{do_completion, CompletionItem, CompletionKind}; |
58 | use insta::assert_debug_snapshot_matches; | ||
58 | 59 | ||
59 | fn check_magic_completion(name: &str, code: &str) { | 60 | fn do_magic_completion(code: &str) -> Vec<CompletionItem> { |
60 | check_completion(name, code, CompletionKind::Magic); | 61 | do_completion(code, CompletionKind::Magic) |
61 | } | 62 | } |
62 | 63 | ||
63 | #[test] | 64 | #[test] |
64 | fn test_param_completion_last_param() { | 65 | fn test_param_completion_last_param() { |
65 | check_magic_completion( | 66 | assert_debug_snapshot_matches!( |
66 | "param_completion_last_param", | 67 | do_magic_completion( |
67 | r" | 68 | r" |
68 | fn foo(file_id: FileId) {} | 69 | fn foo(file_id: FileId) {} |
69 | fn bar(file_id: FileId) {} | 70 | fn bar(file_id: FileId) {} |
70 | fn baz(file<|>) {} | 71 | fn baz(file<|>) {} |
71 | ", | 72 | ", |
73 | ), | ||
74 | @r###"[ | ||
75 | CompletionItem { | ||
76 | label: "file_id: FileId", | ||
77 | source_range: [110; 114), | ||
78 | delete: [110; 114), | ||
79 | insert: "file_id: FileId", | ||
80 | lookup: "file_id" | ||
81 | } | ||
82 | ]"### | ||
72 | ); | 83 | ); |
73 | } | 84 | } |
74 | 85 | ||
75 | #[test] | 86 | #[test] |
76 | fn test_param_completion_nth_param() { | 87 | fn test_param_completion_nth_param() { |
77 | check_magic_completion( | 88 | assert_debug_snapshot_matches!( |
78 | "param_completion_nth_param", | 89 | do_magic_completion( |
79 | r" | 90 | r" |
80 | fn foo(file_id: FileId) {} | 91 | fn foo(file_id: FileId) {} |
81 | fn bar(file_id: FileId) {} | 92 | fn bar(file_id: FileId) {} |
82 | fn baz(file<|>, x: i32) {} | 93 | fn baz(file<|>, x: i32) {} |
83 | ", | 94 | ", |
95 | ), | ||
96 | @r###"[ | ||
97 | CompletionItem { | ||
98 | label: "file_id: FileId", | ||
99 | source_range: [110; 114), | ||
100 | delete: [110; 114), | ||
101 | insert: "file_id: FileId", | ||
102 | lookup: "file_id" | ||
103 | } | ||
104 | ]"### | ||
84 | ); | 105 | ); |
85 | } | 106 | } |
86 | 107 | ||
87 | #[test] | 108 | #[test] |
88 | fn test_param_completion_trait_param() { | 109 | fn test_param_completion_trait_param() { |
89 | check_magic_completion( | 110 | assert_debug_snapshot_matches!( |
90 | "param_completion_trait_param", | 111 | do_magic_completion( |
91 | r" | 112 | r" |
92 | pub(crate) trait SourceRoot { | 113 | pub(crate) trait SourceRoot { |
93 | pub fn contains(&self, file_id: FileId) -> bool; | 114 | pub fn contains(&self, file_id: FileId) -> bool; |
94 | pub fn module_map(&self) -> &ModuleMap; | 115 | pub fn module_map(&self) -> &ModuleMap; |
95 | pub fn lines(&self, file_id: FileId) -> &LineIndex; | 116 | pub fn lines(&self, file_id: FileId) -> &LineIndex; |
96 | pub fn syntax(&self, file<|>) | 117 | pub fn syntax(&self, file<|>) |
97 | } | 118 | } |
98 | ", | 119 | ", |
120 | ), | ||
121 | @r###"[ | ||
122 | CompletionItem { | ||
123 | label: "file_id: FileId", | ||
124 | source_range: [289; 293), | ||
125 | delete: [289; 293), | ||
126 | insert: "file_id: FileId", | ||
127 | lookup: "file_id" | ||
128 | } | ||
129 | ]"### | ||
99 | ); | 130 | ); |
100 | } | 131 | } |
101 | } | 132 | } |
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index e54fe7b7e..bc03a7095 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use hir::Resolution; | 1 | use hir::{Resolution, Either}; |
2 | use ra_syntax::AstNode; | 2 | use ra_syntax::AstNode; |
3 | use test_utils::tested_by; | 3 | use test_utils::tested_by; |
4 | 4 | ||
@@ -9,7 +9,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { | |||
9 | Some(path) => path.clone(), | 9 | Some(path) => path.clone(), |
10 | _ => return, | 10 | _ => return, |
11 | }; | 11 | }; |
12 | let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() { | 12 | let def = match ctx.analyzer.resolve_hir_path(ctx.db, &path).take_types() { |
13 | Some(Resolution::Def(def)) => def, | 13 | Some(Resolution::Def(def)) => def, |
14 | _ => return, | 14 | _ => return, |
15 | }; | 15 | }; |
@@ -19,10 +19,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { | |||
19 | for (name, res) in module_scope.entries() { | 19 | for (name, res) in module_scope.entries() { |
20 | if Some(module) == ctx.module { | 20 | if Some(module) == ctx.module { |
21 | if let Some(import) = res.import { | 21 | if let Some(import) = res.import { |
22 | if let hir::ImportSource::UseTree(tree) = | 22 | if let Either::A(use_tree) = module.import_source(ctx.db, import) { |
23 | module.import_source(ctx.db, import) | 23 | if use_tree.syntax().range().contains_inclusive(ctx.offset) { |
24 | { | ||
25 | if tree.syntax().range().contains_inclusive(ctx.offset) { | ||
26 | // for `use self::foo<|>`, don't suggest `foo` as a completion | 24 | // for `use self::foo<|>`, don't suggest `foo` as a completion |
27 | tested_by!(dont_complete_current_use); | 25 | tested_by!(dont_complete_current_use); |
28 | continue; | 26 | continue; |
diff --git a/crates/ra_ide_api/src/completion/complete_pattern.rs b/crates/ra_ide_api/src/completion/complete_pattern.rs index 7abcd019b..0ef248687 100644 --- a/crates/ra_ide_api/src/completion/complete_pattern.rs +++ b/crates/ra_ide_api/src/completion/complete_pattern.rs | |||
@@ -7,7 +7,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { | |||
7 | } | 7 | } |
8 | // FIXME: ideally, we should look at the type we are matching against and | 8 | // FIXME: ideally, we should look at the type we are matching against and |
9 | // suggest variants + auto-imports | 9 | // suggest variants + auto-imports |
10 | let names = ctx.resolver.all_names(ctx.db); | 10 | let names = ctx.analyzer.all_names(ctx.db); |
11 | for (name, res) in names.into_iter() { | 11 | for (name, res) in names.into_iter() { |
12 | let r = res.as_ref(); | 12 | let r = res.as_ref(); |
13 | let def = match r.take_types().or(r.take_values()) { | 13 | let def = match r.take_types().or(r.take_values()) { |
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index 6146b7bb6..fd256fc3b 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs | |||
@@ -4,7 +4,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { | |||
4 | if !ctx.is_trivial_path { | 4 | if !ctx.is_trivial_path { |
5 | return; | 5 | return; |
6 | } | 6 | } |
7 | let names = ctx.resolver.all_names(ctx.db); | 7 | let names = ctx.analyzer.all_names(ctx.db); |
8 | 8 | ||
9 | names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); | 9 | names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); |
10 | } | 10 | } |
@@ -145,7 +145,7 @@ mod tests { | |||
145 | check_reference_completion( | 145 | check_reference_completion( |
146 | "dont_show_both_completions_for_shadowing", | 146 | "dont_show_both_completions_for_shadowing", |
147 | r" | 147 | r" |
148 | fn foo() -> { | 148 | fn foo() { |
149 | let bar = 92; | 149 | let bar = 92; |
150 | { | 150 | { |
151 | let bar = 62; | 151 | let bar = 62; |
diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index f58bcd03e..48fbf67f7 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs | |||
@@ -4,17 +4,10 @@ use crate::completion::{CompletionContext, Completions}; | |||
4 | 4 | ||
5 | /// Complete fields in fields literals. | 5 | /// Complete fields in fields literals. |
6 | pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { | 6 | pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { |
7 | let (function, struct_lit) = match (&ctx.function, ctx.struct_lit_syntax) { | 7 | let ty = match ctx.struct_lit_syntax.and_then(|it| ctx.analyzer.type_of(ctx.db, it.into())) { |
8 | (Some(function), Some(struct_lit)) => (function, struct_lit), | 8 | Some(it) => it, |
9 | _ => return, | ||
10 | }; | ||
11 | let infer_result = function.infer(ctx.db); | ||
12 | let source_map = function.body_source_map(ctx.db); | ||
13 | let expr = match source_map.node_expr(struct_lit.into()) { | ||
14 | Some(expr) => expr, | ||
15 | None => return, | 9 | None => return, |
16 | }; | 10 | }; |
17 | let ty = infer_result[expr].clone(); | ||
18 | let (adt, substs) = match ty.as_adt() { | 11 | let (adt, substs) = match ty.as_adt() { |
19 | Some(res) => res, | 12 | Some(res) => res, |
20 | _ => return, | 13 | _ => return, |
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 65dffa470..359f2cffa 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs | |||
@@ -5,7 +5,7 @@ use ra_syntax::{ | |||
5 | algo::{find_token_at_offset, find_covering_element, find_node_at_offset}, | 5 | algo::{find_token_at_offset, find_covering_element, find_node_at_offset}, |
6 | SyntaxKind::*, | 6 | SyntaxKind::*, |
7 | }; | 7 | }; |
8 | use hir::{source_binder, Resolver}; | 8 | use hir::source_binder; |
9 | 9 | ||
10 | use crate::{db, FilePosition}; | 10 | use crate::{db, FilePosition}; |
11 | 11 | ||
@@ -14,11 +14,10 @@ use crate::{db, FilePosition}; | |||
14 | #[derive(Debug)] | 14 | #[derive(Debug)] |
15 | pub(crate) struct CompletionContext<'a> { | 15 | pub(crate) struct CompletionContext<'a> { |
16 | pub(super) db: &'a db::RootDatabase, | 16 | pub(super) db: &'a db::RootDatabase, |
17 | pub(super) analyzer: hir::SourceAnalyzer, | ||
17 | pub(super) offset: TextUnit, | 18 | pub(super) offset: TextUnit, |
18 | pub(super) token: SyntaxToken<'a>, | 19 | pub(super) token: SyntaxToken<'a>, |
19 | pub(super) resolver: Resolver, | ||
20 | pub(super) module: Option<hir::Module>, | 20 | pub(super) module: Option<hir::Module>, |
21 | pub(super) function: Option<hir::Function>, | ||
22 | pub(super) function_syntax: Option<&'a ast::FnDef>, | 21 | pub(super) function_syntax: Option<&'a ast::FnDef>, |
23 | pub(super) use_item_syntax: Option<&'a ast::UseItem>, | 22 | pub(super) use_item_syntax: Option<&'a ast::UseItem>, |
24 | pub(super) struct_lit_syntax: Option<&'a ast::StructLit>, | 23 | pub(super) struct_lit_syntax: Option<&'a ast::StructLit>, |
@@ -47,16 +46,16 @@ impl<'a> CompletionContext<'a> { | |||
47 | original_file: &'a SourceFile, | 46 | original_file: &'a SourceFile, |
48 | position: FilePosition, | 47 | position: FilePosition, |
49 | ) -> Option<CompletionContext<'a>> { | 48 | ) -> Option<CompletionContext<'a>> { |
50 | let resolver = source_binder::resolver_for_position(db, position); | ||
51 | let module = source_binder::module_from_position(db, position); | 49 | let module = source_binder::module_from_position(db, position); |
52 | let token = find_token_at_offset(original_file.syntax(), position.offset).left_biased()?; | 50 | let token = find_token_at_offset(original_file.syntax(), position.offset).left_biased()?; |
51 | let analyzer = | ||
52 | hir::SourceAnalyzer::new(db, position.file_id, token.parent(), Some(position.offset)); | ||
53 | let mut ctx = CompletionContext { | 53 | let mut ctx = CompletionContext { |
54 | db, | 54 | db, |
55 | analyzer, | ||
55 | token, | 56 | token, |
56 | offset: position.offset, | 57 | offset: position.offset, |
57 | resolver, | ||
58 | module, | 58 | module, |
59 | function: None, | ||
60 | function_syntax: None, | 59 | function_syntax: None, |
61 | use_item_syntax: None, | 60 | use_item_syntax: None, |
62 | struct_lit_syntax: None, | 61 | struct_lit_syntax: None, |
@@ -147,10 +146,6 @@ impl<'a> CompletionContext<'a> { | |||
147 | .ancestors() | 146 | .ancestors() |
148 | .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) | 147 | .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) |
149 | .find_map(ast::FnDef::cast); | 148 | .find_map(ast::FnDef::cast); |
150 | if let (Some(module), Some(fn_def)) = (self.module, self.function_syntax) { | ||
151 | let function = source_binder::function_from_module(self.db, module, fn_def); | ||
152 | self.function = Some(function); | ||
153 | } | ||
154 | 149 | ||
155 | let parent = match name_ref.syntax().parent() { | 150 | let parent = match name_ref.syntax().parent() { |
156 | Some(it) => it, | 151 | Some(it) => it, |
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 28c8f83ab..9aa346688 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs | |||
@@ -6,6 +6,9 @@ use ra_syntax::ast::NameOwner; | |||
6 | 6 | ||
7 | use crate::completion::{ | 7 | use crate::completion::{ |
8 | Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, | 8 | Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, |
9 | }; | ||
10 | |||
11 | use crate::display::{ | ||
9 | function_label, const_label, type_label, | 12 | function_label, const_label, type_label, |
10 | }; | 13 | }; |
11 | 14 | ||
@@ -101,7 +104,7 @@ impl Completions { | |||
101 | CompletionItemKind::Function | 104 | CompletionItemKind::Function |
102 | }) | 105 | }) |
103 | .set_documentation(func.docs(ctx.db)) | 106 | .set_documentation(func.docs(ctx.db)) |
104 | .set_detail(detail); | 107 | .detail(detail); |
105 | // If not an import, add parenthesis automatically. | 108 | // If not an import, add parenthesis automatically. |
106 | if ctx.use_item_syntax.is_none() && !ctx.is_call { | 109 | if ctx.use_item_syntax.is_none() && !ctx.is_call { |
107 | tested_by!(inserts_parens_for_function_calls); | 110 | tested_by!(inserts_parens_for_function_calls); |
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap index 87691b304..34adcda6c 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap | |||
@@ -1,23 +1,23 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-02-18T09:22:24.188564584Z" | 2 | created: "2019-04-04T14:52:24.531844100Z" |
3 | creator: insta@0.6.2 | 3 | creator: insta@0.7.4 |
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | 4 | source: crates/ra_ide_api/src/completion/completion_item.rs |
5 | expression: kind_completions | 5 | expression: kind_completions |
6 | --- | 6 | --- |
7 | [ | 7 | [ |
8 | CompletionItem { | 8 | CompletionItem { |
9 | label: "bar", | 9 | label: "bar", |
10 | source_range: [129; 129), | 10 | source_range: [126; 126), |
11 | delete: [129; 129), | 11 | delete: [126; 126), |
12 | insert: "bar", | 12 | insert: "bar", |
13 | kind: Binding | 13 | kind: Binding |
14 | }, | 14 | }, |
15 | CompletionItem { | 15 | CompletionItem { |
16 | label: "foo", | 16 | label: "foo", |
17 | source_range: [129; 129), | 17 | source_range: [126; 126), |
18 | delete: [129; 129), | 18 | delete: [126; 126), |
19 | insert: "foo()$0", | 19 | insert: "foo()$0", |
20 | kind: Function, | 20 | kind: Function, |
21 | detail: "fn foo() ->" | 21 | detail: "fn foo()" |
22 | } | 22 | } |
23 | ] | 23 | ] |
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_attr_filtering.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_attr_filtering.snap deleted file mode 100644 index ce8af2159..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_attr_filtering.snap +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.941335305Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "the_method", | ||
10 | source_range: [249; 249), | ||
11 | delete: [249; 249), | ||
12 | insert: "the_method()$0", | ||
13 | kind: Method, | ||
14 | detail: "fn the_method(&self)" | ||
15 | } | ||
16 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap deleted file mode 100644 index 41a10de14..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.939676100Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "the_method", | ||
10 | source_range: [144; 144), | ||
11 | delete: [144; 144), | ||
12 | insert: "the_method()$0", | ||
13 | kind: Method, | ||
14 | detail: "fn the_method(&self)" | ||
15 | } | ||
16 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__no_non_self_method.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__no_non_self_method.snap deleted file mode 100644 index 7cc827532..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__no_non_self_method.snap +++ /dev/null | |||
@@ -1,7 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-01-22T14:45:00.552379600+00:00" | ||
3 | creator: [email protected] | ||
4 | expression: kind_completions | ||
5 | source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" | ||
6 | --- | ||
7 | [] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__no_struct_field_completion_for_method_call.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__no_struct_field_completion_for_method_call.snap deleted file mode 100644 index 7cc827532..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__no_struct_field_completion_for_method_call.snap +++ /dev/null | |||
@@ -1,7 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-01-22T14:45:00.552379600+00:00" | ||
3 | creator: [email protected] | ||
4 | expression: kind_completions | ||
5 | source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" | ||
6 | --- | ||
7 | [] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_last_param.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_last_param.snap deleted file mode 100644 index cab77f5a2..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_last_param.snap +++ /dev/null | |||
@@ -1,15 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.949634602Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "file_id: FileId", | ||
10 | source_range: [98; 102), | ||
11 | delete: [98; 102), | ||
12 | insert: "file_id: FileId", | ||
13 | lookup: "file_id" | ||
14 | } | ||
15 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_nth_param.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_nth_param.snap deleted file mode 100644 index 8fbee160c..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_nth_param.snap +++ /dev/null | |||
@@ -1,15 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.949634355Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "file_id: FileId", | ||
10 | source_range: [98; 102), | ||
11 | delete: [98; 102), | ||
12 | insert: "file_id: FileId", | ||
13 | lookup: "file_id" | ||
14 | } | ||
15 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_trait_param.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_trait_param.snap deleted file mode 100644 index 76eeadb6d..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__param_completion_trait_param.snap +++ /dev/null | |||
@@ -1,15 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.974417169Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "file_id: FileId", | ||
10 | source_range: [269; 273), | ||
11 | delete: [269; 273), | ||
12 | insert: "file_id: FileId", | ||
13 | lookup: "file_id" | ||
14 | } | ||
15 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap index 0738cf466..ff36df707 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap | |||
@@ -1,6 +1,6 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-02-18T09:22:24.182964414Z" | 2 | created: "2019-04-04T14:52:24.525395600Z" |
3 | creator: insta@0.6.2 | 3 | creator: insta@0.7.4 |
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | 4 | source: crates/ra_ide_api/src/completion/completion_item.rs |
5 | expression: kind_completions | 5 | expression: kind_completions |
6 | --- | 6 | --- |
@@ -18,6 +18,6 @@ expression: kind_completions | |||
18 | delete: [47; 47), | 18 | delete: [47; 47), |
19 | insert: "x()$0", | 19 | insert: "x()$0", |
20 | kind: Function, | 20 | kind: Function, |
21 | detail: "fn x() ->" | 21 | detail: "fn x()" |
22 | } | 22 | } |
23 | ] | 23 | ] |
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion.snap deleted file mode 100644 index 58271b873..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion.snap +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.939645902Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "the_field", | ||
10 | source_range: [85; 85), | ||
11 | delete: [85; 85), | ||
12 | insert: "the_field", | ||
13 | kind: Field, | ||
14 | detail: "u32" | ||
15 | } | ||
16 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap deleted file mode 100644 index b38867b81..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap +++ /dev/null | |||
@@ -1,24 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.940872916Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "foo", | ||
10 | source_range: [126; 126), | ||
11 | delete: [126; 126), | ||
12 | insert: "foo()$0", | ||
13 | kind: Method, | ||
14 | detail: "fn foo(&self)" | ||
15 | }, | ||
16 | CompletionItem { | ||
17 | label: "the_field", | ||
18 | source_range: [126; 126), | ||
19 | delete: [126; 126), | ||
20 | insert: "the_field", | ||
21 | kind: Field, | ||
22 | detail: "(u32, i32)" | ||
23 | } | ||
24 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap deleted file mode 100644 index 8e5cab43e..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.940872918Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "foo", | ||
10 | source_range: [187; 187), | ||
11 | delete: [187; 187), | ||
12 | insert: "foo()$0", | ||
13 | kind: Method, | ||
14 | detail: "fn foo(self)" | ||
15 | }, | ||
16 | CompletionItem { | ||
17 | label: "the_field", | ||
18 | source_range: [187; 187), | ||
19 | delete: [187; 187), | ||
20 | insert: "the_field", | ||
21 | kind: Field, | ||
22 | detail: "(u32,)", | ||
23 | documentation: Documentation( | ||
24 | "This is the_field" | ||
25 | ) | ||
26 | } | ||
27 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__tuple_field_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__tuple_field_completion.snap deleted file mode 100644 index 3f2780621..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__tuple_field_completion.snap +++ /dev/null | |||
@@ -1,24 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-02-18T09:22:23.939710971Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "0", | ||
10 | source_range: [75; 75), | ||
11 | delete: [75; 75), | ||
12 | insert: "0", | ||
13 | kind: Field, | ||
14 | detail: "i32" | ||
15 | }, | ||
16 | CompletionItem { | ||
17 | label: "1", | ||
18 | source_range: [75; 75), | ||
19 | delete: [75; 75), | ||
20 | insert: "1", | ||
21 | kind: Field, | ||
22 | detail: "f64" | ||
23 | } | ||
24 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__tuple_field_inference.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__tuple_field_inference.snap deleted file mode 100644 index 72c8973b8..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__tuple_field_inference.snap +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | --- | ||
2 | created: "2019-04-05T23:00:18.283812700Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "blah", | ||
10 | source_range: [299; 300), | ||
11 | delete: [299; 300), | ||
12 | insert: "blah()$0", | ||
13 | kind: Method, | ||
14 | detail: "pub fn blah(&self)" | ||
15 | } | ||
16 | ] | ||