Skip to content

Commit 348f68c

Browse files
bors[bot]mahdifrmz
andauthored
Merge #9478
9478: show imported trait on autocompletion of associated items r=matklad a=mahdi-frms Fixes: #7456 The thing is however, due to the trait being already imported, the path to the trait is just the name of the trait itself. I also updated the test concerning the labels. Co-authored-by: mahdi-frms <mahdif1380@outlook.com>
2 parents e5c1c8c + 02d33c9 commit 348f68c

File tree

9 files changed

+69
-36
lines changed

9 files changed

+69
-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/item.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ impl CompletionItem {
295295
label,
296296
insert_text: None,
297297
is_snippet: false,
298+
trait_name: None,
298299
detail: None,
299300
documentation: None,
300301
lookup: None,
@@ -398,6 +399,7 @@ pub(crate) struct Builder {
398399
source_range: TextRange,
399400
completion_kind: CompletionKind,
400401
import_to_add: Option<ImportEdit>,
402+
trait_name: Option<String>,
401403
label: String,
402404
insert_text: Option<String>,
403405
is_snippet: bool,
@@ -434,6 +436,8 @@ impl Builder {
434436
} else {
435437
format_to!(label, " ({})", original_path)
436438
}
439+
} else if let Some(trait_name) = self.trait_name {
440+
format_to!(label, " ({})", trait_name)
437441
}
438442

439443
let text_edit = match self.text_edit {
@@ -468,6 +472,10 @@ impl Builder {
468472
self.label = label.into();
469473
self
470474
}
475+
pub(crate) fn trait_name(&mut self, trait_name: impl Into<String>) -> &mut Builder {
476+
self.trait_name = Some(trait_name.into());
477+
self
478+
}
471479
pub(crate) fn insert_text(&mut self, insert_text: impl Into<String>) -> &mut Builder {
472480
self.insert_text = Some(insert_text.into());
473481
self

crates/ide_completion/src/render/const_.rs

Lines changed: 10 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};
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,14 @@ 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+
item.trait_name(trt.name(db).to_string());
53+
item.insert_text(name.clone());
54+
}
55+
}
56+
4957
Some(item.build())
5058
}
5159

crates/ide_completion/src/render/function.rs

Lines changed: 13 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};
44
use ide_db::SymbolKind;
55
use itertools::Itertools;
66
use syntax::ast::Fn;
@@ -73,9 +73,18 @@ 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+
item.trait_name(trt.name(db).to_string());
83+
}
84+
}
85+
}
86+
87+
item.add_import(import_to_add).lookup_by(self.name);
7988

8089
let ret_type = self.func.ret_type(self.ctx.db());
8190
item.set_relevance(CompletionRelevance {

crates/ide_completion/src/render/type_alias.rs

Lines changed: 10 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};
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,14 @@ 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+
item.trait_name(trt.name(db).to_string());
66+
item.insert_text(name.clone());
67+
}
68+
}
69+
6270
Some(item.build())
6371
}
6472

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)