public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var expression = root.FindNode(diagnosticSpan, false, true); if (!TryGetInvocationExpression(expression, out SyntaxNode invocationExpression)) { return; } SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(); var durableVersion = SyntaxNodeUtils.GetDurableVersion(semanticModel); if (SyntaxNodeUtils.IsInsideOrchestrator(invocationExpression) && durableVersion.Equals(DurableVersion.V2)) { if (CodeFixProviderUtils.TryGetDurableOrchestrationContextVariableName(invocationExpression, out string variableName)) { var newExpression = ""; if (TryGetMillisecondsParameter(invocationExpression, out string milliseconds)) { if (TryGetCancellationTokenParameter(invocationExpression, semanticModel, out string cancellationToken)) { newExpression = variableName + ".CreateTimer(" + variableName + ".CurrentUtcDateTime.AddMilliseconds(" + milliseconds + "), " + cancellationToken + ")"; } else { newExpression = variableName + ".CreateTimer(" + variableName + ".CurrentUtcDateTime.AddMilliseconds(" + milliseconds + "), CancellationToken.None)"; } } else if (TryGetTimespanParameter(invocationExpression, semanticModel, out string timeSpan)) { if (TryGetCancellationTokenParameter(invocationExpression, semanticModel, out string cancellationToken)) { newExpression = variableName + ".CreateTimer(" + variableName + ".CurrentUtcDateTime.Add(" + timeSpan + "), " + cancellationToken + ")"; } else { newExpression = variableName + ".CreateTimer(" + variableName + ".CurrentUtcDateTime.Add(" + timeSpan + "), CancellationToken.None)"; } } context.RegisterCodeFix( CodeAction.Create(FixTimerInOrchestrator.ToString(), c => ReplaceWithAwaitExpressionAsync(context.Document, expression, c, newExpression), nameof(TimerCodeFixProvider)), diagnostic); } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var expression = root.FindNode(diagnosticSpan); if (SyntaxNodeUtils.IsInsideOrchestrationTrigger(expression)) { if (CodeFixProviderUtils.TryGetDurableOrchestrationContextVariableName(expression, out string variableName)) { context.RegisterCodeFix( CodeAction.Create(FixDateTimeInOrchestrator.ToString(), c => CodeFixProviderUtils.ReplaceWithIdentifierAsync(context.Document, expression, c, variableName + ".CurrentUtcDateTime"), nameof(DateTimeCodeFixProvider)), diagnostic); } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var expression = root.FindNode(diagnosticSpan) as InvocationExpressionSyntax; if (expression == null) { return; } if (SyntaxNodeUtils.IsInsideOrchestrator(expression)) { if (CodeFixProviderUtils.TryGetDurableOrchestrationContextVariableName(expression, out string variableName)) { context.RegisterCodeFix( CodeAction.Create(FixGuidInOrchestrator.ToString(), c => ReplaceWithInvocationExpressionAsync(context.Document, expression, c, variableName + ".NewGuid()"), nameof(GuidCodeFixProvider)), diagnostic); } } }