コード例 #1
0
        public override Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
        {
            CodeAction fixAction;
            switch (fixAllContext.Scope)
            {
            case FixAllScope.Document:
                fixAction = CodeAction.Create(
                    this.CodeActionTitle,
                    cancellationToken => this.GetDocumentFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
                    nameof(DocumentBasedFixAllProvider));
                break;

            case FixAllScope.Project:
                fixAction = CodeAction.Create(
                    this.CodeActionTitle,
                    cancellationToken => this.GetProjectFixesAsync(fixAllContext.WithCancellationToken(cancellationToken), fixAllContext.Project),
                    nameof(DocumentBasedFixAllProvider));
                break;

            case FixAllScope.Solution:
                fixAction = CodeAction.Create(
                    this.CodeActionTitle,
                    cancellationToken => this.GetSolutionFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
                    nameof(DocumentBasedFixAllProvider));
                break;

            case FixAllScope.Custom:
            default:
                fixAction = null;
                break;
            }

            return Task.FromResult(fixAction);
        }
コード例 #2
0
        public override Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext)
        {
            CodeAction?fixAction;

            switch (fixAllContext.Scope)
            {
            case FixAllScope.Document:
                fixAction = CodeAction.Create(
                    CodeActionTitle,
                    cancellationToken => GetDocumentFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
                    nameof(DocumentBasedFixAllProvider));
                break;

            case FixAllScope.Project:
                fixAction = CodeAction.Create(
                    CodeActionTitle,
                    cancellationToken => GetProjectFixesAsync(fixAllContext.WithCancellationToken(cancellationToken), fixAllContext.Project),
                    nameof(DocumentBasedFixAllProvider));
                break;

            case FixAllScope.Solution:
                fixAction = CodeAction.Create(
                    CodeActionTitle,
                    cancellationToken => GetSolutionFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
                    nameof(DocumentBasedFixAllProvider));
                break;

            case FixAllScope.Custom:
            default:
                fixAction = null;
                break;
            }

            return(Task.FromResult(fixAction));
        }
コード例 #3
0
        public override Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
        {
            switch (fixAllContext.Scope)
            {
                case FixAllScope.Document:
                    {
                        return Task.FromResult(CodeAction.Create(UseEmptyStringCodeFixProvider.MessageFormat,
                            async ct =>
                            {
                                var newFixAllContext = fixAllContext.WithCancellationToken(ct);
                                var diagnostics = await newFixAllContext.GetDocumentDiagnosticsAsync(newFixAllContext.Document).ConfigureAwait(false);
                                var root = await GetFixedDocumentAsync(newFixAllContext.Document, diagnostics, ct).ConfigureAwait(false);
                                return newFixAllContext.Document.WithSyntaxRoot(root);
                            }));

                    }
                case FixAllScope.Project:
                    return Task.FromResult(CodeAction.Create(UseEmptyStringCodeFixProvider.MessageFormat,
                        ct =>
                        {
                            var newFixAllContext = fixAllContext.WithCancellationToken(ct);
                            return GetFixedProjectAsync(newFixAllContext, newFixAllContext.WithCancellationToken(ct).Project);
                        }));
                case FixAllScope.Solution:
                    return Task.FromResult(CodeAction.Create(UseEmptyStringCodeFixProvider.MessageFormat,
                        ct => GetFixedSolutionAsync(fixAllContext.WithCancellationToken(ct))));
            }
            return null;
        }
コード例 #4
0
        protected override async Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            FixAllLogger.LogContext(_fixAllContext, IsInternalCodeFixProvider(_fixAllContext.CodeFixProvider));

            var service = _fixAllContext.Project.Solution.Workspace.Services.GetService <IFixAllGetFixesService>();

            // Use the new cancellation token instead of the stale one present inside _fixAllContext.
            return(await service.GetFixAllOperationsAsync(_fixAllProvider, _fixAllContext.WithCancellationToken(cancellationToken), FixAllWaitDialogAndPreviewChangesTitle, ComputingFixAllWaitDialogMessage, _showPreviewChangesDialog).ConfigureAwait(false));
        }
