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