]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/trait_impl.rs
internal: Expand the derive attribute into a pseudo expansion
[rust.git] / crates / ide_completion / src / completions / trait_impl.rs
index 6ff751418739fc852eddf198a4060583f753a9a6..0d59f77a55c975d4ac5b4534d91e201feae904c7 100644 (file)
@@ -1,7 +1,7 @@
 //! Completion for associated items in a trait implementation.
 //!
 //! This module adds the completion items related to implementing associated
-//! items within a `impl Trait for Struct` block. The current context node
+//! items within an `impl Trait for Struct` block. The current context node
 //! must be within either a `FN`, `TYPE_ALIAS`, or `CONST` node
 //! and an direct child of an `IMPL`.
 //!
@@ -31,7 +31,7 @@
 //! }
 //! ```
 
-use hir::{self, HasAttrs, HasSource};
+use hir::{self, HasAttrs};
 use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
 use syntax::{
     ast::{self, edit_in_place::AttrsOwnerEdit},
@@ -40,9 +40,9 @@
 };
 use text_edit::TextEdit;
 
-use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
+use crate::{CompletionContext, CompletionItem, CompletionItemKind, Completions};
 
-#[derive(Debug, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 enum ImplCompletionKind {
     All,
     Fn,
@@ -53,23 +53,22 @@ enum ImplCompletionKind {
 pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
     if let Some((kind, trigger, impl_def)) = completion_match(ctx.token.clone()) {
         if let Some(hir_impl) = ctx.sema.to_def(&impl_def) {
-            get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| match item {
-                hir::AssocItem::Function(fn_item)
-                    if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Fn =>
-                {
-                    add_function_impl(&trigger, acc, ctx, fn_item, hir_impl)
+            get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| {
+                match (item, kind) {
+                    (
+                        hir::AssocItem::Function(fn_item),
+                        ImplCompletionKind::All | ImplCompletionKind::Fn,
+                    ) => add_function_impl(acc, ctx, &trigger, fn_item, hir_impl),
+                    (
+                        hir::AssocItem::TypeAlias(type_item),
+                        ImplCompletionKind::All | ImplCompletionKind::TypeAlias,
+                    ) => add_type_alias_impl(acc, ctx, &trigger, type_item),
+                    (
+                        hir::AssocItem::Const(const_item),
+                        ImplCompletionKind::All | ImplCompletionKind::Const,
+                    ) => add_const_impl(acc, ctx, &trigger, const_item, hir_impl),
+                    _ => {}
                 }
-                hir::AssocItem::TypeAlias(type_item)
-                    if kind == ImplCompletionKind::All || kind == ImplCompletionKind::TypeAlias =>
-                {
-                    add_type_alias_impl(&trigger, acc, ctx, type_item)
-                }
-                hir::AssocItem::Const(const_item)
-                    if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Const =>
-                {
-                    add_const_impl(&trigger, acc, ctx, const_item, hir_impl)
-                }
-                _ => {}
             });
         }
     }
@@ -127,13 +126,13 @@ fn completion_match(mut token: SyntaxToken) -> Option<(ImplCompletionKind, Synta
 }
 
 fn add_function_impl(
-    fn_def_node: &SyntaxNode,
     acc: &mut Completions,
     ctx: &CompletionContext,
+    fn_def_node: &SyntaxNode,
     func: hir::Function,
     impl_def: hir::Impl,
 ) {
-    let fn_name = func.name(ctx.db).to_string();
+    let fn_name = func.name(ctx.db).to_smol_str();
 
     let label = if func.assoc_fn_params(ctx.db).is_empty() {
         format!("fn {}()", fn_name)
@@ -141,17 +140,17 @@ fn add_function_impl(
         format!("fn {}(..)", fn_name)
     };
 
-    let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
-    item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
-
     let completion_kind = if func.self_param(ctx.db).is_some() {
         CompletionItemKind::Method
     } else {
         CompletionItemKind::SymbolKind(SymbolKind::Function)
     };
+
     let range = replacement_range(ctx, fn_def_node);
+    let mut item = CompletionItem::new(completion_kind, range, label);
+    item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
 
-    if let Some(source) = func.source(ctx.db) {
+    if let Some(source) = ctx.sema.source(func) {
         let assoc_item = ast::AssocItem::Fn(source.value);
         if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
             let transformed_fn = match transformed_item {
@@ -170,7 +169,6 @@ fn add_function_impl(
                     item.text_edit(TextEdit::replace(range, header));
                 }
             };
-            item.kind(completion_kind);
             item.add_to(acc);
         }
     }
@@ -185,51 +183,50 @@ fn get_transformed_assoc_item(
     let assoc_item = assoc_item.clone_for_update();
     let trait_ = impl_def.trait_(ctx.db)?;
     let source_scope = &ctx.sema.scope_for_def(trait_);
-    let target_scope = &ctx.sema.scope(impl_def.source(ctx.db)?.syntax().value);
+    let target_scope = &ctx.sema.scope(ctx.sema.source(impl_def)?.syntax().value);
     let transform = PathTransform::trait_impl(
         target_scope,
         source_scope,
         trait_,
-        impl_def.source(ctx.db)?.value,
+        ctx.sema.source(impl_def)?.value,
     );
 
     transform.apply(assoc_item.syntax());
     if let ast::AssocItem::Fn(func) = &assoc_item {
-        func.remove_attrs_and_docs()
+        func.remove_attrs_and_docs();
     }
     Some(assoc_item)
 }
 
 fn add_type_alias_impl(
-    type_def_node: &SyntaxNode,
     acc: &mut Completions,
     ctx: &CompletionContext,
+    type_def_node: &SyntaxNode,
     type_alias: hir::TypeAlias,
 ) {
-    let alias_name = type_alias.name(ctx.db).to_string();
+    let alias_name = type_alias.name(ctx.db).to_smol_str();
 
     let snippet = format!("type {} = ", alias_name);
 
     let range = replacement_range(ctx, type_def_node);
-    let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
+    let mut item = CompletionItem::new(SymbolKind::TypeAlias, range, &snippet);
     item.text_edit(TextEdit::replace(range, snippet))
         .lookup_by(alias_name)
-        .kind(SymbolKind::TypeAlias)
         .set_documentation(type_alias.docs(ctx.db));
     item.add_to(acc);
 }
 
 fn add_const_impl(
-    const_def_node: &SyntaxNode,
     acc: &mut Completions,
     ctx: &CompletionContext,
+    const_def_node: &SyntaxNode,
     const_: hir::Const,
     impl_def: hir::Impl,
 ) {
-    let const_name = const_.name(ctx.db).map(|n| n.to_string());
+    let const_name = const_.name(ctx.db).map(|n| n.to_smol_str());
 
     if let Some(const_name) = const_name {
-        if let Some(source) = const_.source(ctx.db) {
+        if let Some(source) = ctx.sema.source(const_) {
             let assoc_item = ast::AssocItem::Const(source.value);
             if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
                 let transformed_const = match transformed_item {
@@ -240,11 +237,9 @@ fn add_const_impl(
                 let snippet = make_const_compl_syntax(&transformed_const);
 
                 let range = replacement_range(ctx, const_def_node);
-                let mut item =
-                    CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
+                let mut item = CompletionItem::new(SymbolKind::Const, range, &snippet);
                 item.text_edit(TextEdit::replace(range, snippet))
                     .lookup_by(const_name)
-                    .kind(SymbolKind::Const)
                     .set_documentation(const_.docs(ctx.db));
                 item.add_to(acc);
             }
@@ -290,13 +285,10 @@ fn replacement_range(ctx: &CompletionContext, item: &SyntaxNode) -> TextRange {
 mod tests {
     use expect_test::{expect, Expect};
 
-    use crate::{
-        tests::{check_edit, filtered_completion_list},
-        CompletionKind,
-    };
+    use crate::tests::{check_edit, completion_list_no_kw};
 
     fn check(ra_fixture: &str, expect: Expect) {
-        let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
+        let actual = completion_list_no_kw(ra_fixture);
         expect.assert_eq(&actual)
     }
 
@@ -313,7 +305,12 @@ fn test() {
     }
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -356,7 +353,7 @@ fn test() {
     }
 }
 ",
-            expect![[""]],
+            expect![[r#""#]],
         );
 
         check(
@@ -368,7 +365,10 @@ impl Test for T {
     fn test(t$0)
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                st T
+            "#]],
         );
 
         check(
@@ -380,7 +380,10 @@ impl Test for T {
     fn test(f: fn $0)
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                st T
+            "#]],
         );
     }
 
@@ -395,7 +398,7 @@ impl Test for T {
     const TEST: fn $0
 }
 ",
-            expect![[""]],
+            expect![[r#""#]],
         );
 
         check(
@@ -407,7 +410,12 @@ impl Test for T {
     const TEST: T$0
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -419,7 +427,12 @@ impl Test for T {
     const TEST: u32 = f$0
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -433,7 +446,12 @@ impl Test for T {
     };
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -476,7 +494,12 @@ impl Test for T {
     type Test = T$0;
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -488,7 +511,12 @@ impl Test for T {
     type Test = fn $0;
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
     }