diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index a27e0fc159..93e53c9210 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs @@ -16,11 +16,11 @@ mod complete_scope; mod complete_postfix; mod complete_macro_in_item_position; mod complete_trait_impl; +#[cfg(test)] +mod test_utils; use ra_ide_db::RootDatabase; -#[cfg(test)] -use crate::completion::completion_item::do_completion; use crate::{ completion::{ completion_context::CompletionContext, diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index d8f6f0d9d8..81e5037aa1 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 5024587065..9226ac0551 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 e1c0ffb1fb..1e053ea4a6 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 1866d9e6c6..270e96df0d 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 3c4a705612..d588ee3641 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 fa8aecedad..6a1a66ef10 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 6d000548d7..0ba382165b 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 be6e4194fc..83ed1d52c7 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 687c57d3ed..962376428c 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 eb3c8cf1b6..bd4adf23af 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 731b4fd82d..f731e9b9aa 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 2bf654a57c..7fefa2c7a8 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 1d14e96362..ef0eb43b22 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,24 +318,3 @@ impl Into> for Completions { self.buf } } - -#[cfg(test)] -pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec { - use crate::{ - completion::{completions, CompletionOptions}, - 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 options = CompletionOptions::default(); - 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 3dc56e4a3d..5213def200 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::{ + test_utils::{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!( 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 0000000000..136857315c --- /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 +}