예제 #1
0
 private static IEnumerable <string> GetFixAllDiagnosticIds(
     FixAllProviderInfo fixAllProviderInfo,
     IEnumerable <Diagnostic> originalFixDiagnostics)
 {
     return(originalFixDiagnostics
            .Where(fixAllProviderInfo.CanBeFixed)
            .Select(d => d.Id));
 }
예제 #2
0
 private FixAllState CreateFixAllState(
               FixAllProvider fixAllProvider,
               Document document,
               FixAllProviderInfo fixAllProviderInfo,
               CodeFixProvider originalFixProvider,
               IEnumerable<Diagnostic> originalFixDiagnostics,
               Func<Document, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getDocumentDiagnosticsAsync,
               Func<Project, bool, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getProjectDiagnosticsAsync)
 {
     var diagnosticIds = GetFixAllDiagnosticIds(fixAllProviderInfo, originalFixDiagnostics).ToImmutableHashSet();
     var diagnosticProvider = new FixAllDiagnosticProvider(diagnosticIds, getDocumentDiagnosticsAsync, getProjectDiagnosticsAsync);
     return new FixAllState(
            fixAllProvider: fixAllProvider,
         document: document,
         codeFixProvider: originalFixProvider,
         scope: FixAllScope.Document,
         codeActionEquivalenceKey: null,
         diagnosticIds: diagnosticIds,
         fixAllDiagnosticProvider: diagnosticProvider);
 }
예제 #3
0
        static async Task FixAll(TextEditor editor, ValidCodeDiagnosticAction fix, FixAllProvider provider, DiagnosticAnalyzer diagnosticAnalyzer)
        {
            var diagnosticIds = diagnosticAnalyzer.SupportedDiagnostics.Select(d => d.Id).ToImmutableHashSet();

            var analyzers = new [] { diagnosticAnalyzer }.ToImmutableArray();

            var codeFixService           = CompositionManager.GetExportedValue <ICodeFixService> () as CodeFixService;
            var fixAllDiagnosticProvider = codeFixService.CreateFixAllState(
                provider,
                editor.DocumentContext.AnalysisDocument,
                FixAllProviderInfo.Create(null),
                null,
                null,
                async(doc, diagnostics, token) => await GetDiagnosticsForDocument(analyzers, doc, diagnostics, token).ConfigureAwait(false),
                (Project arg1, bool arg2, ImmutableHashSet <string> arg3, CancellationToken arg4) => {
                return(Task.FromResult((IEnumerable <Diagnostic>) new Diagnostic[] { }));
            }).DiagnosticProvider;

            //var fixAllDiagnosticProvider = new FixAllState.FixAllDiagnosticProvider (
            //	diagnosticIds,
            //	async (doc, diagnostics, token) => await GetDiagnosticsForDocument (analyzers, doc, diagnostics, token).ConfigureAwait (false),
            //	(Project arg1, bool arg2, ImmutableHashSet<string> arg3, CancellationToken arg4) => {
            //		return Task.FromResult ((IEnumerable<Diagnostic>)new Diagnostic [] { });
            //	});

            var ctx = new FixAllContext(
                editor.DocumentContext.AnalysisDocument,
                fix.Diagnostic.GetCodeFixProvider(),
                FixAllScope.Document,
                fix.CodeAction.EquivalenceKey,
                diagnosticIds,
                fixAllDiagnosticProvider,
                default(CancellationToken)
                );

            var fixAll = await provider.GetFixAsync(ctx);

            using (var undo = editor.OpenUndoGroup()) {
                await CodeDiagnosticDescriptor.RunAction(editor.DocumentContext, fixAll, default(CancellationToken));
            }
        }
        internal async Task <CodeRefactoring> GetCodeRefactoringAsync(
            Document document,
            TextSpan selectedOrAnnotatedSpan,
            TestWorkspace workspace,
            TestParameters parameters)
        {
            var provider = CreateCodeRefactoringProvider(workspace, parameters);

            var actions = ArrayBuilder <(CodeAction, TextSpan?)> .GetInstance();

            var codeActionOptionsProvider = parameters.globalOptions?.IsEmpty() == false?
                                            CodeActionOptionsStorage.GetCodeActionOptionsProvider(workspace.GlobalOptions) :
                                                CodeActionOptions.DefaultProvider;

            var context = new CodeRefactoringContext(document, selectedOrAnnotatedSpan, (a, t) => actions.Add((a, t)), codeActionOptionsProvider, isBlocking: false, CancellationToken.None);
            await provider.ComputeRefactoringsAsync(context);

            var result = actions.Count > 0 ? new CodeRefactoring(provider, actions.ToImmutable(), FixAllProviderInfo.Create(provider), codeActionOptionsProvider) : null;

            actions.Free();
            return(result);
        }