| Name | Message | Date |
|---|---|---|
| 📄 GraphLayout.cs | 5 days ago | |
| 📄 GraphScene.cs | 5 days ago |
📄
ui/src/Graph/GraphLayout.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Threading; using Avalonia; using Microsoft.CodeAnalysis; namespace Cfa.Ui.Graph; public static class GraphLayout { private const double SiblingGap = 32; private static readonly SymbolDisplayFormat LabelFormat = new( typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameOnly, genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, memberOptions: SymbolDisplayMemberOptions.IncludeParameters, parameterOptions: SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeName | SymbolDisplayParameterOptions.IncludeParamsRefOut, miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes | SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers | SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier ); public static GraphScene Build(CallGraphAnalysis analysis, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(analysis); cancellationToken.ThrowIfCancellationRequested(); var nodes = new List<GraphNode>(); var vertices = new List<Vertex>(); var links = new List<Link>(); var loopbacks = new HashSet<int>(); var origin = CreateNode(analysis.Origin, new Size(280, 80), true, false); origin.Location = new Point(-origin.Width / 2, -origin.Height / 2); var incoming = Expand(analysis.CalledFrom, -1); var outgoing = Expand(analysis.CallsTo, 1); for (var i = vertices.Count - 1; i >= 0; i--) { cancellationToken.ThrowIfCancellationRequested(); var vertex = vertices[i]; vertex.SubtreeHeight = Math.Max(vertex.Node.Height, ChildrenHeight(vertex.Children)); } Position(incoming, -1); Position(outgoing, 1); var edges = new List<GraphEdge>(links.Count); foreach (var link in links) { cancellationToken.ThrowIfCancellationRequested(); var source = link.Direction < 0 ? link.Child : link.Parent; var target = link.Direction < 0 ? link.Parent : link.Child; var loopback = ReferenceEquals(source, target); var start = loopback ? new Point(source.Bounds.Right, source.Bounds.Center.Y - source.Height / 4) : new Point(source.Bounds.Right, source.Bounds.Center.Y); var end = loopback ? new Point(target.Bounds.Right, target.Bounds.Center.Y + target.Height / 4) : new Point(target.Bounds.Left, target.Bounds.Center.Y); edges.Add(new GraphEdge(source, target, start, end, CalculateThickness(link.Call), loopback)); } return new GraphScene(nodes.AsReadOnly(), edges.AsReadOnly()); GraphNode CreateNode(ISymbol symbol, Size size, bool isOrigin, bool isCycleReference) { var syntax = symbol.DeclaringSyntaxReferences.Length > 0 ? symbol.DeclaringSyntaxReferences[0] : null; string? sourceLink = null; if (syntax is not null) { var start = syntax.SyntaxTree.GetLineSpan(syntax.Span, cancellationToken).StartLinePosition; sourceLink = $"file://{syntax.SyntaxTree.FilePath}#{start.Line + 1}:{start.Character + 1}"; } var node = new GraphNode( nodes.Count, symbol, symbol.ToDisplayString(LabelFormat), sourceLink, size, isOrigin, isCycleReference ); nodes.Add(node); return node; } List<Vertex> Expand(IImmutableDictionary<ISymbol, IImmutableList<CallAnalysis>> map, int direction) { var roots = new List<Vertex>(); var path = new HashSet<ISymbol>(SymbolEqualityComparer.Default) { analysis.Origin }; var pending = new Stack<Frame>(); pending.Push(new Frame(origin, roots, GetCalls(analysis.Origin))); while (pending.TryPeek(out var frame)) { cancellationToken.ThrowIfCancellationRequested(); if (frame.Index == frame.Calls.Count) { pending.Pop(); path.Remove(frame.Node.Symbol); continue; } var call = frame.Calls[frame.Index++]; if (SymbolEqualityComparer.Default.Equals(frame.Node.Symbol, call.Target)) { if (loopbacks.Add(frame.Node.Id)) links.Add(new Link(frame.Node, frame.Node, call, direction)); continue; } var cycle = path.Contains(call.Target); var appearances = analysis.Appearances.TryGetValue(call.Target, out var count) ? count : 1; var node = CreateNode(call.Target, CalculateNodeSize(call, appearances), false, cycle); var vertex = new Vertex(node, call); vertices.Add(vertex); frame.Children.Add(vertex); links.Add(new Link(frame.Node, node, call, direction)); if (!cycle) { path.Add(call.Target); pending.Push(new Frame(node, vertex.Children, GetCalls(call.Target))); } } return roots; IImmutableList<CallAnalysis> GetCalls(ISymbol symbol) => map.TryGetValue(symbol, out var calls) ? calls : ImmutableList<CallAnalysis>.Empty; } void Position(List<Vertex> roots, int direction) { var pending = new Queue<(GraphNode Parent, List<Vertex> Children)>(); pending.Enqueue((origin, roots)); while (pending.TryDequeue(out var group)) { cancellationToken.ThrowIfCancellationRequested(); var top = group.Parent.Bounds.Center.Y - ChildrenHeight(group.Children) / 2; foreach (var child in group.Children) { cancellationToken.ThrowIfCancellationRequested(); var centerY = top + child.SubtreeHeight / 2; var offsetY = centerY - group.Parent.Bounds.Center.Y; var length = CalculateEdgeLength(child.Call); // Compact helper branches follow a semicircle; broad subtrees get extra outward space. var projectedY = Math.Min(Math.Abs(offsetY), length * 0.85); var gap = Math.Sqrt(length * length - projectedY * projectedY); gap += Math.Min(160, Math.Max(0, child.SubtreeHeight - child.Node.Height) * 0.08); var x = direction > 0 ? group.Parent.Bounds.Right + gap : group.Parent.Bounds.Left - gap - child.Node.Width; child.Node.Location = new Point(x, centerY - child.Node.Height / 2); top += child.SubtreeHeight + SiblingGap; pending.Enqueue((child.Node, child.Children)); } } } double ChildrenHeight(List<Vertex> children) { var height = 0d; foreach (var child in children) { cancellationToken.ThrowIfCancellationRequested(); height += child.SubtreeHeight; } return height + Math.Max(0, children.Count - 1) * SiblingGap; } } /// <summary>Preferred anchor distance before space for broad branches is added.</summary> public static double CalculateEdgeLength(CallAnalysis call) => 90 + ( call.LinkKind switch { LinkKind.Abstract => 0, LinkKind.Direct => 24, LinkKind.Event => 48, _ => throw new ArgumentOutOfRangeException(nameof(call)), } ) + ( call.Reach switch { CallReach.SameType => 0, CallReach.SameNamespace => 36, CallReach.SameAssembly => 72, CallReach.CrossAssembly => 108, _ => throw new ArgumentOutOfRangeException(nameof(call)), } ); /// <summary>Events and wider reach add prominence; repeats shrink within readable bounds. Origin size is independent.</summary> public static Size CalculateNodeSize(CallAnalysis call, uint appearances) { var labelWidth = Math.Clamp(call.Target.ToDisplayString(LabelFormat).Length * 7.5 + 36, 180, 360); var prominence = 1 + (call.LinkKind == LinkKind.Event ? 0.3 : 0) + ( call.Reach switch { CallReach.SameType => 0, CallReach.SameNamespace => 0.06, CallReach.SameAssembly => 0.12, CallReach.CrossAssembly => 0.2, _ => throw new ArgumentOutOfRangeException(nameof(call)), } ); var scale = 1 / (1 + Math.Log2(Math.Max(1u, appearances)) * 0.12); return new Size( Math.Clamp(labelWidth * prominence * scale, 132, 480), Math.Clamp(64 * prominence * scale, 44, 104) ); } public static double CalculateThickness(CallAnalysis call) => call.LinkKind switch { LinkKind.Abstract => 1.5, LinkKind.Direct => 2.5, LinkKind.Event => 3.5, _ => throw new ArgumentOutOfRangeException(nameof(call)), }; private sealed class Vertex(GraphNode node, CallAnalysis call) { public GraphNode Node { get; } = node; public CallAnalysis Call { get; } = call; public List<Vertex> Children { get; } = []; public double SubtreeHeight { get; set; } } private sealed class Frame(GraphNode node, List<Vertex> children, IImmutableList<CallAnalysis> calls) { public GraphNode Node { get; } = node; public List<Vertex> Children { get; } = children; public IImmutableList<CallAnalysis> Calls { get; } = calls; public int Index { get; set; } } private sealed record Link(GraphNode Parent, GraphNode Child, CallAnalysis Call, int Direction); }