コード例 #5
0
        private CodeAction GetFixAllCodeAction(FixAllProvider fixAllProvider, FixAllContext fixAllContext, string fixAllTitle, string waitDialogMessage, out bool userCancelled)
        {
            userCancelled = false;

            // Compute fix all occurrences code fix for the given fix all context.
            // Bring up a cancellable wait dialog.
            CodeAction codeAction = null;

            using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation, fixAllContext.CancellationToken))
            {
                var result = _waitIndicator.Wait(
                    fixAllTitle,
                    waitDialogMessage,
                    allowCancel: true,
                    action: waitContext =>
                    {
                        fixAllContext.CancellationToken.ThrowIfCancellationRequested();
                        using (var linkedCts =
                            CancellationTokenSource.CreateLinkedTokenSource(waitContext.CancellationToken, fixAllContext.CancellationToken))
                        {
                            try
                            {
                                var fixAllContextWithCancellation = fixAllContext.WithCancellationToken(linkedCts.Token);
                                var fixTask = fixAllProvider.GetFixAsync(fixAllContextWithCancellation);
                                if (fixTask != null)
                                {
                                    codeAction = fixTask.WaitAndGetResult(linkedCts.Token);
                                }
                            }
                            catch (OperationCanceledException)
                            {
                                fixAllContext.CancellationToken.ThrowIfCancellationRequested();
                            }
                        }
                    });

                userCancelled = result == WaitIndicatorResult.Canceled;
                var cancelled = userCancelled || codeAction == null;

                if (cancelled)
                {
                    FixAllLogger.LogComputationResult(completed: false, timedOut: result != WaitIndicatorResult.Canceled);
                    return null;
                }
            }

            FixAllLogger.LogComputationResult(completed: true);
            return codeAction;
        }
コード例 #6
0
        public override Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
        {
            string title = string.Format(MaintainabilityResources.SA1412CodeFix, fixAllContext.CodeActionEquivalenceKey.Substring(fixAllContext.CodeActionEquivalenceKey.IndexOf('.') + 1));

            CodeAction fixAction;
            switch (fixAllContext.Scope)
            {
            case FixAllScope.Document:
                fixAction = CodeAction.Create(
                    title,
                    cancellationToken => GetDocumentFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
                    nameof(SA1412FixAllProvider));
                break;

            case FixAllScope.Project:
                fixAction = CodeAction.Create(
                    title,
                    cancellationToken => GetProjectFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
                    nameof(SA1412FixAllProvider));
                break;

            case FixAllScope.Solution:
                fixAction = CodeAction.Create(
                    title,
                    cancellationToken => GetSolutionFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
                    nameof(SA1412FixAllProvider));
                break;

            case FixAllScope.Custom:
            default:
                fixAction = null;
                break;
            }

            return Task.FromResult(fixAction);
        }
コード例 #7
0
        public async Task<IEnumerable<CodeActionOperation>> GetFixAllOperationsAsync(FixAllProvider fixAllProvider, FixAllContext fixAllContext)
        {
            // Compute fix all occurrences code fix for the given fix all context.
            // Bring up a cancellable wait dialog.
            CodeAction codeAction = null;

            using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation, fixAllContext.CancellationToken))
            {
                var result = _waitIndicator.Wait(
                    EditorFeaturesResources.FixAllOccurrences,
                    EditorFeaturesResources.ComputingFixAllOccurrences,
                    allowCancel: true,
                    action: waitContext =>
                    {
                        fixAllContext.CancellationToken.ThrowIfCancellationRequested();
                        using (var linkedCts =
                            CancellationTokenSource.CreateLinkedTokenSource(waitContext.CancellationToken, fixAllContext.CancellationToken))
                        {
                            try
                            {
                                var fixAllContextWithCancellation = fixAllContext.WithCancellationToken(linkedCts.Token);
                                var fixTask = fixAllProvider.GetFixAsync(fixAllContextWithCancellation);
                                if (fixTask != null)
                                {
                                    codeAction = fixTask.WaitAndGetResult(linkedCts.Token);
                                }
                            }
                            catch (OperationCanceledException)
                            {
                                fixAllContext.CancellationToken.ThrowIfCancellationRequested();
                            }
                        }
                    });

                var cancelled = result == WaitIndicatorResult.Canceled || codeAction == null;

                if (cancelled)
                {
                    FixAllLogger.LogComputationResult(completed: false, timedOut: result != WaitIndicatorResult.Canceled);
                    return null;
                }
            }

            FixAllLogger.LogComputationResult(completed: true);
            return await GetFixAllOperationsAsync(codeAction, fixAllContext).ConfigureAwait(false);
        }