From 1bf1b64cbbc5e70fe46de4aa2e18ec3885003825 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 2 Apr 2019 23:03:40 +0200 Subject: [PATCH] Format `{}` property pattern (#34619) --- .../AutomaticBraceCompletionTests.cs | 6 +- .../Rules/TokenBasedFormattingRule.cs | 8 ++ .../CSharpTest/Formatting/FormattingTests.cs | 101 ++++++++++++++++-- 3 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticBraceCompletionTests.cs b/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticBraceCompletionTests.cs index 2885a9dd05e82..638e3f3c6ca1d 100644 --- a/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticBraceCompletionTests.cs +++ b/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticBraceCompletionTests.cs @@ -388,7 +388,7 @@ class C { void M() { - _ = this is { } + _ = this is {} } }"; @@ -431,7 +431,7 @@ class C { void M() { - _ = this is { } + _ = this is {} M(); } }"; @@ -476,7 +476,7 @@ class C { void M() { - _ = this is (1, 2) { } + _ = this is (1, 2) {} M(); } }"; diff --git a/src/Workspaces/CSharp/Portable/Formatting/Rules/TokenBasedFormattingRule.cs b/src/Workspaces/CSharp/Portable/Formatting/Rules/TokenBasedFormattingRule.cs index 9cbe3b6fbc8b7..1c9fb66e5cbf9 100644 --- a/src/Workspaces/CSharp/Portable/Formatting/Rules/TokenBasedFormattingRule.cs +++ b/src/Workspaces/CSharp/Portable/Formatting/Rules/TokenBasedFormattingRule.cs @@ -257,6 +257,14 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine); } + // empty {} in pattern + if (previousToken.Kind() == SyntaxKind.OpenBraceToken && + currentToken.Kind() == SyntaxKind.CloseBraceToken && + currentToken.Parent.IsKind(SyntaxKindEx.PropertyPatternClause)) + { + return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine); + } + // attribute case // , [ if (previousToken.Kind() == SyntaxKind.CommaToken && currentToken.Kind() == SyntaxKind.OpenBracketToken && currentToken.Parent is AttributeListSyntax) diff --git a/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs b/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs index 5dff817509dc0..a8416263d3aea 100644 --- a/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs +++ b/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs @@ -5142,7 +5142,7 @@ void M() { { void M() { - _ = this is C(1, 2) { }; + _ = this is C(1, 2) {}; } }"; // no space separates the type and the positional pattern @@ -5165,7 +5165,7 @@ void M() { { void M() { - _ = this is C (1, 2) { }; + _ = this is C (1, 2) {}; } }"; await AssertFormatAsync(expectedCode, code, changedOptionSet: changingOptions); @@ -5188,8 +5188,8 @@ void M() { { void M() { - _ = this is C( 1, 2 ) { }; - _ = this is C() { }; + _ = this is C( 1, 2 ) {}; + _ = this is C() {}; } }"; await AssertFormatAsync(expectedCode, code, changedOptionSet: changingOptions); @@ -5212,8 +5212,8 @@ void M() { { void M() { - _ = this is C(1, 2) { }; - _ = this is C( ) { }; + _ = this is C(1, 2) {}; + _ = this is C( ) {}; } }"; await AssertFormatAsync(expectedCode, code, changedOptionSet: changingOptions); @@ -5290,7 +5290,7 @@ void M() { { void M() { - _ = this is { } + _ = this is {} M(); } }"; @@ -5313,7 +5313,7 @@ void M() { { void M() { - _ = this is (1, 2) { } + _ = this is (1, 2) {} M(); } }"; @@ -9069,6 +9069,91 @@ public void Foo() }"); } + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public async Task EmptyIsPropertyPattern() + { + var code = @" +class C +{ + void M() + { + _ = x is { } + } +} +"; + var expected = @" +class C +{ + void M() + { + _ = x is {} + } +} +"; + + await AssertFormatAsync(expected, code); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public async Task EmptySwitchCasePropertyPattern() + { + var code = @" +class C +{ + void M() + { + switch (x) + { + case { } + } + } +} +"; + var expected = @" +class C +{ + void M() + { + switch (x) + { + case {} + } + } +} +"; + + await AssertFormatAsync(expected, code); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public async Task EmptySwitchExpressionCasePropertyPattern() + { + var code = @" +class C +{ + void M() + { + _ = x switch + { + { } => + } +} +"; + var expected = @" +class C +{ + void M() + { + _ = x switch + { + {} => + } +} +"; + + await AssertFormatAsync(expected, code); + } + [Theory, Trait(Traits.Feature, Traits.Features.Formatting)] [WorkItem(31571, "https://github.com/dotnet/roslyn/issues/31571")] [WorkItem(33910, "https://github.com/dotnet/roslyn/issues/33910")]