diff options
author | Aleksey Kladov <[email protected]> | 2020-07-14 12:51:43 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-14 12:51:43 +0100 |
commit | f7f4ea633b974cf90fb69120ff23908990a24bda (patch) | |
tree | 29f9903134d9fe820e3f9afa44ad8b237298bf24 /crates | |
parent | f823386db85ec1f0ee973b9c0534a9902dbcc2e2 (diff) |
Don't duplicate parens in patterns
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide/src/completion/completion_context.rs | 5 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/presentation.rs | 33 |
2 files changed, 37 insertions, 1 deletions
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 3d93f7067..9e82d6854 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs | |||
@@ -63,6 +63,8 @@ pub(crate) struct CompletionContext<'a> { | |||
63 | pub(super) dot_receiver_is_ambiguous_float_literal: bool, | 63 | pub(super) dot_receiver_is_ambiguous_float_literal: bool, |
64 | /// If this is a call (method or function) in particular, i.e. the () are already there. | 64 | /// If this is a call (method or function) in particular, i.e. the () are already there. |
65 | pub(super) is_call: bool, | 65 | pub(super) is_call: bool, |
66 | /// Like `is_call`, but for tuple patterns. | ||
67 | pub(super) is_pattern_call: bool, | ||
66 | /// If this is a macro call, i.e. the () are already there. | 68 | /// If this is a macro call, i.e. the () are already there. |
67 | pub(super) is_macro_call: bool, | 69 | pub(super) is_macro_call: bool, |
68 | pub(super) is_path_type: bool, | 70 | pub(super) is_path_type: bool, |
@@ -136,6 +138,7 @@ impl<'a> CompletionContext<'a> { | |||
136 | is_new_item: false, | 138 | is_new_item: false, |
137 | dot_receiver: None, | 139 | dot_receiver: None, |
138 | is_call: false, | 140 | is_call: false, |
141 | is_pattern_call: false, | ||
139 | is_macro_call: false, | 142 | is_macro_call: false, |
140 | is_path_type: false, | 143 | is_path_type: false, |
141 | has_type_args: false, | 144 | has_type_args: false, |
@@ -370,6 +373,8 @@ impl<'a> CompletionContext<'a> { | |||
370 | .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) | 373 | .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) |
371 | .is_some(); | 374 | .is_some(); |
372 | self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some(); | 375 | self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some(); |
376 | self.is_pattern_call = | ||
377 | path.syntax().parent().and_then(ast::TupleStructPat::cast).is_some(); | ||
373 | 378 | ||
374 | self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); | 379 | self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); |
375 | self.has_type_args = segment.type_arg_list().is_some(); | 380 | self.has_type_args = segment.type_arg_list().is_some(); |
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index e23971526..64349dcb8 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs | |||
@@ -384,10 +384,17 @@ impl Builder { | |||
384 | if !ctx.config.add_call_parenthesis { | 384 | if !ctx.config.add_call_parenthesis { |
385 | return self; | 385 | return self; |
386 | } | 386 | } |
387 | if ctx.use_item_syntax.is_some() || ctx.is_call { | 387 | if ctx.use_item_syntax.is_some() { |
388 | mark::hit!(no_parens_in_use_item); | 388 | mark::hit!(no_parens_in_use_item); |
389 | return self; | 389 | return self; |
390 | } | 390 | } |
391 | if ctx.is_pattern_call { | ||
392 | mark::hit!(dont_duplicate_pattern_parens); | ||
393 | return self; | ||
394 | } | ||
395 | if ctx.is_call { | ||
396 | return self; | ||
397 | } | ||
391 | 398 | ||
392 | // Don't add parentheses if the expected type is some function reference. | 399 | // Don't add parentheses if the expected type is some function reference. |
393 | if let Some(ty) = &ctx.expected_type { | 400 | if let Some(ty) = &ctx.expected_type { |
@@ -908,6 +915,30 @@ fn main(value: Option<i32>) { | |||
908 | } | 915 | } |
909 | 916 | ||
910 | #[test] | 917 | #[test] |
918 | fn dont_duplicate_pattern_parens() { | ||
919 | mark::check!(dont_duplicate_pattern_parens); | ||
920 | check_edit( | ||
921 | "Var", | ||
922 | r#" | ||
923 | enum E { Var(i32) } | ||
924 | fn main() { | ||
925 | match E::Var(92) { | ||
926 | E::<|>(92) => (), | ||
927 | } | ||
928 | } | ||
929 | "#, | ||
930 | r#" | ||
931 | enum E { Var(i32) } | ||
932 | fn main() { | ||
933 | match E::Var(92) { | ||
934 | E::Var(92) => (), | ||
935 | } | ||
936 | } | ||
937 | "#, | ||
938 | ); | ||
939 | } | ||
940 | |||
941 | #[test] | ||
911 | fn no_call_parens_if_fn_ptr_needed() { | 942 | fn no_call_parens_if_fn_ptr_needed() { |
912 | mark::check!(no_call_parens_if_fn_ptr_needed); | 943 | mark::check!(no_call_parens_if_fn_ptr_needed); |
913 | check_edit( | 944 | check_edit( |