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

183 lines
3.9 KiB
Rust
Raw Normal View History

2019-02-24 21:21:31 +03:00
use crate::completion::{Completions, CompletionContext};
2019-01-08 22:33:36 +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 {
return;
2019-01-08 22:33:36 +03:00
}
2019-04-11 16:49:35 +03:00
let names = ctx.analyzer.resolver().all_names(ctx.db);
2019-01-08 22:33:36 +03:00
2019-02-24 21:21:31 +03:00
names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res));
2019-01-08 22:33:36 +03:00
}
#[cfg(test)]
mod tests {
2019-02-24 21:54:13 +03:00
use crate::completion::{CompletionKind, check_completion};
2019-01-08 22:33:36 +03: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(
"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(
"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(
"bindings_from_for",
2019-01-08 22:33:36 +03:00
r"
fn quux() {
for x in &[1, 2, 3] {
<|>
}
}
",
);
}
2019-02-01 23:06:57 +01:00
#[test]
fn completes_generic_params() {
check_reference_completion(
"generic_params",
r"
fn quux<T>() {
<|>
}
",
);
}
#[test]
fn completes_generic_params_in_struct() {
check_reference_completion(
"generic_params_in_struct",
r"
struct X<T> {
x: <|>
}
",
);
}
2019-01-08 22:33:36 +03:00
#[test]
fn completes_module_items() {
check_reference_completion(
"module_items",
2019-01-08 22:33:36 +03:00
r"
struct Foo;
enum Baz {}
fn quux() {
<|>
}
",
);
}
#[test]
2019-02-04 22:09:56 +01:00
fn completes_extern_prelude() {
check_reference_completion(
"extern_prelude",
r"
//- /lib.rs
use <|>;
//- /other_crate/lib.rs
// nothing here
",
);
}
#[test]
2019-01-08 22:33:36 +03:00
fn completes_module_items_in_nested_modules() {
check_reference_completion(
"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(
"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(
"dont_show_both_completions_for_shadowing",
2019-01-08 22:33:36 +03:00
r"
fn foo() {
2019-01-08 22:33:36 +03:00
let bar = 92;
{
let bar = 62;
<|>
}
}
",
)
}
#[test]
fn completes_self_in_methods() {
check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }")
2019-01-08 22:33:36 +03:00
}
2019-02-13 20:53:42 +01:00
#[test]
fn completes_prelude() {
check_reference_completion(
"completes_prelude",
"
//- /main.rs
fn foo() { let x: <|> }
//- /std/lib.rs
#[prelude_import]
use prelude::*;
mod prelude {
struct Option;
}
",
);
}
2019-01-08 22:33:36 +03:00
}