コード例 #1
0
        private static bool AreAllWritesInConstructorsOfDeclaringType(IEnumerable <ExpressionSyntax> writeExpressions, FieldDeclarationSyntax fieldDeclaration,
                                                                      bool shouldBeStatic, SemanticModel model, CancellationToken cancellationToken)
        {
            var typeDeclaration = fieldDeclaration.GetParentTypeDeclaration();

            if (typeDeclaration == null)
            {
                return(false);
            }

            var constructors = typeDeclaration.DescendantNodes().OfType <ConstructorDeclarationSyntax>().ToList();

            if (!constructors.Any())
            {
                return(false);
            }

            var expressions = from expression in writeExpressions
                              let constructor = expression.Ancestors().OfType <ConstructorDeclarationSyntax>().FirstOrDefault()
                                                where constructor != null && constructors.Contains(constructor)
                                                let isStatic = (model.GetDeclaredSymbol(constructor, cancellationToken).IsStatic)
                                                               where isStatic == shouldBeStatic
                                                               select expression;

            return(expressions.Count() == writeExpressions.Count());
        }
        private static SyntaxNode FindInsertionPoint(FieldDeclarationSyntax fieldDeclaration)
        {
            var typeDeclaration     = fieldDeclaration.GetParentTypeDeclaration();
            var existingConstructor = typeDeclaration.DescendantNodes().OfType <ConstructorDeclarationSyntax>().LastOrDefault();

            return(existingConstructor ?? FindInsertionPointBelowFieldGroup(fieldDeclaration));
        }
        private static SyntaxNode FindInsertionPointBelowFieldGroup(FieldDeclarationSyntax fieldDeclaration)
        {
            SyntaxNode insertionPoint  = fieldDeclaration;
            var        typeDeclaration = fieldDeclaration.GetParentTypeDeclaration();

            var tracking = false;

            foreach (var node in typeDeclaration.ChildNodes())
            {
                if (tracking)
                {
                    if (node is FieldDeclarationSyntax)
                    {
                        insertionPoint = node;
                        continue;
                    }

                    break;
                }
                else if (node == fieldDeclaration)
                {
                    tracking = true;
                }
            }

            return(insertionPoint);
        }
        private Task <Document> AddConstructorAsync(Document document, SyntaxNode root, FieldDeclarationSyntax fieldDeclaration)
        {
            var variableDeclaration = fieldDeclaration.DescendantNodes().OfType <VariableDeclarationSyntax>().FirstOrDefault();
            var variableDeclarator  = variableDeclaration.DescendantNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault();
            var fieldName           = variableDeclarator.Identifier.ValueText;

            var typeDeclaration = fieldDeclaration.GetParentTypeDeclaration();

            var newConstructor = CreateEmptyConstructor(typeDeclaration.Identifier.ValueText);
            var parameterName  = nameGenerator.GetNewParameterName(newConstructor.ParameterList, fieldName);

            newConstructor = newConstructor.WithParameterList(
                ParameterList(
                    SingletonSeparatedList(
                        Parameter(Identifier(parameterName))
                        .WithType(variableDeclaration.Type))));

            var assignment = newConstructor.ParameterList.Parameters.Any(p => p.Identifier.ValueText == fieldName)
                    ? CreateThisAssignmentStatement(fieldName, parameterName)
                    : CreateAssignmentStatement(fieldName, parameterName);

            newConstructor = newConstructor.WithBody(newConstructor.Body.AddStatements(assignment));
            newConstructor = InitializeStructFields(fieldDeclaration, typeDeclaration, newConstructor);

            var newRoot = root.InsertNodesAfter(FindInsertionPoint(fieldDeclaration), SingletonList(newConstructor));

            return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
        }