提交 87c75136 编写于 作者: D David Wengier

Use the same type character that the user already uses in their enum when generating new members

上级 ad294e9c
......@@ -1690,6 +1690,102 @@ class Program
void Main()
{
E.C }
}");
}
[WorkItem(49679, "https://github.com/dotnet/roslyn/issues/49679")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)]
public async Task TestWithLeftShift_Long()
{
await TestInRegularAndScriptAsync(
@"class Program
{
void Main()
{
Color.[|Blue|];
}
}
enum Color : long
{
Green = 1L << 0
}",
@"class Program
{
void Main()
{
Color.Blue;
}
}
enum Color : long
{
Green = 1L << 0,
Blue = 1L << 1
}");
}
[WorkItem(49679, "https://github.com/dotnet/roslyn/issues/49679")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)]
public async Task TestWithLeftShift_UInt()
{
await TestInRegularAndScriptAsync(
@"class Program
{
void Main()
{
Color.[|Blue|];
}
}
enum Color : uint
{
Green = 1u << 0
}",
@"class Program
{
void Main()
{
Color.Blue;
}
}
enum Color : uint
{
Green = 1u << 0,
Blue = 1u << 1
}");
}
[WorkItem(49679, "https://github.com/dotnet/roslyn/issues/49679")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)]
public async Task TestWithLeftShift_ULong()
{
await TestInRegularAndScriptAsync(
@"class Program
{
void Main()
{
Color.[|Blue|];
}
}
enum Color : ulong
{
Green = 1UL << 0
}",
@"class Program
{
void Main()
{
Color.Blue;
}
}
enum Color : ulong
{
Green = 1UL << 0,
Blue = 1UL << 1
}");
}
}
......
......@@ -1339,5 +1339,28 @@ Enum E As UShort
Y = &H8000US
End Enum")
End Function
<WorkItem(49679, "https://github.com/dotnet/roslyn/issues/49679")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)>
Public Async Function TestWithLeftShift_Long() As Task
Await TestInRegularAndScriptAsync(
"Module Program
Sub Main(args As String())
Goo([|Color.Blue|])
End Sub
Enum Color
Green = 1L << 0
End Enum
End Module",
"Module Program
Sub Main(args As String())
Goo(Color.Blue)
End Sub
Enum Color
Green = 1L << 0
Blue = 1L << 1
End Enum
End Module")
End Function
End Class
End Namespace
......@@ -123,9 +123,11 @@ private static ExpressionSyntax CreateEnumMemberValue(EnumDeclarationSyntax dest
{
// The user is left shifting ones, stick with that pattern
var shiftValue = IntegerUtilities.LogBase2(value);
// Re-use the numericLiteral text so type suffixes match too
return SyntaxFactory.BinaryExpression(
SyntaxKind.LeftShiftExpression,
SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal("1", 1)),
SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(numericLiteral.Token.Text, 1)),
SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(shiftValue.ToString(), shiftValue)));
}
}
......
......@@ -93,8 +93,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
If numericLiteral.Token.ValueText = "1" Then
' The user is left shifting ones, stick with that pattern
Dim shiftValue = IntegerUtilities.LogBase2(value)
' Using the numericLiteral text will ensure the correct type character, ignoring the None that is passed in below
Return SyntaxFactory.LeftShiftExpression(
left:=SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken("1", LiteralBase.Decimal, TypeCharacter.None, 1)),
left:=SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(numericLiteral.Token.Text, LiteralBase.Decimal, TypeCharacter.None, 1)),
right:=SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(shiftValue.ToString(), LiteralBase.Decimal, TypeCharacter.None, IntegerUtilities.ToUnsigned(shiftValue))))
End If
End If
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册