]> git.lizzy.rs Git - rust.git/commitdiff
Fully render type alias completions from hir
authorLukas Wirth <lukastw97@gmail.com>
Tue, 21 Dec 2021 15:36:04 +0000 (16:36 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Tue, 21 Dec 2021 15:36:23 +0000 (16:36 +0100)
crates/ide_completion/src/completions/qualified_path.rs
crates/ide_completion/src/render/type_alias.rs
crates/ide_completion/src/tests/expression.rs
crates/ide_completion/src/tests/pattern.rs
crates/ide_completion/src/tests/type_pos.rs
crates/syntax/src/display.rs

index 8628de413b5179c2dff31ff0e044e8b1297b91fa..88fb3959203286746991a29c134c21944ee48f97 100644 (file)
@@ -288,7 +288,7 @@ fn foo() { let _ = lib::S::$0 }
             expect![[r#"
                 fn public_method() fn()
                 ct PUBLIC_CONST    pub const PUBLIC_CONST: u32
-                ta PublicType      pub type PublicType;
+                ta PublicType      pub type PublicType = u32
             "#]],
         );
     }
@@ -377,8 +377,8 @@ fn submethod(&self) {}
 fn foo<T: Sub>() { T::$0 }
 "#,
             expect![[r#"
-                ta SubTy (as Sub)        type SubTy;
-                ta Ty (as Super)         type Ty;
+                ta SubTy (as Sub)        type SubTy
+                ta Ty (as Super)         type Ty
                 ct C2 (as Sub)           const C2: ()
                 fn subfunc() (as Sub)    fn()
                 me submethod(…) (as Sub) fn(&self)
@@ -417,8 +417,8 @@ fn subfunc() {
 }
 "#,
             expect![[r#"
-                ta SubTy (as Sub)        type SubTy;
-                ta Ty (as Super)         type Ty;
+                ta SubTy (as Sub)        type SubTy
+                ta Ty (as Super)         type Ty
                 ct CONST (as Super)      const CONST: u8
                 fn func() (as Super)     fn()
                 me method(…) (as Super)  fn(&self)
index 5df21fb36cddea4b842ebeeff18fb2c1d0290924..be1d3212810f05e8f465da31d352789101ff3a1e 100644 (file)
@@ -1,8 +1,7 @@
 //! Renderer for type aliases.
 
-use hir::{AsAssocItem, HasSource};
+use hir::{AsAssocItem, HirDisplay};
 use ide_db::SymbolKind;
-use syntax::{ast::HasName, display::type_label};
 
 use crate::{item::CompletionItem, render::RenderContext};
 
@@ -29,16 +28,13 @@ fn render(
 ) -> Option<CompletionItem> {
     let db = ctx.db();
 
-    // FIXME: This parses the file!
-    let ast_node = type_alias.source(db)?.value;
-    let name = ast_node.name().map(|name| {
-        if with_eq {
-            format!("{} = ", name.text())
-        } else {
-            name.text().to_string()
-        }
-    })?;
-    let detail = type_label(&ast_node);
+    // FIXME: smolstr?
+    let name = if with_eq {
+        format!("{} = ", type_alias.name(db))
+    } else {
+        type_alias.name(db).to_string()
+    };
+    let detail = type_alias.display(db).to_string();
 
     let mut item = CompletionItem::new(SymbolKind::TypeAlias, ctx.source_range(), name.clone());
     item.set_documentation(ctx.docs(type_alias))
index eb95bdcda95b0555953adfff86ae4d48d2050a9f..56a2cd6e9dcc2e912592288bbc96332696c5e3b8 100644 (file)
@@ -548,7 +548,7 @@ fn func() {
             ev UnitV       ()
             ct ASSOC_CONST const ASSOC_CONST: ()
             fn assoc_fn()  fn()
-            ta AssocType   type AssocType;
+            ta AssocType   type AssocType = ()
         "#]],
     );
 }
index 0e2d4088274c236f7dfda06484e66973b2424133..c9a31eea8497928335a608c2156a14d0c6ca4cc0 100644 (file)
@@ -296,7 +296,7 @@ fn func() {
             ev UnitV       ()
             ct ASSOC_CONST const ASSOC_CONST: ()
             fn assoc_fn()  fn()
-            ta AssocType   type AssocType;
+            ta AssocType   type AssocType = ()
         "#]],
     );
 }
index a76f97f3da3f611ab9ea4b0269a7bb5c9889644c..d6c1a787ff9b2a03829ad84eda72f9ad9178436c 100644 (file)
@@ -148,7 +148,7 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
             kw self
             kw super
             kw crate
-            ta Foo =  (as Trait2) type Foo;
+            ta Foo =  (as Trait2) type Foo
             tp T
             cp CONST_PARAM
             tt Trait
@@ -199,7 +199,7 @@ fn assoc_fn() {}
 fn func(_: Enum::$0) {}
 "#,
         expect![[r#"
-            ta AssocType type AssocType;
+            ta AssocType type AssocType = ()
         "#]],
     );
 }
index 10f1c901387de41095131ad80cf763631770c7e5..d03e94d05832ca60e38792cb979b9ec27dce6f29 100644 (file)
@@ -50,21 +50,6 @@ pub fn function_declaration(node: &ast::Fn) -> String {
     buf
 }
 
-pub fn type_label(node: &ast::TypeAlias) -> String {
-    let mut s = String::new();
-    if let Some(vis) = node.visibility() {
-        format_to!(s, "{} ", vis);
-    }
-    format_to!(s, "type ");
-    if let Some(name) = node.name() {
-        format_to!(s, "{}", name);
-    } else {
-        format_to!(s, "?");
-    }
-    format_to!(s, ";");
-    s
-}
-
 pub fn macro_label(node: &ast::Macro) -> String {
     let name = node.name();
     let mut s = String::new();