From b973158aeb337041d4e1434cf5d8c609a0b02bef Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 13 Mar 2020 13:03:31 +0100 Subject: Make MBE expansion more resilient (WIP) --- crates/ra_ide/src/completion/complete_dot.rs | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index f07611d88..a30d1c2de 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -751,6 +751,43 @@ mod tests { ); } + #[test] + fn macro_expansion_resilient() { + assert_debug_snapshot!( + do_ref_completion( + r" + macro_rules! dbg { + () => {}; + ($val:expr) => { + match $val { tmp => { tmp } } + }; + // Trailing comma with single argument is ignored + ($val:expr,) => { $crate::dbg!($val) }; + ($($val:expr),+ $(,)?) => { + ($($crate::dbg!($val)),+,) + }; + } + struct A { the_field: u32 } + fn foo(a: A) { + dbg!(a.<|>) + } + ", + ), + @r###" + [ + CompletionItem { + label: "the_field", + source_range: [552; 553), + delete: [552; 553), + insert: "the_field", + kind: Field, + detail: "u32", + }, + ] + "### + ); + } + #[test] fn test_method_completion_3547() { assert_debug_snapshot!( -- cgit v1.2.3 From 6305d094ac61ed6e437537b93f4e587b415678c9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 13 Mar 2020 15:18:17 +0100 Subject: Attempt to implement ranking of rules when none matches perfectly (wip) --- crates/ra_ide/src/completion/complete_scope.rs | 39 +++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index 5ffff5a1c..2733922f8 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -811,7 +811,44 @@ mod tests { } " ), - @"[]" + @r###" + [ + CompletionItem { + label: "m!", + source_range: [145; 145), + delete: [145; 145), + insert: "m!($0)", + kind: Macro, + detail: "macro_rules! m", + }, + CompletionItem { + label: "quux(…)", + source_range: [145; 145), + delete: [145; 145), + insert: "quux(${1:x})$0", + kind: Function, + lookup: "quux", + detail: "fn quux(x: i32)", + trigger_call_info: true, + }, + CompletionItem { + label: "x", + source_range: [145; 145), + delete: [145; 145), + insert: "x", + kind: Binding, + detail: "i32", + }, + CompletionItem { + label: "y", + source_range: [145; 145), + delete: [145; 145), + insert: "y", + kind: Binding, + detail: "i32", + }, + ] + "### ); } -- cgit v1.2.3 From 0f3a54dd4d439a6598526144c4ecccee9c5f1362 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 14 Mar 2020 20:24:18 +0100 Subject: wip --- crates/ra_ide/src/completion/complete_dot.rs | 4 ++-- crates/ra_ide/src/completion/complete_pattern.rs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index a30d1c2de..22f5077f5 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -777,8 +777,8 @@ mod tests { [ CompletionItem { label: "the_field", - source_range: [552; 553), - delete: [552; 553), + source_range: [552; 552), + delete: [552; 552), insert: "the_field", kind: Field, detail: "u32", diff --git a/crates/ra_ide/src/completion/complete_pattern.rs b/crates/ra_ide/src/completion/complete_pattern.rs index 6a1a66ef1..cb84bb934 100644 --- a/crates/ra_ide/src/completion/complete_pattern.rs +++ b/crates/ra_ide/src/completion/complete_pattern.rs @@ -89,7 +89,6 @@ mod tests { #[test] fn completes_in_simple_macro_call() { - // FIXME: doesn't work yet because of missing error recovery in macro expansion let completions = complete( r" macro_rules! m { ($e:expr) => { $e } } @@ -102,6 +101,16 @@ mod tests { } ", ); - assert_debug_snapshot!(completions, @r###"[]"###); + assert_debug_snapshot!(completions, @r###" + [ + CompletionItem { + label: "E", + source_range: [151; 151), + delete: [151; 151), + insert: "E", + kind: Enum, + }, + ] + "###); } } -- cgit v1.2.3 From c32529ddd0d66a219226dd63da2d4b1825375c0e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 15 Mar 2020 11:17:13 +0100 Subject: Get tests working --- crates/ra_ide/src/completion/complete_dot.rs | 13 ++++++++++++- crates/ra_ide/src/completion/completion_context.rs | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index 22f5077f5..82ec16913 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -720,7 +720,18 @@ mod tests { } ", ), - @r###"[]"### + @r###" + [ + CompletionItem { + label: "the_field", + source_range: [156; 156), + delete: [156; 156), + 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 3646fb8dc..54589a2a8 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -135,7 +135,7 @@ impl<'a> CompletionContext<'a> { ), ) { let new_offset = hypothetical_expansion.1.text_range().start(); - if new_offset >= actual_expansion.text_range().end() { + if new_offset > actual_expansion.text_range().end() { break; } original_file = actual_expansion; -- cgit v1.2.3 From 035db0fbb94e35c78cc76798844db06bd6ad8451 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 15 Mar 2020 11:26:54 +0100 Subject: Add test, remove printlns --- crates/ra_ide/src/completion/complete_scope.rs | 53 ++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index 2733922f8..81d3cc1b6 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -905,6 +905,59 @@ mod tests { ); } + #[test] + fn completes_in_simple_macro_without_closing_parens() { + assert_debug_snapshot!( + do_reference_completion( + r" + macro_rules! m { ($e:expr) => { $e } } + fn quux(x: i32) { + let y = 92; + m!(x<|> + } + " + ), + @r###" + [ + CompletionItem { + label: "m!", + source_range: [145; 146), + delete: [145; 146), + insert: "m!($0)", + kind: Macro, + detail: "macro_rules! m", + }, + CompletionItem { + label: "quux(…)", + source_range: [145; 146), + delete: [145; 146), + insert: "quux(${1:x})$0", + kind: Function, + lookup: "quux", + detail: "fn quux(x: i32)", + trigger_call_info: true, + }, + CompletionItem { + label: "x", + source_range: [145; 146), + delete: [145; 146), + insert: "x", + kind: Binding, + detail: "i32", + }, + CompletionItem { + label: "y", + source_range: [145; 146), + delete: [145; 146), + insert: "y", + kind: Binding, + detail: "i32", + }, + ] + "### + ); + } + #[test] fn completes_unresolved_uses() { assert_debug_snapshot!( -- cgit v1.2.3