Skip to content

Commit 0f2eba5

Browse files
committed
Show only assoc type args in the correct arg pos
1 parent 441e659 commit 0f2eba5

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

crates/hir/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use hir_def::{
4141
adt::{ReprKind, VariantData},
4242
body::{BodyDiagnostic, SyntheticSyntax},
4343
expr::{BindingAnnotation, LabelId, Pat, PatId},
44+
generics::{TypeOrConstParamData, TypeParamProvenance},
4445
item_tree::ItemTreeNode,
4546
lang_item::LangItemTarget,
4647
nameres::{self, diagnostics::DefDiagnostic},
@@ -1707,6 +1708,22 @@ impl Trait {
17071708
pub fn is_unsafe(&self, db: &dyn HirDatabase) -> bool {
17081709
db.trait_data(self.id).is_unsafe
17091710
}
1711+
1712+
pub fn type_parameters(&self, db: &dyn HirDatabase) -> Vec<TypeOrConstParamData> {
1713+
db.generic_params(GenericDefId::from(self.id))
1714+
.type_or_consts
1715+
.iter()
1716+
.filter(|(_, ty)| match ty {
1717+
TypeOrConstParamData::TypeParamData(ty)
1718+
if ty.provenance != TypeParamProvenance::TypeParamList =>
1719+
{
1720+
false
1721+
}
1722+
_ => true,
1723+
})
1724+
.map(|(_, ty)|ty.clone())
1725+
.collect()
1726+
}
17101727
}
17111728

17121729
impl HasVisibility for Trait {

crates/ide-completion/src/completions/type.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Completion of names from the current scope in type position.
22
33
use hir::{HirDisplay, ScopeDef};
4+
use itertools::Itertools;
45
use syntax::{ast, AstNode};
56

67
use crate::{
@@ -140,6 +141,18 @@ pub(crate) fn complete_type_path(
140141
return;
141142
}
142143
TypeLocation::GenericArgList(Some(arg_list)) => {
144+
// the current token is in which generic arg
145+
let arg_pos = if let Some((pos, _)) =
146+
arg_list.generic_args().find_position(|arg| {
147+
arg.syntax()
148+
.descendants_with_tokens()
149+
.any(|t| t.as_token() == Some(&ctx.original_token))
150+
}) {
151+
pos
152+
} else {
153+
0
154+
};
155+
143156
match arg_list.generic_args().next() {
144157
Some(ast::GenericArg::AssocTypeArg(_)) => {}
145158
_ => {
@@ -167,7 +180,10 @@ pub(crate) fn complete_type_path(
167180
acc.add_type_alias_with_eq(ctx, alias);
168181
}
169182
});
170-
return; // only AssocTypeArgs make sense
183+
184+
if arg_pos >= trait_.type_parameters(ctx.sema.db).len() {
185+
return; // only AssocTypeArgs make sense
186+
}
171187
}
172188
}
173189
}

crates/ide-completion/src/tests/type_pos.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,39 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
386386
);
387387
check(
388388
r#"
389+
trait Trait1 {
390+
type Super;
391+
}
392+
trait Trait2<T>: Trait1 {
393+
type Foo;
394+
}
395+
396+
fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
397+
"#,
398+
expect![[r#"
399+
ct CONST
400+
cp CONST_PARAM
401+
en Enum
402+
ma makro!(…) macro_rules! makro
403+
md module
404+
st Record
405+
st Tuple
406+
st Unit
407+
tt Trait
408+
tt Trait1
409+
tt Trait2
410+
ta Foo = (as Trait2) type Foo
411+
ta Super = (as Trait1) type Super
412+
tp T
413+
un Union
414+
bt u32
415+
kw crate::
416+
kw self::
417+
kw super::
418+
"#]],
419+
);
420+
check(
421+
r#"
389422
trait Trait2 {
390423
type Foo;
391424
}
@@ -460,11 +493,11 @@ fn func(_: Enum::$0) {}
460493
fn completes_associated_type_only() {
461494
check(
462495
r#"
463-
trait MyTrait {
496+
trait MyTrait<T> {
464497
type Item;
465498
};
466499
467-
fn f(t: impl MyTrait<I$0
500+
fn f(t: impl MyTrait<u8,I$0
468501
"#,
469502
expect![[r#"
470503
ta Item = (as MyTrait) type Item

0 commit comments

Comments
 (0)