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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Inline Temporary support for top-level statements #44421

Merged
merged 1 commit into from
May 20, 2020
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 @@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.InlineTemporary;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
Expand Down Expand Up @@ -5170,5 +5171,73 @@ C M2()
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTemporary)]
[WorkItem(44263, "https://github.com/dotnet/roslyn/issues/44263")]
public async Task Call_TopLevelStatement()
{
var code = @"
using System;

int [||]x = 1 + 1;
x.ToString();
";

var expected = @"
using System;

(1 + 1).ToString();
";

// Global statements in regular code are local variables, so Inline Temporary works. Script code is not
// tested because global statements in script code are field declarations, which are not considered
// temporary.
await TestAsync(code, expected, TestOptions.Regular.WithLanguageVersion(LanguageVersionExtensions.CSharp9));
}

[WorkItem(44263, "https://github.com/dotnet/roslyn/issues/44263")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTemporary)]
public async Task TopLevelStatement()
{
// Note: we should simplify 'global' as well
// https://github.com/dotnet/roslyn/issues/44420
var code = @"
int val = 0;
int [||]val2 = val + 1;
System.Console.WriteLine(val2);
";

var expected = @"
int val = 0;
global::System.Console.WriteLine(val + 1);
";

// Global statements in regular code are local variables, so Inline Temporary works. Script code is not
// tested because global statements in script code are field declarations, which are not considered
// temporary.
await TestAsync(code, expected, TestOptions.Regular.WithLanguageVersion(LanguageVersionExtensions.CSharp9));
}

[WorkItem(44263, "https://github.com/dotnet/roslyn/issues/44263")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTemporary)]
public async Task TopLevelStatement_InScope()
{
// Note: we should simplify 'global' as well
// https://github.com/dotnet/roslyn/issues/44420
await TestAsync(@"
{
int val = 0;
int [||]val2 = val + 1;
System.Console.WriteLine(val2);
}
",
@"
{
int val = 0;
global::System.Console.WriteLine(val + 1);
}
",
TestOptions.Regular.WithLanguageVersion(LanguageVersionExtensions.CSharp9));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ private SyntaxNode GetScope(VariableDeclaratorSyntax variableDeclarator)
scope = parentExpressions.LastOrDefault().Parent;
}

if (scope.IsKind(SyntaxKind.GlobalStatement))
{
scope = scope.Parent;
}

return scope;
}

Expand Down Expand Up @@ -439,6 +444,12 @@ private SyntaxNode RemoveDeclaratorFromScope(VariableDeclaratorSyntax variableDe
return newScope.ReplaceNode(labeledStatement, newLabeledStatement);
}

// If the local is parented by a global statement, we need to remove the parent global statement.
if (newLocalDeclaration.IsParentKind(SyntaxKind.GlobalStatement, out GlobalStatementSyntax globalStatement))
{
return newScope.RemoveNode(globalStatement, SyntaxRemoveOptions.KeepNoTrivia);
}

return newScope.RemoveNode(newLocalDeclaration, SyntaxRemoveOptions.KeepNoTrivia);
}

Expand Down