773 lines
18 KiB
Rust
Raw Normal View History

2021-01-06 20:15:48 +00:00
//! Completion of paths, i.e. `some::prefix::$0`.
use hir::HasVisibility;
2020-05-04 16:48:50 +02:00
use rustc_hash::FxHashSet;
2020-08-12 18:26:51 +02:00
use syntax::AstNode;
2019-01-27 17:23:49 +01:00
use crate::{CompletionContext, Completions};
2019-01-08 22:33:36 +03:00
2020-10-25 10:59:15 +03:00
pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
if ctx.is_path_disallowed() {
2021-05-27 03:47:20 +02:00
return;
}
let path = match ctx.path_qual() {
Some(path) => path,
2020-05-04 16:48:50 +02:00
None => return,
2019-01-08 22:33:36 +03:00
};
2020-05-04 16:48:50 +02:00
2021-06-13 09:24:16 +05:30
let resolution = match ctx.sema.resolve_path(path) {
2020-04-30 00:10:15 +02:00
Some(res) => res,
None => return,
2019-01-08 22:33:36 +03:00
};
2021-05-27 03:47:20 +02:00
let context_module = ctx.scope.module();
if ctx.expects_item() || ctx.expects_assoc_item() {
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
let module_scope = module.scope(ctx.db, context_module);
for (name, def) in module_scope {
if let hir::ScopeDef::MacroDef(macro_def) = def {
if macro_def.is_fn_like() {
acc.add_macro(ctx, Some(name.clone()), macro_def);
}
}
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
acc.add_resolution(ctx, name, &def);
2021-05-27 20:53:38 +02:00
}
}
}
return;
}
// Add associated types on type parameters and `Self`.
2021-04-01 18:01:18 +02:00
resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
acc.add_type_alias(ctx, alias);
None::<()>
});
2020-07-11 01:26:24 +02:00
match resolution {
hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
let module_scope = module.scope(ctx.db, context_module);
2019-12-21 15:17:10 +01:00
for (name, def) in module_scope {
if ctx.in_use_tree() {
if let hir::ScopeDef::Unknown = def {
2019-12-21 15:17:10 +01:00
if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
2020-01-13 11:27:06 -05:00
if name_ref.syntax().text() == name.to_string().as_str() {
2021-01-06 20:15:48 +00:00
// for `use self::foo$0`, don't suggest `foo` as a completion
2021-03-08 22:19:44 +02:00
cov_mark::hit!(dont_complete_current_use);
2019-04-02 17:58:04 +03:00
continue;
}
}
}
}
2019-12-21 15:17:10 +01:00
let add_resolution = match def {
// Don't suggest attribute macros and derives.
hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
// no values in type places
2021-06-17 17:37:14 +02:00
hir::ScopeDef::ModuleDef(
hir::ModuleDef::Function(_)
| hir::ModuleDef::Variant(_)
| hir::ModuleDef::Static(_),
)
| hir::ScopeDef::Local(_) => !ctx.expects_type(),
// unless its a constant in a generic arg list position
hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(_)) => {
!ctx.expects_type() || ctx.expects_generic_arg()
}
_ => true,
};
if add_resolution {
acc.add_resolution(ctx, name, &def);
}
}
2019-01-08 22:33:36 +03:00
}
2021-06-17 17:37:14 +02:00
hir::PathResolution::Def(
def
@
(hir::ModuleDef::Adt(_)
| hir::ModuleDef::TypeAlias(_)
| hir::ModuleDef::BuiltinType(_)),
) => {
if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def {
add_enum_variants(acc, ctx, e);
2019-02-24 20:56:19 +03:00
}
let ty = match def {
2019-09-13 00:34:52 +03:00
hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
hir::ModuleDef::TypeAlias(a) => {
let ty = a.ty(ctx.db);
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
cov_mark::hit!(completes_variant_through_alias);
add_enum_variants(acc, ctx, e);
}
ty
}
2021-02-11 19:52:46 +01:00
hir::ModuleDef::BuiltinType(builtin) => {
let module = match ctx.scope.module() {
Some(it) => it,
None => return,
};
cov_mark::hit!(completes_primitive_assoc_const);
2021-02-11 19:52:46 +01:00
builtin.ty(ctx.db, module)
}
_ => unreachable!(),
};
// XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
// (where AssocType is defined on a trait, not an inherent impl)
let krate = ctx.krate;
2019-10-31 21:21:48 +01:00
if let Some(krate) = krate {
2020-07-11 01:26:24 +02:00
let traits_in_scope = ctx.scope.traits_in_scope();
2020-01-14 14:42:52 +01:00
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
return None;
}
add_assoc_item(acc, ctx, item);
2020-01-14 14:42:52 +01:00
None::<()>
});
// Iterate assoc types separately
2020-05-05 23:56:10 +08:00
ty.iterate_assoc_items(ctx.db, krate, |item| {
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
return None;
}
if let hir::AssocItem::TypeAlias(ty) = item {
acc.add_type_alias(ctx, ty)
2019-10-31 21:21:48 +01:00
}
None::<()>
2019-04-15 00:03:54 +02:00
});
}
}
hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => {
// Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
2019-11-01 00:15:15 +01:00
for item in t.items(ctx.db) {
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
continue;
}
add_assoc_item(acc, ctx, item);
2019-11-01 00:15:15 +01:00
}
}
hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => {
if let Some(krate) = ctx.krate {
2020-07-11 01:26:24 +02:00
let ty = match resolution {
hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
_ => return,
};
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
add_enum_variants(acc, ctx, e);
}
2020-07-11 01:26:24 +02:00
let traits_in_scope = ctx.scope.traits_in_scope();
let mut seen = FxHashSet::default();
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
return None;
}
// We might iterate candidates of a trait multiple times here, so deduplicate
// them.
if seen.insert(item) {
add_assoc_item(acc, ctx, item);
}
None::<()>
});
}
}
2019-10-30 13:36:37 -04:00
_ => {}
}
2019-01-08 22:33:36 +03:00
}
fn add_assoc_item(acc: &mut Completions, ctx: &CompletionContext, item: hir::AssocItem) {
match item {
hir::AssocItem::Function(func) if !ctx.expects_type() => acc.add_function(ctx, func, None),
hir::AssocItem::Const(ct) if !ctx.expects_type() || ctx.expects_generic_arg() => {
acc.add_const(ctx, ct)
}
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
_ => (),
}
}
fn add_enum_variants(acc: &mut Completions, ctx: &CompletionContext, e: hir::Enum) {
if ctx.expects_type() {
return;
}
e.variants(ctx.db).into_iter().for_each(|variant| acc.add_enum_variant(ctx, variant, None));
}
2019-01-08 22:33:36 +03:00
#[cfg(test)]
mod tests {
2020-08-21 13:19:31 +02:00
use expect_test::{expect, Expect};
2019-01-08 22:33:36 +03:00
use crate::{
2021-06-16 21:45:02 +02:00
tests::{check_edit, filtered_completion_list},
2020-07-04 18:53:30 +02:00
CompletionKind,
};
fn check(ra_fixture: &str, expect: Expect) {
2021-06-16 21:45:02 +02:00
let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
2020-07-04 18:53:30 +02:00
expect.assert_eq(&actual);
}
2019-02-24 21:54:13 +03:00
2020-07-04 18:53:30 +02:00
fn check_builtin(ra_fixture: &str, expect: Expect) {
2021-06-16 21:45:02 +02:00
let actual = filtered_completion_list(ra_fixture, CompletionKind::BuiltinType);
2020-07-04 18:53:30 +02:00
expect.assert_eq(&actual);
2019-01-08 22:33:36 +03:00
}
#[test]
fn dont_complete_values_in_type_pos() {
check(
r#"
const FOO: () = ();
static BAR: () = ();
struct Baz;
fn foo() {
let _: self::$0;
}
"#,
expect![[r#"
st Baz
"#]],
);
}
#[test]
fn dont_complete_enum_variants_in_type_pos() {
check(
r#"
enum Foo { Bar }
fn foo() {
let _: Foo::$0;
}
"#,
expect![[r#""#]],
);
}
2019-05-30 16:10:07 +03:00
#[test]
fn dont_complete_primitive_in_use() {
2021-01-06 20:15:48 +00:00
check_builtin(r#"use self::$0;"#, expect![[""]]);
2019-05-30 16:10:07 +03:00
}
#[test]
fn dont_complete_primitive_in_module_scope() {
2021-01-06 20:15:48 +00:00
check_builtin(r#"fn foo() { self::$0 }"#, expect![[""]]);
}
2019-05-30 16:10:07 +03:00
#[test]
fn completes_primitives() {
2020-07-04 18:53:30 +02:00
check_builtin(
2021-01-06 20:15:48 +00:00
r#"fn main() { let _: $0 = 92; }"#,
2020-07-04 18:53:30 +02:00
expect![[r#"
bt u32
2020-07-04 18:53:30 +02:00
bt bool
bt u8
bt isize
bt u16
bt u64
bt u128
2020-07-04 18:53:30 +02:00
bt f32
bt i128
bt i16
bt str
2020-07-04 18:53:30 +02:00
bt i64
bt char
bt f64
bt i32
2020-07-04 18:53:30 +02:00
bt i8
bt usize
"#]],
2019-01-25 12:51:36 -05:00
);
}
#[test]
2020-07-04 18:53:30 +02:00
fn completes_enum_variant() {
check(
r#"
enum E { Foo, Bar(i32) }
2021-01-06 20:15:48 +00:00
fn foo() { let _ = E::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
2020-07-04 19:03:58 +02:00
ev Foo ()
ev Bar() (i32)
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
2020-07-04 18:53:30 +02:00
fn completes_struct_associated_items() {
check(
r#"
//- /lib.rs
struct S;
impl S {
fn a() {}
fn b(&self) {}
const C: i32 = 42;
type T = i32;
}
2021-01-06 20:15:48 +00:00
fn foo() { let _ = S::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn a() fn()
me b() fn(&self)
ct C const C: i32 = 42;
ta T type T = i32;
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
2020-07-04 18:53:30 +02:00
fn associated_item_visibility() {
check(
r#"
struct S;
2020-07-04 18:53:30 +02:00
mod m {
impl super::S {
2020-10-25 10:59:15 +03:00
pub(crate) fn public_method() { }
2020-07-04 18:53:30 +02:00
fn private_method() { }
2020-10-25 10:59:15 +03:00
pub(crate) type PublicType = u32;
2020-07-04 18:53:30 +02:00
type PrivateType = u32;
2020-10-25 10:59:15 +03:00
pub(crate) const PUBLIC_CONST: u32 = 1;
2020-07-04 18:53:30 +02:00
const PRIVATE_CONST: u32 = 1;
}
2020-07-04 18:53:30 +02:00
}
2021-01-06 20:15:48 +00:00
fn foo() { let _ = S::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn public_method() fn()
2020-10-25 10:59:15 +03:00
ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
ta PublicType pub(crate) type PublicType = u32;
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
fn completes_enum_associated_method() {
2020-07-04 18:53:30 +02:00
check(
r#"
enum E {};
impl E { fn m() { } }
2021-01-06 20:15:48 +00:00
fn foo() { let _ = E::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn m() fn()
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
fn completes_union_associated_method() {
2020-07-04 18:53:30 +02:00
check(
r#"
union U {};
impl U { fn m() { } }
2021-01-06 20:15:48 +00:00
fn foo() { let _ = U::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn m() fn()
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
fn completes_trait_associated_method_1() {
2020-07-04 18:53:30 +02:00
check(
r#"
trait Trait { fn m(); }
2021-01-06 20:15:48 +00:00
fn foo() { let _ = Trait::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn m() fn()
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
fn completes_trait_associated_method_2() {
2020-07-04 18:53:30 +02:00
check(
r#"
trait Trait { fn m(); }
struct S;
impl Trait for S {}
2021-01-06 20:15:48 +00:00
fn foo() { let _ = S::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn m() fn()
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
fn completes_trait_associated_method_3() {
2020-07-04 18:53:30 +02:00
check(
r#"
trait Trait { fn m(); }
2020-07-04 18:53:30 +02:00
struct S;
impl Trait for S {}
2021-01-06 20:15:48 +00:00
fn foo() { let _ = <S as Trait>::$0 }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn m() fn()
2020-07-04 18:53:30 +02:00
"#]],
2019-07-03 03:08:39 +09:00
);
}
#[test]
fn completes_ty_param_assoc_ty() {
2020-07-04 18:53:30 +02:00
check(
r#"
trait Super {
type Ty;
const CONST: u8;
fn func() {}
fn method(&self) {}
}
2020-07-04 18:53:30 +02:00
trait Sub: Super {
type SubTy;
const C2: ();
fn subfunc() {}
fn submethod(&self) {}
}
2021-01-06 20:15:48 +00:00
fn foo<T: Sub>() { T::$0 }
2020-07-04 18:53:30 +02:00
"#,
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)
2020-07-04 18:53:30 +02:00
"#]],
);
}
#[test]
fn completes_self_param_assoc_ty() {
2020-07-04 18:53:30 +02:00
check(
r#"
trait Super {
type Ty;
const CONST: u8 = 0;
fn func() {}
fn method(&self) {}
}
2020-07-04 18:53:30 +02:00
trait Sub: Super {
type SubTy;
const C2: () = ();
fn subfunc() {}
fn submethod(&self) {}
}
2020-07-04 18:53:30 +02:00
struct Wrap<T>(T);
impl<T> Super for Wrap<T> {}
impl<T> Sub for Wrap<T> {
fn subfunc() {
// Should be able to assume `Self: Sub + Super`
2021-01-06 20:15:48 +00:00
Self::$0
2020-07-04 18:53:30 +02:00
}
}
"#,
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)
2020-07-04 18:53:30 +02:00
"#]],
);
}
2019-07-03 03:08:39 +09:00
#[test]
fn completes_type_alias() {
2020-07-04 18:53:30 +02:00
check(
r#"
struct S;
impl S { fn foo() {} }
type T = S;
impl T { fn bar() {} }
2021-01-06 20:15:48 +00:00
fn main() { T::$0; }
2020-07-04 18:53:30 +02:00
"#,
expect![[r#"
fn foo() fn()
fn bar() fn()
2020-07-04 18:53:30 +02:00
"#]],
2019-09-10 13:32:47 +08:00
);
}
#[test]
2019-09-11 22:39:02 +08:00
fn completes_qualified_macros() {
2020-07-04 18:53:30 +02:00
check(
r#"
#[macro_export]
macro_rules! foo { () => {} }
2021-01-06 20:15:48 +00:00
fn main() { let _ = crate::$0 }
"#,
2020-07-04 18:53:30 +02:00
expect![[r##"
fn main() fn()
ma foo!() #[macro_export] macro_rules! foo
2020-07-04 18:53:30 +02:00
"##]],
);
}
#[test]
fn does_not_complete_non_fn_macros() {
check(
r#"
mod m {
#[rustc_builtin_macro]
pub macro Clone {}
}
fn f() {m::$0}
"#,
expect![[r#""#]],
);
check(
r#"
mod m {
#[rustc_builtin_macro]
pub macro bench {}
}
fn f() {m::$0}
"#,
expect![[r#""#]],
);
}
#[test]
fn completes_reexported_items_under_correct_name() {
2020-07-04 18:53:30 +02:00
check(
r#"
2021-01-06 20:15:48 +00:00
fn foo() { self::m::$0 }
2020-07-04 18:53:30 +02:00
mod m {
pub use super::p::wrong_fn as right_fn;
pub use super::p::WRONG_CONST as RIGHT_CONST;
pub use super::p::WrongType as RightType;
}
mod p {
fn wrong_fn() {}
const WRONG_CONST: u32 = 1;
struct WrongType {};
}
"#,
expect![[r#"
ct RIGHT_CONST
fn right_fn() fn()
st RightType
2020-07-04 18:53:30 +02:00
"#]],
);
check_edit(
"RightType",
r#"
2021-01-06 20:15:48 +00:00
fn foo() { self::m::$0 }
2020-07-04 18:53:30 +02:00
mod m {
pub use super::p::wrong_fn as right_fn;
pub use super::p::WRONG_CONST as RIGHT_CONST;
pub use super::p::WrongType as RightType;
}
mod p {
fn wrong_fn() {}
const WRONG_CONST: u32 = 1;
struct WrongType {};
}
"#,
r#"
fn foo() { self::m::RightType }
mod m {
pub use super::p::wrong_fn as right_fn;
pub use super::p::WRONG_CONST as RIGHT_CONST;
pub use super::p::WrongType as RightType;
}
mod p {
fn wrong_fn() {}
const WRONG_CONST: u32 = 1;
struct WrongType {};
}
"#,
);
}
2020-03-07 15:47:10 +01:00
#[test]
fn completes_in_simple_macro_call() {
2020-07-04 18:53:30 +02:00
check(
2020-03-07 15:47:10 +01:00
r#"
2020-07-04 18:53:30 +02:00
macro_rules! m { ($e:expr) => { $e } }
2021-01-06 20:15:48 +00:00
fn main() { m!(self::f$0); }
2020-07-04 18:53:30 +02:00
fn foo() {}
"#,
expect![[r#"
fn main() fn()
fn foo() fn()
2020-07-04 18:53:30 +02:00
"#]],
2020-03-07 15:47:10 +01:00
);
}
2020-03-10 19:58:17 -07:00
#[test]
fn function_mod_share_name() {
2020-07-04 18:53:30 +02:00
check(
r#"
2021-01-06 20:15:48 +00:00
fn foo() { self::m::$0 }
2020-03-10 19:58:17 -07:00
2020-07-04 18:53:30 +02:00
mod m {
pub mod z {}
pub fn z() {}
}
"#,
expect![[r#"
md z
fn z() fn()
2020-07-04 18:53:30 +02:00
"#]],
2020-03-10 19:58:17 -07:00
);
}
#[test]
fn completes_hashmap_new() {
2020-07-04 18:53:30 +02:00
check(
r#"
struct RandomState;
struct HashMap<K, V, S = RandomState> {}
impl<K, V> HashMap<K, V, RandomState> {
pub fn new() -> HashMap<K, V, RandomState> { }
}
fn foo() {
2021-01-06 20:15:48 +00:00
HashMap::$0
2020-07-04 18:53:30 +02:00
}
"#,
expect![[r#"
fn new() fn() -> HashMap<K, V, RandomState>
2020-07-04 18:53:30 +02:00
"#]],
);
}
2020-05-04 16:48:50 +02:00
#[test]
fn dont_complete_attr() {
2020-07-04 18:53:30 +02:00
check(
r#"
mod foo { pub struct Foo; }
2021-01-06 20:15:48 +00:00
#[foo::$0]
2020-07-04 18:53:30 +02:00
fn f() {}
"#,
expect![[""]],
);
2020-05-04 16:48:50 +02:00
}
#[test]
fn completes_variant_through_self() {
check(
r#"
enum Foo {
Bar,
Baz,
}
impl Foo {
fn foo(self) {
2021-01-06 20:15:48 +00:00
Self::$0
}
}
"#,
expect![[r#"
ev Bar ()
ev Baz ()
me foo() fn(self)
"#]],
);
}
2021-02-11 19:52:46 +01:00
#[test]
fn completes_primitive_assoc_const() {
cov_mark::check!(completes_primitive_assoc_const);
2021-02-11 19:52:46 +01:00
check(
r#"
//- /lib.rs crate:lib deps:core
fn f() {
u8::$0
}
//- /core.rs crate:core
#[lang = "u8"]
impl u8 {
pub const MAX: Self = 255;
pub fn func(self) {}
}
"#,
expect![[r#"
ct MAX pub const MAX: Self = 255;
me func() fn(self)
2021-02-11 19:52:46 +01:00
"#]],
);
}
#[test]
fn completes_variant_through_alias() {
cov_mark::check!(completes_variant_through_alias);
check(
r#"
enum Foo {
Bar
}
type Foo2 = Foo;
fn main() {
Foo2::$0
}
"#,
expect![[r#"
ev Bar ()
"#]],
);
}
#[test]
fn completes_types_and_const_in_arg_list() {
check(
r#"
mod foo {
pub const CONST: () = ();
pub type Type = ();
}
struct Foo<T>(t);
fn foo(_: Foo<foo::$0>) {}
"#,
expect![[r#"
ta Type
ct CONST
"#]],
);
}
2019-01-08 22:33:36 +03:00
}