Fully render type alias completions from hir

This commit is contained in:
Lukas Wirth 2021-12-21 16:36:04 +01:00
parent 40d5c58a80
commit 7f7a3644b3
6 changed files with 17 additions and 36 deletions

View File

@ -288,7 +288,7 @@ fn foo() { let _ = lib::S::$0 }
expect![[r#" expect![[r#"
fn public_method() fn() fn public_method() fn()
ct PUBLIC_CONST pub const PUBLIC_CONST: u32 ct PUBLIC_CONST pub const PUBLIC_CONST: u32
ta PublicType pub type PublicType; ta PublicType pub type PublicType = u32
"#]], "#]],
); );
} }
@ -377,8 +377,8 @@ trait Sub: Super {
fn foo<T: Sub>() { T::$0 } fn foo<T: Sub>() { T::$0 }
"#, "#,
expect![[r#" expect![[r#"
ta SubTy (as Sub) type SubTy; ta SubTy (as Sub) type SubTy
ta Ty (as Super) type Ty; ta Ty (as Super) type Ty
ct C2 (as Sub) const C2: () ct C2 (as Sub) const C2: ()
fn subfunc() (as Sub) fn() fn subfunc() (as Sub) fn()
me submethod() (as Sub) fn(&self) me submethod() (as Sub) fn(&self)
@ -417,8 +417,8 @@ impl<T> Sub for Wrap<T> {
} }
"#, "#,
expect![[r#" expect![[r#"
ta SubTy (as Sub) type SubTy; ta SubTy (as Sub) type SubTy
ta Ty (as Super) type Ty; ta Ty (as Super) type Ty
ct CONST (as Super) const CONST: u8 ct CONST (as Super) const CONST: u8
fn func() (as Super) fn() fn func() (as Super) fn()
me method() (as Super) fn(&self) me method() (as Super) fn(&self)

View File

@ -1,8 +1,7 @@
//! Renderer for type aliases. //! Renderer for type aliases.
use hir::{AsAssocItem, HasSource}; use hir::{AsAssocItem, HirDisplay};
use ide_db::SymbolKind; use ide_db::SymbolKind;
use syntax::{ast::HasName, display::type_label};
use crate::{item::CompletionItem, render::RenderContext}; use crate::{item::CompletionItem, render::RenderContext};
@ -29,16 +28,13 @@ fn render(
) -> Option<CompletionItem> { ) -> Option<CompletionItem> {
let db = ctx.db(); let db = ctx.db();
// FIXME: This parses the file! // FIXME: smolstr?
let ast_node = type_alias.source(db)?.value; let name = if with_eq {
let name = ast_node.name().map(|name| { format!("{} = ", type_alias.name(db))
if with_eq { } else {
format!("{} = ", name.text()) type_alias.name(db).to_string()
} else { };
name.text().to_string() let detail = type_alias.display(db).to_string();
}
})?;
let detail = type_label(&ast_node);
let mut item = CompletionItem::new(SymbolKind::TypeAlias, ctx.source_range(), name.clone()); let mut item = CompletionItem::new(SymbolKind::TypeAlias, ctx.source_range(), name.clone());
item.set_documentation(ctx.docs(type_alias)) item.set_documentation(ctx.docs(type_alias))

View File

@ -548,7 +548,7 @@ fn func() {
ev UnitV () ev UnitV ()
ct ASSOC_CONST const ASSOC_CONST: () ct ASSOC_CONST const ASSOC_CONST: ()
fn assoc_fn() fn() fn assoc_fn() fn()
ta AssocType type AssocType; ta AssocType type AssocType = ()
"#]], "#]],
); );
} }

View File

@ -296,7 +296,7 @@ fn func() {
ev UnitV () ev UnitV ()
ct ASSOC_CONST const ASSOC_CONST: () ct ASSOC_CONST const ASSOC_CONST: ()
fn assoc_fn() fn() fn assoc_fn() fn()
ta AssocType type AssocType; ta AssocType type AssocType = ()
"#]], "#]],
); );
} }

View File

@ -148,7 +148,7 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
kw self kw self
kw super kw super
kw crate kw crate
ta Foo = (as Trait2) type Foo; ta Foo = (as Trait2) type Foo
tp T tp T
cp CONST_PARAM cp CONST_PARAM
tt Trait tt Trait
@ -199,7 +199,7 @@ impl Enum {
fn func(_: Enum::$0) {} fn func(_: Enum::$0) {}
"#, "#,
expect![[r#" expect![[r#"
ta AssocType type AssocType; ta AssocType type AssocType = ()
"#]], "#]],
); );
} }

View File

@ -50,21 +50,6 @@ pub fn function_declaration(node: &ast::Fn) -> String {
buf 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 { pub fn macro_label(node: &ast::Macro) -> String {
let name = node.name(); let name = node.name();
let mut s = String::new(); let mut s = String::new();