2019-01-08 22:33:36 +03:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-01-19 22:02:50 +08:00
|
|
|
use ra_syntax::ast::AstNode;
|
2019-01-15 21:09:51 +03:00
|
|
|
use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-01-15 21:09:51 +03:00
|
|
|
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-01-08 22:33:36 +03:00
|
|
|
if !ctx.is_trivial_path {
|
2019-01-15 21:09:51 +03:00
|
|
|
return;
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
let module = match &ctx.module {
|
|
|
|
Some(it) => it,
|
2019-01-15 21:09:51 +03:00
|
|
|
None => return,
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
|
|
|
if let Some(function) = &ctx.function {
|
2019-01-15 19:04:49 +03:00
|
|
|
let scopes = function.scopes(ctx.db);
|
2019-01-19 22:02:50 +08:00
|
|
|
complete_fn(acc, &scopes, ctx);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:15:01 +03:00
|
|
|
let module_scope = module.scope(ctx.db);
|
2019-01-08 22:33:36 +03:00
|
|
|
module_scope
|
|
|
|
.entries()
|
|
|
|
.filter(|(_name, res)| {
|
2019-01-18 16:36:56 +03:00
|
|
|
// For cases like `use self::foo<|>` don't suggest foo itself.
|
2019-01-08 22:33:36 +03:00
|
|
|
match res.import {
|
|
|
|
None => true,
|
|
|
|
Some(import) => {
|
2019-01-18 16:36:56 +03:00
|
|
|
let source = module.import_source(ctx.db, import);
|
|
|
|
!source.syntax().range().is_subrange(&ctx.leaf.range())
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.for_each(|(name, res)| {
|
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 22:33:36 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:02:50 +08:00
|
|
|
fn complete_fn(
|
|
|
|
acc: &mut Completions,
|
|
|
|
scopes: &hir::ScopesWithSyntaxMapping,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
) {
|
2019-01-08 22:33:36 +03:00
|
|
|
let mut shadowed = FxHashSet::default();
|
|
|
|
scopes
|
2019-01-19 22:02:50 +08:00
|
|
|
.scope_chain_for_offset(ctx.offset)
|
2019-01-08 22:33:36 +03:00
|
|
|
.flat_map(|scope| scopes.scopes.entries(scope).iter())
|
|
|
|
.filter(|entry| shadowed.insert(entry.name()))
|
|
|
|
.for_each(|entry| {
|
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
|
|
|
entry.name().to_string(),
|
|
|
|
)
|
|
|
|
.kind(CompletionItemKind::Binding)
|
|
|
|
.add_to(acc)
|
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
|
|
|
|
2019-01-19 22:02:50 +08:00
|
|
|
fn check_reference_completion(name: &str, code: &str) {
|
|
|
|
check_completion(name, code, CompletionKind::Reference);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_bindings_from_let() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"bindings_from_let",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
fn quux(x: i32) {
|
|
|
|
let y = 92;
|
|
|
|
1 + <|>;
|
|
|
|
let z = ();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_bindings_from_if_let() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"bindings_from_if_let",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
fn quux() {
|
|
|
|
if let Some(x) = foo() {
|
|
|
|
let y = 92;
|
|
|
|
};
|
|
|
|
if let Some(a) = bar() {
|
|
|
|
let b = 62;
|
|
|
|
1 + <|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_bindings_from_for() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"bindings_from_for",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
fn quux() {
|
|
|
|
for x in &[1, 2, 3] {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_module_items() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"module_items",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
struct Foo;
|
|
|
|
enum Baz {}
|
|
|
|
fn quux() {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_module_items_in_nested_modules() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"module_items_in_nested_modules",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
struct Foo;
|
|
|
|
mod m {
|
|
|
|
struct Bar;
|
|
|
|
fn quux() { <|> }
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_return_type() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"return_type",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
struct Foo;
|
|
|
|
fn x() -> <|>
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_show_both_completions_for_shadowing() {
|
|
|
|
check_reference_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"dont_show_both_completions_for_shadowing",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
fn foo() -> {
|
|
|
|
let bar = 92;
|
|
|
|
{
|
|
|
|
let bar = 62;
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_self_in_methods() {
|
2019-01-19 22:02:50 +08:00
|
|
|
check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }")
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|