diff options
Diffstat (limited to 'crates/ide_completion/src/context.rs')
-rw-r--r-- | crates/ide_completion/src/context.rs | 72 |
1 files changed, 54 insertions, 18 deletions
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index e49e434fa..f0da98739 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -286,8 +286,11 @@ impl<'a> CompletionContext<'a> { | |||
286 | ) | 286 | ) |
287 | } | 287 | } |
288 | 288 | ||
289 | pub(crate) fn expect_record_field(&self) -> bool { | 289 | pub(crate) fn expect_field(&self) -> bool { |
290 | matches!(self.completion_location, Some(ImmediateLocation::RecordField)) | 290 | matches!( |
291 | self.completion_location, | ||
292 | Some(ImmediateLocation::RecordField | ImmediateLocation::TupleField) | ||
293 | ) | ||
291 | } | 294 | } |
292 | 295 | ||
293 | pub(crate) fn in_use_tree(&self) -> bool { | 296 | pub(crate) fn in_use_tree(&self) -> bool { |
@@ -385,14 +388,19 @@ impl<'a> CompletionContext<'a> { | |||
385 | (ty, name) | 388 | (ty, name) |
386 | }, | 389 | }, |
387 | ast::ArgList(_it) => { | 390 | ast::ArgList(_it) => { |
388 | cov_mark::hit!(expected_type_fn_param_with_leading_char); | 391 | cov_mark::hit!(expected_type_fn_param); |
389 | cov_mark::hit!(expected_type_fn_param_without_leading_char); | ||
390 | ActiveParameter::at_token( | 392 | ActiveParameter::at_token( |
391 | &self.sema, | 393 | &self.sema, |
392 | self.token.clone(), | 394 | self.token.clone(), |
393 | ).map(|ap| { | 395 | ).map(|ap| { |
394 | let name = ap.ident().map(NameOrNameRef::Name); | 396 | let name = ap.ident().map(NameOrNameRef::Name); |
395 | (Some(ap.ty), name) | 397 | let ty = if has_ref(&self.token) { |
398 | cov_mark::hit!(expected_type_fn_param_ref); | ||
399 | ap.ty.remove_ref() | ||
400 | } else { | ||
401 | Some(ap.ty) | ||
402 | }; | ||
403 | (ty, name) | ||
396 | }) | 404 | }) |
397 | .unwrap_or((None, None)) | 405 | .unwrap_or((None, None)) |
398 | }, | 406 | }, |
@@ -697,6 +705,19 @@ fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> { | |||
697 | use_tree.path().zip(Some(true)) | 705 | use_tree.path().zip(Some(true)) |
698 | } | 706 | } |
699 | 707 | ||
708 | fn has_ref(token: &SyntaxToken) -> bool { | ||
709 | let mut token = token.clone(); | ||
710 | for skip in [WHITESPACE, IDENT, T![mut]] { | ||
711 | if token.kind() == skip { | ||
712 | token = match token.prev_token() { | ||
713 | Some(it) => it, | ||
714 | None => return false, | ||
715 | } | ||
716 | } | ||
717 | } | ||
718 | token.kind() == T![&] | ||
719 | } | ||
720 | |||
700 | #[cfg(test)] | 721 | #[cfg(test)] |
701 | mod tests { | 722 | mod tests { |
702 | use expect_test::{expect, Expect}; | 723 | use expect_test::{expect, Expect}; |
@@ -769,14 +790,18 @@ fn foo() { | |||
769 | } | 790 | } |
770 | 791 | ||
771 | #[test] | 792 | #[test] |
772 | fn expected_type_fn_param_without_leading_char() { | 793 | fn expected_type_fn_param() { |
773 | cov_mark::check!(expected_type_fn_param_without_leading_char); | 794 | cov_mark::check!(expected_type_fn_param); |
774 | check_expected_type_and_name( | 795 | check_expected_type_and_name( |
775 | r#" | 796 | r#" |
776 | fn foo() { | 797 | fn foo() { bar($0); } |
777 | bar($0); | 798 | fn bar(x: u32) {} |
778 | } | 799 | "#, |
779 | 800 | expect![[r#"ty: u32, name: x"#]], | |
801 | ); | ||
802 | check_expected_type_and_name( | ||
803 | r#" | ||
804 | fn foo() { bar(c$0); } | ||
780 | fn bar(x: u32) {} | 805 | fn bar(x: u32) {} |
781 | "#, | 806 | "#, |
782 | expect![[r#"ty: u32, name: x"#]], | 807 | expect![[r#"ty: u32, name: x"#]], |
@@ -784,18 +809,29 @@ fn bar(x: u32) {} | |||
784 | } | 809 | } |
785 | 810 | ||
786 | #[test] | 811 | #[test] |
787 | fn expected_type_fn_param_with_leading_char() { | 812 | fn expected_type_fn_param_ref() { |
788 | cov_mark::check!(expected_type_fn_param_with_leading_char); | 813 | cov_mark::check!(expected_type_fn_param_ref); |
789 | check_expected_type_and_name( | 814 | check_expected_type_and_name( |
790 | r#" | 815 | r#" |
791 | fn foo() { | 816 | fn foo() { bar(&$0); } |
792 | bar(c$0); | 817 | fn bar(x: &u32) {} |
793 | } | ||
794 | |||
795 | fn bar(x: u32) {} | ||
796 | "#, | 818 | "#, |
797 | expect![[r#"ty: u32, name: x"#]], | 819 | expect![[r#"ty: u32, name: x"#]], |
798 | ); | 820 | ); |
821 | check_expected_type_and_name( | ||
822 | r#" | ||
823 | fn foo() { bar(&mut $0); } | ||
824 | fn bar(x: &mut u32) {} | ||
825 | "#, | ||
826 | expect![[r#"ty: u32, name: x"#]], | ||
827 | ); | ||
828 | check_expected_type_and_name( | ||
829 | r#" | ||
830 | fn foo() { bar(&c$0); } | ||
831 | fn bar(x: &u32) {} | ||
832 | "#, | ||
833 | expect![[r#"ty: u32, name: x"#]], | ||
834 | ); | ||
799 | } | 835 | } |
800 | 836 | ||
801 | #[test] | 837 | #[test] |