1 //! Renderer for macro invocations.
4 use ide_db::SymbolKind;
5 use syntax::{display::macro_label, SmolStr};
9 item::{CompletionItem, ImportEdit},
10 render::RenderContext,
13 pub(crate) fn render_macro(
14 ctx: RenderContext<'_>,
15 import_to_add: Option<ImportEdit>,
17 macro_: hir::MacroDef,
18 ) -> Option<CompletionItem> {
19 let _p = profile::span("render_macro");
20 MacroRender::new(ctx, name, macro_).render(import_to_add)
24 struct MacroRender<'a> {
25 ctx: RenderContext<'a>,
27 macro_: hir::MacroDef,
28 docs: Option<hir::Documentation>,
33 impl<'a> MacroRender<'a> {
34 fn new(ctx: RenderContext<'a>, name: hir::Name, macro_: hir::MacroDef) -> MacroRender<'a> {
35 let name = name.to_smol_str();
36 let docs = ctx.docs(macro_);
37 let docs_str = docs.as_ref().map_or("", |s| s.as_str());
38 let (bra, ket) = guess_macro_braces(&name, docs_str);
40 MacroRender { ctx, name, macro_, docs, bra, ket }
43 fn render(self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
44 let source_range = if self.ctx.completion.is_immediately_after_macro_bang() {
45 cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token);
46 self.ctx.completion.token.parent().map(|it| it.text_range())
48 Some(self.ctx.source_range())
50 let mut item = CompletionItem::new(SymbolKind::Macro, source_range, self.label());
51 item.set_deprecated(self.ctx.is_deprecated(self.macro_)).set_detail(self.detail());
53 if let Some(import_to_add) = import_to_add {
54 item.add_import(import_to_add);
57 let needs_bang = !(self.ctx.completion.in_use_tree()
58 || matches!(self.ctx.completion.path_call_kind(), Some(CallKind::Mac)));
59 let has_parens = self.ctx.completion.path_call_kind().is_some();
61 match self.ctx.snippet_cap() {
62 Some(cap) if needs_bang && !has_parens => {
63 let snippet = format!("{}!{}$0{}", self.name, self.bra, self.ket);
64 let lookup = self.banged_name();
65 item.insert_snippet(cap, snippet).lookup_by(lookup);
68 let lookup = self.banged_name();
69 item.insert_text(self.banged_name()).lookup_by(lookup);
72 cov_mark::hit!(dont_insert_macro_call_parens_unncessary);
73 item.insert_text(&*self.name);
77 item.set_documentation(self.docs);
81 fn needs_bang(&self) -> bool {
82 !self.ctx.completion.in_use_tree()
83 && !matches!(self.ctx.completion.path_call_kind(), Some(CallKind::Mac))
86 fn label(&self) -> SmolStr {
87 if self.needs_bang() && self.ctx.snippet_cap().is_some() {
88 SmolStr::from_iter([&*self.name, "!", self.bra, "…", self.ket])
89 } else if self.macro_.kind() == hir::MacroKind::Derive {
96 fn banged_name(&self) -> SmolStr {
97 SmolStr::from_iter([&*self.name, "!"])
100 fn detail(&self) -> Option<String> {
101 let ast_node = self.macro_.source(self.ctx.db())?.value.left()?;
102 Some(macro_label(&ast_node))
106 fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static str) {
107 let mut votes = [0, 0, 0];
108 for (idx, s) in docs.match_indices(¯o_name) {
109 let (before, after) = (&docs[..idx], &docs[idx + s.len()..]);
110 // Ensure to match the full word
111 if after.starts_with('!')
112 && !before.ends_with(|c: char| c == '_' || c.is_ascii_alphanumeric())
114 // It may have spaces before the braces like `foo! {}`
115 match after[1..].chars().find(|&c| !c.is_whitespace()) {
116 Some('{') => votes[0] += 1,
117 Some('[') => votes[1] += 1,
118 Some('(') => votes[2] += 1,
124 // Insert a space before `{}`.
125 // We prefer the last one when some votes equal.
126 let (_vote, (bra, ket)) = votes
128 .zip(&[(" {", "}"), ("[", "]"), ("(", ")")])
129 .max_by_key(|&(&vote, _)| vote)
136 use crate::tests::check_edit;
139 fn dont_insert_macro_call_parens_unncessary() {
140 cov_mark::check!(dont_insert_macro_call_parens_unncessary);
144 //- /main.rs crate:main deps:foo
146 //- /foo/lib.rs crate:foo
148 macro_rules! frobnicate { () => () }
158 macro_rules! frobnicate { () => () }
159 fn main() { frob$0!(); }
162 macro_rules! frobnicate { () => () }
163 fn main() { frobnicate!(); }
169 fn add_bang_to_parens() {
173 macro_rules! frobnicate { () => () }
179 macro_rules! frobnicate { () => () }
188 fn guesses_macro_braces() {
192 /// Creates a [`Vec`] containing the arguments.
195 /// let v = vec![1, 2, 3];
196 /// assert_eq!(v[0], 1);
197 /// assert_eq!(v[1], 2);
198 /// assert_eq!(v[2], 3);
200 macro_rules! vec { () => {} }
205 /// Creates a [`Vec`] containing the arguments.
208 /// let v = vec![1, 2, 3];
209 /// assert_eq!(v[0], 1);
210 /// assert_eq!(v[1], 2);
211 /// assert_eq!(v[2], 3);
213 macro_rules! vec { () => {} }
215 fn main() { vec![$0] }
224 /// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
225 /// call as `let _=foo! { hello world };`
226 macro_rules! foo { () => {} }
232 /// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
233 /// call as `let _=foo! { hello world };`
234 macro_rules! foo { () => {} }
235 fn main() { foo! {$0} }
241 fn completes_macro_call_if_cursor_at_bang_token() {
242 // Regression test for https://github.com/rust-analyzer/rust-analyzer/issues/9904
243 cov_mark::check!(completes_macro_call_if_cursor_at_bang_token);