Skip to content

Commit

Permalink
Format {} property pattern (#34619)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Apr 2, 2019
1 parent ef24d3f commit 1bf1b64
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class C
{
void M()
{
_ = this is { }
_ = this is {}
}
}";

Expand Down Expand Up @@ -431,7 +431,7 @@ class C
{
void M()
{
_ = this is { }
_ = this is {}
M();
}
}";
Expand Down Expand Up @@ -476,7 +476,7 @@ class C
{
void M()
{
_ = this is (1, 2) { }
_ = this is (1, 2) {}
M();
}
}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
101 changes: 93 additions & 8 deletions src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -5165,7 +5165,7 @@ void M() {
{
void M()
{
_ = this is C (1, 2) { };
_ = this is C (1, 2) {};
}
}";
await AssertFormatAsync(expectedCode, code, changedOptionSet: changingOptions);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -5290,7 +5290,7 @@ void M() {
{
void M()
{
_ = this is { }
_ = this is {}
M();
}
}";
Expand All @@ -5313,7 +5313,7 @@ void M() {
{
void M()
{
_ = this is (1, 2) { }
_ = this is (1, 2) {}
M();
}
}";
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit 1bf1b64

Please sign in to comment.