rust-analyzer/crates/ra_ide/src/completion/complete_pattern.rs

90 lines
2.4 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-02-24 20:49:47 +00:00
use crate::completion::{CompletionContext, Completions};
/// Completes constats and paths in patterns.
pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_pat_binding {
return;
}
2019-03-23 07:53:48 +00:00
// FIXME: ideally, we should look at the type we are matching against and
2019-02-24 20:49:47 +00:00
// suggest variants + auto-imports
ctx.analyzer.process_all_names(ctx.db, &mut |name, res| {
let def = match &res {
hir::ScopeDef::ModuleDef(def) => def,
_ => return,
2019-02-24 20:49:47 +00:00
};
match def {
2019-09-12 21:34:52 +00:00
hir::ModuleDef::Adt(hir::Adt::Enum(..))
2019-02-24 20:49:47 +00:00
| hir::ModuleDef::EnumVariant(..)
| hir::ModuleDef::Const(..)
| hir::ModuleDef::Module(..) => (),
_ => return,
2019-02-24 20:49:47 +00:00
}
acc.add_resolution(ctx, name.to_string(), &res)
});
2019-02-24 20:49:47 +00:00
}
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
2019-08-29 13:49:10 +00:00
use insta::assert_debug_snapshot;
2019-02-24 20:49:47 +00:00
fn complete(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Reference)
}
#[test]
fn completes_enum_variants_and_modules() {
let completions = complete(
r"
enum E { X }
use self::E::X;
const Z: E = E::X;
mod m {}
static FOO: E = E::X;
struct Bar { f: u32 }
fn foo() {
match E::X {
<|>
}
}
",
);
2019-08-29 13:49:10 +00:00
assert_debug_snapshot!(completions, @r###"
2019-11-15 09:56:24 +00:00
[
CompletionItem {
label: "E",
source_range: [246; 246),
delete: [246; 246),
insert: "E",
kind: Enum,
},
CompletionItem {
label: "X",
source_range: [246; 246),
delete: [246; 246),
insert: "X",
kind: EnumVariant,
},
CompletionItem {
label: "Z",
source_range: [246; 246),
delete: [246; 246),
insert: "Z",
kind: Const,
},
CompletionItem {
label: "m",
source_range: [246; 246),
delete: [246; 246),
insert: "m",
kind: Module,
},
]
2019-05-23 22:46:23 +00:00
"###);
2019-02-24 20:49:47 +00:00
}
}