示例#1
0
        private static SwitchSectionSyntax CreateNewSection(SwitchSectionSyntax section)
        {
            var expressionStatement = (ExpressionStatementSyntax)GetLastStatementBeforeBreakStatementOrDefault(section);

            var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression;

            section = section.ReplaceNode(expressionStatement, ReturnStatement(assignment.Right).WithTriviaFrom(expressionStatement));

            return(section.RemoveStatement(GetStatements(section).Last()));
        }
            static SwitchSectionSyntax CreateNewSection(SwitchSectionSyntax section)
            {
                SyntaxList <StatementSyntax> statements = section.GetStatements();

                if (statements.Last().IsKind(SyntaxKind.ThrowStatement))
                {
                    return(section);
                }

                var expressionStatement = (ExpressionStatementSyntax)statements.LastButOne();

                var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression;

                section = section.ReplaceNode(expressionStatement, ReturnStatement(assignment.Right).WithTriviaFrom(expressionStatement));

                return(section.RemoveStatement(section.GetStatements().Last()));
            }
示例#3
0
        public static Task <Document> RefactorAsync(
            Document document,
            SwitchSectionSyntax switchSection,
            CancellationToken cancellationToken)
        {
            SwitchSectionSyntax newNode = switchSection;

            SyntaxList <StatementSyntax> statements = switchSection.Statements;

            if (statements.Count == 1 &&
                statements.First().IsKind(SyntaxKind.Block))
            {
                var block = (BlockSyntax)statements.First();
                newNode = newNode.ReplaceNode(block, block.AddStatements(BreakStatement()));
            }
            else
            {
                newNode = switchSection.AddStatements(BreakStatement());
            }

            return(document.ReplaceNodeAsync(switchSection, newNode, cancellationToken));
        }