Пример #1
0
            public override SyntaxNode VisitBlock(BlockSyntax node)
            {
                node = (BlockSyntax)base.VisitBlock(node);

                var usingStatements = node
                                      .Descendants <UsingStatementSyntax>()
                                      .ToList();

                foreach (var usingStatement in usingStatements)
                {
                    var position      = node.Statements.IndexOf(usingStatement);
                    var newStatements = node.Statements.RemoveAt(position);

                    var declarationStatement = SyntaxFactory.LocalDeclarationStatement(
                        new SyntaxToken(),
                        SyntaxFactory.Token(SyntaxKind.UsingKeyword).WithTrailingTrivia(SyntaxFactory.Space),
                        new SyntaxTokenList(),
                        usingStatement.Declaration,
                        SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                                               .WithTriviaFrom(usingStatement);

                    newStatements = newStatements.Insert(position, declarationStatement);

                    var currentPosition = position + 1;
                    foreach (var statement in usingStatement.Statement.Descendants <StatementSyntax>())
                    {
                        newStatements = newStatements.Insert(currentPosition, statement);
                        currentPosition++;
                    }

                    node = node.WithStatements(newStatements);
                }

                return(node);
            }
Пример #2
0
        public override SyntaxNode VisitBlock(BlockSyntax node)
        {
            node = (BlockSyntax)base.VisitBlock(node);

            var matchingInvocations = node
                                      .Descendants <InvocationExpressionSyntax>()
                                      .Where(i => InvocationMatches(i, _method.Type, _method.Name))
                                      .ToList();

            foreach (var invocation in matchingInvocations)
            {
                var visitor = new ReplaceMethodVisitor(_context, _method, _newMethod1);
                node = node.ReplaceNode(
                    invocation,
                    visitor.VisitInvocationExpression(invocation)
                    .WithTriviaFrom(invocation));

                var variable = invocation.Expression
                               .Descendants <IdentifierNameSyntax>()
                               .First();

                node = node.AddStatements(ExpressionStatement(
                                              CreateInvocation(variable.ToString(), _newMethod2.Name, _newMethod2.Arguments))
                                          .WithTriviaFrom(node.Statements.Last()));
            }

            return(node);
        }
Пример #3
0
            public override SyntaxNode VisitBlock(BlockSyntax node)
            {
                node = (BlockSyntax)base.VisitBlock(node);

                var switchStatements = node
                                       .Descendants <SwitchStatementSyntax>();

                foreach (var switchStatement in switchStatements)
                {
                    var governingExpression = switchStatement.Expression
                                              .WithTrailingTrivia(Space);

                    var arms = switchStatement.Sections
                               .Select(ConstructSwitchArm)
                               .ToList();

                    var armsList = SeparatedList(
                        arms,
                        Enumerable.Repeat(arms, arms.Count - 1)
                        .Select(a => Token(
                                    SyntaxTriviaList.Empty,
                                    SyntaxKind.CommaToken,
                                    SyntaxTriviaList.Create(EndOfLine(Environment.NewLine)))));

                    var switchExpression = SwitchExpression(
                        governingExpression,
                        Token(SyntaxKind.SwitchKeyword).WithTrailingTrivia(EndOfLine(Environment.NewLine)),
                        switchStatement.OpenBraceToken,
                        armsList,
                        switchStatement.CloseBraceToken.WithTrailingTrivia(SyntaxTriviaList.Empty))
                                           .WithLeadingTrivia(switchStatement.GetLeadingTrivia());

                    var position = node.Statements.IndexOf(switchStatement);

                    node = node.WithStatements(
                        node.Statements
                        .Remove(switchStatement)
                        .Insert(
                            position,
                            ExpressionStatement(switchExpression)
                            .WithTrailingTrivia(EndOfLine(Environment.NewLine))));
                }

                return(node);
            }