From d7a7da8261795e15aef77ad306ada00c853fb913 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 10:39:12 +0300 Subject: don't add macro braces in use items --- crates/ra_ide_api/src/completion/presentation.rs | 70 ++++++++++++++++++------ 1 file changed, 53 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index aed4ce6d4..f7e98e6df 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -164,27 +164,32 @@ impl Completions { name: Option, macro_: hir::MacroDef, ) { + let name = match name { + Some(it) => it, + None => return, + }; + let ast_node = macro_.source(ctx.db).ast; - if let Some(name) = name { - let detail = macro_label(&ast_node); + let detail = macro_label(&ast_node); + + let docs = macro_.docs(ctx.db); + let macro_declaration = format!("{}!", name); + + let mut builder = + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), ¯o_declaration) + .kind(CompletionItemKind::Macro) + .set_documentation(docs.clone()) + .detail(detail); - let docs = macro_.docs(ctx.db); + builder = if ctx.use_item_syntax.is_some() { + builder.insert_text(name) + } else { let macro_braces_to_insert = self.guess_macro_braces(&name, docs.as_ref().map_or("", |s| s.as_str())); - let macro_declaration = name + "!"; - - let builder = CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - ¯o_declaration, - ) - .kind(CompletionItemKind::Macro) - .set_documentation(docs) - .detail(detail) - .insert_snippet(macro_declaration + macro_braces_to_insert); + builder.insert_snippet(macro_declaration + macro_braces_to_insert) + }; - self.add(builder); - } + self.add(builder); } fn add_function_with_name( @@ -281,10 +286,11 @@ fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> b #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; use test_utils::covers; + use crate::completion::{do_completion, CompletionItem, CompletionKind}; + fn do_reference_completion(code: &str) -> Vec { do_completion(code, CompletionKind::Reference) } @@ -576,4 +582,34 @@ mod tests { "### ); } + + #[test] + fn dont_insert_macro_call_braces_in_use() { + assert_debug_snapshot!( + do_reference_completion( + r" + //- /main.rs + use foo::<|>; + + //- /foo/lib.rs + #[macro_export] + macro_rules frobnicate { + () => () + } + " + ), + @r###" + [ + CompletionItem { + label: "frobnicate!", + source_range: [9; 9), + delete: [9; 9), + insert: "frobnicate", + kind: Macro, + detail: "#[macro_export]\nmacro_rules! frobnicate", + }, + ] + "### + ) + } } -- cgit v1.2.3