Пример #1
0
        static SeparatedSyntaxList <ExpressionSyntax> ToggleIncrement(ForStatementSyntax @for)
        {
            var incrementor    = (PostfixUnaryExpressionSyntax)@for.Incrementors[0];
            var newIncrementor = SyntaxFactory.PostfixUnaryExpression(
                InvertForAnalyzer.IsPostIncrement(incrementor) ? SyntaxKind.PostDecrementExpression : SyntaxKind.PostIncrementExpression,
                incrementor.Operand);

            return(new SeparatedSyntaxList <ExpressionSyntax>().Add(newIncrementor));
        }
Пример #2
0
        private async static Task <Document> InvertForAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var @for           = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ForStatementSyntax>().First();

            if (InvertForAnalyzer.IsPostIncrement(@for.Incrementors[0]))
            {
                return(await ConvertToDecrementingCounterForLoopAsync(document, @for, cancellationToken));
            }
            return(await ConvertToIncrementingCounterForLoopAsync(document, @for, cancellationToken));
        }