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)); }
private SyntaxNode AddFlagAttributeCSharp(Document document, SyntaxNode root, SyntaxNode statement) { var generator = SyntaxGenerator.GetGenerator(document); var flagsAttribute = CSharpSyntaxFactory.Attribute(CSharpSyntaxFactory.ParseName("Flags")); var newStatement = generator.AddAttributes(statement, flagsAttribute); var newRoot = root.ReplaceNode(statement, newStatement); var compilationUnit = (CSharpCompilationUnitSyntax)newRoot; var usingSystemDirective = CSharpSyntaxFactory.UsingDirective(CSharpSyntaxFactory.ParseName("System")); var usingDirectives = compilationUnit.Usings.Select(u => u.Name.GetText().ToString()); if (usingDirectives.All(u => u != usingSystemDirective.Name.GetText().ToString())) { newRoot = generator.AddNamespaceImports(compilationUnit, usingSystemDirective); } return(newRoot); }