private static CompilationUnitSyntax ReplaceStaticCallWithExtionMethodCall(CompilationUnitSyntax root, InvocationExpressionSyntax staticInvocationExpression, ExpressionSyntax sourceExpression, SimpleNameSyntax methodName, ArgumentListSyntax argumentList) { var extensionInvocationExpression = CallExtensionMethodAsExtensionAnalyzer.CreateInvocationExpression(sourceExpression, methodName, argumentList) .WithLeadingTrivia(staticInvocationExpression.GetLeadingTrivia()); return(root.ReplaceNode(staticInvocationExpression, extensionInvocationExpression)); }
private static async Task <Document> CallAsExtensionAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var root = (CompilationUnitSyntax)await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var diagnosticSpan = diagnostic.Location.SourceSpan; var staticInvocationExpression = root .FindToken(diagnosticSpan.Start) .Parent.AncestorsAndSelf() .OfType <InvocationExpressionSyntax>() .First(); var childNodes = staticInvocationExpression.ChildNodes(); var parameterExpressions = CallExtensionMethodAsExtensionAnalyzer.GetParameterExpressions(childNodes); var firstArgument = parameterExpressions.FirstOrDefault(); var callerMethod = childNodes.OfType <MemberAccessExpressionSyntax>().FirstOrDefault(); root = ReplaceStaticCallWithExtionMethodCall( root, staticInvocationExpression, firstArgument, callerMethod.Name, CallExtensionMethodAsExtensionAnalyzer.CreateArgumentListSyntaxFrom(parameterExpressions.Skip(1)) ).WithAdditionalAnnotations(Formatter.Annotation); var semanticModel = await document.GetSemanticModelAsync(); root = ImportNeededNamespace(root, semanticModel, callerMethod).WithAdditionalAnnotations(Formatter.Annotation); var newDocument = document.WithSyntaxRoot(root); return(newDocument); }