You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CodeFixVerifier.Helper.cs 4.0 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading;
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.CodeActions;
  6. using Microsoft.CodeAnalysis.Formatting;
  7. using Microsoft.CodeAnalysis.Simplification;
  8. namespace TestHelper
  9. {
  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. {
  16. /// <summary>
  17. /// Apply the inputted CodeAction to the inputted document.
  18. /// Meant to be used to apply codefixes.
  19. /// </summary>
  20. /// <param name="document">The Document to apply the fix on</param>
  21. /// <param name="codeAction">A CodeAction that will be applied to the Document.</param>
  22. /// <returns>A Document with the changes from the CodeAction</returns>
  23. private static Document ApplyFix(Document document, CodeAction codeAction)
  24. {
  25. var operations = codeAction.GetOperationsAsync(CancellationToken.None).Result;
  26. var solution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
  27. return solution.GetDocument(document.Id);
  28. }
  29. /// <summary>
  30. /// Compare two collections of Diagnostics,and return a list of any new diagnostics that appear only in the second
  31. /// collection.
  32. /// Note: Considers Diagnostics to be the same if they have the same Ids. In the case of multiple diagnostics with the
  33. /// 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,
  40. IEnumerable<Diagnostic> newDiagnostics)
  41. {
  42. var oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
  43. var newArray = newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
  44. var oldIndex = 0;
  45. var newIndex = 0;
  46. while (newIndex < newArray.Length)
  47. if (oldIndex < oldArray.Length && oldArray[oldIndex].Id == newArray[newIndex].Id)
  48. {
  49. ++oldIndex;
  50. ++newIndex;
  51. }
  52. else
  53. yield return newArray[newIndex++];
  54. }
  55. /// <summary>
  56. /// Get the existing compiler diagnostics on the inputted document.
  57. /// </summary>
  58. /// <param name="document">The Document to run the compiler diagnostic analyzers on</param>
  59. /// <returns>The compiler diagnostics that were found in the code</returns>
  60. private static IEnumerable<Diagnostic> GetCompilerDiagnostics(Document document) =>
  61. document.GetSemanticModelAsync().Result.GetDiagnostics();
  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. {
  69. var simplifiedDoc = Simplifier.ReduceAsync(document, Simplifier.Annotation).Result;
  70. var root = simplifiedDoc.GetSyntaxRootAsync().Result;
  71. root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace);
  72. return root.GetText().ToString();
  73. }
  74. }
  75. }