From b719e211cf6c39f80b8b402e9b46eddedf6b2200 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 7 Mar 2020 16:48:39 +0100 Subject: Fix record literal completion --- .../src/completion/complete_record_literal.rs | 25 ++++++++++++++++++++++ crates/ra_ide/src/completion/completion_context.rs | 11 +++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/completion/complete_record_literal.rs b/crates/ra_ide/src/completion/complete_record_literal.rs index f98353d76..be6e4194f 100644 --- a/crates/ra_ide/src/completion/complete_record_literal.rs +++ b/crates/ra_ide/src/completion/complete_record_literal.rs @@ -153,4 +153,29 @@ mod tests { ] "###); } + + #[test] + fn test_record_literal_field_in_simple_macro() { + let completions = complete( + r" + macro_rules! m { ($e:expr) => { $e } } + struct A { the_field: u32 } + fn foo() { + m!(A { the<|> }) + } + ", + ); + assert_debug_snapshot!(completions, @r###" + [ + CompletionItem { + label: "the_field", + source_range: [137; 140), + delete: [137; 140), + insert: "the_field", + kind: Field, + detail: "u32", + }, + ] + "###); + } } diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 81a033fcb..fe9777487 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -165,7 +165,7 @@ impl<'a> CompletionContext<'a> { self.is_param = true; return; } - self.classify_name_ref(original_file, name_ref); + self.classify_name_ref(original_file, name_ref, offset); } // Otherwise, see if this is a declaration. We can use heuristics to @@ -190,13 +190,18 @@ impl<'a> CompletionContext<'a> { } } - fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameRef) { + fn classify_name_ref( + &mut self, + original_file: &SyntaxNode, + name_ref: ast::NameRef, + offset: TextUnit, + ) { self.name_ref_syntax = find_node_at_offset(&original_file, name_ref.syntax().text_range().start()); let name_range = name_ref.syntax().text_range(); if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() { self.record_lit_syntax = - self.sema.find_node_at_offset_with_macros(&original_file, self.offset); + self.sema.find_node_at_offset_with_macros(&original_file, offset); } self.impl_def = self -- cgit v1.2.3