show imported trait on autocompletion of associated items

This commit is contained in:
mahdi-frms 2021-07-04 00:59:47 +04:30
parent e5c1c8cf2f
commit 486bffc23e
8 changed files with 74 additions and 36 deletions

View file

@ -250,7 +250,7 @@ impl Trait for A {}
fn foo(a: A) { a.$0 }
"#,
expect![[r#"
me the_method() fn(&self)
me the_method() (Trait) fn(&self)
"#]],
);
}
@ -265,7 +265,7 @@ impl<T> Trait for T {}
fn foo(a: &A) { a.$0 }
",
expect![[r#"
me the_method() fn(&self)
me the_method() (Trait) fn(&self)
"#]],
);
}
@ -283,7 +283,7 @@ impl Trait for A {}
fn foo(a: A) { a.$0 }
",
expect![[r#"
me the_method() fn(&self)
me the_method() (Trait) fn(&self)
"#]],
);
}

View file

@ -333,7 +333,7 @@ trait Trait { fn m(); }
fn foo() { let _ = Trait::$0 }
"#,
expect![[r#"
fn m() fn()
fn m() (Trait) fn()
"#]],
);
}
@ -350,7 +350,7 @@ impl Trait for S {}
fn foo() { let _ = S::$0 }
"#,
expect![[r#"
fn m() fn()
fn m() (Trait) fn()
"#]],
);
}
@ -367,7 +367,7 @@ impl Trait for S {}
fn foo() { let _ = <S as Trait>::$0 }
"#,
expect![[r#"
fn m() fn()
fn m() (Trait) fn()
"#]],
);
}
@ -393,14 +393,14 @@ trait Sub: Super {
fn foo<T: Sub>() { T::$0 }
"#,
expect![[r#"
ta SubTy type SubTy;
ta Ty type Ty;
ct C2 const C2: ();
fn subfunc() fn()
me submethod() fn(&self)
ct CONST const CONST: u8;
fn func() fn()
me method() fn(&self)
ta SubTy (Sub) type SubTy;
ta Ty (Super) type Ty;
ct C2 (Sub) const C2: ();
fn subfunc() (Sub) fn()
me submethod() (Sub) fn(&self)
ct CONST (Super) const CONST: u8;
fn func() (Super) fn()
me method() (Super) fn(&self)
"#]],
);
}
@ -433,14 +433,14 @@ impl<T> Sub for Wrap<T> {
}
"#,
expect![[r#"
ta SubTy type SubTy;
ta Ty type Ty;
ct CONST const CONST: u8 = 0;
fn func() fn()
me method() fn(&self)
ct C2 const C2: () = ();
fn subfunc() fn()
me submethod() fn(&self)
ta SubTy (Sub) type SubTy;
ta Ty (Super) type Ty;
ct CONST (Super) const CONST: u8 = 0;
fn func() (Super) fn()
me method() (Super) fn(&self)
ct C2 (Sub) const C2: () = ();
fn subfunc() (Sub) fn()
me submethod() (Sub) fn(&self)
"#]],
);
}

View file

@ -1,6 +1,6 @@
//! Renderer for `const` fields.
use hir::HasSource;
use hir::{AsAssocItem, HasSource, ModuleDef};
use ide_db::SymbolKind;
use syntax::{
ast::{Const, NameOwner},
@ -37,7 +37,7 @@ impl<'a> ConstRender<'a> {
let detail = self.detail();
let mut item =
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name);
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name.clone());
item.kind(SymbolKind::Const)
.set_documentation(self.ctx.docs(self.const_))
.set_deprecated(
@ -46,6 +46,17 @@ impl<'a> ConstRender<'a> {
)
.detail(detail);
let db = self.ctx.db();
if let Some(actm) = self.const_.as_assoc_item(db) {
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
let module = self.ctx.completion.scope.module().unwrap();
if let Some(path) = module.find_use_path(db, ModuleDef::Trait(trt)) {
item.label(format!("{} ({})", name.clone(), path));
item.insert_text(name.clone());
}
}
}
Some(item.build())
}

View file

@ -1,6 +1,6 @@
//! Renderer for function calls.
use hir::{HasSource, HirDisplay};
use hir::{AsAssocItem, HasSource, HirDisplay, ModuleDef};
use ide_db::SymbolKind;
use itertools::Itertools;
use syntax::ast::Fn;
@ -73,9 +73,25 @@ impl<'a> FunctionRender<'a> {
self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func),
)
.detail(self.detail())
.add_call_parens(self.ctx.completion, call.clone(), params)
.add_import(import_to_add)
.lookup_by(self.name);
.add_call_parens(self.ctx.completion, call.clone(), params);
if import_to_add.is_none() {
let db = self.ctx.db();
if let Some(actm) = self.func.as_assoc_item(db) {
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
let module = self.ctx.completion.scope.module().unwrap();
if let Some(path) = module.find_use_path(db, ModuleDef::Trait(trt)) {
item.label(format!(
"{} ({})",
item.clone().build().label().to_owned(),
path
));
}
}
}
}
item.add_import(import_to_add).lookup_by(self.name);
let ret_type = self.func.ret_type(self.ctx.db());
item.set_relevance(CompletionRelevance {

View file

@ -1,6 +1,6 @@
//! Renderer for type aliases.
use hir::HasSource;
use hir::{AsAssocItem, HasSource, ModuleDef};
use ide_db::SymbolKind;
use syntax::{
ast::{NameOwner, TypeAlias},
@ -50,7 +50,7 @@ impl<'a> TypeAliasRender<'a> {
let detail = self.detail();
let mut item =
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name);
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name.clone());
item.kind(SymbolKind::TypeAlias)
.set_documentation(self.ctx.docs(self.type_alias))
.set_deprecated(
@ -59,6 +59,17 @@ impl<'a> TypeAliasRender<'a> {
)
.detail(detail);
let db = self.ctx.db();
if let Some(actm) = self.type_alias.as_assoc_item(db) {
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
let module = self.ctx.completion.scope.module().unwrap();
if let Some(path) = module.find_use_path(db, ModuleDef::Trait(trt)) {
item.label(format!("{} ({})", name.clone(), path));
item.insert_text(name.clone());
}
}
}
Some(item.build())
}

View file

@ -126,7 +126,7 @@ fn render_completion_list(completions: Vec<CompletionItem>) -> String {
s.chars().count()
}
let label_width =
completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(16);
completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(22);
completions
.into_iter()
.map(|it| {

View file

@ -140,7 +140,7 @@ trait Trait2 {
fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
"#,
expect![[r#"
ta Foo = type Foo;
ta Foo = (Trait2) type Foo;
tp T
cp CONST_PARAM
tt Trait