From f41c98342476087d0a4387e7d337ce2d897e0346 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 27 May 2021 04:34:21 +0200 Subject: Don't complete non-macro item paths in impls and modules --- crates/ide_completion/src/completions/flyimport.rs | 6 ++++- crates/ide_completion/src/completions/keyword.rs | 27 +++++++++++----------- .../src/completions/qualified_path.rs | 12 ++++++++++ .../src/completions/unqualified_path.rs | 27 +++++++++++++++++++++- 4 files changed, 56 insertions(+), 16 deletions(-) (limited to 'crates/ide_completion/src/completions') diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index be9cfbded..df27e7a84 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs @@ -110,7 +110,11 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) if !ctx.config.enable_imports_on_the_fly { return None; } - if ctx.use_item_syntax.is_some() || ctx.is_path_disallowed() { + if ctx.use_item_syntax.is_some() + || ctx.is_path_disallowed() + || ctx.expects_item() + || ctx.expects_assoc_item() + { return None; } let potential_import_name = { diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index 58e35bad9..14d6ae54e 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs @@ -49,35 +49,35 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte return; } - let has_trait_or_impl_parent = ctx.has_impl_or_trait_parent(); + let expects_assoc_item = ctx.expects_assoc_item(); let has_block_expr_parent = ctx.has_block_expr_parent(); - let has_item_list_parent = ctx.has_item_list_parent(); + let expects_item = ctx.expects_item(); if ctx.has_impl_or_trait_prev_sibling() { add_keyword(ctx, acc, "where", "where "); return; } if ctx.previous_token_is(T![unsafe]) { - if has_item_list_parent || has_block_expr_parent { + if expects_item || has_block_expr_parent { add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}") } - if has_item_list_parent || has_block_expr_parent { + if expects_item || has_block_expr_parent { add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}"); add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}"); } return; } - if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent { + if expects_item || expects_assoc_item || has_block_expr_parent { add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}"); } - if has_item_list_parent || has_block_expr_parent { + if expects_item || has_block_expr_parent { add_keyword(ctx, acc, "use", "use "); add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}"); add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}"); } - if has_item_list_parent { + if expects_item { add_keyword(ctx, acc, "enum", "enum $1 {\n $0\n}"); add_keyword(ctx, acc, "struct", "struct $0"); add_keyword(ctx, acc, "union", "union $1 {\n $0\n}"); @@ -101,24 +101,23 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte add_keyword(ctx, acc, "else", "else {\n $0\n}"); add_keyword(ctx, acc, "else if", "else if $1 {\n $0\n}"); } - if has_item_list_parent || has_block_expr_parent { + if expects_item || has_block_expr_parent { add_keyword(ctx, acc, "mod", "mod $0"); } if ctx.has_ident_or_ref_pat_parent() { add_keyword(ctx, acc, "mut", "mut "); } - if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent { + if expects_item || expects_assoc_item || has_block_expr_parent { add_keyword(ctx, acc, "const", "const "); add_keyword(ctx, acc, "type", "type "); } - if has_item_list_parent || has_block_expr_parent { + if expects_item || has_block_expr_parent { add_keyword(ctx, acc, "static", "static "); }; - if has_item_list_parent || has_block_expr_parent { + if expects_item || has_block_expr_parent { add_keyword(ctx, acc, "extern", "extern "); } - if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent || ctx.is_match_arm - { + if expects_item || expects_assoc_item || has_block_expr_parent || ctx.is_match_arm { add_keyword(ctx, acc, "unsafe", "unsafe "); } if ctx.in_loop_body { @@ -130,7 +129,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte add_keyword(ctx, acc, "break", "break"); } } - if has_item_list_parent || ctx.has_impl_parent() || ctx.has_field_list_parent() { + if expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field() { add_keyword(ctx, acc, "pub(crate)", "pub(crate) "); add_keyword(ctx, acc, "pub", "pub "); } diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index ed48f61af..a90325e06 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -21,6 +21,18 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon }; let context_module = ctx.scope.module(); + if ctx.expects_item() || ctx.expects_assoc_item() { + if let PathResolution::Def(hir::ModuleDef::Module(module)) = resolution { + let module_scope = module.scope(ctx.db, context_module); + for (name, def) in module_scope { + if let ScopeDef::MacroDef(macro_def) = def { + acc.add_macro(ctx, Some(name.to_string()), macro_def); + } + } + } + return; + } + // Add associated types on type parameters and `Self`. resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| { acc.add_type_alias(ctx, alias); diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index 046a393ae..cbac88240 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -12,6 +12,14 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC if ctx.is_path_disallowed() { return; } + if ctx.expects_item() || 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.to_string()), macro_def); + } + }); + return; + } if let Some(hir::Adt::Enum(e)) = ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt()) @@ -647,7 +655,7 @@ fn f() {} } #[test] - fn completes_type_or_trait_in_impl_block() { + fn completes_target_type_or_trait_in_impl_block() { check( r#" trait MyTrait {} @@ -662,4 +670,21 @@ impl My$0 "#]], ) } + + #[test] + fn only_completes_macros_in_assoc_item_list() { + check( + r#" +struct MyStruct {} +macro_rules! foo {} + +impl MyStruct { + $0 +} +"#, + expect![[r#" + ma foo! macro_rules! foo + "#]], + ) + } } -- cgit v1.2.3