From 9b0b59c5d2eb1eee8ef16118ddd20ad1302f2c62 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 11 Mar 2020 10:39:39 +0100 Subject: Add a test for disabled argument snippets --- crates/ra_ide/src/completion/completion_item.rs | 12 +++++-- crates/ra_ide/src/completion/presentation.rs | 48 +++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs index 1d14e9636..ef6848607 100644 --- a/crates/ra_ide/src/completion/completion_item.rs +++ b/crates/ra_ide/src/completion/completion_item.rs @@ -321,8 +321,17 @@ impl Into> for Completions { #[cfg(test)] pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec { + do_completion_with_options(code, kind, &crate::completion::CompletionOptions::default()) +} + +#[cfg(test)] +pub(crate) fn do_completion_with_options( + code: &str, + kind: CompletionKind, + options: &crate::completion::CompletionOptions, +) -> Vec { use crate::{ - completion::{completions, CompletionOptions}, + completion::completions, mock_analysis::{analysis_and_position, single_file_with_position}, }; @@ -331,7 +340,6 @@ pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec = completions.into(); let mut kind_completions: Vec = diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 3dc56e4a3..5fc8b483c 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -307,12 +307,22 @@ mod tests { use insta::assert_debug_snapshot; use test_utils::covers; - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{ + do_completion, do_completion_with_options, CompletionItem, CompletionKind, + CompletionOptions, + }; fn do_reference_completion(ra_fixture: &str) -> Vec { do_completion(ra_fixture, CompletionKind::Reference) } + fn do_reference_completion_with_options( + ra_fixture: &str, + options: CompletionOptions, + ) -> Vec { + do_completion_with_options(ra_fixture, CompletionKind::Reference, &options) + } + #[test] fn enum_detail_includes_names_for_record() { assert_debug_snapshot!( @@ -533,7 +543,7 @@ mod tests { } #[test] - fn parens_for_method_call() { + fn arg_snippets_for_method_call() { assert_debug_snapshot!( do_reference_completion( r" @@ -562,6 +572,40 @@ mod tests { ) } + #[test] + fn no_arg_snippets_for_method_call() { + assert_debug_snapshot!( + do_reference_completion_with_options( + r" + struct S {} + impl S { + fn foo(&self, x: i32) {} + } + fn bar(s: &S) { + s.f<|> + } + ", + CompletionOptions { + add_call_argument_snippets: false, + .. Default::default() + } + ), + @r###" + [ + CompletionItem { + label: "foo(…)", + source_range: [171; 172), + delete: [171; 172), + insert: "foo($0)", + kind: Method, + lookup: "foo", + detail: "fn foo(&self, x: i32)", + }, + ] + "### + ) + } + #[test] fn dont_render_function_parens_in_use_item() { assert_debug_snapshot!( -- cgit v1.2.3 From dad8f1e064eda525b5b98750ac4b28e5c34b0726 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 11 Mar 2020 10:46:43 +0100 Subject: Introduce completion test utils --- crates/ra_ide/src/completion/complete_dot.rs | 2 +- crates/ra_ide/src/completion/complete_fn_param.rs | 2 +- crates/ra_ide/src/completion/complete_keyword.rs | 2 +- .../completion/complete_macro_in_item_position.rs | 3 ++- crates/ra_ide/src/completion/complete_path.rs | 2 +- crates/ra_ide/src/completion/complete_pattern.rs | 2 +- crates/ra_ide/src/completion/complete_postfix.rs | 2 +- .../src/completion/complete_record_literal.rs | 2 +- .../src/completion/complete_record_pattern.rs | 2 +- crates/ra_ide/src/completion/complete_scope.rs | 6 ++--- crates/ra_ide/src/completion/complete_snippet.rs | 2 +- .../ra_ide/src/completion/complete_trait_impl.rs | 2 +- crates/ra_ide/src/completion/completion_item.rs | 31 +--------------------- crates/ra_ide/src/completion/presentation.rs | 4 +-- crates/ra_ide/src/completion/test_utils.rs | 29 ++++++++++++++++++++ 15 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 crates/ra_ide/src/completion/test_utils.rs (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 d8f6f0d9d..81e5037aa 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -70,7 +70,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn do_ref_completion(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_fn_param.rs b/crates/ra_ide/src/completion/complete_fn_param.rs index 502458706..9226ac055 100644 --- a/crates/ra_ide/src/completion/complete_fn_param.rs +++ b/crates/ra_ide/src/completion/complete_fn_param.rs @@ -52,7 +52,7 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn do_magic_completion(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs index e1c0ffb1f..1e053ea4a 100644 --- a/crates/ra_ide/src/completion/complete_keyword.rs +++ b/crates/ra_ide/src/completion/complete_keyword.rs @@ -117,7 +117,7 @@ fn complete_return( #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn do_keyword_completion(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs index 1866d9e6c..270e96df0 100644 --- a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs +++ b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs @@ -15,9 +15,10 @@ pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; + fn do_reference_completion(code: &str) -> Vec { do_completion(code, CompletionKind::Reference) } diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs index 3c4a70561..d588ee364 100644 --- a/crates/ra_ide/src/completion/complete_path.rs +++ b/crates/ra_ide/src/completion/complete_path.rs @@ -103,7 +103,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { mod tests { use test_utils::covers; - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn do_reference_completion(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_pattern.rs b/crates/ra_ide/src/completion/complete_pattern.rs index fa8aeceda..6a1a66ef1 100644 --- a/crates/ra_ide/src/completion/complete_pattern.rs +++ b/crates/ra_ide/src/completion/complete_pattern.rs @@ -27,7 +27,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn complete(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs index 6d000548d..0ba382165 100644 --- a/crates/ra_ide/src/completion/complete_postfix.rs +++ b/crates/ra_ide/src/completion/complete_postfix.rs @@ -81,7 +81,7 @@ fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: mod tests { use insta::assert_debug_snapshot; - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; fn do_postfix_completion(code: &str) -> Vec { do_completion(code, CompletionKind::Postfix) diff --git a/crates/ra_ide/src/completion/complete_record_literal.rs b/crates/ra_ide/src/completion/complete_record_literal.rs index be6e4194f..83ed1d52c 100644 --- a/crates/ra_ide/src/completion/complete_record_literal.rs +++ b/crates/ra_ide/src/completion/complete_record_literal.rs @@ -18,7 +18,7 @@ pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionCon #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn complete(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_record_pattern.rs b/crates/ra_ide/src/completion/complete_record_pattern.rs index 687c57d3e..962376428 100644 --- a/crates/ra_ide/src/completion/complete_record_pattern.rs +++ b/crates/ra_ide/src/completion/complete_record_pattern.rs @@ -17,7 +17,7 @@ pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionCon #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn complete(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index eb3c8cf1b..bd4adf23a 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -14,10 +14,10 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { mod tests { use insta::assert_debug_snapshot; - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; - fn do_reference_completion(code: &str) -> Vec { - do_completion(code, CompletionKind::Reference) + fn do_reference_completion(ra_fixture: &str) -> Vec { + do_completion(ra_fixture, CompletionKind::Reference) } #[test] diff --git a/crates/ra_ide/src/completion/complete_snippet.rs b/crates/ra_ide/src/completion/complete_snippet.rs index 731b4fd82..f731e9b9a 100644 --- a/crates/ra_ide/src/completion/complete_snippet.rs +++ b/crates/ra_ide/src/completion/complete_snippet.rs @@ -42,7 +42,7 @@ fn ${1:feature}() { #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn do_snippet_completion(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index 2bf654a57..7fefa2c7a 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -217,7 +217,7 @@ fn make_const_compl_syntax(const_: &ast::ConstDef) -> String { #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; fn complete(code: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs index ef6848607..ef0eb43b2 100644 --- a/crates/ra_ide/src/completion/completion_item.rs +++ b/crates/ra_ide/src/completion/completion_item.rs @@ -13,7 +13,7 @@ pub struct CompletionItem { /// Used only internally in tests, to check only specific kind of /// completion (postfix, keyword, reference, etc). #[allow(unused)] - completion_kind: CompletionKind, + pub(crate) completion_kind: CompletionKind, /// Label in the completion pop up which identifies completion. label: String, /// Range of identifier that is being completed. @@ -318,32 +318,3 @@ impl Into> for Completions { self.buf } } - -#[cfg(test)] -pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec { - do_completion_with_options(code, kind, &crate::completion::CompletionOptions::default()) -} - -#[cfg(test)] -pub(crate) fn do_completion_with_options( - code: &str, - kind: CompletionKind, - options: &crate::completion::CompletionOptions, -) -> Vec { - use crate::{ - completion::completions, - mock_analysis::{analysis_and_position, single_file_with_position}, - }; - - let (analysis, position) = if code.contains("//-") { - analysis_and_position(code) - } else { - single_file_with_position(code) - }; - let completions = completions(&analysis.db, position, &options).unwrap(); - let completion_items: Vec = completions.into(); - let mut kind_completions: Vec = - completion_items.into_iter().filter(|c| c.completion_kind == kind).collect(); - kind_completions.sort_by_key(|c| c.label.clone()); - kind_completions -} diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 5fc8b483c..5213def20 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -308,8 +308,8 @@ mod tests { use test_utils::covers; use crate::completion::{ - do_completion, do_completion_with_options, CompletionItem, CompletionKind, - CompletionOptions, + test_utils::{do_completion, do_completion_with_options}, + CompletionItem, CompletionKind, CompletionOptions, }; fn do_reference_completion(ra_fixture: &str) -> Vec { diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs new file mode 100644 index 000000000..136857315 --- /dev/null +++ b/crates/ra_ide/src/completion/test_utils.rs @@ -0,0 +1,29 @@ +//! Runs completion for testing purposes. + +use crate::{ + completion::{completion_item::CompletionKind, CompletionOptions}, + mock_analysis::{analysis_and_position, single_file_with_position}, + CompletionItem, +}; + +pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec { + do_completion_with_options(code, kind, &CompletionOptions::default()) +} + +pub(crate) fn do_completion_with_options( + code: &str, + kind: CompletionKind, + options: &CompletionOptions, +) -> Vec { + let (analysis, position) = if code.contains("//-") { + analysis_and_position(code) + } else { + single_file_with_position(code) + }; + let completions = analysis.completions(position, options).unwrap().unwrap(); + let completion_items: Vec = completions.into(); + let mut kind_completions: Vec = + completion_items.into_iter().filter(|c| c.completion_kind == kind).collect(); + kind_completions.sort_by_key(|c| c.label().to_owned()); + kind_completions +} -- cgit v1.2.3