From 6353b1621f44e1b0db65ebbe414aa7c5f1864b9d Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Wed, 11 Sep 2019 22:44:44 +0800 Subject: [PATCH] Split out `complete_macro_in_item_position` --- crates/ra_ide_api/src/completion.rs | 2 + .../complete_macro_in_item_position.rs | 50 +++++++++++++++++++ .../src/completion/complete_scope.rs | 37 -------------- 3 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs index a4f080adc6..0ad4148311 100644 --- a/crates/ra_ide_api/src/completion.rs +++ b/crates/ra_ide_api/src/completion.rs @@ -12,6 +12,7 @@ mod complete_snippet; mod complete_path; mod complete_scope; mod complete_postfix; +mod complete_macro_in_item_position; use ra_db::SourceDatabase; @@ -69,5 +70,6 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti complete_record_pattern::complete_record_pattern(&mut acc, &ctx); complete_pattern::complete_pattern(&mut acc, &ctx); complete_postfix::complete_postfix(&mut acc, &ctx); + complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); Some(acc) } diff --git a/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs new file mode 100644 index 0000000000..708dc97771 --- /dev/null +++ b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs @@ -0,0 +1,50 @@ +use crate::completion::{CompletionContext, Completions}; + +pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) { + // Show only macros in top level. + if ctx.is_new_item { + for (name, res) in ctx.analyzer.all_names(ctx.db) { + if res.get_macros().is_some() { + acc.add_resolution(ctx, name.to_string(), &res.only_macros()); + } + } + } +} + +#[cfg(test)] +mod tests { + use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use insta::assert_debug_snapshot; + + fn do_reference_completion(code: &str) -> Vec { + do_completion(code, CompletionKind::Reference) + } + + #[test] + fn completes_macros_as_item() { + assert_debug_snapshot!( + do_reference_completion( + " + //- /main.rs + macro_rules! foo { + () => {} + } + + fn foo() {} + + <|> + " + ), + @r##"[ + CompletionItem { + label: "foo", + source_range: [46; 46), + delete: [46; 46), + insert: "foo!", + kind: Macro, + detail: "macro_rules! foo", + }, +]"## + ); + } +} diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index e2e1d7872a..2062e7300c 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -6,15 +6,6 @@ use rustc_hash::FxHashMap; use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions}; pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { - // Show only macros in top level. - if ctx.is_new_item { - for (name, res) in ctx.analyzer.all_names(ctx.db) { - if res.get_macros().is_some() { - acc.add_resolution(ctx, name.to_string(), &res.only_macros()); - } - } - } - if !ctx.is_trivial_path { return; } @@ -730,34 +721,6 @@ mod tests { kind: Function, detail: "fn main()", }, -]"## - ); - } - - #[test] - fn completes_macros_as_item() { - assert_debug_snapshot!( - do_reference_completion( - " - //- /main.rs - macro_rules! foo { - () => {} - } - - fn foo() {} - - <|> - " - ), - @r##"[ - CompletionItem { - label: "foo", - source_range: [46; 46), - delete: [46; 46), - insert: "foo!", - kind: Macro, - detail: "macro_rules! foo", - }, ]"## ); }