private async Task <Document> AddAnnotation(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var root = await document.GetSyntaxRootAsync().ConfigureAwait(false); var methodDeclaration = root.FindToken(diagnostic.Location.SourceSpan.Start).Parent .AncestorsAndSelf() .OfType <MethodDeclarationSyntax>() .First(); if (methodDeclaration == null) { return(document); } var attributesList = methodDeclaration.AttributeLists[0]; var annotationValidate = SF.AttributeList() .AddAttributes(SF.Attribute(SF.IdentifierName("ValidateAntiForgeryToken"))) .WithLeadingTrivia(CodeFixUtil.KeepLastLine(attributesList.GetLeadingTrivia())); var nodes = new List <SyntaxNode> { annotationValidate }; var newRoot = root.InsertNodesAfter(attributesList, nodes); return(document.WithSyntaxRoot(newRoot)); }