rust/crates/ra_ide_api/src/completion/complete_path.rs

788 lines
21 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
use hir::{Adt, Either, PathResolution};
2019-02-24 21:21:31 +03:00
use ra_syntax::AstNode;
use test_utils::tested_by;
2019-01-27 17:23:49 +01:00
use crate::completion::{CompletionContext, Completions};
2019-01-08 22:33:36 +03:00
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
2019-01-27 20:50:57 +01:00
let path = match &ctx.path_prefix {
Some(path) => path.clone(),
_ => return,
2019-01-08 22:33:36 +03:00
};
2019-09-14 11:05:42 +02:00
let def = match ctx.analyzer.resolve_hir_path(ctx.db, &path) {
Some(PathResolution::Def(def)) => def,
2019-01-27 20:50:57 +01:00
_ => return,
2019-01-08 22:33:36 +03:00
};
2019-01-27 17:23:49 +01:00
match def {
hir::ModuleDef::Module(module) => {
2019-01-15 19:15:01 +03:00
let module_scope = module.scope(ctx.db);
2019-10-31 18:45:10 +03:00
for (name, def, import) in module_scope {
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def {
2019-05-30 16:10:07 +03:00
if ctx.use_item_syntax.is_some() {
tested_by!(dont_complete_primitive_in_use);
continue;
}
}
if Some(module) == ctx.module {
2019-10-31 18:45:10 +03:00
if let Some(import) = import {
2019-04-10 10:46:43 +03:00
if let Either::A(use_tree) = module.import_source(ctx.db, import) {
2019-07-20 12:58:27 +03:00
if use_tree.syntax().text_range().contains_inclusive(ctx.offset) {
2019-04-02 17:58:04 +03:00
// for `use self::foo<|>`, don't suggest `foo` as a completion
tested_by!(dont_complete_current_use);
continue;
}
}
}
}
2019-10-31 18:45:10 +03:00
acc.add_resolution(ctx, name.to_string(), &def);
}
2019-01-08 22:33:36 +03:00
}
2019-09-13 00:34:52 +03:00
hir::ModuleDef::Adt(_) | hir::ModuleDef::TypeAlias(_) => {
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
for variant in e.variants(ctx.db) {
acc.add_enum_variant(ctx, variant);
}
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),
2019-07-03 03:08:39 +09:00
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
_ => unreachable!(),
};
ctx.analyzer.iterate_path_candidates(ctx.db, ty.clone(), None, |_ty, item| {
match item {
hir::AssocItem::Function(func) => {
let data = func.data(ctx.db);
if !data.has_self_param() {
acc.add_function(ctx, func);
2019-02-24 20:59:12 +03:00
}
}
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
}
None::<()>
});
2019-10-31 21:21:48 +01:00
// Iterate assoc types separately
// FIXME: complete T::AssocType
let krate = ctx.module.map(|m| m.krate());
if let Some(krate) = krate {
ty.iterate_impl_items(ctx.db, krate, |item| {
match item {
hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => {}
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
}
None::<()>
2019-04-15 00:03:54 +02:00
});
}
}
2019-11-01 00:15:15 +01:00
hir::ModuleDef::Trait(t) => {
for item in t.items(ctx.db) {
match item {
hir::AssocItem::Function(func) => {
let data = func.data(ctx.db);
if !data.has_self_param() {
acc.add_function(ctx, func);
}
}
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
}
}
}
2019-10-30 13:36:37 -04:00
_ => {}
2019-01-08 22:33:36 +03:00
};
}
#[cfg(test)]
mod tests {
use test_utils::covers;
2019-01-08 22:33:36 +03:00
use crate::completion::{do_completion, CompletionItem, CompletionKind};
2019-08-29 16:49:10 +03:00
use insta::assert_debug_snapshot;
2019-02-24 21:54:13 +03:00
2019-07-02 00:51:18 +03:00
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Reference)
2019-01-08 22:33:36 +03:00
}
2019-02-01 23:06:57 +01:00
#[test]
fn dont_complete_current_use() {
covers!(dont_complete_current_use);
let completions = do_completion(r"use self::foo<|>;", CompletionKind::Reference);
assert!(completions.is_empty());
2019-02-01 23:06:57 +01:00
}
#[test]
fn dont_complete_current_use_in_braces_with_glob() {
let completions = do_completion(
r"
mod foo { pub struct S; }
use self::{foo::*, bar<|>};
",
CompletionKind::Reference,
);
assert_eq!(completions.len(), 2);
}
2019-05-30 16:10:07 +03:00
#[test]
fn dont_complete_primitive_in_use() {
covers!(dont_complete_primitive_in_use);
let completions = do_completion(r"use self::<|>;", CompletionKind::BuiltinType);
assert!(completions.is_empty());
}
#[test]
fn completes_primitives() {
let completions =
do_completion(r"fn main() { let _: <|> = 92; }", CompletionKind::BuiltinType);
assert_eq!(completions.len(), 17);
}
2019-01-25 12:51:36 -05:00
#[test]
fn completes_mod_with_docs() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
r"
use self::my<|>;
/// Some simple
/// docs describing `mod my`.
mod my {
struct Bar;
}
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "my",
source_range: [27; 29),
delete: [27; 29),
insert: "my",
kind: Module,
documentation: Documentation(
"Some simple\ndocs describing `mod my`.",
),
},
]
"###
2019-01-25 12:51:36 -05:00
);
}
2019-01-08 22:33:36 +03:00
#[test]
fn completes_use_item_starting_with_self() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
r"
use self::m::<|>;
2019-01-08 22:33:36 +03:00
2019-07-02 00:51:18 +03:00
mod m {
struct Bar;
}
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "Bar",
source_range: [30; 30),
delete: [30; 30),
insert: "Bar",
kind: Struct,
},
]
"###
2019-01-08 22:33:36 +03:00
);
}
#[test]
fn completes_use_item_starting_with_crate() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
mod foo;
struct Spam;
//- /foo.rs
use crate::Sp<|>
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "Spam",
source_range: [11; 13),
delete: [11; 13),
insert: "Spam",
kind: Struct,
},
CompletionItem {
label: "foo",
source_range: [11; 13),
delete: [11; 13),
insert: "foo",
kind: Module,
},
]
"###
2019-01-08 22:33:36 +03:00
);
}
#[test]
fn completes_nested_use_tree() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
mod foo;
struct Spam;
//- /foo.rs
use crate::{Sp<|>};
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "Spam",
source_range: [12; 14),
delete: [12; 14),
insert: "Spam",
kind: Struct,
},
CompletionItem {
label: "foo",
source_range: [12; 14),
delete: [12; 14),
insert: "foo",
kind: Module,
},
]
"###
2019-01-08 22:33:36 +03:00
);
}
#[test]
fn completes_deeply_nested_use_tree() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
mod foo;
pub mod bar {
pub mod baz {
pub struct Spam;
}
2019-01-08 22:33:36 +03:00
}
2019-07-02 00:51:18 +03:00
//- /foo.rs
use crate::{bar::{baz::Sp<|>}};
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "Spam",
source_range: [23; 25),
delete: [23; 25),
insert: "Spam",
kind: Struct,
},
]
"###
2019-01-08 22:33:36 +03:00
);
}
#[test]
fn completes_enum_variant() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
/// An enum
enum E {
/// Foo Variant
Foo,
/// Bar Variant with i32
Bar(i32)
}
fn foo() { let _ = E::<|> }
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "Bar",
source_range: [116; 116),
delete: [116; 116),
insert: "Bar",
kind: EnumVariant,
detail: "(i32)",
documentation: Documentation(
"Bar Variant with i32",
),
},
CompletionItem {
label: "Foo",
source_range: [116; 116),
delete: [116; 116),
insert: "Foo",
kind: EnumVariant,
detail: "()",
documentation: Documentation(
"Foo Variant",
),
},
]
"###
2019-01-08 22:33:36 +03:00
);
}
2019-01-25 11:50:59 -05:00
#[test]
fn completes_enum_variant_with_details() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
struct S { field: u32 }
/// An enum
enum E {
/// Foo Variant (empty)
Foo,
/// Bar Variant with i32 and u32
Bar(i32, u32),
///
S(S),
}
fn foo() { let _ = E::<|> }
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "Bar",
source_range: [180; 180),
delete: [180; 180),
insert: "Bar",
kind: EnumVariant,
detail: "(i32, u32)",
documentation: Documentation(
"Bar Variant with i32 and u32",
),
},
CompletionItem {
label: "Foo",
source_range: [180; 180),
delete: [180; 180),
insert: "Foo",
kind: EnumVariant,
detail: "()",
documentation: Documentation(
"Foo Variant (empty)",
),
},
CompletionItem {
label: "S",
source_range: [180; 180),
delete: [180; 180),
insert: "S",
kind: EnumVariant,
detail: "(S)",
documentation: Documentation(
"",
),
},
]
"###
2019-01-25 11:50:59 -05:00
);
}
#[test]
fn completes_struct_associated_method() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
/// A Struct
struct S;
impl S {
/// An associated method
fn m() { }
}
2019-07-02 00:51:18 +03:00
fn foo() { let _ = S::<|> }
"
),
@r###"
[
CompletionItem {
2019-10-10 16:37:56 +03:00
label: "m()",
source_range: [100; 100),
delete: [100; 100),
insert: "m()$0",
kind: Function,
lookup: "m",
detail: "fn m()",
documentation: Documentation(
"An associated method",
),
},
]
"###
);
}
#[test]
fn completes_struct_associated_const() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
/// A Struct
struct S;
impl S {
/// An associated const
const C: i32 = 42;
}
2019-07-02 00:51:18 +03:00
fn foo() { let _ = S::<|> }
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "C",
source_range: [107; 107),
delete: [107; 107),
insert: "C",
kind: Const,
detail: "const C: i32 = 42;",
documentation: Documentation(
"An associated const",
),
},
]
"###
);
}
#[test]
fn completes_struct_associated_type() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
/// A Struct
struct S;
impl S {
/// An associated type
type T = i32;
}
2019-07-02 00:51:18 +03:00
fn foo() { let _ = S::<|> }
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "T",
source_range: [101; 101),
delete: [101; 101),
insert: "T",
kind: TypeAlias,
detail: "type T = i32;",
documentation: Documentation(
"An associated type",
),
},
]
"###
);
}
#[test]
fn completes_enum_associated_method() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
/// An enum
enum S {};
impl S {
/// An associated method
fn m() { }
}
2019-07-02 00:51:18 +03:00
fn foo() { let _ = S::<|> }
"
),
@r###"
[
CompletionItem {
2019-10-10 16:37:56 +03:00
label: "m()",
source_range: [100; 100),
delete: [100; 100),
insert: "m()$0",
kind: Function,
lookup: "m",
detail: "fn m()",
documentation: Documentation(
"An associated method",
),
},
]
"###
);
}
#[test]
fn completes_union_associated_method() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /lib.rs
/// A union
union U {};
impl U {
/// An associated method
fn m() { }
}
2019-07-02 00:51:18 +03:00
fn foo() { let _ = U::<|> }
"
),
@r###"
[
CompletionItem {
2019-10-10 16:37:56 +03:00
label: "m()",
source_range: [101; 101),
delete: [101; 101),
insert: "m()$0",
kind: Function,
lookup: "m",
detail: "fn m()",
documentation: Documentation(
"An associated method",
),
},
]
"###
);
}
#[test]
fn completes_use_paths_across_crates() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-02 00:51:18 +03:00
do_reference_completion(
"
//- /main.rs
use foo::<|>;
//- /foo/lib.rs
pub mod bar {
pub struct S;
}
"
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "bar",
source_range: [9; 9),
delete: [9; 9),
insert: "bar",
kind: Module,
},
]
"###
);
}
#[test]
fn completes_trait_associated_method_1() {
assert_debug_snapshot!(
do_reference_completion(
"
//- /lib.rs
trait Trait {
/// A trait method
fn m();
}
fn foo() { let _ = Trait::<|> }
"
),
2019-11-01 00:15:15 +01:00
@r###"
[
CompletionItem {
label: "m()",
source_range: [73; 73),
delete: [73; 73),
insert: "m()$0",
kind: Function,
lookup: "m",
detail: "fn m()",
documentation: Documentation(
"A trait method",
),
},
]
"###
);
}
#[test]
fn completes_trait_associated_method_2() {
assert_debug_snapshot!(
do_reference_completion(
"
//- /lib.rs
trait Trait {
/// A trait method
fn m();
}
struct S;
impl Trait for S {}
fn foo() { let _ = S::<|> }
"
),
2019-10-31 21:21:48 +01:00
@r###"
[
CompletionItem {
label: "m()",
source_range: [99; 99),
delete: [99; 99),
insert: "m()$0",
kind: Function,
lookup: "m",
detail: "fn m()",
documentation: Documentation(
"A trait method",
),
},
]
"###
);
}
#[test]
fn completes_trait_associated_method_3() {
assert_debug_snapshot!(
do_reference_completion(
"
//- /lib.rs
trait Trait {
/// A trait method
fn m();
}
struct S;
impl Trait for S {}
fn foo() { let _ = <S as Trait>::<|> }
"
),
2019-11-01 00:15:15 +01:00
@r###"
[
CompletionItem {
label: "m()",
source_range: [110; 110),
delete: [110; 110),
insert: "m()$0",
kind: Function,
lookup: "m",
detail: "fn m()",
documentation: Documentation(
"A trait method",
),
},
]
"###
2019-07-03 03:08:39 +09:00
);
}
#[test]
fn completes_type_alias() {
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
2019-07-03 03:08:39 +09:00
do_reference_completion(
"
struct S;
impl S { fn foo() {} }
type T = S;
impl T { fn bar() {} }
fn main() {
T::<|>;
}
"
),
@r###"
[
CompletionItem {
2019-10-10 16:37:56 +03:00
label: "bar()",
source_range: [185; 185),
delete: [185; 185),
insert: "bar()$0",
kind: Function,
lookup: "bar",
detail: "fn bar()",
},
CompletionItem {
2019-10-10 16:37:56 +03:00
label: "foo()",
source_range: [185; 185),
delete: [185; 185),
insert: "foo()$0",
kind: Function,
lookup: "foo",
detail: "fn foo()",
},
]
"###
2019-09-10 13:32:47 +08:00
);
}
#[test]
2019-09-11 22:39:02 +08:00
fn completes_qualified_macros() {
2019-09-10 13:32:47 +08:00
assert_debug_snapshot!(
do_reference_completion(
"
#[macro_export]
macro_rules! foo {
() => {}
}
fn main() {
let _ = crate::<|>
}
"
),
@r###"
[
CompletionItem {
label: "foo!",
source_range: [179; 179),
delete: [179; 179),
insert: "foo!($0)",
kind: Macro,
detail: "#[macro_export]\nmacro_rules! foo",
},
CompletionItem {
2019-10-10 16:37:56 +03:00
label: "main()",
source_range: [179; 179),
delete: [179; 179),
insert: "main()$0",
kind: Function,
lookup: "main",
detail: "fn main()",
},
]
"###
);
}
2019-01-08 22:33:36 +03:00
}