aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion/complete_trait_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/completion/complete_trait_impl.rs')
-rw-r--r--crates/ide/src/completion/complete_trait_impl.rs377
1 files changed, 311 insertions, 66 deletions
diff --git a/crates/ide/src/completion/complete_trait_impl.rs b/crates/ide/src/completion/complete_trait_impl.rs
index 26f268bd1..ff115df92 100644
--- a/crates/ide/src/completion/complete_trait_impl.rs
+++ b/crates/ide/src/completion/complete_trait_impl.rs
@@ -46,76 +46,86 @@ use crate::{
46 display::function_declaration, 46 display::function_declaration,
47}; 47};
48 48
49#[derive(Debug, PartialEq, Eq)]
50enum ImplCompletionKind {
51 All,
52 Fn,
53 TypeAlias,
54 Const,
55}
56
49pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { 57pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
50 if let Some((trigger, impl_def)) = completion_match(ctx) { 58 if let Some((kind, trigger, impl_def)) = completion_match(ctx) {
51 match trigger.kind() { 59 get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| match item {
52 SyntaxKind::NAME_REF => get_missing_assoc_items(&ctx.sema, &impl_def) 60 hir::AssocItem::Function(fn_item)
53 .into_iter() 61 if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Fn =>
54 .for_each(|item| match item { 62 {
55 hir::AssocItem::Function(fn_item) => { 63 add_function_impl(&trigger, acc, ctx, fn_item)
56 add_function_impl(&trigger, acc, ctx, fn_item)
57 }
58 hir::AssocItem::TypeAlias(type_item) => {
59 add_type_alias_impl(&trigger, acc, ctx, type_item)
60 }
61 hir::AssocItem::Const(const_item) => {
62 add_const_impl(&trigger, acc, ctx, const_item)
63 }
64 }),
65
66 SyntaxKind::FN => {
67 for missing_fn in get_missing_assoc_items(&ctx.sema, &impl_def)
68 .into_iter()
69 .filter_map(|item| match item {
70 hir::AssocItem::Function(fn_item) => Some(fn_item),
71 _ => None,
72 })
73 {
74 add_function_impl(&trigger, acc, ctx, missing_fn);
75 }
76 } 64 }
77 65 hir::AssocItem::TypeAlias(type_item)
78 SyntaxKind::TYPE_ALIAS => { 66 if kind == ImplCompletionKind::All || kind == ImplCompletionKind::TypeAlias =>
79 for missing_fn in get_missing_assoc_items(&ctx.sema, &impl_def) 67 {
80 .into_iter() 68 add_type_alias_impl(&trigger, acc, ctx, type_item)
81 .filter_map(|item| match item {
82 hir::AssocItem::TypeAlias(type_item) => Some(type_item),
83 _ => None,
84 })
85 {
86 add_type_alias_impl(&trigger, acc, ctx, missing_fn);
87 }
88 } 69 }
89 70 hir::AssocItem::Const(const_item)
90 SyntaxKind::CONST => { 71 if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Const =>
91 for missing_fn in get_missing_assoc_items(&ctx.sema, &impl_def) 72 {
92 .into_iter() 73 add_const_impl(&trigger, acc, ctx, const_item)
93 .filter_map(|item| match item {
94 hir::AssocItem::Const(const_item) => Some(const_item),
95 _ => None,
96 })
97 {
98 add_const_impl(&trigger, acc, ctx, missing_fn);
99 }
100 } 74 }
101
102 _ => {} 75 _ => {}
103 } 76 });
104 } 77 }
105} 78}
106 79
107fn completion_match(ctx: &CompletionContext) -> Option<(SyntaxNode, Impl)> { 80fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, SyntaxNode, Impl)> {
108 let (trigger, impl_def_offset) = ctx.token.ancestors().find_map(|p| match p.kind() { 81 let mut token = ctx.token.clone();
109 SyntaxKind::FN | SyntaxKind::TYPE_ALIAS | SyntaxKind::CONST | SyntaxKind::BLOCK_EXPR => { 82 // For keywork without name like `impl .. { fn <|> }`, the current position is inside
110 Some((p, 2)) 83 // the whitespace token, which is outside `FN` syntax node.
111 } 84 // We need to follow the previous token in this case.
112 SyntaxKind::NAME_REF => Some((p, 5)), 85 if token.kind() == SyntaxKind::WHITESPACE {
113 _ => None, 86 token = token.prev_token()?;
114 })?; 87 }
115 let impl_def = (0..impl_def_offset - 1) 88
116 .try_fold(trigger.parent()?, |t, _| t.parent()) 89 let impl_item_offset = match token.kind() {
117 .and_then(ast::Impl::cast)?; 90 // `impl .. { const <|> }`
118 Some((trigger, impl_def)) 91 // ERROR 0
92 // CONST_KW <- *
93 SyntaxKind::CONST_KW => 0,
94 // `impl .. { fn/type <|> }`
95 // FN/TYPE_ALIAS 0
96 // FN_KW <- *
97 SyntaxKind::FN_KW | SyntaxKind::TYPE_KW => 0,
98 // `impl .. { fn/type/const foo<|> }`
99 // FN/TYPE_ALIAS/CONST 1
100 // NAME 0
101 // IDENT <- *
102 SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME => 1,
103 // `impl .. { foo<|> }`
104 // MACRO_CALL 3
105 // PATH 2
106 // PATH_SEGMENT 1
107 // NAME_REF 0
108 // IDENT <- *
109 SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME_REF => 3,
110 _ => return None,
111 };
112
113 let impl_item = token.ancestors().nth(impl_item_offset)?;
114 // Must directly belong to an impl block.
115 // IMPL
116 // ASSOC_ITEM_LIST
117 // <item>
118 let impl_def = ast::Impl::cast(impl_item.parent()?.parent()?)?;
119 let kind = match impl_item.kind() {
120 // `impl ... { const <|> fn/type/const }`
121 _ if token.kind() == SyntaxKind::CONST_KW => ImplCompletionKind::Const,
122 SyntaxKind::CONST | SyntaxKind::ERROR => ImplCompletionKind::Const,
123 SyntaxKind::TYPE_ALIAS => ImplCompletionKind::TypeAlias,
124 SyntaxKind::FN => ImplCompletionKind::Fn,
125 SyntaxKind::MACRO_CALL => ImplCompletionKind::All,
126 _ => return None,
127 };
128 Some((kind, impl_item, impl_def))
119} 129}
120 130
121fn add_function_impl( 131fn add_function_impl(
@@ -261,19 +271,191 @@ ta type TestType = \n\
261 } 271 }
262 272
263 #[test] 273 #[test]
264 fn no_nested_fn_completions() { 274 fn no_completion_inside_fn() {
265 check( 275 check(
266 r" 276 r"
267trait Test { 277trait Test { fn test(); fn test2(); }
268 fn test(); 278struct T;
269 fn test2(); 279
280impl Test for T {
281 fn test() {
282 t<|>
283 }
284}
285",
286 expect![[""]],
287 );
288
289 check(
290 r"
291trait Test { fn test(); fn test2(); }
292struct T;
293
294impl Test for T {
295 fn test() {
296 fn t<|>
297 }
298}
299",
300 expect![[""]],
301 );
302
303 check(
304 r"
305trait Test { fn test(); fn test2(); }
306struct T;
307
308impl Test for T {
309 fn test() {
310 fn <|>
311 }
270} 312}
313",
314 expect![[""]],
315 );
316
317 // https://github.com/rust-analyzer/rust-analyzer/pull/5976#issuecomment-692332191
318 check(
319 r"
320trait Test { fn test(); fn test2(); }
271struct T; 321struct T;
272 322
273impl Test for T { 323impl Test for T {
274 fn test() { 324 fn test() {
325 foo.<|>
326 }
327}
328",
329 expect![[""]],
330 );
331
332 check(
333 r"
334trait Test { fn test(_: i32); fn test2(); }
335struct T;
336
337impl Test for T {
338 fn test(t<|>)
339}
340",
341 expect![[""]],
342 );
343
344 check(
345 r"
346trait Test { fn test(_: fn()); fn test2(); }
347struct T;
348
349impl Test for T {
350 fn test(f: fn <|>)
351}
352",
353 expect![[""]],
354 );
355 }
356
357 #[test]
358 fn no_completion_inside_const() {
359 check(
360 r"
361trait Test { const TEST: fn(); const TEST2: u32; type Test; fn test(); }
362struct T;
363
364impl Test for T {
365 const TEST: fn <|>
366}
367",
368 expect![[""]],
369 );
370
371 check(
372 r"
373trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
374struct T;
375
376impl Test for T {
377 const TEST: T<|>
378}
379",
380 expect![[""]],
381 );
382
383 check(
384 r"
385trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
386struct T;
387
388impl Test for T {
389 const TEST: u32 = f<|>
390}
391",
392 expect![[""]],
393 );
394
395 check(
396 r"
397trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
398struct T;
399
400impl Test for T {
401 const TEST: u32 = {
275 t<|> 402 t<|>
403 };
404}
405",
406 expect![[""]],
407 );
408
409 check(
410 r"
411trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
412struct T;
413
414impl Test for T {
415 const TEST: u32 = {
416 fn <|>
417 };
418}
419",
420 expect![[""]],
421 );
422
423 check(
424 r"
425trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
426struct T;
427
428impl Test for T {
429 const TEST: u32 = {
430 fn t<|>
431 };
432}
433",
434 expect![[""]],
435 );
276 } 436 }
437
438 #[test]
439 fn no_completion_inside_type() {
440 check(
441 r"
442trait Test { type Test; type Test2; fn test(); }
443struct T;
444
445impl Test for T {
446 type Test = T<|>;
447}
448",
449 expect![[""]],
450 );
451
452 check(
453 r"
454trait Test { type Test; type Test2; fn test(); }
455struct T;
456
457impl Test for T {
458 type Test = fn <|>;
277} 459}
278", 460",
279 expect![[""]], 461 expect![[""]],
@@ -485,4 +667,67 @@ impl Test for () {
485", 667",
486 ); 668 );
487 } 669 }
670
671 #[test]
672 fn complete_without_name() {
673 let test = |completion: &str, hint: &str, completed: &str, next_sibling: &str| {
674 println!(
675 "completion='{}', hint='{}', next_sibling='{}'",
676 completion, hint, next_sibling
677 );
678
679 check_edit(
680 completion,
681 &format!(
682 r#"
683trait Test {{
684 type Foo;
685 const CONST: u16;
686 fn bar();
687}}
688struct T;
689
690impl Test for T {{
691 {}
692 {}
693}}
694"#,
695 hint, next_sibling
696 ),
697 &format!(
698 r#"
699trait Test {{
700 type Foo;
701 const CONST: u16;
702 fn bar();
703}}
704struct T;
705
706impl Test for T {{
707 {}
708 {}
709}}
710"#,
711 completed, next_sibling
712 ),
713 )
714 };
715
716 // Enumerate some possible next siblings.
717 for next_sibling in &[
718 "",
719 "fn other_fn() {}", // `const <|> fn` -> `const fn`
720 "type OtherType = i32;",
721 "const OTHER_CONST: i32 = 0;",
722 "async fn other_fn() {}",
723 "unsafe fn other_fn() {}",
724 "default fn other_fn() {}",
725 "default type OtherType = i32;",
726 "default const OTHER_CONST: i32 = 0;",
727 ] {
728 test("bar", "fn <|>", "fn bar() {\n $0\n}", next_sibling);
729 test("Foo", "type <|>", "type Foo = ", next_sibling);
730 test("CONST", "const <|>", "const CONST: u16 = ", next_sibling);
731 }
732 }
488} 733}