2020-11-01 10:48:42 +00:00
|
|
|
//! Renderer for macro invocations.
|
|
|
|
|
2021-12-05 14:57:28 +00:00
|
|
|
use either::Either;
|
2021-05-31 12:13:09 +00:00
|
|
|
use hir::HasSource;
|
2021-01-20 17:38:12 +00:00
|
|
|
use ide_db::SymbolKind;
|
2021-12-05 14:57:28 +00:00
|
|
|
use syntax::{
|
|
|
|
display::{fn_as_proc_macro_label, macro_label},
|
|
|
|
SmolStr,
|
|
|
|
};
|
2020-11-01 09:35:04 +00:00
|
|
|
|
|
|
|
use crate::{
|
2021-06-06 18:02:26 +00:00
|
|
|
context::CallKind,
|
2021-10-27 15:18:42 +00:00
|
|
|
item::{CompletionItem, ImportEdit},
|
2020-11-01 09:35:04 +00:00
|
|
|
render::RenderContext,
|
|
|
|
};
|
|
|
|
|
2021-07-21 17:50:28 +00:00
|
|
|
pub(crate) fn render_macro(
|
|
|
|
ctx: RenderContext<'_>,
|
2020-12-03 09:13:28 +00:00
|
|
|
import_to_add: Option<ImportEdit>,
|
2021-05-31 12:13:09 +00:00
|
|
|
name: hir::Name,
|
2020-11-03 07:33:13 +00:00
|
|
|
macro_: hir::MacroDef,
|
|
|
|
) -> Option<CompletionItem> {
|
2020-11-27 16:00:03 +00:00
|
|
|
let _p = profile::span("render_macro");
|
2020-11-27 10:22:10 +00:00
|
|
|
MacroRender::new(ctx, name, macro_).render(import_to_add)
|
2020-11-03 07:33:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 09:35:04 +00:00
|
|
|
#[derive(Debug)]
|
2020-11-03 07:33:13 +00:00
|
|
|
struct MacroRender<'a> {
|
2020-11-01 09:35:04 +00:00
|
|
|
ctx: RenderContext<'a>,
|
2021-11-05 11:30:39 +00:00
|
|
|
name: SmolStr,
|
2020-11-01 09:35:04 +00:00
|
|
|
macro_: hir::MacroDef,
|
2021-05-31 12:13:09 +00:00
|
|
|
docs: Option<hir::Documentation>,
|
2020-11-01 09:35:04 +00:00
|
|
|
bra: &'static str,
|
|
|
|
ket: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> MacroRender<'a> {
|
2021-05-31 12:13:09 +00:00
|
|
|
fn new(ctx: RenderContext<'a>, name: hir::Name, macro_: hir::MacroDef) -> MacroRender<'a> {
|
2021-11-05 11:30:39 +00:00
|
|
|
let name = name.to_smol_str();
|
2020-11-01 09:59:43 +00:00
|
|
|
let docs = ctx.docs(macro_);
|
2020-11-13 19:25:45 +00:00
|
|
|
let docs_str = docs.as_ref().map_or("", |s| s.as_str());
|
2021-12-05 14:57:28 +00:00
|
|
|
let (bra, ket) =
|
|
|
|
if macro_.is_fn_like() { guess_macro_braces(&name, docs_str) } else { ("", "") };
|
2020-11-13 19:25:45 +00:00
|
|
|
|
2020-11-01 09:35:04 +00:00
|
|
|
MacroRender { ctx, name, macro_, docs, bra, ket }
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:41:16 +00:00
|
|
|
fn render(self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
|
2021-09-01 06:11:20 +00:00
|
|
|
let source_range = if self.ctx.completion.is_immediately_after_macro_bang() {
|
|
|
|
cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token);
|
|
|
|
self.ctx.completion.token.parent().map(|it| it.text_range())
|
|
|
|
} else {
|
|
|
|
Some(self.ctx.source_range())
|
|
|
|
}?;
|
2021-12-05 14:57:28 +00:00
|
|
|
let kind = match self.macro_.kind() {
|
|
|
|
hir::MacroKind::Derive => SymbolKind::Derive,
|
|
|
|
hir::MacroKind::Attr => SymbolKind::Attribute,
|
|
|
|
hir::MacroKind::BuiltIn | hir::MacroKind::Declarative | hir::MacroKind::ProcMacro => {
|
|
|
|
SymbolKind::Macro
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mut item = CompletionItem::new(kind, source_range, self.label());
|
2021-11-08 18:41:16 +00:00
|
|
|
item.set_deprecated(self.ctx.is_deprecated(self.macro_)).set_detail(self.detail());
|
2020-11-01 09:35:04 +00:00
|
|
|
|
2021-10-04 19:44:33 +00:00
|
|
|
if let Some(import_to_add) = import_to_add {
|
|
|
|
item.add_import(import_to_add);
|
|
|
|
}
|
|
|
|
|
2021-12-05 14:57:28 +00:00
|
|
|
let needs_bang = self.macro_.is_fn_like()
|
|
|
|
&& !(self.ctx.completion.in_use_tree()
|
|
|
|
|| matches!(self.ctx.completion.path_call_kind(), Some(CallKind::Mac)));
|
2021-08-08 12:49:00 +00:00
|
|
|
let has_parens = self.ctx.completion.path_call_kind().is_some();
|
|
|
|
|
2021-03-11 15:46:41 +00:00
|
|
|
match self.ctx.snippet_cap() {
|
2021-08-08 12:49:00 +00:00
|
|
|
Some(cap) if needs_bang && !has_parens => {
|
|
|
|
let snippet = format!("{}!{}$0{}", self.name, self.bra, self.ket);
|
|
|
|
let lookup = self.banged_name();
|
2021-03-12 09:12:32 +00:00
|
|
|
item.insert_snippet(cap, snippet).lookup_by(lookup);
|
2021-03-11 15:46:41 +00:00
|
|
|
}
|
2021-08-08 12:49:00 +00:00
|
|
|
_ if needs_bang => {
|
|
|
|
let lookup = self.banged_name();
|
|
|
|
item.insert_text(self.banged_name()).lookup_by(lookup);
|
2020-11-01 09:35:04 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::hit!(dont_insert_macro_call_parens_unncessary);
|
2021-11-05 11:30:39 +00:00
|
|
|
item.insert_text(&*self.name);
|
2020-11-01 09:35:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-08 18:41:16 +00:00
|
|
|
item.set_documentation(self.docs);
|
2021-03-12 09:12:32 +00:00
|
|
|
Some(item.build())
|
2020-11-01 09:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn needs_bang(&self) -> bool {
|
2021-06-17 11:56:55 +00:00
|
|
|
!self.ctx.completion.in_use_tree()
|
2021-06-06 18:02:26 +00:00
|
|
|
&& !matches!(self.ctx.completion.path_call_kind(), Some(CallKind::Mac))
|
2020-11-01 09:35:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 11:30:39 +00:00
|
|
|
fn label(&self) -> SmolStr {
|
2021-12-05 14:57:28 +00:00
|
|
|
if !self.macro_.is_fn_like() {
|
2021-11-05 11:30:39 +00:00
|
|
|
self.name.clone()
|
2021-12-05 14:57:28 +00:00
|
|
|
} else if self.needs_bang() && self.ctx.snippet_cap().is_some() {
|
|
|
|
SmolStr::from_iter([&*self.name, "!", self.bra, "…", self.ket])
|
2020-11-01 10:48:42 +00:00
|
|
|
} else {
|
2021-07-21 17:50:28 +00:00
|
|
|
self.banged_name()
|
2020-11-01 10:48:42 +00:00
|
|
|
}
|
2020-11-01 09:35:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 11:30:39 +00:00
|
|
|
fn banged_name(&self) -> SmolStr {
|
|
|
|
SmolStr::from_iter([&*self.name, "!"])
|
2020-11-01 09:35:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 03:14:09 +00:00
|
|
|
fn detail(&self) -> Option<String> {
|
2021-12-05 14:57:28 +00:00
|
|
|
let detail = match self.macro_.source(self.ctx.db())?.value {
|
|
|
|
Either::Left(node) => macro_label(&node),
|
|
|
|
Either::Right(node) => fn_as_proc_macro_label(&node),
|
|
|
|
};
|
|
|
|
Some(detail)
|
2020-11-01 09:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:25:45 +00:00
|
|
|
fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static str) {
|
|
|
|
let mut votes = [0, 0, 0];
|
|
|
|
for (idx, s) in docs.match_indices(¯o_name) {
|
|
|
|
let (before, after) = (&docs[..idx], &docs[idx + s.len()..]);
|
|
|
|
// Ensure to match the full word
|
|
|
|
if after.starts_with('!')
|
|
|
|
&& !before.ends_with(|c: char| c == '_' || c.is_ascii_alphanumeric())
|
|
|
|
{
|
|
|
|
// It may have spaces before the braces like `foo! {}`
|
|
|
|
match after[1..].chars().find(|&c| !c.is_whitespace()) {
|
|
|
|
Some('{') => votes[0] += 1,
|
|
|
|
Some('[') => votes[1] += 1,
|
|
|
|
Some('(') => votes[2] += 1,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a space before `{}`.
|
|
|
|
// We prefer the last one when some votes equal.
|
|
|
|
let (_vote, (bra, ket)) = votes
|
|
|
|
.iter()
|
|
|
|
.zip(&[(" {", "}"), ("[", "]"), ("(", ")")])
|
|
|
|
.max_by_key(|&(&vote, _)| vote)
|
|
|
|
.unwrap();
|
|
|
|
(*bra, *ket)
|
|
|
|
}
|
|
|
|
|
2020-11-01 10:36:30 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-16 19:45:02 +00:00
|
|
|
use crate::tests::check_edit;
|
2020-11-01 10:36:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_insert_macro_call_parens_unncessary() {
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::check!(dont_insert_macro_call_parens_unncessary);
|
2020-11-01 10:36:30 +00:00
|
|
|
check_edit(
|
|
|
|
"frobnicate!",
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:foo
|
2021-01-06 20:15:48 +00:00
|
|
|
use foo::$0;
|
2020-11-01 10:36:30 +00:00
|
|
|
//- /foo/lib.rs crate:foo
|
|
|
|
#[macro_export]
|
2020-12-15 14:37:37 +00:00
|
|
|
macro_rules! frobnicate { () => () }
|
2020-11-01 10:36:30 +00:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use foo::frobnicate;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_edit(
|
|
|
|
"frobnicate!",
|
|
|
|
r#"
|
2020-12-15 14:37:37 +00:00
|
|
|
macro_rules! frobnicate { () => () }
|
2021-01-06 20:15:48 +00:00
|
|
|
fn main() { frob$0!(); }
|
2020-11-01 10:36:30 +00:00
|
|
|
"#,
|
|
|
|
r#"
|
2020-12-15 14:37:37 +00:00
|
|
|
macro_rules! frobnicate { () => () }
|
2020-11-01 10:36:30 +00:00
|
|
|
fn main() { frobnicate!(); }
|
2021-08-08 12:49:00 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_bang_to_parens() {
|
|
|
|
check_edit(
|
|
|
|
"frobnicate!",
|
|
|
|
r#"
|
|
|
|
macro_rules! frobnicate { () => () }
|
|
|
|
fn main() {
|
|
|
|
frob$0()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
macro_rules! frobnicate { () => () }
|
|
|
|
fn main() {
|
|
|
|
frobnicate!()
|
|
|
|
}
|
2020-11-01 10:36:30 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guesses_macro_braces() {
|
|
|
|
check_edit(
|
|
|
|
"vec!",
|
|
|
|
r#"
|
|
|
|
/// Creates a [`Vec`] containing the arguments.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let v = vec![1, 2, 3];
|
|
|
|
/// assert_eq!(v[0], 1);
|
|
|
|
/// assert_eq!(v[1], 2);
|
|
|
|
/// assert_eq!(v[2], 3);
|
|
|
|
/// ```
|
|
|
|
macro_rules! vec { () => {} }
|
|
|
|
|
2021-06-16 13:46:58 +00:00
|
|
|
fn main() { v$0 }
|
2020-11-01 10:36:30 +00:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
/// Creates a [`Vec`] containing the arguments.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let v = vec![1, 2, 3];
|
|
|
|
/// assert_eq!(v[0], 1);
|
|
|
|
/// assert_eq!(v[1], 2);
|
|
|
|
/// assert_eq!(v[2], 3);
|
|
|
|
/// ```
|
|
|
|
macro_rules! vec { () => {} }
|
|
|
|
|
2021-06-16 13:46:58 +00:00
|
|
|
fn main() { vec![$0] }
|
2020-11-01 10:36:30 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_edit(
|
|
|
|
"foo!",
|
|
|
|
r#"
|
|
|
|
/// Foo
|
|
|
|
///
|
|
|
|
/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
|
|
|
|
/// call as `let _=foo! { hello world };`
|
|
|
|
macro_rules! foo { () => {} }
|
2021-01-06 20:15:48 +00:00
|
|
|
fn main() { $0 }
|
2020-11-01 10:36:30 +00:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
/// Foo
|
|
|
|
///
|
|
|
|
/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
|
|
|
|
/// call as `let _=foo! { hello world };`
|
|
|
|
macro_rules! foo { () => {} }
|
|
|
|
fn main() { foo! {$0} }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2021-09-01 06:11:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_macro_call_if_cursor_at_bang_token() {
|
|
|
|
// Regression test for https://github.com/rust-analyzer/rust-analyzer/issues/9904
|
|
|
|
cov_mark::check!(completes_macro_call_if_cursor_at_bang_token);
|
|
|
|
check_edit(
|
|
|
|
"foo!",
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo!$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo!($0)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2020-11-01 10:36:30 +00:00
|
|
|
}
|