Skip to content

Commit e0e7f0c

Browse files
committed
Move "complete macro call if cursor at ! token" logic to MacroRender
1 parent c0d2b44 commit e0e7f0c

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -304,31 +304,4 @@ pub mod prelude {
304304
"#]],
305305
);
306306
}
307-
308-
#[test]
309-
fn completes_macro_call_if_cursor_at_bang_token() {
310-
// Regression test for https://github.com/rust-analyzer/rust-analyzer/issues/9904
311-
cov_mark::check!(completes_macro_call_if_cursor_at_bang_token);
312-
check_edit(
313-
"foo!",
314-
r#"
315-
macro_rules! foo {
316-
() => {}
317-
}
318-
319-
fn main() {
320-
foo!$0
321-
}
322-
"#,
323-
r#"
324-
macro_rules! foo {
325-
() => {}
326-
}
327-
328-
fn main() {
329-
foo!($0)
330-
}
331-
"#,
332-
);
333-
}
334307
}

crates/ide_completion/src/context.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,14 @@ impl<'a> CompletionContext<'a> {
236236
let kind = self.token.kind();
237237
if kind == IDENT || kind == LIFETIME_IDENT || kind == UNDERSCORE || kind.is_keyword() {
238238
cov_mark::hit!(completes_if_prefix_is_keyword);
239-
return self.original_token.text_range();
239+
self.original_token.text_range()
240240
} else if kind == CHAR {
241241
// assume we are completing a lifetime but the user has only typed the '
242242
cov_mark::hit!(completes_if_lifetime_without_idents);
243-
return TextRange::at(self.original_token.text_range().start(), TextSize::from(1));
244-
} else if kind == BANG {
245-
if let Some(n) = self.token.parent() {
246-
if n.kind() == SyntaxKind::MACRO_CALL {
247-
cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token);
248-
return n.text_range();
249-
}
250-
}
243+
TextRange::at(self.original_token.text_range().start(), TextSize::from(1))
244+
} else {
245+
TextRange::empty(self.position.offset)
251246
}
252-
253-
TextRange::empty(self.position.offset)
254247
}
255248

256249
pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
@@ -402,6 +395,10 @@ impl<'a> CompletionContext<'a> {
402395
}
403396
}
404397

398+
pub(crate) fn is_immediately_after_macro_bang(&self) -> bool {
399+
self.token.kind() == BANG && self.token.parent().map_or(false, |it| it.kind() == MACRO_CALL)
400+
}
401+
405402
/// A version of [`SemanticsScope::process_all_names`] that filters out `#[doc(hidden)]` items.
406403
pub(crate) fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
407404
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)