public override SyntaxNode Visit(SyntaxNode node)
        {
            // Check each node until we find the property's parent. When  we've found it, let's add a private field to it
            if (_propertyParent.IsEquivalentTo(node))
            {
                var variableDeclarator = SyntaxFactory.VariableDeclarator(
                                                    SyntaxFactory.Identifier(this.PrivateFieldName));

                if (_crunchedProperty.IsExpressionProperty())
                {
                    variableDeclarator = variableDeclarator.WithInitializer(
                        SyntaxFactory.EqualsValueClause(
                            SyntaxFactory.Token(SyntaxKind.EqualsToken),
                            _crunchedProperty.ExpressionBody.Expression));
                }

                // Create the private "backing field" of the property
                var privateField = SyntaxFactory.FieldDeclaration(
                                        SyntaxFactory.VariableDeclaration(_crunchedProperty.Type, // Property Type
                                            SyntaxFactory.SeparatedList(
                                                new[] { variableDeclarator }))) // Field Name
                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)); // Modifier (always private)

                // Insert the backing field just ahead of the original property
                var newParent = node.InsertNodesBefore(_crunchedProperty, new[] { privateField });

                return base.Visit(newParent); // Return the new parent which replaces the original.
            }
            return base.Visit(node);
        }
        private static SyntaxNode MoveMember(SyntaxNode syntaxRoot, MemberDeclarationSyntax member, MemberDeclarationSyntax targetMember, IndentationOptions indentationOptions)
        {
            var firstToken = syntaxRoot.GetFirstToken();
            var fileHeader = GetFileHeader(firstToken.LeadingTrivia);
            syntaxRoot = syntaxRoot.TrackNodes(member, targetMember, firstToken.Parent);
            var memberToMove = syntaxRoot.GetCurrentNode(member);
            var targetMemberTracked = syntaxRoot.GetCurrentNode(targetMember);
            if (!memberToMove.HasLeadingTrivia)
            {
                var targetIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, targetMember);
                var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, targetIndentationLevel);
                memberToMove = memberToMove.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString));
            }

            if (!HasLeadingBlankLines(targetMember)
                && HasLeadingBlankLines(member))
            {
                memberToMove = memberToMove.WithTrailingTrivia(memberToMove.GetTrailingTrivia().Add(SyntaxFactory.CarriageReturnLineFeed));
                memberToMove = memberToMove.WithLeadingTrivia(GetLeadingTriviaWithoutLeadingBlankLines(memberToMove));
            }

            syntaxRoot = syntaxRoot.InsertNodesBefore(targetMemberTracked, new[] { memberToMove });
            var fieldToMoveTracked = syntaxRoot.GetCurrentNodes(member).Last();
            syntaxRoot = syntaxRoot.RemoveNode(fieldToMoveTracked, SyntaxRemoveOptions.KeepNoTrivia);
            if (fileHeader.Any())
            {
                var oldFirstToken = syntaxRoot.GetCurrentNode(firstToken.Parent).ChildTokens().First();
                syntaxRoot = syntaxRoot.ReplaceToken(oldFirstToken, oldFirstToken.WithLeadingTrivia(StripFileHeader(oldFirstToken.LeadingTrivia)));
                var newFirstToken = syntaxRoot.GetFirstToken();
                syntaxRoot = syntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(fileHeader.AddRange(newFirstToken.LeadingTrivia)));
            }

            return syntaxRoot;
        }
 internal static SyntaxNode AddBefore(SyntaxNode root, SyntaxNode loationToAddBefore, SyntaxNode nodeToAdd)
 {
     return root.InsertNodesBefore(
         root.GetCurrentNode(loationToAddBefore)
         , new List<SyntaxNode> { nodeToAdd });
 }
