From 5540193fc87a33a6c141956a63f9f56fe6207ee8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Apr 2020 16:37:33 +0200 Subject: Don't insert !() if there's already some --- crates/ra_ide/src/completion/completion_context.rs | 4 ++ crates/ra_ide/src/completion/presentation.rs | 43 ++++++++++++++++++++-- crates/ra_ide/src/marks.rs | 1 + 3 files changed, 45 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index b8213d62f..f833d2a9a 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -50,6 +50,8 @@ pub(crate) struct CompletionContext<'a> { pub(super) dot_receiver_is_ambiguous_float_literal: bool, /// If this is a call (method or function) in particular, i.e. the () are already there. pub(super) is_call: bool, + /// If this is a macro call, i.e. the () are already there. + pub(super) is_macro_call: bool, pub(super) is_path_type: bool, pub(super) has_type_args: bool, } @@ -102,6 +104,7 @@ impl<'a> CompletionContext<'a> { is_new_item: false, dot_receiver: None, is_call: false, + is_macro_call: false, is_path_type: false, has_type_args: false, dot_receiver_is_ambiguous_float_literal: false, @@ -269,6 +272,7 @@ impl<'a> CompletionContext<'a> { .and_then(ast::PathExpr::cast) .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) .is_some(); + self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some(); self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); self.has_type_args = segment.type_arg_list().is_some(); diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index cdfd7bc32..55f75b15a 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -174,7 +174,8 @@ impl Completions { .set_deprecated(is_deprecated(macro_, ctx.db)) .detail(detail); - builder = if ctx.use_item_syntax.is_some() { + builder = if ctx.use_item_syntax.is_some() || ctx.is_macro_call { + tested_by!(dont_insert_macro_call_parens_unncessary); builder.insert_text(name) } else { let macro_braces_to_insert = @@ -960,7 +961,8 @@ mod tests { } #[test] - fn dont_insert_macro_call_braces_in_use() { + fn dont_insert_macro_call_parens_unncessary() { + covers!(dont_insert_macro_call_parens_unncessary); assert_debug_snapshot!( do_reference_completion( r" @@ -986,6 +988,41 @@ mod tests { }, ] "### - ) + ); + + assert_debug_snapshot!( + do_reference_completion( + r" + //- /main.rs + macro_rules frobnicate { + () => () + } + fn main() { + frob<|>!(); + } + " + ), + @r###" + [ + CompletionItem { + label: "frobnicate!", + source_range: [56; 60), + delete: [56; 60), + insert: "frobnicate", + kind: Macro, + detail: "macro_rules! frobnicate", + }, + CompletionItem { + label: "main()", + source_range: [56; 60), + delete: [56; 60), + insert: "main()$0", + kind: Function, + lookup: "main", + detail: "fn main()", + }, + ] + "### + ); } } diff --git a/crates/ra_ide/src/marks.rs b/crates/ra_ide/src/marks.rs index 1236cb773..5e1f135c5 100644 --- a/crates/ra_ide/src/marks.rs +++ b/crates/ra_ide/src/marks.rs @@ -7,4 +7,5 @@ test_utils::marks!( dont_complete_current_use test_resolve_parent_module_on_module_decl search_filters_by_range + dont_insert_macro_call_parens_unncessary ); -- cgit v1.2.3