提交 a3f76b60 编写于 作者: P Paul Chen

Treat [,..,] as empty. Add combo test cases.

上级 6af8c3e9
......@@ -163,11 +163,9 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceBeforeOpenSquareBracket);
}
// For spacing empty square braces
if (previousKind == SyntaxKind.OpenBracketToken
&& (currentKind == SyntaxKind.CloseBracketToken
|| (currentKind == SyntaxKind.OmittedArraySizeExpressionToken
&& (previousToken.Parent as ArrayRankSpecifierSyntax)?.Rank == 1))
// For spacing empty square braces, also treat [,] as empty
if (((currentKind == SyntaxKind.CloseBracketToken && previousKind == SyntaxKind.OpenBracketToken)
|| currentKind == SyntaxKind.OmittedArraySizeExpressionToken)
&& HasFormattableBracketParent(previousToken))
{
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceBetweenEmptySquareBrackets);
......@@ -178,23 +176,12 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
{
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceWithinSquareBrackets);
}
if (currentKind == SyntaxKind.OmittedArraySizeExpressionToken && previousKind == SyntaxKind.CommaToken)
{
var rankSpec = previousToken.Parent as ArrayRankSpecifierSyntax;
if (rankSpec != null && currentToken.Parent == rankSpec.Sizes.Last())
{
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceWithinSquareBrackets);
}
}
else if (currentKind == SyntaxKind.CloseBracketToken && HasFormattableBracketParent(currentToken))
if (currentKind == SyntaxKind.CloseBracketToken && previousKind != SyntaxKind.OmittedArraySizeExpressionToken && HasFormattableBracketParent(currentToken))
{
// int []: added spacing operation on [
// int[,]: added spacing operation on ,
// int[1]: need spacing operation
if (previousKind != SyntaxKind.OmittedArraySizeExpressionToken)
{
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceWithinSquareBrackets);
}
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceWithinSquareBrackets);
}
// For spacing delimiters - after colon
......@@ -210,15 +197,21 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
}
// For spacing delimiters - after comma
if ((previousToken.IsCommaInArgumentOrParameterList() && currentKind != SyntaxKind.OmittedTypeArgumentToken) ||
previousToken.IsCommaInInitializerExpression())
if ((previousToken.IsCommaInArgumentOrParameterList() && currentKind != SyntaxKind.OmittedTypeArgumentToken)
|| previousToken.IsCommaInInitializerExpression()
|| (previousKind == SyntaxKind.CommaToken
&& currentKind != SyntaxKind.OmittedArraySizeExpressionToken
&& HasFormattableBracketParent(previousToken)))
{
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceAfterComma);
}
// For spacing delimiters - before comma
if ((currentToken.IsCommaInArgumentOrParameterList() && previousKind != SyntaxKind.OmittedTypeArgumentToken) ||
currentToken.IsCommaInInitializerExpression())
if ((currentToken.IsCommaInArgumentOrParameterList() && previousKind != SyntaxKind.OmittedTypeArgumentToken)
|| currentToken.IsCommaInInitializerExpression()
|| (currentKind == SyntaxKind.CommaToken
&& previousKind != SyntaxKind.OmittedArraySizeExpressionToken
&& HasFormattableBracketParent(currentToken)))
{
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceBeforeComma);
}
......
......@@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using static Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Formatting
{
......@@ -5352,57 +5353,271 @@ void foo()
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void SquareBracesEmpty_True()
public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_00()
{
var code = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[1,2,3];
var a = new[ ] { 0 };
}";
var expected00 = @"
class Program
{
int[] x;
int[ ,] y;
var z = new[] { 0 };
int[,] y;
int[,,] z = new int[1,2,3];
var a = new[] { 0 };
}";
var expected = @"
var expected01 = @"
class Program
{
int[ ] x;
int[] x;
int[,] y;
int[,,] z = new int[1, 2, 3];
var a = new[] { 0 };
}";
var expected10 = @"
class Program
{
int[] x;
int[,] y;
var z = new[ ] { 0 };
int[,,] z = new int[1 ,2 ,3];
var a = new[] { 0 };
}";
var expected11 = @"
class Program
{
int[] x;
int[,] y;
int[,,] z = new int[1 , 2 , 3];
var a = new[] { 0 };
}";
var options = new Dictionary<OptionKey, object>()
{
{ CSharpFormattingOptions.SpaceBetweenEmptySquareBrackets, true },
{ CSharpFormattingOptions.SpaceWithinSquareBrackets, false },
{ SpaceBeforeComma, false },
{ SpaceAfterComma, false },
{ SpaceBetweenEmptySquareBrackets, false },
{ SpaceWithinSquareBrackets, false },
};
AssertFormat(expected, code, changedOptionSet: options);
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);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void SquareBracesEmpty_False()
public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_01()
{
var code = @"
class Program
{
int[ ] x;
var z = new[ ] { 0 };
int[, ] x;
int[, ] y;
int[, , ] z = new int[1,2,3];
var a = new[ ] { 0 };
}";
var expected = @"
var expected00 = @"
class Program
{
int[] x;
int[,] y;
int[,,] z = new int[ 1,2,3 ];
var a = new[] { 0 };
}";
var expected01 = @"
class Program
{
int[] x;
int[,] y;
int[,,] z = new int[ 1, 2, 3 ];
var a = new[] { 0 };
}";
var expected10 = @"
class Program
{
int[] x;
var z = new[] { 0 };
int[ , ] x;
int[,] y;
int[,,] z = new int[ 1 ,2 ,3 ];
var a = new[] { 0 };
}";
var expected11 = @"
class Program
{
int[] x;
int[,] y;
int[,,] z = new int[ 1 , 2 , 3 ];
var a = new[] { 0 };
}";
var options = new Dictionary<OptionKey, object>()
{
{ CSharpFormattingOptions.SpaceBetweenEmptySquareBrackets, false },
{ CSharpFormattingOptions.SpaceWithinSquareBrackets, true },
{ SpaceBeforeComma, false },
{ SpaceAfterComma, false },
{ SpaceBetweenEmptySquareBrackets, false },
{ SpaceWithinSquareBrackets, true },
};
AssertFormat(expected, code, changedOptionSet: options);
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);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_10()
{
var code = @"
class Program
{
int[] x;
int[ ,] y;
int[ , ,] z = new int[1,2,3];
var a = new[] { 0 };
}";
var expected00 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[1,2,3];
var a = new[ ] { 0 };
}";
var expected01 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[1, 2, 3];
var a = new[ ] { 0 };
}";
var expected10 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[1 ,2 ,3];
var a = new[ ] { 0 };
}";
var expected11 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[1 , 2 , 3];
var a = new[ ] { 0 };
}";
var options = new Dictionary<OptionKey, object>()
{
{ SpaceBeforeComma, false },
{ SpaceAfterComma, false },
{ SpaceBetweenEmptySquareBrackets, true },
{ SpaceWithinSquareBrackets, false },
};
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);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void SpaceWithinEmptyBracketPrecedencesSpaceBeforeOrAfterComma_11()
{
var code = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[1,2,3];
var a = new[ ] { 0 };
}";
var expected00 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[ 1,2,3 ];
var a = new[ ] { 0 };
}";
var expected01 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[ 1, 2, 3 ];
var a = new[ ] { 0 };
}";
var expected10 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[ 1 ,2 ,3 ];
var a = new[ ] { 0 };
}";
var expected11 = @"
class Program
{
int[ ] x;
int[ , ] y;
int[ , , ] z = new int[ 1 , 2 , 3 ];
var a = new[ ] { 0 };
}";
var options = new Dictionary<OptionKey, object>()
{
{ SpaceBeforeComma, false },
{ SpaceAfterComma, false },
{ SpaceBetweenEmptySquareBrackets, true },
{ SpaceWithinSquareBrackets, 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);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册