private BlockSyntax ReplaceNodes(BlockSyntax oldBody)
        {
            List <SyntaxNode> nodesToTrack = this.markedNodes;
            var continueSts = continueStatements.SelectMany(x => x.Value);
            var breakSts    = breakStatements.SelectMany(x => x.Value);

            nodesToTrack.AddRange(continueSts);
            nodesToTrack.AddRange(breakSts);
            oldBody = oldBody.TrackNodes(nodesToTrack);
            foreach (var tuple in this.whileReplacementNodes)
            {
                var currentA = oldBody.GetCurrentNode(tuple.Item1);
                if (currentA != null)
                {
                    //create new statement
                    var localCondition  = tuple.Item3 as LocalDeclarationStatementSyntax;
                    var initializer     = localCondition.Declaration.Variables.First();
                    var updateCondition = SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                                                                                               SyntaxFactory.IdentifierName(initializer.Identifier), initializer.Initializer.Value));

                    var whileStatement = currentA.Parent;
                    oldBody = oldBody.InsertNodesBefore(whileStatement, new List <SyntaxNode>()
                    {
                        tuple.Item3
                    });
                    var currentB = oldBody.GetCurrentNode(tuple.Item1);
                    oldBody = oldBody.ReplaceNode(currentB, tuple.Item2);
                    //update continue statements
                    foreach (var cont in continueStatements[tuple.Item4])
                    {
                        var currentContinue = oldBody.GetCurrentNode(cont);
                        oldBody = oldBody.ReplaceNode(currentContinue, new List <SyntaxNode>()
                        {
                            updateCondition, cont
                        });
                    }
                    //update break statements
                    foreach (var brk in breakStatements[tuple.Item4])
                    {
                        var currentBreak        = oldBody.GetCurrentNode(brk);
                        var invalidateCondition = SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                                                                                                       SyntaxFactory.IdentifierName(initializer.Identifier), SyntaxFactoryExtensions.BooleanLiteralExpression(false)));
                        oldBody = oldBody.ReplaceNode(currentBreak, new List <SyntaxNode>()
                        {
                            invalidateCondition, SyntaxFactory.ContinueStatement()
                        });
                    }
                    var currentWhile = oldBody.GetCurrentNode(tuple.Item4);
                    //modify body
                    var whileBody = currentWhile.Statement as BlockSyntax;

                    var newStatements = whileBody.Statements.Add(updateCondition);
                    whileBody = whileBody.WithStatements(newStatements);
                    //updateWhile
                    var newWhile = currentWhile.WithStatement(whileBody);
                    oldBody = oldBody.ReplaceNode(currentWhile, newWhile);
                }
            }
            return(oldBody);
        }
        private StatementSyntax SectionToIf(ExpressionSyntax condition, SwitchSectionSyntax section)
        {
            SwitchBreakRefactoringVisitor breakRefactoring = new SwitchBreakRefactoringVisitor();
            var bodyList = section.Statements.First();

            bodyList = (StatementSyntax)breakRefactoring.Visit(bodyList);

            bool hasDefault = false;
            List <ExpressionSyntax> labelExpressions = new List <ExpressionSyntax>();

            foreach (var label in section.Labels)
            {
                if (label.Keyword.Kind() == SyntaxKind.DefaultKeyword)
                {
                    hasDefault = true;
                }

                foreach (var child in label.ChildNodes())
                {
                    var labelCondition = (ExpressionSyntax)child;
                    if ((labelCondition.Kind() != SyntaxKind.NumericLiteralExpression) ||
                        (labelCondition.Kind() != SyntaxKind.StringLiteralExpression) ||
                        (labelCondition.Kind() != SyntaxKind.CharacterLiteralExpression) ||
                        (labelCondition.Kind() != SyntaxKind.SimpleMemberAccessExpression)
                        )
                    {
                        labelCondition = SyntaxFactory.ParenthesizedExpression(labelCondition).WithTriviaFrom(labelCondition);
                    }
                    labelExpressions.Add(labelCondition);
                    break;
                }
            }

            List <ExpressionSyntax> flagConditions = new List <ExpressionSyntax>();

            foreach (var expression in labelExpressions)
            {
                var memberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, condition,
                                                                        SyntaxFactory.IdentifierName("Equals"));
                var arg     = SyntaxFactory.Argument(expression);
                var argList = SyntaxFactory.SeparatedList <ArgumentSyntax>(new List <ArgumentSyntax>()
                {
                    arg
                });

                var invocation = SyntaxFactory.InvocationExpression(memberAccess, SyntaxFactory.ArgumentList(argList));

                flagConditions.Add(invocation);
            }

            ExpressionSyntax ifCondition = SyntaxFactoryExtensions.BooleanLiteralExpression(true);

            if (flagConditions.Count == 1)
            {
                ifCondition = flagConditions[0];
            }

            BinaryExpressionSyntax boolCondition = null;

            for (int i = 1; i < flagConditions.Count; i++)
            {
                var right = flagConditions[i];
                if (boolCondition == null)
                {
                    boolCondition = SyntaxFactory.BinaryExpression(SyntaxKind.LogicalOrExpression, flagConditions[0], right);
                }
                else
                {
                    boolCondition = SyntaxFactory.BinaryExpression(SyntaxKind.LogicalOrExpression, boolCondition, right);
                }
                ifCondition = boolCondition;
            }

            if (hasDefault)
            {
//                ifCondition = SyntaxFactory.BinaryExpression(SyntaxKind.LogicalOrExpression, ifCondition, SyntaxFactoryExtensions.BooleanLiteralExpression(true));
                return(bodyList);
            }

            var ifStatement = SyntaxFactory.IfStatement(ifCondition, bodyList);

            return(ifStatement);
        }