Esempio n. 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));
        }
Esempio n. 2
0
        public override Task <CodeAction> GetFixAsync(FixAllContext fixAllContext)
        {
            switch (fixAllContext.Scope)
            {
            case FixAllScope.Document:
            {
                return(Task.FromResult(CodeAction.Create(UseStringEmptyCodeFixProvider.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(UseStringEmptyCodeFixProvider.MessageFormat,
                                                         ct =>
                {
                    var newFixAllContext = fixAllContext.WithCancellationToken(ct);
                    return GetFixedProjectAsync(newFixAllContext, newFixAllContext.WithCancellationToken(ct).Project);
                })));

            case FixAllScope.Solution:
                return(Task.FromResult(CodeAction.Create(UseStringEmptyCodeFixProvider.MessageFormat,
                                                         ct => GetFixedSolutionAsync(fixAllContext.WithCancellationToken(ct)))));
            }
            return(null);
        }
 public override Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext)
 {
     return(Task.FromResult(fixAllContext.Scope switch
     {
         FixAllScope.Document => CodeAction.Create(
             CodeActionTitle,
             cancellationToken => GetDocumentFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
             nameof(DocumentBasedFixAllProvider)),
         FixAllScope.Project => CodeAction.Create(
             CodeActionTitle,
             cancellationToken => GetProjectFixesAsync(fixAllContext.WithCancellationToken(cancellationToken), fixAllContext.Project),
             nameof(DocumentBasedFixAllProvider)),
         FixAllScope.Solution => CodeAction.Create(
             CodeActionTitle,
             cancellationToken => GetSolutionFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
             nameof(DocumentBasedFixAllProvider)),
         _ => null,
     }));
Esempio n. 4
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));
        }
        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);
        }
Esempio n. 6
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));
        }