From 1b0c7701cc97cd7bef8bb9729011d4cf291a60c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 17:42:52 +0200 Subject: Rename ra_ide -> ide --- crates/ide/src/completion/complete_pattern.rs | 88 +++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 crates/ide/src/completion/complete_pattern.rs (limited to 'crates/ide/src/completion/complete_pattern.rs') diff --git a/crates/ide/src/completion/complete_pattern.rs b/crates/ide/src/completion/complete_pattern.rs new file mode 100644 index 000000000..aceb77cb5 --- /dev/null +++ b/crates/ide/src/completion/complete_pattern.rs @@ -0,0 +1,88 @@ +//! FIXME: write short doc here + +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 { + return; + } + if ctx.record_pat_syntax.is_some() { + return; + } + + // FIXME: ideally, we should look at the type we are matching against and + // 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, + }; + + acc.add_resolution(ctx, name.to_string(), &res) + }); +} + +#[cfg(test)] +mod tests { + use expect::{expect, Expect}; + + use crate::completion::{test_utils::completion_list, CompletionKind}; + + fn check(ra_fixture: &str, expect: Expect) { + let actual = completion_list(ra_fixture, CompletionKind::Reference); + expect.assert_eq(&actual) + } + + #[test] + fn completes_enum_variants_and_modules() { + check( + 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 { <|> } +} +"#, + expect![[r#" + st Bar + en E + ev X () + ct Z + md m + "#]], + ); + } + + #[test] + fn completes_in_simple_macro_call() { + check( + r#" +macro_rules! m { ($e:expr) => { $e } } +enum E { X } + +fn foo() { + m!(match E::X { <|> }) +} +"#, + expect![[r#" + en E + ma m!(…) macro_rules! m + "#]], + ); + } +} -- cgit v1.2.3 From b0fd3faf36c94c3fc52151c6aa82b36b43b7cceb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Aug 2020 13:19:31 +0200 Subject: Switch to expect_test from crates.io --- crates/ide/src/completion/complete_pattern.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ide/src/completion/complete_pattern.rs') diff --git a/crates/ide/src/completion/complete_pattern.rs b/crates/ide/src/completion/complete_pattern.rs index aceb77cb5..5a13574d4 100644 --- a/crates/ide/src/completion/complete_pattern.rs +++ b/crates/ide/src/completion/complete_pattern.rs @@ -33,7 +33,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { #[cfg(test)] mod tests { - use expect::{expect, Expect}; + use expect_test::{expect, Expect}; use crate::completion::{test_utils::completion_list, CompletionKind}; -- cgit v1.2.3