[mono-api-html] Make it possible to hide/show non-breaking changes in the html output.
[mono.git] / mcs / tools / corcompare / mono-api-html / ApiChange.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Xml.Linq;
5
6 namespace Xamarin.ApiDiff
7 {
8         public class ApiChange
9         {
10                 public string Header;
11                 public StringBuilder Member = new StringBuilder ();
12                 public bool Breaking;
13                 public bool AnyChange;
14                 public bool HasIgnoredChanges;
15
16                 public ApiChange Append (string text)
17                 {
18                         Member.Append (text);
19                         return this;
20                 }
21
22                 public ApiChange AppendAdded (string text, bool breaking = false)
23                 {
24                         Member.Append ("<span class='added ").Append (breaking ? "added-breaking-inline" : string.Empty).Append ("'>");
25                         Member.Append (text);
26                         Member.Append ("</span>");
27                         Breaking |= breaking;
28                         AnyChange = true;
29                         return this;
30                 }
31
32                 public ApiChange AppendRemoved (string text, bool breaking = true)
33                 {
34                         Member.Append ("<span class='removed removed-inline ").Append (breaking ? "removed-breaking-inline" : string.Empty).Append ("'>");
35                         Member.Append (text);
36                         Member.Append ("</span>");
37                         Breaking |= breaking;
38                         AnyChange = true;
39                         return this;
40                 }
41
42                 public ApiChange AppendModified (string old, string @new, bool breaking = true)
43                 {
44                         if (old.Length > 0)
45                                 AppendRemoved (old, breaking);
46                         if (old.Length > 0 && @new.Length > 0)
47                                 Append (" ");
48                         if (@new.Length > 0)
49                                 AppendAdded (@new);
50                         Breaking |= breaking;
51                         AnyChange = true;
52                         return this;
53                 }
54         }
55
56         public class ApiChanges : Dictionary<string, List<ApiChange>> {
57                 public void Add (XElement source, XElement target, ApiChange change)
58                 {
59                         if (!change.AnyChange) {
60                                 // This is most likely because the rendering doesn't take into account something that's different (solution: fix rendering).
61                                 if (!change.HasIgnoredChanges) {
62                                         var isField = source.Name.LocalName == "field";
63                                         if (isField) {
64                                                 Console.WriteLine ("Comparison resulting in no changes (src: {2} dst: {3}) :\n{0}\n{1}\n\n", source.ToString (), target.ToString (), source.GetFieldAttributes (), target.GetFieldAttributes ());
65                                         } else {
66                                                 Console.WriteLine ("Comparison resulting in no changes (src: {2} dst: {3}) :\n{0}\n{1}\n\n", source.ToString (), target.ToString (), source.GetMethodAttributes (), target.GetMethodAttributes ());
67                                         }
68                                 }
69                                 return;
70                         }
71
72                         List<ApiChange> list;
73                         if (!TryGetValue (change.Header, out list)) {
74                                 list = new List<ApiChange> ();
75                                 base.Add (change.Header, list);
76                         }
77                         list.Add (change);
78                 }
79         }
80 }
81