Skip to content

Commit 486bffc

Browse files
committed
show imported trait on autocompletion of associated items
1 parent e5c1c8c commit 486bffc

File tree

8 files changed

+74
-36
lines changed

8 files changed

+74
-36
lines changed

crates/ide_completion/src/completions/dot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl Trait for A {}
250250
fn foo(a: A) { a.$0 }
251251
"#,
252252
expect![[r#"
253-
me the_method() fn(&self)
253+
me the_method() (Trait) fn(&self)
254254
"#]],
255255
);
256256
}
@@ -265,7 +265,7 @@ impl<T> Trait for T {}
265265
fn foo(a: &A) { a.$0 }
266266
",
267267
expect![[r#"
268-
me the_method() fn(&self)
268+
me the_method() (Trait) fn(&self)
269269
"#]],
270270
);
271271
}
@@ -283,7 +283,7 @@ impl Trait for A {}
283283
fn foo(a: A) { a.$0 }
284284
",
285285
expect![[r#"
286-
me the_method() fn(&self)
286+
me the_method() (Trait) fn(&self)
287287
"#]],
288288
);
289289
}

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ trait Trait { fn m(); }
333333
fn foo() { let _ = Trait::$0 }
334334
"#,
335335
expect![[r#"
336-
fn m() fn()
336+
fn m() (Trait) fn()
337337
"#]],
338338
);
339339
}
@@ -350,7 +350,7 @@ impl Trait for S {}
350350
fn foo() { let _ = S::$0 }
351351
"#,
352352
expect![[r#"
353-
fn m() fn()
353+
fn m() (Trait) fn()
354354
"#]],
355355
);
356356
}
@@ -367,7 +367,7 @@ impl Trait for S {}
367367
fn foo() { let _ = <S as Trait>::$0 }
368368
"#,
369369
expect![[r#"
370-
fn m() fn()
370+
fn m() (Trait) fn()
371371
"#]],
372372
);
373373
}
@@ -393,14 +393,14 @@ trait Sub: Super {
393393
fn foo<T: Sub>() { T::$0 }
394394
"#,
395395
expect![[r#"
396-
ta SubTy type SubTy;
397-
ta Ty type Ty;
398-
ct C2 const C2: ();
399-
fn subfunc() fn()
400-
me submethod(…) fn(&self)
401-
ct CONST const CONST: u8;
402-
fn func() fn()
403-
me method(…) fn(&self)
396+
ta SubTy (Sub) type SubTy;
397+
ta Ty (Super) type Ty;
398+
ct C2 (Sub) const C2: ();
399+
fn subfunc() (Sub) fn()
400+
me submethod(…) (Sub) fn(&self)
401+
ct CONST (Super) const CONST: u8;
402+
fn func() (Super) fn()
403+
me method(…) (Super) fn(&self)
404404
"#]],
405405
);
406406
}
@@ -433,14 +433,14 @@ impl<T> Sub for Wrap<T> {
433433
}
434434
"#,
435435
expect![[r#"
436-
ta SubTy type SubTy;
437-
ta Ty type Ty;
438-
ct CONST const CONST: u8 = 0;
439-
fn func() fn()
440-
me method(…) fn(&self)
441-
ct C2 const C2: () = ();
442-
fn subfunc() fn()
443-
me submethod(…) fn(&self)
436+
ta SubTy (Sub) type SubTy;
437+
ta Ty (Super) type Ty;
438+
ct CONST (Super) const CONST: u8 = 0;
439+
fn func() (Super) fn()
440+
me method(…) (Super) fn(&self)
441+
ct C2 (Sub) const C2: () = ();
442+
fn subfunc() (Sub) fn()
443+
me submethod(…) (Sub) fn(&self)
444444
"#]],
445445
);
446446
}

crates/ide_completion/src/render/const_.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Renderer for `const` fields.
22
3-
use hir::HasSource;
3+
use hir::{AsAssocItem, HasSource, ModuleDef};
44
use ide_db::SymbolKind;
55
use syntax::{
66
ast::{Const, NameOwner},
@@ -37,7 +37,7 @@ impl<'a> ConstRender<'a> {
3737
let detail = self.detail();
3838

3939
let mut item =
40-
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name);
40+
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name.clone());
4141
item.kind(SymbolKind::Const)
4242
.set_documentation(self.ctx.docs(self.const_))
4343
.set_deprecated(
@@ -46,6 +46,17 @@ impl<'a> ConstRender<'a> {
4646
)
4747
.detail(detail);
4848

49+
let db = self.ctx.db();
50+
if let Some(actm) = self.const_.as_assoc_item(db) {
51+
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
52+
let module = self.ctx.completion.scope.module().unwrap();
53+
if let Some(path) = module.find_use_path(db, ModuleDef::Trait(trt)) {
54+
item.label(format!("{} ({})", name.clone(), path));
55+
item.insert_text(name.clone());
56+
}
57+
}
58+
}
59+
4960
Some(item.build())
5061
}
5162

