Merge pull request #5370 from matklad/parens

Don't duplicate parens in patterns
This commit is contained in:
Aleksey Kladov 2020-07-14 14:39:46 +02:00 committed by GitHub
commit 92c5036a7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View file

@ -63,6 +63,8 @@ pub(crate) struct CompletionContext<'a> {
pub(super) dot_receiver_is_ambiguous_float_literal: bool,
/// If this is a call (method or function) in particular, i.e. the () are already there.
pub(super) is_call: bool,
/// Like `is_call`, but for tuple patterns.
pub(super) is_pattern_call: bool,
/// If this is a macro call, i.e. the () are already there.
pub(super) is_macro_call: bool,
pub(super) is_path_type: bool,
@ -136,6 +138,7 @@ impl<'a> CompletionContext<'a> {
is_new_item: false,
dot_receiver: None,
is_call: false,
is_pattern_call: false,
is_macro_call: false,
is_path_type: false,
has_type_args: false,
@ -370,6 +373,8 @@ impl<'a> CompletionContext<'a> {
.and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
.is_some();
self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some();
self.is_pattern_call =
path.syntax().parent().and_then(ast::TupleStructPat::cast).is_some();
self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
self.has_type_args = segment.type_arg_list().is_some();

View file

@ -315,6 +315,7 @@ impl Completions {
}
if variant_kind == StructKind::Tuple {
mark::hit!(inserts_parens_for_tuple_enums);
let params = Params::Anonymous(variant.fields(ctx.db).len());
res = res.add_call_parens(ctx, qualified_name, params)
}
@ -383,10 +384,17 @@ impl Builder {
if !ctx.config.add_call_parenthesis {
return self;
}
if ctx.use_item_syntax.is_some() || ctx.is_call {
if ctx.use_item_syntax.is_some() {
mark::hit!(no_parens_in_use_item);
return self;
}
if ctx.is_pattern_call {
mark::hit!(dont_duplicate_pattern_parens);
return self;
}
if ctx.is_call {
return self;
}
// Don't add parentheses if the expected type is some function reference.
if let Some(ty) = &ctx.expected_type {
@ -865,6 +873,7 @@ fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 }
#[test]
fn inserts_parens_for_tuple_enums() {
mark::check!(inserts_parens_for_tuple_enums);
check_edit(
"Some",
r#"
@ -905,6 +914,30 @@ fn main(value: Option<i32>) {
);
}
#[test]
fn dont_duplicate_pattern_parens() {
mark::check!(dont_duplicate_pattern_parens);
check_edit(
"Var",
r#"
enum E { Var(i32) }
fn main() {
match E::Var(92) {
E::<|>(92) => (),
}
}
"#,
r#"
enum E { Var(i32) }
fn main() {
match E::Var(92) {
E::Var(92) => (),
}
}
"#,
);
}
#[test]
fn no_call_parens_if_fn_ptr_needed() {
mark::check!(no_call_parens_if_fn_ptr_needed);