From 5546f8614809ac00a7b0502957a11b5a54f87212 Mon Sep 17 00:00:00 2001 From: Alireza Habibi Date: Thu, 4 May 2017 23:07:09 +0430 Subject: [PATCH 1/2] Preserve trivia when converting a numeric literal base --- .../ConvertNumericLiteralTests.cs | 27 ++++++++++++++++++- .../ConvertNumericLiteralTests.vb | 24 ++++++++++++++++- ...rtNumericLiteralCodeRefactoringProvider.cs | 3 ++- 3 files changed, 51 insertions(+), 3 deletions(-) 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..b38d228341454 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)); })); From 9364b8c6c0320da0c99c9a0642185992da43108e Mon Sep 17 00:00:00 2001 From: Alireza Habibi Date: Wed, 10 May 2017 17:46:52 +0430 Subject: [PATCH 2/2] PR feedback --- .../ConvertNumericLiteral/ConvertNumericLiteralTests.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb b/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb index b38d228341454..d3810bad590af 100644 --- a/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb +++ b/src/EditorFeatures/VisualBasicTest/CodeActions/ConvertNumericLiteral/ConvertNumericLiteralTests.vb @@ -109,7 +109,7 @@ End Class", &B1, &H2 } End Sub -End Class", index := Refactoring.ChangeBase2, ignoreTrivia := False) +End Class", index:=Refactoring.ChangeBase2, ignoreTrivia:=False) End Function End Class End Namespace