private async Task<CodeAction> GetFixAllCodeActionAsync(FixAllProvider fixAllProvider, FixAllContext fixAllContext) { using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation, fixAllContext.CancellationToken)) { CodeAction action = null; try { action = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false); } catch (OperationCanceledException) { FixAllLogger.LogComputationResult(completed: false); } finally { if (action != null) { FixAllLogger.LogComputationResult(completed: true); } else { FixAllLogger.LogComputationResult(completed: false, timedOut: true); } } return action; } }
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; }
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); }
private static void RunFixAllProvider(DiagnosticAnalyzer diagnosticAnalyzer, CodeFixProvider codeFixProvider, string codeFixTitle, FixAllProvider fixAllProvider, Document document, ParseOptions parseOption, string pathToExpected) { var currentDocument = document; List<Diagnostic> diagnostics; string actualCode; CalculateDiagnosticsAndCode(diagnosticAnalyzer, currentDocument, parseOption, out diagnostics, out actualCode); Assert.AreNotEqual(0, diagnostics.Count); var fixAllDiagnosticProvider = new FixAllDiagnosticProvider( codeFixProvider.FixableDiagnosticIds.ToImmutableHashSet(), (doc, ids, ct) => Task.FromResult( GetDiagnostics(currentDocument.Project.GetCompilationAsync(ct).Result, diagnosticAnalyzer)), null); var fixAllContext = new FixAllContext(currentDocument, codeFixProvider, FixAllScope.Document, codeFixTitle, codeFixProvider.FixableDiagnosticIds, fixAllDiagnosticProvider, CancellationToken.None); var codeActionToExecute = fixAllProvider.GetFixAsync(fixAllContext).Result; Assert.IsNotNull(codeActionToExecute); currentDocument = ApplyCodeFix(currentDocument, codeActionToExecute); CalculateDiagnosticsAndCode(diagnosticAnalyzer, currentDocument, parseOption, out diagnostics, out actualCode); Assert.AreEqual(File.ReadAllText(pathToExpected), actualCode); }