提交 4b6ee166 编写于 作者: B Balaji Soundrarajan

Merge pull request #4281 from basoundr/4280DictionaryInitializers

Add spacing rules around Dictionary Initializers
......@@ -357,5 +357,11 @@ public static bool IsValidAttributeTarget(this SyntaxToken token)
return false;
}
}
public static bool IsOpenBraceOrCommaOfObjectInitializer(this SyntaxToken token)
{
return (token.IsKind(SyntaxKind.OpenBraceToken) || token.IsKind(SyntaxKind.CommaToken)) &&
token.Parent.IsKind(SyntaxKind.ObjectInitializerExpression);
}
}
}
......@@ -2,7 +2,6 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting.Rules;
......@@ -141,7 +140,7 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
}
// For spacing Before Square Braces
if (currentKind == SyntaxKind.OpenBracketToken && HasFormattableBracketParent(currentToken))
if (currentKind == SyntaxKind.OpenBracketToken && HasFormattableBracketParent(currentToken) && !previousToken.IsOpenBraceOrCommaOfObjectInitializer())
{
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceBeforeOpenSquareBracket);
}
......
......@@ -229,7 +229,6 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
}
// * )
// * [
// * ]
// * ,
// * .
......@@ -237,7 +236,6 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
switch (currentToken.Kind())
{
case SyntaxKind.CloseParenToken:
case SyntaxKind.OpenBracketToken:
case SyntaxKind.CloseBracketToken:
case SyntaxKind.CommaToken:
case SyntaxKind.DotToken:
......@@ -245,6 +243,12 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
}
// * [
if (currentToken.IsKind(SyntaxKind.OpenBracketToken) && !previousToken.IsOpenBraceOrCommaOfObjectInitializer())
{
return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
}
// case * :
// default:
// <label> :
......
......@@ -6258,5 +6258,27 @@ static void Main(string[] args)
}";
AssertFormat(code, code);
}
[WorkItem(1184285)]
[WorkItem(4280, "https://github.com/dotnet/roslyn/issues/4280")]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void FormatDictionaryInitializers()
{
var code = @"class Program
{
void Main()
{
var sample = new Dictionary<string, string> {[""x""] = ""d"" ,[""z""] = ""XX"" };
}
}";
var expected = @"class Program
{
void Main()
{
var sample = new Dictionary<string, string> { [""x""] = ""d"", [""z""] = ""XX"" };
}
}";
AssertFormat(expected, code);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册