提交 0e49af31 编写于 作者: T Ty Overby 提交者: GitHub

generate binary literals for enums that are already binary (#13376)

* generate binary literals for enums that are already binary

* adds test for binary enum int-max
上级 cac716de
...@@ -129,6 +129,14 @@ public async Task TestWithDifferentStyles() ...@@ -129,6 +129,14 @@ public async Task TestWithDifferentStyles()
@"class Program { void Main ( ) { Color . Blue ; } } enum Color { Red = 2 , Green = 1 << 5 , Blue = 33 } "); @"class Program { void Main ( ) { Color . Blue ; } } enum Color { Red = 2 , Green = 1 << 5 , Blue = 33 } ");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)]
public async Task TestBinary()
{
await TestAsync(
@"class Program { void Main ( ) { Color . [|Blue|] ; } } enum Color { Red = 0b01 } ",
@"class Program { void Main ( ) { Color . Blue ; } } enum Color { Red = 0b01 , Blue = 0b10 } ");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)]
public async Task TestHex1() public async Task TestHex1()
{ {
...@@ -177,6 +185,14 @@ public async Task TestGenerateEnumMemberOfTypeLong() ...@@ -177,6 +185,14 @@ public async Task TestGenerateEnumMemberOfTypeLong()
@"class Program { void Main ( ) { Color . Blue ; } } enum Color : long { Red = long.MaxValue , Blue = long.MinValue } "); @"class Program { void Main ( ) { Color . Blue ; } } enum Color : long { Red = long.MaxValue , Blue = long.MinValue } ");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)]
public async Task TestGenerateAfterEnumWithLongMaxValueInBinary()
{
await TestAsync(
@"class Program { void Main ( ) { Color . [|Blue|] ; } } enum Color : long { Red = 0b0111111111111111111111111111111111111111111111111111111111111111 } ",
@"class Program { void Main ( ) { Color . Blue ; } } enum Color : long { Red = 0b0111111111111111111111111111111111111111111111111111111111111111 , Blue = 0b1000000000000000000000000000000000000000000000000000000000000000 } ");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)]
public async Task TestGenerateAfterEnumWithLongMaxValueInHex() public async Task TestGenerateAfterEnumWithLongMaxValueInHex()
{ {
......
...@@ -34,6 +34,13 @@ NewLines("Module Program \n Sub Main(args As String()) \n Foo([|Color.Green|]) \ ...@@ -34,6 +34,13 @@ NewLines("Module Program \n Sub Main(args As String()) \n Foo([|Color.Green|]) \
NewLines("Module Program \n Sub Main(args As String()) \n Foo(Color.Green) \n End Sub \n End Module \n Enum Color \n Red = 1 \n Green = 2 \n End Enum")) NewLines("Module Program \n Sub Main(args As String()) \n Foo(Color.Green) \n End Sub \n End Module \n Enum Color \n Red = 1 \n Green = 2 \n End Enum"))
End Function End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)>
Public Async Function TestGenerateBinaryLiteral() As Task
Await TestAsync(
NewLines("Module Program \n Sub Main(args As String()) \n Foo([|Color.Green|]) \n End Sub \n End Module \n Enum Color \n Red = &B01 \n End Enum"),
NewLines("Module Program \n Sub Main(args As String()) \n Foo(Color.Green) \n End Sub \n End Module \n Enum Color \n Red = &B01 \n Green = &B10 \n End Enum"))
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)> <Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)>
Public Async Function TestGenerateIntoLinearIncreasingSequence() As Task Public Async Function TestGenerateIntoLinearIncreasingSequence() As Task
Await TestAsync( Await TestAsync(
......
...@@ -139,6 +139,11 @@ private static ExpressionSyntax CreateEnumMemberValue(EnumDeclarationSyntax dest ...@@ -139,6 +139,11 @@ private static ExpressionSyntax CreateEnumMemberValue(EnumDeclarationSyntax dest
return SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, return SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression,
SyntaxFactory.Literal(numericText.Substring(0, 2) + value.ToString("X"), value)); SyntaxFactory.Literal(numericText.Substring(0, 2) + value.ToString("X"), value));
} }
else if (numericText.StartsWith("0b", StringComparison.OrdinalIgnoreCase))
{
return SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression,
SyntaxFactory.Literal(numericText.Substring(0, 2) + Convert.ToString(value, 2), value));
}
} }
} }
} }
......
...@@ -121,6 +121,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration ...@@ -121,6 +121,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End If End If
ElseIf numericText.StartsWith("&O", StringComparison.OrdinalIgnoreCase) Then ElseIf numericText.StartsWith("&O", StringComparison.OrdinalIgnoreCase) Then
Return SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(numericText.Substring(0, 2) + Convert.ToString(value, 8), LiteralBase.Octal, TypeCharacter.None, IntegerUtilities.ToUnsigned(value))) Return SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(numericText.Substring(0, 2) + Convert.ToString(value, 8), LiteralBase.Octal, TypeCharacter.None, IntegerUtilities.ToUnsigned(value)))
ElseIf numericText.StartsWith("&B", StringComparison.OrdinalIgnoreCase) Then
Return SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(numericText.Substring(0, 2) + Convert.ToString(value, 2), LiteralBase.Binary, TypeCharacter.None, IntegerUtilities.ToUnsigned(value)))
End If End If
End If End If
End If End If
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册