diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide/src/completion/complete_trait_impl.rs | 73 |
1 files changed, 44 insertions, 29 deletions
diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index 81899308b..bff19c5bb 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs | |||
@@ -79,16 +79,16 @@ fn add_function_impl( | |||
79 | ) { | 79 | ) { |
80 | let display = FunctionSignature::from_hir(ctx.db, func.clone()); | 80 | let display = FunctionSignature::from_hir(ctx.db, func.clone()); |
81 | 81 | ||
82 | let func_name = func.name(ctx.db); | 82 | let fn_name = func.name(ctx.db).to_string(); |
83 | 83 | ||
84 | let label = if func.params(ctx.db).len() > 0 { | 84 | let label = if func.params(ctx.db).len() > 0 { |
85 | format!("fn {}(..)", func_name.to_string()) | 85 | format!("fn {}(..)", fn_name) |
86 | } else { | 86 | } else { |
87 | format!("fn {}()", func_name.to_string()) | 87 | format!("fn {}()", fn_name) |
88 | }; | 88 | }; |
89 | 89 | ||
90 | let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label.clone()) | 90 | let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label.clone()) |
91 | .lookup_by(label) | 91 | .lookup_by(fn_name) |
92 | .set_documentation(func.docs(ctx.db)); | 92 | .set_documentation(func.docs(ctx.db)); |
93 | 93 | ||
94 | let completion_kind = if func.has_self_param(ctx.db) { | 94 | let completion_kind = if func.has_self_param(ctx.db) { |
@@ -111,10 +111,13 @@ fn add_type_alias_impl( | |||
111 | ctx: &CompletionContext, | 111 | ctx: &CompletionContext, |
112 | type_alias: &hir::TypeAlias, | 112 | type_alias: &hir::TypeAlias, |
113 | ) { | 113 | ) { |
114 | let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string()); | 114 | let alias_name = type_alias.name(ctx.db).to_string(); |
115 | |||
116 | let snippet = format!("type {} = ", alias_name); | ||
115 | 117 | ||
116 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) | 118 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) |
117 | .text_edit(TextEdit::replace(type_def_node.text_range(), snippet)) | 119 | .text_edit(TextEdit::replace(type_def_node.text_range(), snippet)) |
120 | .lookup_by(alias_name) | ||
118 | .kind(CompletionItemKind::TypeAlias) | 121 | .kind(CompletionItemKind::TypeAlias) |
119 | .set_documentation(type_alias.docs(ctx.db)) | 122 | .set_documentation(type_alias.docs(ctx.db)) |
120 | .add_to(acc); | 123 | .add_to(acc); |
@@ -126,13 +129,18 @@ fn add_const_impl( | |||
126 | ctx: &CompletionContext, | 129 | ctx: &CompletionContext, |
127 | const_: &hir::Const, | 130 | const_: &hir::Const, |
128 | ) { | 131 | ) { |
129 | let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); | 132 | let const_name = const_.name(ctx.db).map(|n| n.to_string()); |
130 | 133 | ||
131 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) | 134 | if let Some(const_name) = const_name { |
132 | .text_edit(TextEdit::replace(const_def_node.text_range(), snippet)) | 135 | let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); |
133 | .kind(CompletionItemKind::Const) | 136 | |
134 | .set_documentation(const_.docs(ctx.db)) | 137 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) |
135 | .add_to(acc); | 138 | .text_edit(TextEdit::replace(const_def_node.text_range(), snippet)) |
139 | .lookup_by(const_name) | ||
140 | .kind(CompletionItemKind::Const) | ||
141 | .set_documentation(const_.docs(ctx.db)) | ||
142 | .add_to(acc); | ||
143 | } | ||
136 | } | 144 | } |
137 | 145 | ||
138 | fn make_const_compl_syntax(const_: &ast::ConstDef) -> String { | 146 | fn make_const_compl_syntax(const_: &ast::ConstDef) -> String { |
@@ -178,7 +186,7 @@ mod tests { | |||
178 | struct T1; | 186 | struct T1; |
179 | 187 | ||
180 | impl Test for T1 { | 188 | impl Test for T1 { |
181 | fn<|> | 189 | fn f<|> |
182 | } | 190 | } |
183 | ", | 191 | ", |
184 | ); | 192 | ); |
@@ -186,10 +194,11 @@ mod tests { | |||
186 | [ | 194 | [ |
187 | CompletionItem { | 195 | CompletionItem { |
188 | label: "fn foo()", | 196 | label: "fn foo()", |
189 | source_range: [140; 140), | 197 | source_range: [141; 142), |
190 | delete: [138; 140), | 198 | delete: [138; 142), |
191 | insert: "fn foo() {}", | 199 | insert: "fn foo() {}", |
192 | kind: Function, | 200 | kind: Function, |
201 | lookup: "foo", | ||
193 | }, | 202 | }, |
194 | ] | 203 | ] |
195 | "###); | 204 | "###); |
@@ -201,7 +210,7 @@ mod tests { | |||
201 | r" | 210 | r" |
202 | trait Test { | 211 | trait Test { |
203 | fn foo(); | 212 | fn foo(); |
204 | fn bar(); | 213 | fn foo_bar(); |
205 | } | 214 | } |
206 | 215 | ||
207 | struct T1; | 216 | struct T1; |
@@ -209,18 +218,19 @@ mod tests { | |||
209 | impl Test for T1 { | 218 | impl Test for T1 { |
210 | fn foo() {} | 219 | fn foo() {} |
211 | 220 | ||
212 | fn<|> | 221 | fn f<|> |
213 | } | 222 | } |
214 | ", | 223 | ", |
215 | ); | 224 | ); |
216 | assert_debug_snapshot!(completions, @r###" | 225 | assert_debug_snapshot!(completions, @r###" |
217 | [ | 226 | [ |
218 | CompletionItem { | 227 | CompletionItem { |
219 | label: "fn bar()", | 228 | label: "fn foo_bar()", |
220 | source_range: [195; 195), | 229 | source_range: [200; 201), |
221 | delete: [193; 195), | 230 | delete: [197; 201), |
222 | insert: "fn bar() {}", | 231 | insert: "fn foo_bar() {}", |
223 | kind: Function, | 232 | kind: Function, |
233 | lookup: "foo_bar", | ||
224 | }, | 234 | }, |
225 | ] | 235 | ] |
226 | "###); | 236 | "###); |
@@ -237,7 +247,7 @@ mod tests { | |||
237 | struct T1; | 247 | struct T1; |
238 | 248 | ||
239 | impl Test for T1 { | 249 | impl Test for T1 { |
240 | fn<|> | 250 | fn f<|> |
241 | } | 251 | } |
242 | ", | 252 | ", |
243 | ); | 253 | ); |
@@ -245,10 +255,11 @@ mod tests { | |||
245 | [ | 255 | [ |
246 | CompletionItem { | 256 | CompletionItem { |
247 | label: "fn foo()", | 257 | label: "fn foo()", |
248 | source_range: [143; 143), | 258 | source_range: [144; 145), |
249 | delete: [141; 143), | 259 | delete: [141; 145), |
250 | insert: "fn foo<T>() {}", | 260 | insert: "fn foo<T>() {}", |
251 | kind: Function, | 261 | kind: Function, |
262 | lookup: "foo", | ||
252 | }, | 263 | }, |
253 | ] | 264 | ] |
254 | "###); | 265 | "###); |
@@ -265,7 +276,7 @@ mod tests { | |||
265 | struct T1; | 276 | struct T1; |
266 | 277 | ||
267 | impl Test for T1 { | 278 | impl Test for T1 { |
268 | fn<|> | 279 | fn f<|> |
269 | } | 280 | } |
270 | ", | 281 | ", |
271 | ); | 282 | ); |
@@ -273,10 +284,11 @@ mod tests { | |||
273 | [ | 284 | [ |
274 | CompletionItem { | 285 | CompletionItem { |
275 | label: "fn foo()", | 286 | label: "fn foo()", |
276 | source_range: [165; 165), | 287 | source_range: [166; 167), |
277 | delete: [163; 165), | 288 | delete: [163; 167), |
278 | insert: "fn foo<T>()\nwhere T: Into<String> {}", | 289 | insert: "fn foo<T>()\nwhere T: Into<String> {}", |
279 | kind: Function, | 290 | kind: Function, |
291 | lookup: "foo", | ||
280 | }, | 292 | }, |
281 | ] | 293 | ] |
282 | "###); | 294 | "###); |
@@ -291,7 +303,7 @@ mod tests { | |||
291 | } | 303 | } |
292 | 304 | ||
293 | impl Test for () { | 305 | impl Test for () { |
294 | type<|> | 306 | type S<|> |
295 | } | 307 | } |
296 | ", | 308 | ", |
297 | ); | 309 | ); |
@@ -299,10 +311,11 @@ mod tests { | |||
299 | [ | 311 | [ |
300 | CompletionItem { | 312 | CompletionItem { |
301 | label: "type SomeType = ", | 313 | label: "type SomeType = ", |
302 | source_range: [123; 123), | 314 | source_range: [124; 125), |
303 | delete: [119; 123), | 315 | delete: [119; 125), |
304 | insert: "type SomeType = ", | 316 | insert: "type SomeType = ", |
305 | kind: TypeAlias, | 317 | kind: TypeAlias, |
318 | lookup: "SomeType", | ||
306 | }, | 319 | }, |
307 | ] | 320 | ] |
308 | "###); | 321 | "###); |
@@ -329,6 +342,7 @@ mod tests { | |||
329 | delete: [127; 134), | 342 | delete: [127; 134), |
330 | insert: "const SOME_CONST: u16 = ", | 343 | insert: "const SOME_CONST: u16 = ", |
331 | kind: Const, | 344 | kind: Const, |
345 | lookup: "SOME_CONST", | ||
332 | }, | 346 | }, |
333 | ] | 347 | ] |
334 | "###); | 348 | "###); |
@@ -355,6 +369,7 @@ mod tests { | |||
355 | delete: [132; 139), | 369 | delete: [132; 139), |
356 | insert: "const SOME_CONST: u16 = ", | 370 | insert: "const SOME_CONST: u16 = ", |
357 | kind: Const, | 371 | kind: Const, |
372 | lookup: "SOME_CONST", | ||
358 | }, | 373 | }, |
359 | ] | 374 | ] |
360 | "###); | 375 | "###); |