2019-01-08 22:33:36 +03:00
|
|
|
use crate::{
|
|
|
|
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
|
|
|
|
};
|
|
|
|
|
2019-01-23 16:22:10 -05:00
|
|
|
use hir::Docs;
|
|
|
|
|
2019-01-15 21:09:51 +03:00
|
|
|
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-01-08 22:33:36 +03:00
|
|
|
let (path, module) = match (&ctx.path_prefix, &ctx.module) {
|
|
|
|
(Some(path), Some(module)) => (path.clone(), module),
|
2019-01-15 21:09:51 +03:00
|
|
|
_ => return,
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
2019-01-15 19:15:01 +03:00
|
|
|
let def_id = match module.resolve_path(ctx.db, &path).take_types() {
|
2019-01-08 22:33:36 +03:00
|
|
|
Some(it) => it,
|
2019-01-15 21:09:51 +03:00
|
|
|
None => return,
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
2019-01-15 18:50:16 +03:00
|
|
|
match def_id.resolve(ctx.db) {
|
2019-01-08 22:33:36 +03:00
|
|
|
hir::Def::Module(module) => {
|
2019-01-15 19:15:01 +03:00
|
|
|
let module_scope = module.scope(ctx.db);
|
2019-01-08 00:30:49 +01:00
|
|
|
for (name, res) in module_scope.entries() {
|
2019-01-20 00:38:34 +08:00
|
|
|
CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
2019-01-20 12:02:00 +08:00
|
|
|
ctx.source_range(),
|
2019-01-20 00:38:34 +08:00
|
|
|
name.to_string(),
|
|
|
|
)
|
|
|
|
.from_resolution(ctx, res)
|
|
|
|
.add_to(acc);
|
2019-01-08 00:30:49 +01:00
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-01-08 16:01:19 +01:00
|
|
|
hir::Def::Enum(e) => {
|
2019-01-15 18:43:25 +03:00
|
|
|
e.variants(ctx.db)
|
2019-01-08 16:01:19 +01:00
|
|
|
.into_iter()
|
2019-01-23 16:22:10 -05:00
|
|
|
.for_each(|(variant_name, variant)| {
|
2019-01-20 00:38:34 +08:00
|
|
|
CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
2019-01-20 12:02:00 +08:00
|
|
|
ctx.source_range(),
|
2019-01-20 00:38:34 +08:00
|
|
|
variant_name.to_string(),
|
|
|
|
)
|
|
|
|
.kind(CompletionItemKind::EnumVariant)
|
2019-01-23 16:22:10 -05:00
|
|
|
.set_documentation(variant.docs(ctx.db))
|
2019-01-20 00:38:34 +08:00
|
|
|
.add_to(acc)
|
2019-01-10 02:07:42 +01:00
|
|
|
});
|
2019-01-08 16:01:19 +01:00
|
|
|
}
|
2019-01-15 21:09:51 +03:00
|
|
|
_ => return,
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-01-19 22:02:50 +08:00
|
|
|
use crate::completion::CompletionKind;
|
|
|
|
use crate::completion::completion_item::check_completion;
|
2019-01-08 22:33:36 +03:00
|
|
|
|
|
|
|
fn check_reference_completion(code: &str, expected_completions: &str) {
|
|
|
|
check_completion(code, expected_completions, CompletionKind::Reference);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_use_item_starting_with_self() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"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(
|
2019-01-19 22:02:50 +08:00
|
|
|
"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(
|
2019-01-19 22:02:50 +08:00
|
|
|
"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(
|
2019-01-19 22:02:50 +08:00
|
|
|
"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-19 22:02:50 +08:00
|
|
|
"reference_completion",
|
2019-01-08 22:33:36 +03:00
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
enum E { Foo, Bar(i32) }
|
|
|
|
fn foo() { let _ = E::<|> }
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|