diff --git a/crates/ide_completion/src/completions/fn_param.rs b/crates/ide_completion/src/completions/fn_param.rs index d299f2e263..80ad3b9c8f 100644 --- a/crates/ide_completion/src/completions/fn_param.rs +++ b/crates/ide_completion/src/completions/fn_param.rs @@ -75,124 +75,3 @@ fn add_new_item_to_acc( item.kind(CompletionItemKind::Binding).lookup_by(lookup); item.add_to(acc) } - -#[cfg(test)] -mod tests { - use expect_test::{expect, Expect}; - - use crate::{tests::filtered_completion_list, CompletionKind}; - - fn check(ra_fixture: &str, expect: Expect) { - let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic); - expect.assert_eq(&actual); - } - - #[test] - fn test_param_completion_last_param() { - check( - r#" -fn foo(file_id: FileId) {} -fn bar(file_id: FileId) {} -fn baz(file$0) {} -"#, - expect![[r#" - bn file_id: FileId - "#]], - ); - } - - #[test] - fn test_param_completion_first_param() { - check( - r#" -fn foo(file_id: FileId) {} -fn bar(file_id: FileId) {} -fn baz(file$0 id: u32) {} -"#, - expect![[r#" - bn file_id: FileId - "#]], - ); - } - - #[test] - fn test_param_completion_nth_param() { - check( - r#" -fn foo(file_id: FileId) {} -fn baz(file$0, x: i32) {} -"#, - expect![[r#" - bn file_id: FileId - "#]], - ); - } - - #[test] - fn test_param_completion_trait_param() { - check( - r#" -pub(crate) trait SourceRoot { - pub fn contains(&self, file_id: FileId) -> bool; - pub fn module_map(&self) -> &ModuleMap; - pub fn lines(&self, file_id: FileId) -> &LineIndex; - pub fn syntax(&self, file$0) -} -"#, - expect![[r#" - bn file_id: FileId - "#]], - ); - } - - #[test] - fn completes_param_in_inner_function() { - check( - r#" -fn outer(text: String) { - fn inner($0) -} -"#, - expect![[r#" - bn text: String - "#]], - ) - } - - #[test] - fn completes_non_ident_pat_param() { - check( - r#" -struct Bar { bar: u32 } - -fn foo(Bar { bar }: Bar) {} -fn foo2($0) {} -"#, - expect![[r#" - bn Bar { bar }: Bar - "#]], - ) - } - - #[test] - fn test_param_completion_self_param() { - check( - r#" - struct A {} - - impl A { - fn foo(file_id: FileId) {} - fn new($0) { - } - } - "#, - expect![[r#" - bn self - bn &self - bn mut self - bn &mut self - bn file_id: FileId - "#]], - ) - } -} diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs index 3e65c65963..0b0c828617 100644 --- a/crates/ide_completion/src/tests.rs +++ b/crates/ide_completion/src/tests.rs @@ -5,6 +5,7 @@ //! Notable examples for completions that are being tested in this module's submodule are paths. mod attribute; +mod fn_param; mod item_list; mod item; mod pattern; diff --git a/crates/ide_completion/src/tests/fn_param.rs b/crates/ide_completion/src/tests/fn_param.rs new file mode 100644 index 0000000000..8a07aefafe --- /dev/null +++ b/crates/ide_completion/src/tests/fn_param.rs @@ -0,0 +1,150 @@ +use expect_test::{expect, Expect}; + +use crate::tests::completion_list; + +fn check(ra_fixture: &str, expect: Expect) { + let actual = completion_list(ra_fixture); + expect.assert_eq(&actual); +} + +#[test] +fn only_param() { + check( + r#" +fn foo(file_id: usize) {} +fn bar(file_id: usize) {} +fn baz(file$0) {} +"#, + expect![[r#" + bn file_id: usize + kw mut + "#]], + ); +} + +#[test] +fn last_param() { + check( + r#" +fn foo(file_id: usize) {} +fn bar(file_id: usize) {} +fn baz(foo: (), file$0) {} +"#, + expect![[r#" + bn file_id: usize + kw mut + "#]], + ); +} + +#[test] +fn first_param() { + check( + r#" +fn foo(file_id: usize) {} +fn bar(file_id: usize) {} +fn baz(file$0 id: u32) {} +"#, + expect![[r#" + bn file_id: usize + kw mut + "#]], + ); +} + +#[test] +fn trait_param() { + check( + r#" +pub(crate) trait SourceRoot { + pub fn contains(file_id: usize) -> bool; + pub fn syntax(file$0) +} +"#, + expect![[r#" + bn file_id: usize + kw mut + "#]], + ); +} + +#[test] +fn in_inner_function() { + check( + r#" +fn outer(text: &str) { + fn inner($0) +} +"#, + expect![[r#" + bn text: &str + kw mut + "#]], + ) +} + +#[test] +fn shows_non_ident_pat_param() { + check( + r#" +struct Bar { bar: u32 } +fn foo(Bar { bar }: Bar) {} +fn foo2($0) {} +"#, + expect![[r#" + bn Bar { bar }: Bar + kw mut + bn Bar Bar { bar$1 }: Bar$0 + st Bar + "#]], + ) +} + +#[test] +fn in_impl_only_param() { + check( + r#" +struct A {} + +impl A { + fn foo(file_id: usize) {} + fn new($0) {} +} +"#, + expect![[r#" + bn self + bn &self + bn mut self + bn &mut self + bn file_id: usize + kw mut + sp Self + st A + "#]], + ) +} + +#[test] +fn in_impl_after_self() { + // FIXME: self completions should not be here + check( + r#" +struct A {} + +impl A { + fn foo(file_id: usize) {} + fn new(self, $0) {} +} +"#, + expect![[r#" + bn self + bn &self + bn mut self + bn &mut self + bn file_id: usize + kw mut + sp Self + st A + "#]], + ) +} diff --git a/crates/ide_db/src/call_info.rs b/crates/ide_db/src/call_info.rs index da3b9bf1e9..4795e25650 100644 --- a/crates/ide_db/src/call_info.rs +++ b/crates/ide_db/src/call_info.rs @@ -166,7 +166,7 @@ impl ActiveParameter { let idx = active_parameter?; let mut params = signature.params(sema.db); - if params.len() <= idx { + if !(idx < params.len()) { cov_mark::hit!(too_many_arguments); return None; }