From 73ccf7f495f328e10dca38e0f069de89ab52b9d3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Apr 2020 15:07:18 +0200 Subject: Reorder imports --- crates/ra_ide/src/completion/complete_record.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_record.rs b/crates/ra_ide/src/completion/complete_record.rs index 01dd8c6db..780a98aea 100644 --- a/crates/ra_ide/src/completion/complete_record.rs +++ b/crates/ra_ide/src/completion/complete_record.rs @@ -1,7 +1,8 @@ //! Complete fields in record literals and patterns. -use crate::completion::{CompletionContext, Completions}; use ra_syntax::{ast, ast::NameOwner, SmolStr}; +use crate::completion::{CompletionContext, Completions}; + pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { let (ty, variant, already_present_fields) = match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) { @@ -59,9 +60,10 @@ fn pattern_ascribed_fields(record_pat: &ast::RecordPat) -> Vec { #[cfg(test)] mod tests { mod record_lit_tests { - use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; + fn complete(code: &str) -> Vec { do_completion(code, CompletionKind::Reference) } @@ -204,9 +206,10 @@ mod tests { } mod record_pat_tests { - use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; + fn complete(code: &str) -> Vec { do_completion(code, CompletionKind::Reference) } -- cgit v1.2.3 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 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide/src/completion') 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()", + }, + ] + "### + ); } } -- cgit v1.2.3 From 3bde2b742388f3ec3bb08841f93a06a62be04e4d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Apr 2020 16:40:39 +0200 Subject: A more precise panic macro --- crates/ra_ide/src/completion/complete_record.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_record.rs b/crates/ra_ide/src/completion/complete_record.rs index 780a98aea..79f5c8c8f 100644 --- a/crates/ra_ide/src/completion/complete_record.rs +++ b/crates/ra_ide/src/completion/complete_record.rs @@ -7,7 +7,7 @@ pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> let (ty, variant, already_present_fields) = match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) { (None, None) => return None, - (Some(_), Some(_)) => panic!("A record cannot be both a literal and a pattern"), + (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"), (Some(record_pat), _) => ( ctx.sema.type_of_pat(&record_pat.clone().into())?, ctx.sema.resolve_record_pattern(record_pat)?, -- cgit v1.2.3