public Solution ApplyFixAll( Solution solution, IEnumerable <TestAdditionalDocument> additionalFiles, bool allowNewCompilerDiagnostics) { foreach (var projectId in solution.ProjectIds) { var project = solution.GetProject(projectId); var compilation = project.GetCompilationAsync().Result; var analyzerDiagnostics = GetSortedDiagnostics(compilation, additionalFiles); var compilerDiagnostics = compilation.GetDiagnostics(); var fixAllProvider = _codeFixProvider.GetFixAllProvider(); var diagnosticProvider = new FixAllDiagnosticProvider(_getFixableDiagnostics(analyzerDiagnostics.Concat(compilerDiagnostics))); var fixAllContext = new FixAllContext(project, _codeFixProvider, FixAllScope.Project, string.Empty, _fixableDiagnosticIds, diagnosticProvider, CancellationToken.None); var codeAction = fixAllProvider.GetFixAsync(fixAllContext).Result; if (codeAction != null) { solution = DiagnosticFixerTestsExtensions.Apply(codeAction); } compilation = project.GetCompilationAsync().Result; analyzerDiagnostics = GetSortedDiagnostics(compilation, additionalFiles); project = solution.GetProject(projectId); var updatedCompilerDiagnostics = project.GetCompilationAsync().Result.GetDiagnostics(); if (!allowNewCompilerDiagnostics) { CheckNewCompilerDiagnostics(project, compilerDiagnostics, updatedCompilerDiagnostics); } } return(solution); }
public Solution ApplySingleFix( Project project, IEnumerable <TestAdditionalDocument> additionalFiles, int codeFixIndex, int fixableDiagnosticIndex = 0) { var compilation = project.GetCompilationAsync().Result; var analyzerDiagnostics = GetSortedDiagnostics(compilation, additionalFiles: additionalFiles); var compilerDiagnostics = compilation.GetDiagnostics(); var fixableDiagnostics = _getFixableDiagnostics(analyzerDiagnostics.Concat(compilerDiagnostics)); var actions = new List <CodeAction>(); var diagnostic = fixableDiagnostics[fixableDiagnosticIndex]; var document = FindDocument(diagnostic, project); var context = new CodeFixContext(document, diagnostic, (a, d) => actions.Add(a), CancellationToken.None); _codeFixProvider.RegisterCodeFixesAsync(context).Wait(); if (!actions.Any()) { return(project.Solution); } if (codeFixIndex >= actions.Count) { throw new Exception($"Unable to invoke code fix at index '{codeFixIndex}', only '{actions.Count}' code fixes were registered."); } return(DiagnosticFixerTestsExtensions.Apply(actions.ElementAt(codeFixIndex))); }
public Solution ApplyFixesOneByOne( Solution solution, IEnumerable <TestAdditionalDocument> additionalFiles, bool allowNewCompilerDiagnostics, int codeFixIndex) { foreach (var projectId in solution.ProjectIds) { var project = solution.GetProject(projectId); var compilation = project.GetCompilationAsync().Result; var analyzerDiagnostics = GetSortedDiagnostics(compilation, additionalFiles); var compilerDiagnostics = compilation.GetDiagnostics(); var fixableDiagnostics = _getFixableDiagnostics(analyzerDiagnostics.Concat(compilerDiagnostics)); var diagnosticIndexToFix = 0; while (diagnosticIndexToFix < fixableDiagnostics.Length) { var actions = new List <CodeAction>(); var diagnostic = fixableDiagnostics[diagnosticIndexToFix]; var document = FindDocument(diagnostic, project); var context = new CodeFixContext(document, diagnostic, (a, d) => actions.Add(a), CancellationToken.None); _codeFixProvider.RegisterCodeFixesAsync(context).Wait(); if (!actions.Any() || codeFixIndex >= actions.Count) { break; } solution = DiagnosticFixerTestsExtensions.Apply(actions.ElementAt(codeFixIndex)); project = solution.GetProject(projectId); additionalFiles = project.AdditionalDocuments.Select(a => new TestAdditionalDocument(a)); compilation = project.GetCompilationAsync().Result; analyzerDiagnostics = GetSortedDiagnostics(compilation, additionalFiles); var updatedCompilerDiagnostics = project.GetCompilationAsync().Result.GetDiagnostics(); if (!allowNewCompilerDiagnostics) { CheckNewCompilerDiagnostics(project, compilerDiagnostics, updatedCompilerDiagnostics); } var newFixableDiagnostics = _getFixableDiagnostics(analyzerDiagnostics.Concat(updatedCompilerDiagnostics)); if (!fixableDiagnostics.Except(newFixableDiagnostics, DiagnosticComparer.Instance).Any() && !newFixableDiagnostics.Except(fixableDiagnostics, DiagnosticComparer.Instance).Any()) { diagnosticIndexToFix++; } else { fixableDiagnostics = newFixableDiagnostics; diagnosticIndexToFix = 0; } } } return(solution); }
public Solution ApplySingleFix(Project project, IEnumerable <TestAdditionalDocument> additionalFiles, int codeFixIndex) { var compilation = project.GetCompilationAsync().Result; var analyzerDiagnostics = GetSortedDiagnostics(compilation, additionalFiles: additionalFiles); var compilerDiagnostics = compilation.GetDiagnostics(); var fixableDiagnostics = _getFixableDiagnostics(analyzerDiagnostics.Concat(compilerDiagnostics)); var diagnostic = fixableDiagnostics[0]; var document = FindDocument(diagnostic, project); List <CodeAction> actions = RegisterCodeFixes(document, diagnostic); if (!actions.Any()) { return(project.Solution); } if (codeFixIndex >= actions.Count) { throw new Exception($"Unable to invoke code fix at index '{codeFixIndex}', only '{actions.Count}' code fixes were registered."); } return(DiagnosticFixerTestsExtensions.Apply(actions.ElementAt(codeFixIndex))); }
public Solution ApplyFixAll( Solution solution, FixAllScope fixAllScope, bool allowNewCompilerDiagnostics, int codeFixIndex, IEnumerable <TestAdditionalDocument> additionalFiles = null) { additionalFiles = additionalFiles ?? ImmutableArray <TestAdditionalDocument> .Empty; Document triggerDocument = null; Diagnostic triggerDiagnostic = null; var allFixableDiagnostics = new List <Diagnostic>(); var compilerDiagnosticMap = new Dictionary <ProjectId, ImmutableArray <Diagnostic> >(); foreach (var projectId in solution.ProjectIds) { var project = solution.GetProject(projectId); var compilation = project.GetCompilationAsync().Result; var analyzerDiagnostics = GetSortedDiagnostics(compilation, additionalFiles); var compilerDiagnostics = compilation.GetDiagnostics(); compilerDiagnosticMap.Add(project.Id, compilerDiagnostics); var fixableDiagnostics = _getFixableDiagnostics(analyzerDiagnostics.Concat(compilerDiagnostics)); allFixableDiagnostics.AddRange(fixableDiagnostics); if (triggerDiagnostic == null) { triggerDiagnostic = fixableDiagnostics.FirstOrDefault(d => _fixableDiagnosticIds.Contains(d.Id)); if (triggerDiagnostic != null) { triggerDocument = FindDocument(triggerDiagnostic, project); } } } if (triggerDiagnostic == null || triggerDocument == null) { return(solution); } List <CodeAction> actions = RegisterCodeFixes(triggerDocument, triggerDiagnostic); if (!actions.Any()) { return(solution); } if (codeFixIndex >= actions.Count) { throw new Exception($"Unable to invoke code fix at index '{codeFixIndex}', only '{actions.Count}' code fixes were registered."); } var fixAllProvider = _codeFixProvider.GetFixAllProvider(); if (fixAllProvider == null) { throw new Exception($"Requested 'testFixAll' but the underlying CodeFixProvider returned 'null' for 'GetFixAllProvider' API."); } var diagnosticProvider = new FixAllDiagnosticProvider(allFixableDiagnostics); var equivalenceKey = actions[codeFixIndex].EquivalenceKey; var fixAllContext = new FixAllContext(triggerDocument, _codeFixProvider, fixAllScope, equivalenceKey, _fixableDiagnosticIds, diagnosticProvider, CancellationToken.None); var codeAction = fixAllProvider.GetFixAsync(fixAllContext).Result; if (codeAction != null) { solution = DiagnosticFixerTestsExtensions.Apply(codeAction); foreach (var kvp in compilerDiagnosticMap) { var projectId = kvp.Key; var compilerDiagnostics = kvp.Value; var project = solution.GetProject(projectId); var updatedCompilerDiagnostics = project.GetCompilationAsync().Result.GetDiagnostics(); if (!allowNewCompilerDiagnostics) { CheckNewCompilerDiagnostics(project, compilerDiagnostics, updatedCompilerDiagnostics); } } } return(solution); }