From b29e8ed994c573273a8182efd9b74ec8c664a848 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 7 Jun 2021 19:35:24 +0200 Subject: Remove unnecessary completion::macro_in_item_position --- .../src/completions/macro_in_item_position.rs | 48 ---------------------- .../src/completions/qualified_path.rs | 11 ++--- .../src/completions/unqualified_path.rs | 22 +++++----- 3 files changed, 16 insertions(+), 65 deletions(-) delete mode 100644 crates/ide_completion/src/completions/macro_in_item_position.rs (limited to 'crates/ide_completion/src/completions') diff --git a/crates/ide_completion/src/completions/macro_in_item_position.rs b/crates/ide_completion/src/completions/macro_in_item_position.rs deleted file mode 100644 index 781b96ff1..000000000 --- a/crates/ide_completion/src/completions/macro_in_item_position.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! Completes macro invocations used in item position. - -use crate::{CompletionContext, Completions}; - -// Ideally this should be removed and moved into `(un)qualified_path` respectively -pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) { - // Show only macros in top level. - if !ctx.expects_item() { - return; - } - - ctx.scope.process_all_names(&mut |name, res| { - if let hir::ScopeDef::MacroDef(mac) = res { - acc.add_macro(ctx, Some(name.clone()), mac); - } - // FIXME: This should be done in qualified_path/unqualified_path instead? - if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res { - acc.add_resolution(ctx, name, &res); - } - }) -} - -#[cfg(test)] -mod tests { - use expect_test::{expect, Expect}; - - use crate::{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_macros_as_item() { - check( - r#" -macro_rules! foo { () => {} } -fn foo() {} - -$0 -"#, - expect![[r#" - ma foo!(…) macro_rules! foo - "#]], - ) - } -} diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index c072de7b5..d58745fb4 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -7,7 +7,7 @@ use syntax::AstNode; use crate::{CompletionContext, Completions}; pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { - if ctx.is_path_disallowed() || ctx.expects_item() { + if ctx.is_path_disallowed() { return; } let path = match ctx.path_qual() { @@ -20,7 +20,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon None => return, }; let context_module = ctx.scope.module(); - if ctx.expects_assoc_item() { + + if ctx.expects_item() || ctx.expects_assoc_item() { if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution { let module_scope = module.scope(ctx.db, context_module); for (name, def) in module_scope { @@ -631,17 +632,17 @@ impl MyStruct { "#, expect![[r##" md bar - ma foo! #[macro_export] macro_rules! foo + ma foo!(…) #[macro_export] macro_rules! foo "##]], ); } #[test] - #[ignore] // FIXME doesn't complete anything atm fn completes_in_item_list() { check( r#" struct MyStruct {} +#[macro_export] macro_rules! foo {} mod bar {} @@ -649,7 +650,7 @@ crate::$0 "#, expect![[r#" md bar - ma foo! macro_rules! foo + ma foo!(…) #[macro_export] macro_rules! foo "#]], ) } diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index f321ed52b..8b22933e0 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -5,26 +5,25 @@ use hir::ScopeDef; use crate::{CompletionContext, Completions}; pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.is_trivial_path() { - return; - } - if ctx.is_path_disallowed() || ctx.expects_item() { + if ctx.is_path_disallowed() || !ctx.is_trivial_path() { return; } - if ctx.expects_assoc_item() { - ctx.scope.process_all_names(&mut |name, def| { - if let ScopeDef::MacroDef(macro_def) = def { - acc.add_macro(ctx, Some(name.clone()), macro_def); + if ctx.expects_item() || ctx.expects_assoc_item() { + // only show macros in {Assoc}ItemList + ctx.scope.process_all_names(&mut |name, res| { + if let hir::ScopeDef::MacroDef(mac) = res { + acc.add_macro(ctx, Some(name.clone()), mac); } - if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def { - acc.add_resolution(ctx, name, &def); + if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res { + acc.add_resolution(ctx, name, &res); } }); return; } if ctx.expects_use_tree() { + // only show modules in a fresh UseTree cov_mark::hit!(only_completes_modules_in_import); ctx.scope.process_all_names(&mut |name, res| { if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res { @@ -693,12 +692,11 @@ impl MyStruct { "#, expect![[r#" md bar - ma foo! macro_rules! foo + ma foo!(…) macro_rules! foo "#]], ) } - // FIXME: The completions here currently come from `macro_in_item_position`, but they shouldn't #[test] fn completes_in_item_list() { check( -- cgit v1.2.3