crates/ide_completion/src/render/function.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Renderer for function calls.
22
3-
use hir::{HasSource, HirDisplay};
3+
use hir::{AsAssocItem, HasSource, HirDisplay, ModuleDef};
44
use ide_db::SymbolKind;
55
use itertools::Itertools;
66
use syntax::ast::Fn;
@@ -73,9 +73,25 @@ impl<'a> FunctionRender<'a> {
7373
self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func),
7474
)
7575
.detail(self.detail())
76-
.add_call_parens(self.ctx.completion, call.clone(), params)
77-
.add_import(import_to_add)
78-
.lookup_by(self.name);
76+
.add_call_parens(self.ctx.completion, call.clone(), params);
77+
78+
if import_to_add.is_none() {
79+
let db = self.ctx.db();
80+
if let Some(actm) = self.func.as_assoc_item(db) {
81+
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
82+
let module = self.ctx.completion.scope.module().unwrap();
83+
if let Some(path) = module.find_use_path(db, ModuleDef::Trait(trt)) {
84+
item.label(format!(
85+
"{} ({})",
86+
item.clone().build().label().to_owned(),
87+
path
88+
));
89+
}
90+
}
91+
}
92+
}
93+
94+
item.add_import(import_to_add).lookup_by(self.name);
7995

8096
let ret_type = self.func.ret_type(self.ctx.db());
8197
item.set_relevance(CompletionRelevance {

crates/ide_completion/src/render/type_alias.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Renderer for type aliases.
22
3-
use hir::HasSource;
3+
use hir::{AsAssocItem, HasSource, ModuleDef};
44
use ide_db::SymbolKind;
55
use syntax::{
66
ast::{NameOwner, TypeAlias},
@@ -50,7 +50,7 @@ impl<'a> TypeAliasRender<'a> {
5050
let detail = self.detail();
5151

5252
let mut item =
53-
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name);
53+
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name.clone());
5454
item.kind(SymbolKind::TypeAlias)
5555
.set_documentation(self.ctx.docs(self.type_alias))
5656
.set_deprecated(
@@ -59,6 +59,17 @@ impl<'a> TypeAliasRender<'a> {
5959
)
6060
.detail(detail);
6161

62+
let db = self.ctx.db();
63+
if let Some(actm) = self.type_alias.as_assoc_item(db) {
64+
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
65+
let module = self.ctx.completion.scope.module().unwrap();
66+
if let Some(path) = module.find_use_path(db, ModuleDef::Trait(trt)) {
67+
item.label(format!("{} ({})", name.clone(), path));
68+
item.insert_text(name.clone());
69+
}
70+
}
71+
}
72+
6273
Some(item.build())
6374
}
6475

crates/ide_completion/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn render_completion_list(completions: Vec<CompletionItem>) -> String {
126126
s.chars().count()
127127
}
128128
let label_width =
129-
completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(16);
129+
completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(22);
130130
completions
131131
.into_iter()
132132
.map(|it| {

crates/ide_completion/src/tests/item_list.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn in_mod_item_list() {
3030
sn tmod (Test module)
3131
sn tfn (Test function)
3232
sn macro_rules
33-
ma makro!(…) #[macro_export] macro_rules! makro
33+
ma makro!(…) #[macro_export] macro_rules! makro
3434
"##]],
3535
)
3636
}
@@ -58,9 +58,9 @@ fn in_source_file_item_list() {
5858
sn tmod (Test module)
5959
sn tfn (Test function)
6060
sn macro_rules
61-
ma makro!(…) #[macro_export] macro_rules! makro
61+
ma makro!(…) #[macro_export] macro_rules! makro
6262
md module
63-
ma makro!(…) #[macro_export] macro_rules! makro
63+
ma makro!(…) #[macro_export] macro_rules! makro
6464
"##]],
6565
)
6666
}

crates/ide_completion/src/tests/type_pos.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ trait Trait2 {
140140
fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
141141
"#,
142142
expect![[r#"
143-
ta Foo = type Foo;
143+
ta Foo = (Trait2) type Foo;
144144
tp T
145145
cp CONST_PARAM
146146
tt Trait
@@ -151,7 +151,7 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
151151
md module
152152
st Unit
153153
ct CONST
154-
ma makro!(…) macro_rules! makro
154+
ma makro!(…) macro_rules! makro
155155
bt u32
156156
"#]],
157157
);

0 commit comments

Comments
 (0)