Пример #1
0
        /// <summary>
        /// Get, order, and filter code actions, and then transform them into VSCodeActions.
        /// </summary>
        /// <remarks>
        /// Used by CodeActionsHandler.
        /// </remarks>
        public static async Task <VSInternalCodeAction[]> GetVSCodeActionsAsync(
            CodeActionParams request,
            CodeActionsCache codeActionsCache,
            Document document,
            CodeActionOptions options,
            ICodeFixService codeFixService,
            ICodeRefactoringService codeRefactoringService,
            CancellationToken cancellationToken)
        {
            var actionSets = await GetActionSetsAsync(
                document, options, codeFixService, codeRefactoringService, request.Range, cancellationToken).ConfigureAwait(false);

            if (actionSets.IsDefaultOrEmpty)
            {
                return(Array.Empty <VSInternalCodeAction>());
            }

            await codeActionsCache.UpdateActionSetsAsync(document, request.Range, actionSets, cancellationToken).ConfigureAwait(false);

            var documentText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            // Each suggested action set should have a unique set number, which is used for grouping code actions together.
            var currentHighestSetNumber = 0;

            using var _ = ArrayBuilder <VSInternalCodeAction> .GetInstance(out var codeActions);

            foreach (var set in actionSets)
            {
                var currentSetNumber = ++currentHighestSetNumber;
                foreach (var suggestedAction in set.Actions)
                {
                    // Filter out code actions with options since they'll show dialogs and we can't remote the UI and the options.
                    if (suggestedAction.OriginalCodeAction is CodeActionWithOptions)
                    {
                        continue;
                    }

                    // TO-DO: Re-enable code actions involving package manager once supported by LSP.
                    // https://github.com/dotnet/roslyn/issues/48698
                    if (suggestedAction.OriginalCodeAction.Tags.Equals(WellKnownTagArrays.NuGet))
                    {
                        continue;
                    }

                    codeActions.Add(GenerateVSCodeAction(
                                        request, documentText,
                                        suggestedAction: suggestedAction,
                                        codeActionKind: GetCodeActionKindFromSuggestedActionCategoryName(set.CategoryName !),
                                        setPriority: set.Priority,
                                        applicableRange: set.ApplicableToSpan.HasValue ? ProtocolConversions.TextSpanToRange(set.ApplicableToSpan.Value, documentText) : null,
                                        currentSetNumber: currentSetNumber,
                                        currentHighestSetNumber: ref currentHighestSetNumber));
                }
            }

            return(codeActions.ToArray());
        }
Пример #2
0
        /// <summary>
        /// Get, order, and filter code actions.
        /// </summary>
        /// <remarks>
        /// Used by CodeActionResolveHandler and RunCodeActionHandler.
        /// </remarks>
        public static async Task <ImmutableArray <CodeAction> > GetCodeActionsAsync(
            CodeActionsCache codeActionsCache,
            Document document,
            LSP.Range selection,
            ICodeFixService codeFixService,
            ICodeRefactoringService codeRefactoringService,
            CancellationToken cancellationToken)
        {
            var actionSets = await GetActionSetsAsync(
                document, codeFixService, codeRefactoringService, selection, cancellationToken).ConfigureAwait(false);

            if (!actionSets.HasValue)
            {
                actionSets = await GetActionSetsAsync(
                    document, codeFixService, codeRefactoringService, selection, cancellationToken).ConfigureAwait(false);

                if (!actionSets.HasValue)
                {
                    return(ImmutableArray <CodeAction> .Empty);
                }

                await codeActionsCache.UpdateActionSetsAsync(document, selection, actionSets.Value, cancellationToken).ConfigureAwait(false);
            }

            var _ = ArrayBuilder <CodeAction> .GetInstance(out var codeActions);

            foreach (var set in actionSets)
            {
                foreach (var suggestedAction in set.Actions)
                {
                    // Filter out code actions with options since they'll show dialogs and we can't remote the UI and the options.
                    if (suggestedAction.OriginalCodeAction is CodeActionWithOptions)
                    {
                        continue;
                    }

                    codeActions.Add(GetNestedActionsFromActionSet(suggestedAction));
                }
            }

            return(codeActions.ToImmutable());
        }
Пример #3
0
        /// <summary>
        /// Get, order, and filter code actions, and then transform them into VSCodeActions.
        /// </summary>
        /// <remarks>
        /// Used by CodeActionsHandler.
        /// </remarks>
        public static async Task <VSCodeAction[]> GetVSCodeActionsAsync(
            CodeActionParams request,
            CodeActionsCache codeActionsCache,
            Document document,
            ICodeFixService codeFixService,
            ICodeRefactoringService codeRefactoringService,
            CancellationToken cancellationToken)
        {
            var actionSets = await GetActionSetsAsync(
                document, codeFixService, codeRefactoringService, request.Range, cancellationToken).ConfigureAwait(false);

            if (!actionSets.HasValue)
            {
                return(Array.Empty <VSCodeAction>());
            }

            await codeActionsCache.UpdateActionSetsAsync(document, request.Range, actionSets.Value, cancellationToken).ConfigureAwait(false);

            var _ = ArrayBuilder <VSCodeAction> .GetInstance(out var codeActions);

            foreach (var set in actionSets)
            {
                foreach (var suggestedAction in set.Actions)
                {
                    // Filter out code actions with options since they'll show dialogs and we can't remote the UI and the options.
                    if (suggestedAction.OriginalCodeAction is CodeActionWithOptions)
                    {
                        continue;
                    }

                    codeActions.Add(GenerateVSCodeAction(
                                        request, GetNestedActionsFromActionSet(suggestedAction),
                                        GetCodeActionKindFromSuggestedActionCategoryName(set.CategoryName !)));
                }
            }

            return(codeActions.ToArray());
        }
Пример #4
0
 public TestAccessor(CodeActionsCache codeActionsCache)
 => _codeActionsCache = codeActionsCache;