diff --git a/src/Workspaces/CSharp/Portable/Formatting/Rules/SpacingFormattingRule.cs b/src/Workspaces/CSharp/Portable/Formatting/Rules/SpacingFormattingRule.cs index e4e9ad18b1916d9997f080ff2200056d7449fe34..b9f8ad4ee518ce96e2127952707e382aca89475f 100644 --- a/src/Workspaces/CSharp/Portable/Formatting/Rules/SpacingFormattingRule.cs +++ b/src/Workspaces/CSharp/Portable/Formatting/Rules/SpacingFormattingRule.cs @@ -176,11 +176,9 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ { return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceWithinSquareBrackets); } + if (currentKind == SyntaxKind.CloseBracketToken && previousKind != SyntaxKind.OmittedArraySizeExpressionToken && HasFormattableBracketParent(currentToken)) { - // int []: added spacing operation on [ - // int[,]: added spacing operation on , - // int[1]: need spacing operation return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceWithinSquareBrackets); } diff --git a/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs b/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs index 90f93d89e72c4be1c2ac050dd4d06f3548ddacb8..3402fcdabeefe1dccd780a0cccd1cc17b58beb55 100644 --- a/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs +++ b/src/Workspaces/CSharpTest/Formatting/FormattingTests.cs @@ -5353,7 +5353,7 @@ void foo() } [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] - public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_00() + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0000() { var code = @" class Program @@ -5364,7 +5364,7 @@ class Program var a = new[ ] { 0 }; }"; - var expected00 = @" + var expected = @" class Program { int[] x; @@ -5373,7 +5373,29 @@ class Program var a = new[] { 0 }; }"; - var expected01 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, false }, + { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, false }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0001() + { + var code = @" +class Program +{ + int[ ] x; + int[ , ] y; + int[ , , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; + + var expected = @" class Program { int[] x; @@ -5382,7 +5404,29 @@ class Program var a = new[] { 0 }; }"; - var expected10 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, false }, + { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, true }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0010() + { + var code = @" +class Program +{ + int[ ] x; + int[ , ] y; + int[ , , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; + + var expected = @" class Program { int[] x; @@ -5391,7 +5435,29 @@ class Program var a = new[] { 0 }; }"; - var expected11 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, false }, + { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, false }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0011() + { + var code = @" +class Program +{ + int[ ] x; + int[ , ] y; + int[ , , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; + + var expected = @" class Program { int[] x; @@ -5402,25 +5468,16 @@ class Program var options = new Dictionary() { - { SpaceBeforeComma, false }, - { SpaceAfterComma, false }, { SpaceBetweenEmptySquareBrackets, false }, { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, true }, }; - AssertFormat("// 00" + expected00, "// 00" + code, changedOptionSet: options); - - options[SpaceBeforeComma] = true; - AssertFormat("// 10" + expected10, "// 10" + code, changedOptionSet: options); - - options[SpaceAfterComma] = true; - AssertFormat("// 11" + expected11, "// 11" + code, changedOptionSet: options); - - options[SpaceBeforeComma] = false; - AssertFormat("// 01" + expected01, "// 01" + code, changedOptionSet: options); + AssertFormat(expected, code, changedOptionSet: options); } [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] - public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_01() + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0100() { var code = @" class Program @@ -5431,7 +5488,7 @@ class Program var a = new[ ] { 0 }; }"; - var expected00 = @" + var expected = @" class Program { int[] x; @@ -5440,7 +5497,29 @@ class Program var a = new[] { 0 }; }"; - var expected01 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, false }, + { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, false }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0101() + { + var code = @" +class Program +{ + int[ ] x; + int[, ] y; + int[, , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; + + var expected = @" class Program { int[] x; @@ -5449,7 +5528,29 @@ class Program var a = new[] { 0 }; }"; - var expected10 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, false }, + { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, true }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0110() + { + var code = @" +class Program +{ + int[ ] x; + int[, ] y; + int[, , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; + + var expected = @" class Program { int[] x; @@ -5458,7 +5559,29 @@ class Program var a = new[] { 0 }; }"; - var expected11 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, false }, + { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, false }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_0111() + { + var code = @" +class Program +{ + int[ ] x; + int[, ] y; + int[, , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; + + var expected = @" class Program { int[] x; @@ -5469,25 +5592,16 @@ class Program var options = new Dictionary() { - { SpaceBeforeComma, false }, - { SpaceAfterComma, false }, { SpaceBetweenEmptySquareBrackets, false }, { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, true }, }; - AssertFormat("// 00" + expected00, "// 00" + code, changedOptionSet: options); - - options[SpaceBeforeComma] = true; - AssertFormat("// 10" + expected10, "// 10" + code, changedOptionSet: options); - - options[SpaceAfterComma] = true; - AssertFormat("// 11" + expected11, "// 11" + code, changedOptionSet: options); - - options[SpaceBeforeComma] = false; - AssertFormat("// 01" + expected01, "// 01" + code, changedOptionSet: options); + AssertFormat(expected, code, changedOptionSet: options); } [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] - public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_10() + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1000() { var code = @" class Program @@ -5498,7 +5612,7 @@ class Program var a = new[] { 0 }; }"; - var expected00 = @" + var expected = @" class Program { int[ ] x; @@ -5507,7 +5621,29 @@ class Program var a = new[ ] { 0 }; }"; - var expected01 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, true }, + { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, false }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1001() + { + var code = @" +class Program +{ + int[] x; + int[ ,] y; + int[ , ,] z = new int[1,2,3]; + var a = new[] { 0 }; +}"; + + var expected = @" class Program { int[ ] x; @@ -5516,7 +5652,29 @@ class Program var a = new[ ] { 0 }; }"; - var expected10 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, true }, + { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, true }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1010() + { + var code = @" +class Program +{ + int[] x; + int[ ,] y; + int[ , ,] z = new int[1,2,3]; + var a = new[] { 0 }; +}"; + + var expected = @" class Program { int[ ] x; @@ -5525,7 +5683,29 @@ class Program var a = new[ ] { 0 }; }"; - var expected11 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, true }, + { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, false }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1011() + { + var code = @" +class Program +{ + int[] x; + int[ ,] y; + int[ , ,] z = new int[1,2,3]; + var a = new[] { 0 }; +}"; + + var expected = @" class Program { int[ ] x; @@ -5536,25 +5716,16 @@ class Program var options = new Dictionary() { - { SpaceBeforeComma, false }, - { SpaceAfterComma, false }, { SpaceBetweenEmptySquareBrackets, true }, { SpaceWithinSquareBrackets, false }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, true }, }; - AssertFormat("// 00" + expected00, "// 00" + code, changedOptionSet: options); - - options[SpaceBeforeComma] = true; - AssertFormat("// 10" + expected10, "// 10" + code, changedOptionSet: options); - - options[SpaceAfterComma] = true; - AssertFormat("// 11" + expected11, "// 11" + code, changedOptionSet: options); - - options[SpaceBeforeComma] = false; - AssertFormat("// 01" + expected01, "// 01" + code, changedOptionSet: options); + AssertFormat(expected, code, changedOptionSet: options); } [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] - public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_11() + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1100() { var code = @" class Program @@ -5565,7 +5736,7 @@ class Program var a = new[ ] { 0 }; }"; - var expected00 = @" + var expected = @" class Program { int[ ] x; @@ -5574,7 +5745,29 @@ class Program var a = new[ ] { 0 }; }"; - var expected01 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, true }, + { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, false }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1101() + { + var code = @" +class Program +{ + int[ ] x; + int[ , ] y; + int[ , , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; + + var expected = @" class Program { int[ ] x; @@ -5583,41 +5776,76 @@ class Program var a = new[ ] { 0 }; }"; - var expected10 = @" + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, true }, + { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, false }, + { SpaceAfterComma, true }, + }; + AssertFormat(expected, code, changedOptionSet: options); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1110() + { + var code = @" class Program { int[ ] x; int[ , ] y; - int[ , , ] z = new int[ 1 ,2 ,3 ]; + int[ , , ] z = new int[1,2,3]; var a = new[ ] { 0 }; }"; - var expected11 = @" + var expected = @" class Program { int[ ] x; int[ , ] y; - int[ , , ] z = new int[ 1 , 2 , 3 ]; + int[ , , ] z = new int[ 1 ,2 ,3 ]; var a = new[ ] { 0 }; }"; var options = new Dictionary() { - { SpaceBeforeComma, false }, - { SpaceAfterComma, false }, { SpaceBetweenEmptySquareBrackets, true }, { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, false }, }; - AssertFormat("// 00" + expected00, "// 00" + code, changedOptionSet: options); + AssertFormat(expected, code, changedOptionSet: options); + } - options[SpaceBeforeComma] = true; - AssertFormat("// 10" + expected10, "// 10" + code, changedOptionSet: options); + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_1111() + { + var code = @" +class Program +{ + int[ ] x; + int[ , ] y; + int[ , , ] z = new int[1,2,3]; + var a = new[ ] { 0 }; +}"; - options[SpaceAfterComma] = true; - AssertFormat("// 11" + expected11, "// 11" + code, changedOptionSet: options); + var expected = @" +class Program +{ + int[ ] x; + int[ , ] y; + int[ , , ] z = new int[ 1 , 2 , 3 ]; + var a = new[ ] { 0 }; +}"; - options[SpaceBeforeComma] = false; - AssertFormat("// 01" + expected01, "// 01" + code, changedOptionSet: options); + var options = new Dictionary() + { + { SpaceBetweenEmptySquareBrackets, true }, + { SpaceWithinSquareBrackets, true }, + { SpaceBeforeComma, true }, + { SpaceAfterComma, true }, + }; + AssertFormat(expected, code, changedOptionSet: options); } [Fact, Trait(Traits.Feature, Traits.Features.Formatting)]