Exemple #1
0
        private static async Task <Document> RemoveEmptyFinallyClauseAsync(
            Document document,
            FinallyClauseSyntax finallyClause,
            CancellationToken cancellationToken)
        {
            var tryStatement = (TryStatementSyntax)finallyClause.Parent;

            SyntaxList <CatchClauseSyntax> catches = tryStatement.Catches;

            if (catches.Any())
            {
                if (finallyClause.GetLeadingTrivia().IsEmptyOrWhitespace())
                {
                    CatchClauseSyntax lastCatch = catches.Last();

                    if (lastCatch.GetTrailingTrivia().IsEmptyOrWhitespace())
                    {
                        TryStatementSyntax newTryStatement = tryStatement
                                                             .WithCatches(catches.Replace(lastCatch, lastCatch.WithTrailingTrivia(finallyClause.GetTrailingTrivia())))
                                                             .WithFinally(null);

                        return(await document.ReplaceNodeAsync(tryStatement, newTryStatement, cancellationToken).ConfigureAwait(false));
                    }
                }

                return(await document.RemoveNodeAsync(finallyClause, SyntaxRemoveOptions.KeepExteriorTrivia, cancellationToken).ConfigureAwait(false));
            }
            else
            {
                IEnumerable <StatementSyntax> newNodes = tryStatement
                                                         .Block
                                                         .Statements
                                                         .Select(f => f.WithFormatterAnnotation());

                return(await document.ReplaceNodeAsync(tryStatement, newNodes, cancellationToken).ConfigureAwait(false));
            }
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            FinallyClauseSyntax finallyClause,
            CancellationToken cancellationToken)
        {
            if (finallyClause.GetLeadingTrivia().IsEmptyOrWhitespace())
            {
                var tryStatement = (TryStatementSyntax)finallyClause.Parent;

                SyntaxList <CatchClauseSyntax> catches = tryStatement.Catches;
                CatchClauseSyntax lastCatch            = catches[catches.Count - 1];

                if (lastCatch.GetTrailingTrivia().IsEmptyOrWhitespace())
                {
                    TryStatementSyntax newTryStatement = tryStatement
                                                         .WithCatches(catches.Replace(lastCatch, lastCatch.WithTrailingTrivia(finallyClause.GetTrailingTrivia())))
                                                         .WithFinally(null);

                    return(await document.ReplaceNodeAsync(tryStatement, newTryStatement, cancellationToken).ConfigureAwait(false));
                }
            }

            return(await document.RemoveNodeAsync(finallyClause, SyntaxRemoveOptions.KeepExteriorTrivia, cancellationToken).ConfigureAwait(false));
        }
Exemple #3
0
        private static SyntaxNode GetNewRoot(SyntaxNode oldRoot, FinallyClauseSyntax finallyClause)
        {
            if (finallyClause.GetLeadingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                var tryStatement = (TryStatementSyntax)finallyClause.Parent;

                CatchClauseSyntax lastCatch = tryStatement.Catches[tryStatement.Catches.Count - 1];

                if (lastCatch.GetTrailingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                {
                    TryStatementSyntax newTryStatement = tryStatement
                                                         .WithCatches(tryStatement.Catches.Replace(lastCatch, lastCatch.WithTrailingTrivia(finallyClause.GetTrailingTrivia())))
                                                         .WithFinally(null);

                    return(oldRoot.ReplaceNode(tryStatement, newTryStatement));
                }
            }

            return(oldRoot.RemoveNode(finallyClause, SyntaxRemoveOptions.KeepExteriorTrivia));
        }