|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.Collections.Concurrent;
|
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Specialized; |
5 | 7 | using System.Diagnostics;
|
6 | 8 | using System.Diagnostics.CodeAnalysis;
|
| 9 | +using System.Reflection; |
7 | 10 | using DiffPlex.DiffBuilder;
|
8 | 11 | using DiffPlex.DiffBuilder.Model;
|
9 | 12 | using Microsoft.CodeAnalysis;
|
@@ -39,6 +42,7 @@ public class MemoryOutputDiffGenerator : IDiffGenerator
|
39 | 42 | private readonly SyntaxList<AttributeListSyntax> _emptyAttributeList;
|
40 | 43 | private readonly IEnumerable<KeyValuePair<string, ReportDiagnostic>> _diagnosticOptions;
|
41 | 44 | private readonly ConcurrentDictionary<string, string> _results;
|
| 45 | + private readonly ChildrenNodesComparer _childrenNodesComparer; |
42 | 46 |
|
43 | 47 | /// <summary>
|
44 | 48 | /// Initializes a new instance of the <see cref="MemoryOutputDiffGenerator"/> class.
|
@@ -80,6 +84,7 @@ internal MemoryOutputDiffGenerator(
|
80 | 84 | _emptyAttributeList = SyntaxFactory.List<AttributeListSyntax>();
|
81 | 85 | _results = [];
|
82 | 86 | _endOfLineTrivia = Environment.NewLine == "\r\n" ? SyntaxFactory.CarriageReturnLineFeed : SyntaxFactory.LineFeed;
|
| 87 | + _childrenNodesComparer = new ChildrenNodesComparer(); |
83 | 88 | }
|
84 | 89 |
|
85 | 90 | /// <inheritdoc/>
|
@@ -217,7 +222,7 @@ private static string GetFinalAssemblyDiff(string assemblyName, string diffText)
|
217 | 222 | StringBuilder sb = new();
|
218 | 223 | // Traverse all the elements found on the left side. This only visits unchanged, modified and deleted APIs.
|
219 | 224 | // In other words, this loop excludes those that are new on the right. Those are handled later.
|
220 |
| - foreach ((string beforeMemberName, MemberDeclarationSyntax beforeMemberNode) in beforeChildrenNodes.OrderBy(x => x.Key)) |
| 225 | + foreach ((string beforeMemberName, MemberDeclarationSyntax beforeMemberNode) in beforeChildrenNodes.Order(_childrenNodesComparer)) |
221 | 226 | {
|
222 | 227 | if (afterChildrenNodes.TryGetValue(beforeMemberName, out MemberDeclarationSyntax? afterMemberNode) &&
|
223 | 228 | beforeMemberNode.Kind() == afterMemberNode.Kind())
|
@@ -265,7 +270,7 @@ private static string GetFinalAssemblyDiff(string assemblyName, string diffText)
|
265 | 270 | if (!afterChildrenNodes.IsEmpty)
|
266 | 271 | {
|
267 | 272 | // Traverse all the elements that are new on the right side which were not found on the left. They are all treated as new APIs.
|
268 |
| - foreach ((string newMemberName, MemberDeclarationSyntax newMemberNode) in afterChildrenNodes.OrderBy(x => x.Key)) |
| 273 | + foreach ((string newMemberName, MemberDeclarationSyntax newMemberNode) in afterChildrenNodes.Order(_childrenNodesComparer)) |
269 | 274 | {
|
270 | 275 | // Need to do a full visit of each member of namespaces and types anyway, so that leaf bodies are removed
|
271 | 276 | if (newMemberNode is BaseTypeDeclarationSyntax or BaseNamespaceDeclarationSyntax)
|
@@ -311,30 +316,37 @@ private static ConcurrentDictionary<string, MemberDeclarationSyntax> CollectChil
|
311 | 316 |
|
312 | 317 | ConcurrentDictionary<string, MemberDeclarationSyntax> dictionary = [];
|
313 | 318 |
|
314 |
| - if (parentNode is RecordDeclarationSyntax record && record.Members.Any()) |
| 319 | + if (parentNode is BaseNamespaceDeclarationSyntax) |
315 | 320 | {
|
316 |
| - foreach (MemberDeclarationSyntax memberNode in record.ChildNodes().Where(n => n is MemberDeclarationSyntax m && IsPublicOrProtectedOrDestructor(m)).Cast<MemberDeclarationSyntax>()) |
317 |
| - { |
318 |
| - // Note that these could also be nested types |
319 |
| - dictionary.TryAdd(GetDocId(memberNode, model), memberNode); |
320 |
| - } |
321 |
| - } |
322 |
| - else if (parentNode is BaseNamespaceDeclarationSyntax) |
323 |
| - { |
324 |
| - foreach (BaseTypeDeclarationSyntax typeNode in parentNode.ChildNodes().Where(n => n is BaseTypeDeclarationSyntax t && IsPublicOrProtectedOrDestructor(t)).Cast<BaseTypeDeclarationSyntax>()) |
| 321 | + // Find all types |
| 322 | + foreach (BaseTypeDeclarationSyntax typeNode in GetMembersOfType<BaseTypeDeclarationSyntax>(parentNode)) |
325 | 323 | {
|
326 | 324 | dictionary.TryAdd(GetDocId(typeNode, model), typeNode);
|
327 | 325 | }
|
328 |
| - foreach (DelegateDeclarationSyntax delegateNode in parentNode.ChildNodes().Where(n => n is DelegateDeclarationSyntax d && IsPublicOrProtectedOrDestructor(d)).Cast<DelegateDeclarationSyntax>()) |
| 326 | + |
| 327 | + // Find all delegates |
| 328 | + foreach (DelegateDeclarationSyntax delegateNode in GetMembersOfType<DelegateDeclarationSyntax>(parentNode)) |
329 | 329 | {
|
330 | 330 | dictionary.TryAdd(GetDocId(delegateNode, model), delegateNode);
|
331 | 331 | }
|
332 | 332 | }
|
333 | 333 | else if (parentNode is BaseTypeDeclarationSyntax)
|
334 | 334 | {
|
335 |
| - foreach (MemberDeclarationSyntax memberNode in parentNode.ChildNodes().Where(n => n is MemberDeclarationSyntax m && IsPublicOrProtectedOrDestructor(m)).Cast<MemberDeclarationSyntax>()) |
| 335 | + // Special case for records that have members |
| 336 | + if (parentNode is RecordDeclarationSyntax record && record.Members.Any()) |
336 | 337 | {
|
337 |
| - dictionary.TryAdd(GetDocId(memberNode, model), memberNode); |
| 338 | + foreach (MemberDeclarationSyntax memberNode in GetMembersOfType<MemberDeclarationSyntax>(parentNode)) |
| 339 | + { |
| 340 | + // Note that these could also be nested types |
| 341 | + dictionary.TryAdd(GetDocId(memberNode, model), memberNode); |
| 342 | + } |
| 343 | + } |
| 344 | + else |
| 345 | + { |
| 346 | + foreach (MemberDeclarationSyntax memberNode in GetMembersOfType<MemberDeclarationSyntax>(parentNode)) |
| 347 | + { |
| 348 | + dictionary.TryAdd(GetDocId(memberNode, model), memberNode); |
| 349 | + } |
338 | 350 | }
|
339 | 351 | }
|
340 | 352 | else if (parentNode is CompilationUnitSyntax)
|
@@ -417,10 +429,22 @@ private static ConcurrentDictionary<string, MemberDeclarationSyntax> CollectChil
|
417 | 429 | return null;
|
418 | 430 | }
|
419 | 431 |
|
420 |
| - if (node is BaseNamespaceDeclarationSyntax namespaceNode) |
| 432 | + if (node is EnumMemberDeclarationSyntax enumMember) |
| 433 | + { |
| 434 | + SyntaxTriviaList commaTrivia = SyntaxFactory.TriviaList(SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, ",")); |
| 435 | + return enumMember |
| 436 | + .WithAttributeLists(_emptyAttributeList) |
| 437 | + .WithLeadingTrivia(node.GetLeadingTrivia()) |
| 438 | + .WithTrailingTrivia(commaTrivia); |
| 439 | + } |
| 440 | + else if (node is BaseNamespaceDeclarationSyntax namespaceNode) |
421 | 441 | {
|
422 | 442 | return namespaceNode.WithAttributeLists(_emptyAttributeList).WithLeadingTrivia(node.GetLeadingTrivia());
|
423 | 443 | }
|
| 444 | + else if (node is MemberDeclarationSyntax memberDeclaration) |
| 445 | + { |
| 446 | + return memberDeclaration.WithAttributeLists(_emptyAttributeList).WithLeadingTrivia(node.GetLeadingTrivia()); |
| 447 | + } |
424 | 448 |
|
425 | 449 | return node.WithAttributeLists(_emptyAttributeList).WithLeadingTrivia(node.GetLeadingTrivia());
|
426 | 450 | }
|
@@ -614,9 +638,17 @@ private string GetClosingBraceCode(SyntaxNode childlessNode, SyntaxTriviaList le
|
614 | 638 | .ToFullString();
|
615 | 639 | }
|
616 | 640 |
|
617 |
| - private static bool IsPublicOrProtectedOrDestructor(MemberDeclarationSyntax m) => |
| 641 | + private static bool IsEnumMemberOrHasPublicOrProtectedModifierOrIsDestructor(MemberDeclarationSyntax m) => |
618 | 642 | // Destructors don't have visibility modifiers so they're special-cased
|
619 |
| - m.Modifiers.Any(SyntaxKind.PublicKeyword) || m.Modifiers.Any(SyntaxKind.ProtectedKeyword) || m.IsKind(SyntaxKind.DestructorDeclaration); |
| 643 | + m.Modifiers.Any(SyntaxKind.PublicKeyword) || m.Modifiers.Any(SyntaxKind.ProtectedKeyword) || |
| 644 | + // Enum member declarations don't have any modifiers |
| 645 | + m is EnumMemberDeclarationSyntax || |
| 646 | + m.IsKind(SyntaxKind.DestructorDeclaration); |
| 647 | + |
| 648 | + private static IEnumerable<T> GetMembersOfType<T>(SyntaxNode node) where T : MemberDeclarationSyntax => node |
| 649 | + .ChildNodes() |
| 650 | + .Where(n => n is T m && IsEnumMemberOrHasPublicOrProtectedModifierOrIsDestructor(m)) |
| 651 | + .Cast<T>(); |
620 | 652 |
|
621 | 653 | private static string GetDocId(SyntaxNode node, SemanticModel model)
|
622 | 654 | {
|
@@ -692,4 +724,22 @@ private static string GetDocId(SyntaxNode node, SemanticModel model)
|
692 | 724 | }
|
693 | 725 | return sb.Length == 0 ? null : sb.ToString();
|
694 | 726 | }
|
| 727 | + |
| 728 | + private class ChildrenNodesComparer : IComparer<KeyValuePair<string, MemberDeclarationSyntax>> |
| 729 | + { |
| 730 | + public int Compare(KeyValuePair<string, MemberDeclarationSyntax> first, KeyValuePair<string, MemberDeclarationSyntax> second) |
| 731 | + { |
| 732 | + // Enum members need to be sorted by their value, not alphabetically, so they need to be special-cased. |
| 733 | + if (first.Value is EnumMemberDeclarationSyntax beforeMember && second.Value is EnumMemberDeclarationSyntax afterMember && |
| 734 | + beforeMember.EqualsValue is EqualsValueClauseSyntax beforeEVCS && afterMember.EqualsValue is EqualsValueClauseSyntax afterEVCS && |
| 735 | + beforeEVCS.Value is LiteralExpressionSyntax beforeLes && afterEVCS.Value is LiteralExpressionSyntax afterLes) |
| 736 | + { |
| 737 | + return beforeLes.Token.ValueText.CompareTo(afterLes.Token.ValueText); |
| 738 | + } |
| 739 | + |
| 740 | + // Everything else is shown alphabetically. |
| 741 | + return first.Key.CompareTo(second.Key); |
| 742 | + } |
| 743 | + |
| 744 | + } |
695 | 745 | }
|
0 commit comments