-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMoveNextBeautifier.cs
More file actions
128 lines (111 loc) · 4.62 KB
/
Copy pathMoveNextBeautifier.cs
File metadata and controls
128 lines (111 loc) · 4.62 KB
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
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using NetStackBeautifier.Core;
namespace NetStackBeautifier.Services.LineBeautifiers;
internal class MoveNextBeautifier<T> : LineBeautifierBase<T>
where T : IBeautifier
{
// Matches state machine info.
// For example: aa<bbaera>M<>anagedIdentitySource+<AuthenticateAsync>d__9 will match
// Group[1]-ClassName-aa<bbaera>M<>anagedIdentitySource
// Group[2]-MethodName-AuthenticateAsync
// Optionally, the method might be a generics, for example: CacheExtensions+<GetOrCreateAsync>d__9`1
// Group[1]-ClassName-CacheExtensions
// Group[2]-MethodName-GetOrCreateAsync
// Group[3]-Generic Parameter Count -1
private const string StateMachineMethodMatherExp = @"^(.*)\+<(.*?)>d[_]*[\d]*(?:`([\d])+)*$";
private static readonly Regex _stateMachineMethodMatcher = new Regex(StateMachineMethodMatherExp, RegexOptions.CultureInvariant, TimeSpan.FromSeconds(1));
// Match DisplayClass tag in class
// For example: CentralStorageAuthority+<>c__DisplayClass2_1
// Group[1]-ClassName-CentralStorageAuthority
// Group[2]-Display class place holder-+<>c__DisplayClass2_1
private const string DisplayClassCleanerExp = @"(.*)\+<.*?c__DisplayClass[\d]+_[\d]+$";
private static readonly Regex _displayClassHolderMatcher = new Regex(DisplayClassCleanerExp, RegexOptions.CultureInvariant, TimeSpan.FromSeconds(1));
// Lambda expression in this form: <GetCacheSettingsAsync>b__0
// Group[1]-Lambda method-GetCacheSettingsAsync
private const string DeLambdaExp = @"^<(.*?)>b(?:__[\d]*)?$";
private static readonly Regex _deLambdaMatcher = new Regex(DeLambdaExp, RegexOptions.CultureInvariant, TimeSpan.FromSeconds(1));
public MoveNextBeautifier(ILogger<MoveNextBeautifier<T>> logger) : base(logger)
{
}
protected override Task<FrameItem> BeautifyImpAsync(FrameItem line, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(line.Method?.Name) || string.IsNullOrEmpty(line.FullClass?.ShortClassNameOrDefault))
{
return Task.FromResult(line);
}
if (string.Equals(line.Method.Name, "MoveNext", StringComparison.OrdinalIgnoreCase))
{
Match match = _stateMachineMethodMatcher.Match(line.FullClass.ShortClassNameOrDefault);
if (match.Success)
{
string newShortClassName = CleanUpDisplayClass(match.Groups[1].Value);
string newMethodName = CleanupMethod(match.Groups[2].Value);
int genericMethodCount = 0;
if (match.Groups.Count >= 4 && int.TryParse(match.Groups[3].Value, out genericMethodCount))
{
// genericMethodCount has been assigned upon success already.
}
line = line with
{
FullClass = line.FullClass with
{
NameSections = line.FullClass.NameSections.SkipLast(0).Append(newShortClassName),
},
Method = line.Method with
{
Name = newMethodName,
GenericParameterTypes = line.Method.GenericParameterTypes.NullAsEmpty().Any() ? line.Method.GenericParameterTypes : GenerateTypeParameter(genericMethodCount),
RawGenericParameterTypes = line.Method.GenericParameterTypes
},
};
}
}
return Task.FromResult(line);
}
private IEnumerable<string> GenerateTypeParameter(int count)
{
if (count <= 0)
{
return Enumerable.Empty<string>();
}
List<string> results = new List<string>();
for (int i = 0; i < count; i++)
{
if (i == 0)
{
results.Add("T");
continue;
}
int suffix = i + 1;
results.Add("T" + suffix);
}
return results;
}
private string CleanUpDisplayClass(string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
Match match = _displayClassHolderMatcher.Match(input);
if (!match.Success)
{
return input;
}
return match.Groups[1].Value;
}
private string CleanupMethod(string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
Match match = _deLambdaMatcher.Match(input);
if (!match.Success)
{
return input;
}
return "() => " + match.Groups[1].Value;
}
}