Skip to content

Commit 911659a

Browse files
bors[bot]unexge
andauthored
Merge #10048
10048: fix: correctly complete macro call if cursor at `!` r=Veykril a=unexge Fixes #9904 Co-authored-by: unexge <[email protected]>
2 parents 4c9eef7 + e0e7f0c commit 911659a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

crates/ide_completion/src/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ impl<'a> CompletionContext<'a> {
438438
}
439439
}
440440

441+
pub(crate) fn is_immediately_after_macro_bang(&self) -> bool {
442+
self.token.kind() == BANG && self.token.parent().map_or(false, |it| it.kind() == MACRO_CALL)
443+
}
444+
441445
/// A version of [`SemanticsScope::process_all_names`] that filters out `#[doc(hidden)]` items.
442446
pub(crate) fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
443447
self.scope.process_all_names(&mut |name, def| {

crates/ide_completion/src/render/macro_.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ impl<'a> MacroRender<'a> {
4141
}
4242

4343
fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
44-
let mut item =
45-
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label());
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())
47+
} else {
48+
Some(self.ctx.source_range())
49+
}?;
50+
let mut item = CompletionItem::new(CompletionKind::Reference, source_range, &self.label());
4651
item.kind(SymbolKind::Macro)
4752
.set_documentation(self.docs.clone())
4853
.set_deprecated(self.ctx.is_deprecated(self.macro_))
@@ -230,4 +235,31 @@ fn main() { foo! {$0} }
230235
"#,
231236
)
232237
}
238+
239+
#[test]
240+
fn completes_macro_call_if_cursor_at_bang_token() {
241+
// Regression test for https://github.com/rust-analyzer/rust-analyzer/issues/9904
242+
cov_mark::check!(completes_macro_call_if_cursor_at_bang_token);
243+
check_edit(
244+
"foo!",
245+
r#"
246+
macro_rules! foo {
247+
() => {}
248+
}
249+
250+
fn main() {
251+
foo!$0
252+
}
253+
"#,
254+
r#"
255+
macro_rules! foo {
256+
() => {}
257+
}
258+
259+
fn main() {
260+
foo!($0)
261+
}
262+
"#,
263+
);
264+
}
233265
}

0 commit comments

Comments
 (0)