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

318 lines
8.7 KiB
Rust
Raw Normal View History

2019-01-25 11:50:59 -05:00
use join_to_string::join;
2019-01-27 17:23:49 +01:00
use hir::{Docs, Resolution};
use ra_syntax::{AstNode, ast::NameOwner};
use test_utils::tested_by;
2019-01-27 17:23:49 +01:00
use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
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-01-27 20:50:57 +01:00
let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() {
Some(Resolution::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);
for (name, res) in module_scope.entries() {
if Some(module) == ctx.module {
if let Some(import) = res.import {
let path = module.import_source(ctx.db, import);
if path.syntax().range().contains_inclusive(ctx.offset) {
// for `use self::foo<|>`, don't suggest `foo` as a completion
tested_by!(dont_complete_current_use);
continue;
}
}
}
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
name.to_string(),
)
.from_resolution(ctx, &res.def.map(hir::Resolution::Def))
.add_to(acc);
}
2019-01-08 22:33:36 +03:00
}
2019-01-24 19:04:02 +03:00
hir::ModuleDef::Enum(e) => {
2019-01-25 12:41:23 +03:00
e.variants(ctx.db).into_iter().for_each(|variant| {
if let Some(name) = variant.name(ctx.db) {
2019-02-08 14:49:43 +03:00
let detail_types =
variant.fields(ctx.db).into_iter().map(|field| field.ty(ctx.db));
let detail =
join(detail_types).separator(", ").surround_with("(", ")").to_string();
2019-01-25 11:50:59 -05:00
2019-01-24 19:04:02 +03:00
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
2019-01-25 12:41:23 +03:00
name.to_string(),
2019-01-24 19:04:02 +03:00
)
.kind(CompletionItemKind::EnumVariant)
.set_documentation(variant.docs(ctx.db))
2019-01-25 11:50:59 -05:00
.set_detail(Some(detail))
2019-01-24 19:04:02 +03:00
.add_to(acc)
2019-01-25 12:41:23 +03:00
}
});
2019-01-24 19:04:02 +03:00
}
hir::ModuleDef::Struct(s) => {
let ty = s.ty(ctx.db);
ty.iterate_impl_items(ctx.db, |item| match item {
hir::ImplItem::Method(func) => {
let sig = func.signature(ctx.db);
if !sig.has_self_param() {
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
sig.name().to_string(),
)
.from_function(ctx, func)
.kind(CompletionItemKind::Method)
.add_to(acc);
}
None::<()>
}
hir::ImplItem::Const(ct) => {
let source = ct.source(ctx.db);
if let Some(name) = source.1.name() {
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
name.text().to_string(),
)
.from_const(ctx, ct)
.add_to(acc);
}
None::<()>
}
hir::ImplItem::Type(ty) => {
let source = ty.source(ctx.db);
if let Some(name) = source.1.name() {
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
name.text().to_string(),
)
.from_type(ctx, ty)
.add_to(acc);
}
None::<()>
}
});
}
2019-01-25 01:31:32 +03:00
_ => return,
2019-01-08 22:33:36 +03:00
};
}
#[cfg(test)]
mod tests {
use crate::completion::{
CompletionKind,
completion_item::{check_completion, do_completion},
};
use test_utils::covers;
2019-01-08 22:33:36 +03:00
fn check_reference_completion(code: &str, expected_completions: &str) {
check_completion(code, expected_completions, CompletionKind::Reference);
}
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
}
2019-01-25 12:51:36 -05:00
#[test]
fn completes_mod_with_docs() {
check_reference_completion(
"mod_with_docs",
r"
use self::my<|>;
/// Some simple
/// docs describing `mod my`.
mod my {
struct Bar;
}
",
);
}
2019-01-08 22:33:36 +03:00
#[test]
fn completes_use_item_starting_with_self() {
check_reference_completion(
"use_item_starting_with_self",
2019-01-08 22:33:36 +03:00
r"
use self::m::<|>;
mod m {
struct Bar;
}
",
);
}
#[test]
fn completes_use_item_starting_with_crate() {
check_reference_completion(
"use_item_starting_with_crate",
2019-01-08 22:33:36 +03:00
"
//- /lib.rs
mod foo;
struct Spam;
//- /foo.rs
use crate::Sp<|>
",
);
}
#[test]
fn completes_nested_use_tree() {
check_reference_completion(
"nested_use_tree",
2019-01-08 22:33:36 +03:00
"
//- /lib.rs
mod foo;
struct Spam;
//- /foo.rs
use crate::{Sp<|>};
",
);
}
#[test]
fn completes_deeply_nested_use_tree() {
check_reference_completion(
"deeply_nested_use_tree",
2019-01-08 22:33:36 +03:00
"
//- /lib.rs
mod foo;
pub mod bar {
pub mod baz {
pub struct Spam;
}
}
//- /foo.rs
use crate::{bar::{baz::Sp<|>}};
",
);
}
#[test]
fn completes_enum_variant() {
check_reference_completion(
2019-01-25 11:50:59 -05:00
"enum_variant",
2019-01-08 22:33:36 +03:00
"
//- /lib.rs
2019-01-23 18:50:46 -05:00
/// An enum
enum E {
/// Foo Variant
Foo,
/// Bar Variant with i32
Bar(i32)
}
2019-01-08 22:33:36 +03:00
fn foo() { let _ = E::<|> }
",
);
}
2019-01-25 11:50:59 -05:00
#[test]
fn completes_enum_variant_with_details() {
check_reference_completion(
"enum_variant_with_details",
"
//- /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::<|> }
",
);
}
#[test]
fn completes_struct_associated_method() {
check_reference_completion(
"struct_associated_method",
"
//- /lib.rs
/// A Struct
struct S;
impl S {
/// An associated method
fn m() { }
}
fn foo() { let _ = S::<|> }
",
);
}
#[test]
fn completes_struct_associated_const() {
check_reference_completion(
"struct_associated_const",
"
//- /lib.rs
/// A Struct
struct S;
impl S {
/// An associated const
const C: i32 = 42;
}
fn foo() { let _ = S::<|> }
",
);
}
#[test]
fn completes_struct_associated_type() {
check_reference_completion(
"struct_associated_type",
"
//- /lib.rs
/// A Struct
struct S;
impl S {
/// An associated type
type T = i32;
}
fn foo() { let _ = S::<|> }
",
);
}
#[test]
fn completes_use_paths_across_crates() {
check_reference_completion(
"completes_use_paths_across_crates",
"
//- /main.rs
use foo::<|>;
//- /foo/lib.rs
pub mod bar {
pub struct S;
}
",
);
}
2019-01-08 22:33:36 +03:00
}