From 0e437c809b349a13fe4e82d295673fb1eb6bfd18 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 8 Aug 2021 15:49:00 +0300 Subject: [PATCH] fix: add `!` to macro completions with existing arg --- crates/ide_completion/src/render/macro_.rs | 43 +++++++++++++++------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/crates/ide_completion/src/render/macro_.rs b/crates/ide_completion/src/render/macro_.rs index 80c05273ca3..eb9fb4d8bf0 100644 --- a/crates/ide_completion/src/render/macro_.rs +++ b/crates/ide_completion/src/render/macro_.rs @@ -49,15 +49,19 @@ fn render(&self, import_to_add: Option) -> Option { .add_import(import_to_add) .set_detail(self.detail()); - let needs_bang = self.needs_bang(); + let needs_bang = !(self.ctx.completion.in_use_tree() + || matches!(self.ctx.completion.path_call_kind(), Some(CallKind::Mac))); + let has_parens = self.ctx.completion.path_call_kind().is_some(); + match self.ctx.snippet_cap() { - Some(cap) if needs_bang => { - let snippet = self.snippet(); - let lookup = self.lookup(); + Some(cap) if needs_bang && !has_parens => { + let snippet = format!("{}!{}$0{}", self.name, self.bra, self.ket); + let lookup = self.banged_name(); item.insert_snippet(cap, snippet).lookup_by(lookup); } - None if needs_bang => { - item.insert_text(self.banged_name()); + _ if needs_bang => { + let lookup = self.banged_name(); + item.insert_text(self.banged_name()).lookup_by(lookup); } _ => { cov_mark::hit!(dont_insert_macro_call_parens_unncessary); @@ -83,14 +87,6 @@ fn label(&self) -> String { } } - fn snippet(&self) -> String { - format!("{}!{}$0{}", self.name, self.bra, self.ket) - } - - fn lookup(&self) -> String { - self.banged_name() - } - fn banged_name(&self) -> String { format!("{}!", self.name) } @@ -163,6 +159,25 @@ macro_rules! frobnicate { () => () } ); } + #[test] + fn add_bang_to_parens() { + check_edit( + "frobnicate!", + r#" +macro_rules! frobnicate { () => () } +fn main() { + frob$0() +} +"#, + r#" +macro_rules! frobnicate { () => () } +fn main() { + frobnicate!() +} +"#, + ); + } + #[test] fn guesses_macro_braces() { check_edit(