Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve trivia when converting a numeric literal base #19264

Merged
merged 2 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 + @"; } }";
}
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -89,5 +89,27 @@ End Class"
Public Async Function TestTypeCharacter() As Task
Await TestFixOneAsync("&H1e5UL", "&B111100101UL", Refactoring.ChangeBase2)
End Function

<WorkItem(19225, "https://github.com/dotnet/roslyn/issues/19225")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertNumericLiteral)>
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
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}));
Expand Down