Esempio n. 4
0
        private static decimal ProcessObjectInitializer(ObjectCreationExpressionSyntax target,
            decimal lastTemporaryIndex, ref SyntaxNode newRoot, ref SyntaxNode[] originalObjectExpressionSyntax,
            ref IEnumerable<KeyValuePair<SyntaxNode, SyntaxNode>> replacedNodes, int depth = 0)
        {
            depth++;
            var objectCreation = target;

            if (objectCreation.Initializer.IsKind(SyntaxKind.ObjectInitializerExpression))
            {
                var initializerInfo = GetModel(target).GetTypeInfo(target);

                var tempName = initializerInfo.Type.Name + "_helper_" + lastTemporaryIndex++;

                var assignments = new List<SyntaxNode>();

                foreach (var expression in objectCreation.Initializer.Expressions)
                {
                    var localExpression = (expression as AssignmentExpressionSyntax);
                    if (localExpression == null)
                        continue;

                    var right = localExpression.Right;

                    var targets =
                        expression.DescendantNodes()
                            .OfType<ObjectCreationExpressionSyntax>()
                            .Where(k => k.Ancestors().OfType<ObjectCreationExpressionSyntax>().First() == target);

                    var objectCreationExpressionSyntaxs = targets as ObjectCreationExpressionSyntax[] ??
                                                          targets.ToArray();

                    var noriginalObjectExpressionSyntax =
                        objectCreationExpressionSyntaxs.ToArray<SyntaxNode>().ToArray();

                    foreach (var ntarget in noriginalObjectExpressionSyntax)
                    {
                        lastTemporaryIndex = ProcessObjectInitializer((ObjectCreationExpressionSyntax)ntarget,
                            lastTemporaryIndex, ref newRoot,
                            ref originalObjectExpressionSyntax, ref replacedNodes, depth);
                    }

                    if (noriginalObjectExpressionSyntax.Any())
                    {
                        bool contained;
                        try
                        {
                            newRoot.GetCurrentNode(right);
                            contained = true;
                        }
                        catch (Exception)
                        {
                            contained = false;
                        }

                        if (contained)
                        {
                            if (right is MemberAccessExpressionSyntax) // Since Roslyn 1.0.0, old method doesnt work ...
                            {
                                var mar = right as MemberAccessExpressionSyntax;

                                contained = !(mar.Expression.ToString() == replacedNodes.Last().Key.ToString());
                            }
                            else if (right is ObjectCreationExpressionSyntax)
                            {
                                var mar = right as ObjectCreationExpressionSyntax;

                                contained =
                                    !mar.Initializer.DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Any();
                                //!(mar.ToString () == replacedNodes.Last ().Key.ToString ());
                            }
                        }

                        if (!contained || replacedNodes.Count() == 1) // We been replaced 
                        {
                            try
                            {
                                var str = replacedNodes.Last().Value.ToFullString();
                                if (right is MemberAccessExpressionSyntax)
                                {
                                    var mar = right as MemberAccessExpressionSyntax;
                                    right = SyntaxFactory.ParseExpression(str + "." + mar.Name.ToFullString());
                                }
                                else
                                {
                                    // for

                                    // replacedNodes.Any(k => k.Key == right);
                                    right = SyntaxFactory.ParseExpression(str);
                                    // (ExpressionSyntax)replacedNodes.FirstOrDefault(o => o.Key.ToFullString() == right.ToFullString()).Value;
                                }

                                replacedNodes = replacedNodes.Except(replacedNodes.Last());
                            }
                            catch (Exception)
                            {
                                //      throw;
                            }
                        }
                    }

                    var newAssignment =
                        SyntaxFactory.ExpressionStatement(
                            SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                SyntaxFactory.MemberAccessExpression(
                                    SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(
                                        tempName), SyntaxFactory.IdentifierName(
                                        localExpression.Left.As<IdentifierNameSyntax>()
                                                .ToFullString())), right)
                        ).NormalizeWhitespace();

                    assignments.Add(newAssignment);
                    //  var varName = declarator.Identifier.Text + "_helper_"+ e + lastTemporaryIndex++;
                }

                var statements = new List<StatementSyntax>();

                ObjectCreationExpressionSyntax newObjectCreation;
                if (objectCreation.ArgumentList == null ||
                    (!objectCreation.ArgumentList.Arguments.Any()))
                {
                    var text = objectCreation.WithInitializer(null).ToFullString().Trim();
                    if (!text.EndsWith("()"))
                        text += "()";

                    var exp = SyntaxFactory.ParseExpression(text);
                    newObjectCreation = (ObjectCreationExpressionSyntax)exp;
                    //SyntaxFactory.ObjectCreationExpression(objectCreation.Type);
                }
                else
                {
                    newObjectCreation =
                        objectCreation.WithInitializer(null).WithArgumentList(objectCreation.ArgumentList);
                }

                var local = SyntaxFactory.LocalDeclarationStatement(
                                SyntaxFactory.VariableDeclaration(
                                    target.Type,
                                    SyntaxFactory.SeparatedList(new[]
                            {
                                SyntaxFactory.VariableDeclarator(
                                    SyntaxFactory.Identifier(tempName),
                                    null,
                                    SyntaxFactory.EqualsValueClause(newObjectCreation))
                            })
                                )
                            ).NormalizeWhitespace();

                statements.Add(local);
                statements.AddRange(assignments.Cast<StatementSyntax>());

                var listS = SyntaxFactory.List(statements);

                var newList2 = new List<ObjectCreationExpressionSyntax>();
                var union2 = originalObjectExpressionSyntax.Union(new SyntaxNode[] { target });
                foreach (var statementSyntax in union2)
                {
                    try
                    {
                        newList2.Add((ObjectCreationExpressionSyntax)newRoot.GetCurrentNode(statementSyntax));
                    }
                    catch (Exception ex)
                    {
                    }
                }

                if (!newList2.Contains(target))
                {
                    //                 
                    originalObjectExpressionSyntax =
                        originalObjectExpressionSyntax.Union(new SyntaxNode[] { target }).ToArray();
                }

                originalObjectExpressionSyntax = originalObjectExpressionSyntax.Where(l => l != null).ToArray();
                newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);
                try
                {
                    var currentNode = newRoot.GetCurrentNode(target);

                    var lastStatement = currentNode.Ancestors().OfType<StatementSyntax>().First();

                    newRoot = newRoot.InsertNodesBefore(lastStatement, listS);

                    //  var currentNodes = newRoot.GetCurrentNodes<SyntaxNode>(originalObjectExpressionSyntax);
                    newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);

                    currentNode = newRoot.GetCurrentNode(target);
                    var newName = SyntaxFactory.ParseExpression(tempName);

                    var nDict = new Dictionary<SyntaxNode, SyntaxNode>();
                    foreach (var replacedNode in replacedNodes)
                        nDict[replacedNode.Key] = replacedNode.Value;

                    nDict[currentNode] = newName;
                    replacedNodes = nDict;
                    // newRoot = newRoot;
                    newRoot = newRoot.ReplaceNode(currentNode, newName);

                    var newList = new List<ObjectCreationExpressionSyntax>();
                    var union = originalObjectExpressionSyntax.Union(nDict.Values); //.Union(new SyntaxNode[]{newName});
                    foreach (var statementSyntax in union)
                    {
                        try
                        {
                            //                          if(statementSyntax==newName)
                            //                          {
                            //                              newList.Add((ObjectCreationExpressionSyntax) (newName));
                            //                              continue;
                            //                          }

                            var syntaxNode = newRoot.GetCurrentNode(statementSyntax);
                            newList.Add((ObjectCreationExpressionSyntax)syntaxNode);
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    originalObjectExpressionSyntax = newList.ToArray();
                    newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);
                }
                catch (Exception ex)
                {
                }
            }

            return lastTemporaryIndex;
        }