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 = GetParameterExpressions(childNodes); var firstArgument = parameterExpressions.FirstOrDefault(); var callerMethod = childNodes.OfType<MemberAccessExpressionSyntax>().FirstOrDefault(); root = ReplaceStaticCallWithExtionMethodCall( root, staticInvocationExpression, firstArgument, callerMethod.Name, CreateArgumentListSyntaxFrom(parameterExpressions.Skip(1)) ).WithAdditionalAnnotations(Formatter.Annotation); SemanticModel semanticModel; if (document.TryGetSemanticModel(out semanticModel)) root = ImportNeededNamespace(root, semanticModel, callerMethod).WithAdditionalAnnotations(Formatter.Annotation); var newDocument = document.WithSyntaxRoot(root); return newDocument; }
private static async Task<ElementOrderingChecks> GetEnabledRulesForDocumentAsync(Document document, CancellationToken cancellationToken) { SemanticModel semanticModel; if (!document.TryGetSemanticModel(out semanticModel)) { semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); } return ElementOrderingChecks.GetElementOrderingChecksForSemanticModel(semanticModel); }