Skip to content

Commit

Permalink
Merge pull request #61129 from DoctorKrolic/generate-parameter-before…
Browse files Browse the repository at this point in the history
…-cancellation-token

Generate parameter before cancellation token in async methods
  • Loading branch information
CyrusNajmabadi authored May 4, 2022
2 parents 4de4270 + 2b61806 commit e1f2e7d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9724,5 +9724,59 @@ void Main()
var x = [|{keyword}|];
}}");
}

[WorkItem(60842, "https://github.com/dotnet/roslyn/issues/60842")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)]
public async Task TestGenerateParameterBeforeCancellationToken_OneParameter()
{
await TestInRegularAndScriptAsync(
@"using System.Threading;
using System.Threading.Tasks;
class C
{
public async Task M(CancellationToken cancellationToken)
{
await Task.Delay([|time|]);
}
}",
@"using System.Threading;
using System.Threading.Tasks;
class C
{
public async Task M(System.TimeSpan time, CancellationToken cancellationToken)
{
await Task.Delay(time);
}
}", index: 4);
}

[WorkItem(60842, "https://github.com/dotnet/roslyn/issues/60842")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)]
public async Task TestGenerateParameterBeforeCancellationToken_SeveralParameters()
{
await TestInRegularAndScriptAsync(
@"using System.Threading;
using System.Threading.Tasks;
class C
{
public async Task M(string someParameter, CancellationToken cancellationToken)
{
await Task.Delay([|time|]);
}
}",
@"using System.Threading;
using System.Threading.Tasks;
class C
{
public async Task M(string someParameter, System.TimeSpan time, CancellationToken cancellationToken)
{
await Task.Delay(time);
}
}", index: 4);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2990,5 +2990,49 @@ count:=5)
End Sub
End Namespace")
End Function

<WorkItem(60842, "https://github.com/dotnet/roslyn/issues/60842")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)>
Public Async Function TestGenerateParameterBeforeCancellationToken_OneParameter() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading
Imports System.Threading.Tasks

Class Test
Private Async Function Test(token As CancellationToken) As Task
Await Task.Delay([|time|])
End Function
End Class",
"Imports System.Threading
Imports System.Threading.Tasks

Class Test
Private Async Function Test(time As System.TimeSpan, token As CancellationToken) As Task
Await Task.Delay(time)
End Function
End Class", index:=4)
End Function

<WorkItem(60842, "https://github.com/dotnet/roslyn/issues/60842")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)>
Public Async Function TestGenerateParameterBeforeCancellationToken_SeveralParameters() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading
Imports System.Threading.Tasks

Class Test
Private Async Function Test(someParameter As String, token As CancellationToken) As Task
Await Task.Delay([|time|])
End Function
End Class",
"Imports System.Threading
Imports System.Threading.Tasks

Class Test
Private Async Function Test(someParameter As String, time As System.TimeSpan, token As CancellationToken) As Task
Await Task.Delay(time)
End Function
End Class", index:=4)
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ private class GenerateParameterCodeAction : CodeAction
private readonly Document _document;
private readonly State _state;
private readonly bool _includeOverridesAndImplementations;
private readonly int _parameterIndex;

public GenerateParameterCodeAction(Document document, State state, bool includeOverridesAndImplementations)
public GenerateParameterCodeAction(Document document, State state, bool includeOverridesAndImplementations, int parameterIndex)
{
_document = document;
_state = state;
_includeOverridesAndImplementations = includeOverridesAndImplementations;
_parameterIndex = parameterIndex;
}

public override string Title
Expand All @@ -47,7 +49,7 @@ public override string Title
_state.LocalType,
RefKind.None,
_state.IdentifierToken.ValueText,
newParameterIndex: null,
_parameterIndex,
_includeOverridesAndImplementations,
cancellationToken).AsNullable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,22 @@ private static async Task AddParameterCodeActionsAsync(
return;
}

result.Add(new GenerateParameterCodeAction(document, state, includeOverridesAndImplementations: false));
var containingMethod = state.ContainingMethod;
var parameterIndex = containingMethod.Parameters.Length;

if (containingMethod.Parameters.Length > 0)
{
var compilation = await document.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);
var lastParameterIsCancellationToken = Equals(containingMethod.Parameters.Last().Type, compilation.CancellationTokenType());

if (lastParameterIsCancellationToken)
parameterIndex--;
}

result.Add(new GenerateParameterCodeAction(document, state, includeOverridesAndImplementations: false, parameterIndex));

if (AddParameterService.HasCascadingDeclarations(state.ContainingMethod))
result.Add(new GenerateParameterCodeAction(document, state, includeOverridesAndImplementations: true));
result.Add(new GenerateParameterCodeAction(document, state, includeOverridesAndImplementations: true, parameterIndex));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.PooledObjects;

Expand Down Expand Up @@ -220,5 +221,8 @@ public static ImmutableArray<IAssemblySymbol> GetReferencedAssemblySymbols(this

public static INamedTypeSymbol? DataContractAttribute(this Compilation compilation)
=> compilation.GetTypeByMetadataName(typeof(DataContractAttribute).FullName!);

public static INamedTypeSymbol? CancellationTokenType(this Compilation compilation)
=> compilation.GetTypeByMetadataName(typeof(CancellationToken).FullName!);
}
}

0 comments on commit e1f2e7d

Please sign in to comment.