internal override Task <IEnumerable <CodeAction> > GetFixesAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, CancellationToken cancellationToken) { var actions = ImmutableArray.CreateBuilder <CodeAction>(); // Fix 1: Add a NonSerialized attribute to the field var fieldNode = GetFieldDeclarationNode(nodeToFix); if (fieldNode != null) { var generator = SyntaxGenerator.GetGenerator(document); var codeAction = new MyDocumentCodeAction(FxCopFixersResources.AddNonSerializedAttribute, async ct => await AddNonSerializedAttribute(document, model, root, fieldNode, generator).ConfigureAwait(false)); actions.Add(codeAction); // Fix 2: If the type of the field is defined in source, then add the serializable attribute to the type. var fieldSymbol = model.GetDeclaredSymbol(nodeToFix, cancellationToken) as IFieldSymbol; var type = fieldSymbol.Type; if (type.Locations.Any(l => l.IsInSource)) { var typeCodeAction = new MySolutionCodeAction(FxCopFixersResources.AddSerializableAttribute, async ct => await AddSerializableAttributeToType(document, model, generator, type, cancellationToken).ConfigureAwait(false)); actions.Add(typeCodeAction); } } return(Task.FromResult <IEnumerable <CodeAction> >(actions.ToImmutable())); }
internal override Task<IEnumerable<CodeAction>> GetFixesAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, CancellationToken cancellationToken) { var actions = ImmutableArray.CreateBuilder<CodeAction>(); // Fix 1: Add a NonSerialized attribute to the field var fieldNode = GetFieldDeclarationNode(nodeToFix); if (fieldNode != null) { var generator = SyntaxGenerator.GetGenerator(document); var codeAction = new MyDocumentCodeAction(FxCopFixersResources.AddNonSerializedAttribute, async ct => await AddNonSerializedAttribute(document, model, root, fieldNode, generator).ConfigureAwait(false)); actions.Add(codeAction); // Fix 2: If the type of the field is defined in source, then add the serializable attribute to the type. var fieldSymbol = model.GetDeclaredSymbol(nodeToFix, cancellationToken) as IFieldSymbol; var type = fieldSymbol.Type; if (type.Locations.Any(l => l.IsInSource)) { var typeCodeAction = new MySolutionCodeAction(FxCopFixersResources.AddSerializableAttribute, async ct => await AddSerializableAttributeToType(document, model, generator, type, cancellationToken).ConfigureAwait(false)); actions.Add(typeCodeAction); } } return Task.FromResult<IEnumerable<CodeAction>>(actions.ToImmutable()); }
internal async override Task <IEnumerable <CodeAction> > GetFixesAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, CancellationToken cancellationToken) { IEnumerable <CodeAction> actions = null; // Fix 1: Add a NonSerialized attribute to the field var fieldNode = GetFieldDeclarationNode(nodeToFix); if (fieldNode != null) { var generator = SyntaxGenerator.GetGenerator(document); var attr = generator.Attribute(generator.TypeExpression(WellKnownTypes.NonSerializedAttribute(model.Compilation))); var newNode = generator.AddAttributes(fieldNode, attr); var newDocument = document.WithSyntaxRoot(root.ReplaceNode(fieldNode, newNode)); var codeAction = new MyDocumentCodeAction(FxCopFixersResources.AddNonSerializedAttribute, newDocument); actions = SpecializedCollections.SingletonEnumerable(codeAction); // Fix 2: If the type of the field is defined in source, then add the serializable attribute to the type. var fieldSymbol = model.GetDeclaredSymbol(nodeToFix) as IFieldSymbol; var type = fieldSymbol.Type; if (type.Locations.Any(l => l.IsInSource)) { var typeDeclNode = type.DeclaringSyntaxReferences.First().GetSyntax(); var serializableAttr = generator.Attribute(generator.TypeExpression(WellKnownTypes.SerializableAttribute(model.Compilation))); var newTypeDeclNode = generator.AddAttributes(typeDeclNode, serializableAttr); var documentContainingNode = document.Project.Solution.GetDocument(typeDeclNode.SyntaxTree); var docRoot = await documentContainingNode.GetSyntaxRootAsync(cancellationToken); var newDocumentContainingNode = documentContainingNode.WithSyntaxRoot(docRoot.ReplaceNode(typeDeclNode, newTypeDeclNode)); var typeCodeAction = new MySolutionCodeAction(FxCopFixersResources.AddSerializableAttribute, newDocumentContainingNode.Project.Solution); actions = actions.Concat(typeCodeAction); } } return(actions); }