Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/ide_completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ impl<'a> CompletionContext<'a> {
}
}

pub(crate) fn is_immediately_after_macro_bang(&self) -> bool {
self.token.kind() == BANG && self.token.parent().map_or(false, |it| it.kind() == MACRO_CALL)
}

/// A version of [`SemanticsScope::process_all_names`] that filters out `#[doc(hidden)]` items.
pub(crate) fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
self.scope.process_all_names(&mut |name, def| {
Expand Down
36 changes: 34 additions & 2 deletions crates/ide_completion/src/render/macro_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ impl<'a> MacroRender<'a> {
}

fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
let mut item =
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label());
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())
}?;
let mut item = CompletionItem::new(CompletionKind::Reference, source_range, &self.label());
item.kind(SymbolKind::Macro)
.set_documentation(self.docs.clone())
.set_deprecated(self.ctx.is_deprecated(self.macro_))
Expand Down Expand Up @@ -230,4 +235,31 @@ fn main() { foo! {$0} }
"#,
)
}

#[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)
}
"#,
);
}
}