Fully render const item completions from hir

This commit is contained in:
Lukas Wirth 2021-12-21 16:34:55 +01:00
parent 929cae74b1
commit 40d5c58a80
6 changed files with 11 additions and 34 deletions

View file

@ -420,7 +420,7 @@ where
E: Fn(&D) -> Option<V>,
V: Display,
{
let label = if let Some(value) = (value_extractor)(&def) {
let label = if let Some(value) = value_extractor(&def) {
format!("{} = {}", def.display(db), value)
} else {
def.display(db).to_string()

View file

@ -287,7 +287,7 @@ fn foo() { let _ = lib::S::$0 }
"#,
expect![[r#"
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;
"#]],
);
@ -379,10 +379,10 @@ fn foo<T: Sub>() { T::$0 }
expect![[r#"
ta SubTy (as Sub) type SubTy;
ta Ty (as Super) type Ty;
ct C2 (as Sub) const C2: ();
ct C2 (as Sub) const C2: ()
fn subfunc() (as Sub) fn()
me submethod() (as Sub) fn(&self)
ct CONST (as Super) const CONST: u8;
ct CONST (as Super) const CONST: u8
fn func() (as Super) fn()
me method() (as Super) fn(&self)
"#]],
@ -419,10 +419,10 @@ impl<T> Sub for Wrap<T> {
expect![[r#"
ta SubTy (as Sub) type SubTy;
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()
me method() (as Super) fn(&self)
ct C2 (as Sub) const C2: ();
ct C2 (as Sub) const C2: ()
fn subfunc() (as Sub) fn()
me submethod() (as Sub) fn(&self)
"#]],
@ -653,7 +653,7 @@ impl u8 {
}
"#,
expect![[r#"
ct MAX pub const MAX: Self;
ct MAX pub const MAX: Self
me func() fn(self)
"#]],
);

View file

@ -1,8 +1,7 @@
//! Renderer for `const` fields.
use hir::{AsAssocItem, HasSource};
use hir::{AsAssocItem, HirDisplay};
use ide_db::SymbolKind;
use syntax::display::const_label;
use crate::{item::CompletionItem, render::RenderContext};
@ -14,8 +13,7 @@ pub(crate) fn render_const(ctx: RenderContext<'_>, const_: hir::Const) -> Option
fn render(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem> {
let db = ctx.db();
let name = const_.name(db)?.to_smol_str();
// FIXME: This is parsing files!
let detail = const_label(&const_.source(db)?.value);
let detail = const_.display(db).to_string();
let mut item = CompletionItem::new(SymbolKind::Const, ctx.source_range(), name.clone());
item.set_documentation(ctx.docs(const_))

View file

@ -546,7 +546,7 @@ fn func() {
ev TupleV() (u32)
ev RecordV {field: u32}
ev UnitV ()
ct ASSOC_CONST const ASSOC_CONST: ();
ct ASSOC_CONST const ASSOC_CONST: ()
fn assoc_fn() fn()
ta AssocType type AssocType;
"#]],

View file

@ -294,7 +294,7 @@ fn func() {
ev TupleV() (u32)
ev RecordV {field: u32}
ev UnitV ()
ct ASSOC_CONST const ASSOC_CONST: ();
ct ASSOC_CONST const ASSOC_CONST: ()
fn assoc_fn() fn()
ta AssocType type AssocType;
"#]],

View file

@ -50,27 +50,6 @@ pub fn function_declaration(node: &ast::Fn) -> String {
buf
}
pub fn const_label(node: &ast::Const) -> String {
let mut s = String::new();
if let Some(vis) = node.visibility() {
format_to!(s, "{} ", vis);
}
format_to!(s, "const ");
if let Some(name) = node.name() {
format_to!(s, "{}", name);
} else {
format_to!(s, "?");
}
format_to!(s, ": ");
if let Some(ty) = node.ty() {
format_to!(s, "{}", ty);
} else {
format_to!(s, "?");
}
format_to!(s, ";");
s
}
pub fn type_label(node: &ast::TypeAlias) -> String {
let mut s = String::new();
if let Some(vis) = node.visibility() {