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

135 lines
3.6 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-02-24 14:49:47 -06: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_or_const {
2019-02-24 14:49:47 -06:00
return;
}
2019-03-23 02:53:48 -05:00
// FIXME: ideally, we should look at the type we are matching against and
2019-02-24 14:49:47 -06:00
// suggest variants + auto-imports
ctx.scope().process_all_names(&mut |name, res| {
match &res {
hir::ScopeDef::ModuleDef(def) => match def {
hir::ModuleDef::Adt(hir::Adt::Enum(..))
| hir::ModuleDef::Adt(hir::Adt::Struct(..))
| hir::ModuleDef::EnumVariant(..)
| hir::ModuleDef::Const(..)
| hir::ModuleDef::Module(..) => (),
_ => return,
},
hir::ScopeDef::MacroDef(_) => (),
_ => return,
2019-02-24 14:49:47 -06:00
};
2019-02-24 14:49:47 -06:00
acc.add_resolution(ctx, name.to_string(), &res)
});
2019-02-24 14:49:47 -06:00
}
#[cfg(test)]
mod tests {
2020-03-11 04:46:43 -05:00
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
2019-08-29 08:49:10 -05:00
use insta::assert_debug_snapshot;
2019-02-24 14:49:47 -06: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 08:49:10 -05:00
assert_debug_snapshot!(completions, @r###"
2019-11-15 03:56:24 -06:00
[
CompletionItem {
label: "Bar",
source_range: [246; 246),
delete: [246; 246),
insert: "Bar",
kind: Struct,
},
CompletionItem {
label: "E",
source_range: [246; 246),
delete: [246; 246),
insert: "E",
kind: Enum,
},
2019-11-15 03:56:24 -06:00
CompletionItem {
label: "X",
source_range: [246; 246),
delete: [246; 246),
insert: "X",
kind: EnumVariant,
2020-04-03 12:33:12 -05:00
detail: "()",
2019-11-15 03:56:24 -06:00
},
CompletionItem {
label: "Z",
source_range: [246; 246),
delete: [246; 246),
insert: "Z",
kind: Const,
},
2019-11-15 03:56:24 -06:00
CompletionItem {
label: "m",
source_range: [246; 246),
delete: [246; 246),
insert: "m",
kind: Module,
},
]
2019-05-23 17:46:23 -05:00
"###);
2019-02-24 14:49:47 -06:00
}
2020-03-07 08:47:10 -06:00
#[test]
fn completes_in_simple_macro_call() {
let completions = complete(
r"
macro_rules! m { ($e:expr) => { $e } }
enum E { X }
fn foo() {
m!(match E::X {
<|>
})
}
",
);
2020-03-14 14:24:18 -05:00
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "E",
source_range: [151; 151),
delete: [151; 151),
insert: "E",
kind: Enum,
},
CompletionItem {
label: "m!",
source_range: [151; 151),
delete: [151; 151),
insert: "m!($0)",
kind: Macro,
detail: "macro_rules! m",
},
2020-03-14 14:24:18 -05:00
]
"###);
2020-03-07 08:47:10 -06:00
}
2019-02-24 14:49:47 -06:00
}