Example #1
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);
        }