diff --git a/src/EditorFeatures/CSharpTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.cs index 6d0dc701e9814..a1045549a699f 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.cs @@ -26,7 +26,7 @@ private async Task TestFixOneAsync(string initial, string expected, Refactoring await TestInRegularAndScriptAsync(CreateTreeText("[||]" + initial), CreateTreeText(expected), index: (int)refactoring); } - private string CreateTreeText(string initial) + private static string CreateTreeText(string initial) { return @"class X { void F() { var x = " + initial + @"; } }"; } @@ -96,5 +96,30 @@ public async Task TestTypeCharacter() { await TestFixOneAsync("0x1e5UL", "0b111100101UL", Refactoring.ChangeBase2); } + + [WorkItem(19225, "https://github.com/dotnet/roslyn/issues/19225")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertNumericLiteral)] + public async Task TestPreserveWhitespaces() + { + await TestInRegularAndScriptAsync( +@"class Program +{ + void M() + { + var numbers = new int[] { + [||]0x1, 0x2 + }; + } +}", +@"class Program +{ + void M() + { + var numbers = new int[] { + 0b1, 0x2 + }; + } +}", index: (int)Refactoring.ChangeBase2, ignoreTrivia: false); + } } } diff --git a/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb b/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb index 9af183e810f99..d3810bad590af 100644 --- a/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb +++ b/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb @@ -26,7 +26,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeActions.Conver Await TestInRegularAndScriptAsync(CreateTreeText("[||]" + initial), CreateTreeText(expected), index:=DirectCast(refactoring, Integer)) End Function - Private Function CreateTreeText(initial As String) As String + Private Shared Function CreateTreeText(initial As String) As String Return " Class X Sub M() @@ -89,5 +89,27 @@ End Class" Public Async Function TestTypeCharacter() As Task Await TestFixOneAsync("&H1e5UL", "&B111100101UL", Refactoring.ChangeBase2) End Function + + + + Public Async Function TestPreserveTrivia() As Task + Await TestInRegularAndScriptAsync( +"Class X + Sub M() + Dim x As Integer() = + { + [||]&H1, &H2 + } + End Sub +End Class", +"Class X + Sub M() + Dim x As Integer() = + { + &B1, &H2 + } + End Sub +End Class", index:=Refactoring.ChangeBase2, ignoreTrivia:=False) + End Function End Class End Namespace diff --git a/src/Features/Core/Portable/ConvertNumericLiteral/AbstractConvertNumericLiteralCodeRefactoringProvider.cs b/src/Features/Core/Portable/ConvertNumericLiteral/AbstractConvertNumericLiteralCodeRefactoringProvider.cs index 7d88b14b68cc7..51306e6df2871 100644 --- a/src/Features/Core/Portable/ConvertNumericLiteral/AbstractConvertNumericLiteralCodeRefactoringProvider.cs +++ b/src/Features/Core/Portable/ConvertNumericLiteral/AbstractConvertNumericLiteralCodeRefactoringProvider.cs @@ -114,7 +114,8 @@ void RegisterRefactoringWithResult(string text, string title) context.RegisterRefactoring(new MyCodeAction(title, c => { var generator = SyntaxGenerator.GetGenerator(document); - var updatedToken = generator.NumericLiteralToken(text + suffix, (ulong)value); + var updatedToken = generator.NumericLiteralToken(text + suffix, (ulong)value) + .WithTriviaFrom(numericToken); var updatedRoot = root.ReplaceToken(numericToken, updatedToken); return Task.FromResult(document.WithSyntaxRoot(updatedRoot)); }));