private static async Task <ImmutableArray <CodeAction> > ComputeRefactoringsAsync(
            Document document,
            TextSpan span,
            ExpressionBodyPreference option,
            CancellationToken cancellationToken
            )
        {
            var lambdaNode = await document
                             .TryGetRelevantNodeAsync <LambdaExpressionSyntax>(span, cancellationToken)
                             .ConfigureAwait(false);

            if (lambdaNode == null)
            {
                return(ImmutableArray <CodeAction> .Empty);
            }

            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            using var resultDisposer = ArrayBuilder <CodeAction> .GetInstance(out var result);

            if (CanOfferUseExpressionBody(option, lambdaNode))
            {
                result.Add(
                    new MyCodeAction(
                        UseExpressionBodyTitle.ToString(),
                        c => UpdateDocumentAsync(document, root, lambdaNode, c)
                        )
                    );
            }

            var semanticModel = await document
                                .GetSemanticModelAsync(cancellationToken)
                                .ConfigureAwait(false);

            if (CanOfferUseBlockBody(semanticModel, option, lambdaNode, cancellationToken))
            {
                result.Add(
                    new MyCodeAction(
                        UseBlockBodyTitle.ToString(),
                        c => UpdateDocumentAsync(document, root, lambdaNode, c)
                        )
                    );
            }

            return(result.ToImmutable());
        }
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            if (context.Span.Length > 0)
            {
                return;
            }

            var position          = context.Span.Start;
            var document          = context.Document;
            var cancellationToken = context.CancellationToken;

            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var lambdaNode = root.FindToken(position).Parent.FirstAncestorOrSelf <LambdaExpressionSyntax>();

            if (lambdaNode == null)
            {
                return;
            }

            var optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            if (CanOfferUseExpressionBody(optionSet, lambdaNode, forAnalyzer: false))
            {
                context.RegisterRefactoring(new MyCodeAction(
                                                UseExpressionBodyTitle.ToString(),
                                                c => UpdateDocumentAsync(
                                                    document, root, lambdaNode,
                                                    useExpressionBody: true, c)));
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var(canOffer, _) = CanOfferUseBlockBody(
                semanticModel, optionSet, lambdaNode, forAnalyzer: false, cancellationToken);
            if (canOffer)
            {
                context.RegisterRefactoring(new MyCodeAction(
                                                UseBlockBodyTitle.ToString(),
                                                c => UpdateDocumentAsync(
                                                    document, root, lambdaNode,
                                                    useExpressionBody: false, c)));
            }
        }
예제 #3
0
        private async Task <ImmutableArray <CodeAction> > ComputeRefactoringsAsync(
            Document document, TextSpan span, ExpressionBodyPreference option, CancellationToken cancellationToken)
        {
            if (span.Length > 0)
            {
                return(ImmutableArray <CodeAction> .Empty);
            }

            var position = span.Start;

            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var lambdaNode = root.FindToken(position).Parent.FirstAncestorOrSelf <LambdaExpressionSyntax>();

            if (lambdaNode == null)
            {
                return(ImmutableArray <CodeAction> .Empty);
            }

            var optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            var result = ArrayBuilder <CodeAction> .GetInstance();

            if (CanOfferUseExpressionBody(option, lambdaNode))
            {
                result.Add(new MyCodeAction(
                               UseExpressionBodyTitle.ToString(),
                               c => UpdateDocumentAsync(
                                   document, root, lambdaNode, c)));
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (CanOfferUseBlockBody(semanticModel, option, lambdaNode, cancellationToken))
            {
                result.Add(new MyCodeAction(
                               UseBlockBodyTitle.ToString(),
                               c => UpdateDocumentAsync(
                                   document, root, lambdaNode, c)));
            }

            return(result.ToImmutableAndFree());
        }