diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index 4dc4d212e60..7dc29c3d5ac 100644 --- a/crates/ide-completion/src/context.rs +++ b/crates/ide-completion/src/context.rs @@ -387,8 +387,7 @@ pub(crate) struct CompletionContext<'a> { impl<'a> CompletionContext<'a> { /// The range of the identifier that is being completed. pub(crate) fn source_range(&self) -> TextRange { - // check kind of macro-expanded token, but use range of original token - let kind = self.token.kind(); + let kind = self.original_token.kind(); match kind { CHAR => { // assume we are completing a lifetime but the user has only typed the ' diff --git a/crates/ide-completion/src/render/macro_.rs b/crates/ide-completion/src/render/macro_.rs index ffcad1185aa..44e88607633 100644 --- a/crates/ide-completion/src/render/macro_.rs +++ b/crates/ide-completion/src/render/macro_.rs @@ -264,6 +264,65 @@ macro_rules! foo { fn main() { foo!($0) } +"#, + ); + } + + #[test] + fn complete_missing_macro_arg() { + // Regression test for https://github.com/rust-lang/rust-analyzer/issues/14246 + check_edit( + "BAR", + r#" +macro_rules! foo { + ($val:ident, $val2: ident) => { + $val $val2 + }; +} + +const BAR: u32 = 9; +fn main() { + foo!(BAR, $0) +} +"#, + r#" +macro_rules! foo { + ($val:ident, $val2: ident) => { + $val $val2 + }; +} + +const BAR: u32 = 9; +fn main() { + foo!(BAR, BAR) +} +"#, + ); + check_edit( + "BAR", + r#" +macro_rules! foo { + ($val:ident, $val2: ident) => { + $val $val2 + }; +} + +const BAR: u32 = 9; +fn main() { + foo!($0) +} +"#, + r#" +macro_rules! foo { + ($val:ident, $val2: ident) => { + $val $val2 + }; +} + +const BAR: u32 = 9; +fn main() { + foo!(BAR) +} "#, ); }