From 2dbf58c579d6fe6a8acefcd9ae17eef7e984bca1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Jan 2019 16:05:13 +0300 Subject: move completion item tests closer to the code this is the reason why we need marks: the tests were spread across two files, because I've forgotten that there were tests already --- crates/ra_ide_api/src/completion/complete_path.rs | 26 --------- crates/ra_ide_api/src/completion/complete_scope.rs | 17 ------ .../ra_ide_api/src/completion/completion_item.rs | 61 +++++++++++++++++++++- crates/ra_ide_api/src/lib.rs | 3 ++ crates/ra_ide_api/src/marks.rs | 3 ++ 5 files changed, 66 insertions(+), 44 deletions(-) create mode 100644 crates/ra_ide_api/src/marks.rs (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 804954ee1..6bed299d2 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -121,30 +121,4 @@ mod tests { ", ); } - - #[test] - fn dont_render_function_parens_in_use_item() { - check_reference_completion( - "dont_render_function_parens_in_use_item", - " - //- /lib.rs - mod m { pub fn foo() {} } - use crate::m::f<|>; - ", - ) - } - - #[test] - fn dont_render_function_parens_if_already_call() { - check_reference_completion( - "dont_render_function_parens_if_already_call", - " - //- /lib.rs - fn frobnicate() {} - fn main() { - frob<|>(); - } - ", - ) - } } diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index 20fc77f06..f837bb1db 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -175,21 +175,4 @@ mod tests { check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }") } - #[test] - fn inserts_parens_for_function_calls() { - check_reference_completion( - "inserts_parens_for_function_calls1", - r" - fn no_args() {} - fn main() { no_<|> } - ", - ); - check_reference_completion( - "inserts_parens_for_function_calls2", - r" - fn with_args(x: i32, y: String) {} - fn main() { with_<|> } - ", - ); - } } diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 680fd8d1b..48be812a0 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -3,9 +3,10 @@ use hir::PerNs; use crate::completion::completion_context::CompletionContext; use ra_syntax::{ ast::{self, AstNode}, - TextRange + TextRange, }; use ra_text_edit::TextEdit; +use test_utils::tested_by; /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a @@ -255,6 +256,7 @@ impl Builder { ) -> Builder { // If not an import, add parenthesis automatically. if ctx.use_item_syntax.is_none() && !ctx.is_call { + tested_by!(inserts_parens_for_function_calls); if function.signature(ctx.db).params().is_empty() { self.insert_text = Some(format!("{}()$0", self.label)); } else { @@ -344,3 +346,60 @@ pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind .collect(); assert_debug_snapshot_matches!(test_name, kind_completions); } + +#[cfg(test)] +mod tests { + use test_utils::covers; + + use super::*; + + fn check_reference_completion(code: &str, expected_completions: &str) { + check_completion(code, expected_completions, CompletionKind::Reference); + } + + #[test] + fn inserts_parens_for_function_calls() { + covers!(inserts_parens_for_function_calls); + check_reference_completion( + "inserts_parens_for_function_calls1", + r" + fn no_args() {} + fn main() { no_<|> } + ", + ); + check_reference_completion( + "inserts_parens_for_function_calls2", + r" + fn with_args(x: i32, y: String) {} + fn main() { with_<|> } + ", + ); + } + + #[test] + fn dont_render_function_parens_in_use_item() { + check_reference_completion( + "dont_render_function_parens_in_use_item", + " + //- /lib.rs + mod m { pub fn foo() {} } + use crate::m::f<|>; + ", + ) + } + + #[test] + fn dont_render_function_parens_if_already_call() { + check_reference_completion( + "dont_render_function_parens_if_already_call", + " + //- /lib.rs + fn frobnicate() {} + fn main() { + frob<|>(); + } + ", + ) + } + +} diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 3c53e75ac..3502bfd2e 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -26,6 +26,9 @@ mod syntax_highlighting; mod parent_module; mod rename; +#[cfg(test)] +mod marks; + use std::{fmt, sync::Arc}; use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit}; diff --git a/crates/ra_ide_api/src/marks.rs b/crates/ra_ide_api/src/marks.rs new file mode 100644 index 000000000..b4a726ef0 --- /dev/null +++ b/crates/ra_ide_api/src/marks.rs @@ -0,0 +1,3 @@ +use test_utils::mark; + +mark!(inserts_parens_for_function_calls); -- cgit v1.2.3 From 71b9f06c89735b78439d0720e314b71969c17eb3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Jan 2019 16:21:20 +0300 Subject: don't insert parens for self methods --- .../ra_ide_api/src/completion/completion_item.rs | 15 ++++++++++++- ...n_item__inserts_parens_for_function_calls3.snap | 26 ++++++++++++++++++++++ .../completion_item__method_completion.snap | 8 +++---- ...on_item__struct_field_completion_autoderef.snap | 8 +++---- ...pletion_item__struct_field_completion_self.snap | 8 +++---- 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 48be812a0..e3bf82304 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -257,7 +257,8 @@ impl Builder { // If not an import, add parenthesis automatically. if ctx.use_item_syntax.is_none() && !ctx.is_call { tested_by!(inserts_parens_for_function_calls); - if function.signature(ctx.db).params().is_empty() { + let sig = function.signature(ctx.db); + if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 { self.insert_text = Some(format!("{}()$0", self.label)); } else { self.insert_text = Some(format!("{}($0)", self.label)); @@ -374,6 +375,18 @@ mod tests { fn main() { with_<|> } ", ); + check_reference_completion( + "inserts_parens_for_function_calls3", + r" + struct S {} + impl S { + fn foo(&self) {} + } + fn bar(s: &S) { + s.f<|> + } + ", + ) } #[test] diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap new file mode 100644 index 000000000..92068e50a --- /dev/null +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap @@ -0,0 +1,26 @@ +--- +created: "2019-01-23T13:19:23.525922020+00:00" +creator: insta@0.5.2 +expression: kind_completions +source: crates/ra_ide_api/src/completion/completion_item.rs +--- +[ + CompletionItem { + completion_kind: Reference, + label: "foo", + kind: Some( + Method + ), + detail: Some( + "fn foo(&self)" + ), + documentation: None, + lookup: None, + insert_text: Some( + "foo()$0" + ), + insert_text_format: Snippet, + source_range: [139; 140), + text_edit: None + } +] diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap index ffeac0bf4..10fe59248 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap @@ -1,8 +1,8 @@ --- -created: "2019-01-22T15:38:19.541947400+00:00" -creator: insta@0.4.0 +created: "2019-01-23T13:19:23.501258181+00:00" +creator: insta@0.5.2 expression: kind_completions -source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" +source: crates/ra_ide_api/src/completion/completion_item.rs --- [ CompletionItem { @@ -17,7 +17,7 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" documentation: None, lookup: None, insert_text: Some( - "the_method($0)" + "the_method()$0" ), insert_text_format: Snippet, source_range: [144; 144), diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap index 5ef140b28..5f5df0033 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap @@ -1,8 +1,8 @@ --- -created: "2019-01-22T15:38:19.541947400+00:00" -creator: insta@0.4.0 +created: "2019-01-23T13:19:23.501353210+00:00" +creator: insta@0.5.2 expression: kind_completions -source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" +source: crates/ra_ide_api/src/completion/completion_item.rs --- [ CompletionItem { @@ -33,7 +33,7 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" documentation: None, lookup: None, insert_text: Some( - "foo($0)" + "foo()$0" ), insert_text_format: Snippet, source_range: [126; 126), diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap index b7903c5bc..80e8f3df5 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap @@ -1,8 +1,8 @@ --- -created: "2019-01-22T15:38:19.541947400+00:00" -creator: insta@0.4.0 +created: "2019-01-23T13:19:23.501297515+00:00" +creator: insta@0.5.2 expression: kind_completions -source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" +source: crates/ra_ide_api/src/completion/completion_item.rs --- [ CompletionItem { @@ -33,7 +33,7 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" documentation: None, lookup: None, insert_text: Some( - "foo($0)" + "foo()$0" ), insert_text_format: Snippet, source_range: [121; 121), -- cgit v1.2.3