1 | using Microsoft.CodeAnalysis;
|
---|
2 | using Microsoft.CodeAnalysis.CodeActions;
|
---|
3 | using Microsoft.CodeAnalysis.Formatting;
|
---|
4 | using Microsoft.CodeAnalysis.Simplification;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Threading;
|
---|
8 |
|
---|
9 | namespace TestHelper {
|
---|
10 | /// <summary>
|
---|
11 | /// Diagnostic Producer class with extra methods dealing with applying codefixes
|
---|
12 | /// All methods are static
|
---|
13 | /// </summary>
|
---|
14 | public abstract partial class CodeFixVerifier : DiagnosticVerifier {
|
---|
15 | /// <summary>
|
---|
16 | /// Apply the inputted CodeAction to the inputted document.
|
---|
17 | /// Meant to be used to apply codefixes.
|
---|
18 | /// </summary>
|
---|
19 | /// <param name="document">The Document to apply the fix on</param>
|
---|
20 | /// <param name="codeAction">A CodeAction that will be applied to the Document.</param>
|
---|
21 | /// <returns>A Document with the changes from the CodeAction</returns>
|
---|
22 | private static Document ApplyFix(Document document, CodeAction codeAction) {
|
---|
23 | var operations = codeAction.GetOperationsAsync(CancellationToken.None).Result;
|
---|
24 | var solution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
|
---|
25 | return solution.GetDocument(document.Id);
|
---|
26 | }
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// Compare two collections of Diagnostics,and return a list of any new diagnostics that appear only in the second collection.
|
---|
30 | /// Note: Considers Diagnostics to be the same if they have the same Ids. In the case of multiple diagnostics with the same Id in a row,
|
---|
31 | /// this method may not necessarily return the new one.
|
---|
32 | /// </summary>
|
---|
33 | /// <param name="diagnostics">The Diagnostics that existed in the code before the CodeFix was applied</param>
|
---|
34 | /// <param name="newDiagnostics">The Diagnostics that exist in the code after the CodeFix was applied</param>
|
---|
35 | /// <returns>A list of Diagnostics that only surfaced in the code after the CodeFix was applied</returns>
|
---|
36 | private static IEnumerable<Diagnostic> GetNewDiagnostics(IEnumerable<Diagnostic> diagnostics, IEnumerable<Diagnostic> newDiagnostics) {
|
---|
37 | var oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
|
---|
38 | var newArray = newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
|
---|
39 |
|
---|
40 | int oldIndex = 0;
|
---|
41 | int newIndex = 0;
|
---|
42 |
|
---|
43 | while (newIndex < newArray.Length) {
|
---|
44 | if (oldIndex < oldArray.Length && oldArray[oldIndex].Id == newArray[newIndex].Id) {
|
---|
45 | ++oldIndex;
|
---|
46 | ++newIndex;
|
---|
47 | } else {
|
---|
48 | yield return newArray[newIndex++];
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Get the existing compiler diagnostics on the inputted document.
|
---|
55 | /// </summary>
|
---|
56 | /// <param name="document">The Document to run the compiler diagnostic analyzers on</param>
|
---|
57 | /// <returns>The compiler diagnostics that were found in the code</returns>
|
---|
58 | private static IEnumerable<Diagnostic> GetCompilerDiagnostics(Document document) {
|
---|
59 | return document.GetSemanticModelAsync().Result.GetDiagnostics();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Given a document, turn it into a string based on the syntax root
|
---|
64 | /// </summary>
|
---|
65 | /// <param name="document">The Document to be converted to a string</param>
|
---|
66 | /// <returns>A string containing the syntax of the Document after formatting</returns>
|
---|
67 | private static string GetStringFromDocument(Document document) {
|
---|
68 | var simplifiedDoc = Simplifier.ReduceAsync(document, Simplifier.Annotation).Result;
|
---|
69 | var root = simplifiedDoc.GetSyntaxRootAsync().Result;
|
---|
70 | root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace);
|
---|
71 | return root.GetText().ToString();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|