]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/trait_impl.rs
Replace some String usages with SmolStr in completions
[rust.git] / crates / ide_completion / src / completions / trait_impl.rs
index 65f0f38430c5f2261d240c8274b6e3bcfa6fae23..46da7cfa372f059d291e570d084e903c01036fba 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`.
 //!
 use hir::{self, HasAttrs, HasSource};
 use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
 use syntax::{
-    ast::{self, edit},
+    ast::{self, edit_in_place::AttrsOwnerEdit},
     display::function_declaration,
     AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, T,
 };
 use text_edit::TextEdit;
 
-use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
+use crate::{CompletionContext, CompletionItem, CompletionItemKind, Completions};
 
 #[derive(Debug, PartialEq, Eq)]
 enum ImplCompletionKind {
@@ -133,7 +133,7 @@ fn add_function_impl(
     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,14 +141,14 @@ 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 mut item = CompletionItem::new(completion_kind, ctx.source_range(), label);
+    item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
+
     let range = replacement_range(ctx, fn_def_node);
 
     if let Some(source) = func.source(ctx.db) {
@@ -170,7 +170,6 @@ fn add_function_impl(
                     item.text_edit(TextEdit::replace(range, header));
                 }
             };
-            item.kind(completion_kind);
             item.add_to(acc);
         }
     }
@@ -185,18 +184,19 @@ 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 transform = PathTransform {
-        subst: (trait_, impl_def.source(ctx.db)?.value),
-        source_scope,
+    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,
+    );
 
-    transform.apply(assoc_item.clone());
-    Some(match assoc_item {
-        ast::AssocItem::Fn(func) => ast::AssocItem::Fn(edit::remove_attrs_and_docs(&func)),
-        _ => assoc_item,
-    })
+    transform.apply(assoc_item.syntax());
+    if let ast::AssocItem::Fn(func) = &assoc_item {
+        func.remove_attrs_and_docs()
+    }
+    Some(assoc_item)
 }
 
 fn add_type_alias_impl(
@@ -205,15 +205,14 @@ fn add_type_alias_impl(
     ctx: &CompletionContext,
     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, ctx.source_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);
 }
@@ -225,7 +224,7 @@ fn add_const_impl(
     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) {
@@ -239,11 +238,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, ctx.source_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);
             }
@@ -252,7 +249,7 @@ fn add_const_impl(
 }
 
 fn make_const_compl_syntax(const_: &ast::Const) -> String {
-    let const_ = edit::remove_attrs_and_docs(const_);
+    const_.remove_attrs_and_docs();
 
     let const_start = const_.syntax().text_range().start();
     let const_end = const_.syntax().text_range().end();
@@ -289,39 +286,13 @@ 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)
     }
 
-    #[test]
-    fn name_ref_function_type_const() {
-        check(
-            r#"
-trait Test {
-    type TestType;
-    const TEST_CONST: u16;
-    fn test();
-}
-struct T;
-
-impl Test for T {
-    t$0
-}
-"#,
-            expect![["
-ta type TestType = \n\
-ct const TEST_CONST: u16 = \n\
-fn fn test()
-"]],
-        );
-    }
-
     #[test]
     fn no_completion_inside_fn() {
         check(
@@ -335,7 +306,12 @@ fn test() {
     }
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -378,7 +354,7 @@ fn test() {
     }
 }
 ",
-            expect![[""]],
+            expect![[r#""#]],
         );
 
         check(
@@ -390,7 +366,10 @@ impl Test for T {
     fn test(t$0)
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                st T
+            "#]],
         );
 
         check(
@@ -402,7 +381,10 @@ impl Test for T {
     fn test(f: fn $0)
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                st T
+            "#]],
         );
     }
 
@@ -417,7 +399,7 @@ impl Test for T {
     const TEST: fn $0
 }
 ",
-            expect![[""]],
+            expect![[r#""#]],
         );
 
         check(
@@ -429,7 +411,12 @@ impl Test for T {
     const TEST: T$0
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -441,7 +428,12 @@ impl Test for T {
     const TEST: u32 = f$0
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -455,7 +447,12 @@ impl Test for T {
     };
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -498,7 +495,12 @@ impl Test for T {
     type Test = T$0;
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
 
         check(
@@ -510,7 +512,12 @@ impl Test for T {
     type Test = fn $0;
 }
 ",
-            expect![[""]],
+            expect![[r#"
+                sp Self
+                tt Test
+                st T
+                bt u32
+            "#]],
         );
     }
 
@@ -572,27 +579,6 @@ fn test() {
         );
     }
 
-    #[test]
-    fn hide_implemented_fn() {
-        check(
-            r#"
-trait Test {
-    fn foo();
-    fn foo_bar();
-}
-struct T;
-
-impl Test for T {
-    fn foo() {}
-    fn f$0
-}
-"#,
-            expect![[r#"
-                fn fn foo_bar()
-            "#]],
-        );
-    }
-
     #[test]
     fn generic_fn() {
         check_edit(