static void AddRedundantAwaitHighlightings(
            [NotNull] IHighlightingConsumer consumer,
            [NotNull] ITokenNode asyncKeyword,
            [NotNull] Action removeAsync,
            [NotNull] IAwaitExpression awaitExpression,
            [CanBeNull] IExpressionStatement statementToBeReplacedWithReturnStatement,
            [NotNull] ICSharpExpression expressionToReturn,
            [CanBeNull] IAttributesOwnerDeclaration attributesOwnerDeclaration,
            [CanBeNull] IInvocationExpression configureAwaitInvocationExpression = null)
        {
            var configureAwaitNode = configureAwaitInvocationExpression?.InvokedExpression?.LastChild;

            var highlightConfigureAwait = configureAwaitNode != null && configureAwaitInvocationExpression.ArgumentList != null;

            var highlighting = new RedundantAwaitSuggestion(
                $"Redundant 'await' (remove 'async'/'await'{(highlightConfigureAwait ? "/'" + nameof(Task.ConfigureAwait) + "(...)'" : "")})",
                removeAsync,
                awaitExpression,
                statementToBeReplacedWithReturnStatement,
                expressionToReturn,
                attributesOwnerDeclaration);

            consumer.AddHighlighting(highlighting, awaitExpression.AwaitKeyword.GetDocumentRange());

            consumer.AddHighlighting(highlighting, asyncKeyword.GetDocumentRange(), isSecondaryHighlighting: true);

            if (highlightConfigureAwait)
            {
                var dotToken              = configureAwaitNode.GetPreviousMeaningfulToken();
                var leftParenthesisToken  = configureAwaitInvocationExpression.ArgumentList.GetPreviousMeaningfulToken();
                var rightParenthesisToken = configureAwaitInvocationExpression.ArgumentList.GetNextMeaningfulToken();

                consumer.AddHighlighting(
                    highlighting,
                    configureAwaitNode.GetDocumentRange()
                    .JoinLeft(dotToken.GetDocumentRange())
                    .JoinRight(
                        configureAwaitInvocationExpression.ArgumentList.GetDocumentRange()
                        .JoinLeft(leftParenthesisToken.GetDocumentRange())
                        .JoinRight(rightParenthesisToken.GetDocumentRange())),
                    isSecondaryHighlighting: true);
            }
        }
Beispiel #2
0
 public RemoveAsyncAwaitFix([NotNull] RedundantAwaitSuggestion highlighting) => this.highlighting = highlighting;