Skip to content

Commit 250d303

Browse files
[RGen] Add docs to the SmartEnum generated methods. (#22268)
Add the documentation to the smart enum extension class and its methods.
1 parent 0172b3d commit 250d303

10 files changed

Lines changed: 214 additions & 8 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Microsoft.Macios.Generator.Emitters;
2+
3+
/// <summary>
4+
/// Static class that holds all the documentation strings. This allows to make the code generator
5+
/// cleaner by removing the need to have the documentation strings in the code.
6+
/// </summary>
7+
public static class Documentation {
8+
9+
/// <summary>
10+
/// Smart enum documentation.
11+
/// </summary>
12+
public static class SmartEnum {
13+
14+
public static string ClassDocumentation (string name) =>
15+
@$"/// <summary>
16+
/// Extension methods for the <see cref=""{name}"" /> enumeration.
17+
/// </summary>";
18+
19+
public static string GetConstant () =>
20+
@"/// <summary>
21+
/// Retrieves the <see cref=""global::Foundation.NSString"" /> constant that describes <paramref name=""self"" />.
22+
/// </summary>
23+
/// <param name=""self"">The instance on which this method operates.</param>";
24+
25+
public static string GetValueNSString (string name) =>
26+
@$"/// <summary>
27+
/// Retrieves the <see cref=""{name}"" /> value named by <paramref name=""constant"" />.
28+
/// </summary>
29+
/// <param name=""constant"">The name of the constant to retrieve.</param>";
30+
31+
public static string GetValueHandle (string name) =>
32+
@$"/// <summary>
33+
/// Retrieves the <see cref=""{name}"" /> value represented by the backing field value in <paramref name=""handle"" />.
34+
/// </summary>
35+
/// <param name=""handle"">The native handle with the name of the constant to retrieve.</param>";
36+
37+
public static string ToConstantArray (string name) =>
38+
@$"/// <summary>
39+
/// Converts an array of <see cref=""{name}"" /> enum values into an array of their corresponding constants.
40+
/// </summary>
41+
/// <param name=""values"">The array of enum values to convert.</param>";
42+
43+
public static string ToEnumArray (string _) =>
44+
@"/// <summary>
45+
/// Converts an array of <see cref=""NSString"" /> values into an array of their corresponding enum values.
46+
/// </summary>
47+
/// <param name=""values"">The array if <see cref=""NSString"" /> values to convert.</param>";
48+
49+
}
50+
}

src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ bool TryEmit (TabbedWriter<StringWriter> classBlock, in Binding binding)
5757
return true;
5858
}
5959

60-
void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, in Binding binding)
60+
void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, string symbolName, in Binding binding)
6161
{
6262
if (binding.EnumMembers.Length == 0)
6363
return;
6464

6565
// smart enum require 4 diff methods to be able to retrieve the values
6666

6767
// Get constant
68+
classBlock.WriteDocumentation (Documentation.SmartEnum.GetConstant ());
6869
using (var getConstantBlock = classBlock.CreateBlock ($"public static NSString? GetConstant (this {binding.Name} self)", true)) {
6970
getConstantBlock.WriteLine ("IntPtr ptr = IntPtr.Zero;");
7071
using (var switchBlock = getConstantBlock.CreateBlock ("switch ((int) self)", true)) {
@@ -84,6 +85,7 @@ void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, in Binding bin
8485

8586
classBlock.WriteLine ();
8687
// Get value
88+
classBlock.WriteDocumentation (Documentation.SmartEnum.GetValueNSString (symbolName));
8789
using (var getValueBlock = classBlock.CreateBlock ($"public static {binding.Name} GetValue (NSString constant)", true)) {
8890
getValueBlock.WriteLine ("if (constant is null)");
8991
getValueBlock.WriteLine ("\tthrow new ArgumentNullException (nameof (constant));");
@@ -101,6 +103,7 @@ void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, in Binding bin
101103
classBlock.WriteLine ();
102104

103105
// get value from a handle, this is a helper method used in the BindAs bindings.
106+
classBlock.WriteDocumentation (Documentation.SmartEnum.GetValueHandle (symbolName));
104107
using (var getValueFromHandle =
105108
classBlock.CreateBlock ($"public static {binding.Name} GetValue (NativeHandle handle)",
106109
true)) {
@@ -112,6 +115,7 @@ void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, in Binding bin
112115

113116
classBlock.WriteLine ();
114117
// To ConstantArray
118+
classBlock.WriteDocumentation (Documentation.SmartEnum.ToConstantArray (symbolName));
115119
classBlock.WriteRaw (
116120
@$"internal static NSString?[]? ToConstantArray (this {binding.Name}[]? values)
117121
{{
@@ -127,6 +131,7 @@ void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, in Binding bin
127131
classBlock.WriteLine ();
128132
classBlock.WriteLine ();
129133
// ToEnumArray
134+
classBlock.WriteDocumentation (Documentation.SmartEnum.ToEnumArray (symbolName));
130135
classBlock.WriteRaw (
131136
@$"internal static {binding.Name}[]? ToEnumArray (this NSString[]? values)
132137
{{
@@ -165,10 +170,13 @@ bool TryEmitSmartEnum (in BindingTypeData<SmartEnum> _, in BindingContext bindin
165170
bindingContext.Builder.WriteLine ($"namespace {string.Join (".", bindingContext.Changes.Namespace)};");
166171
bindingContext.Builder.WriteLine ();
167172

173+
var symbolName = GetSymbolName (bindingContext.Changes);
174+
var extensionClassDeclaration =
175+
bindingContext.Changes.ToSmartEnumExtensionDeclaration (symbolName);
176+
177+
bindingContext.Builder.WriteDocumentation (Documentation.SmartEnum.ClassDocumentation (symbolName));
168178
bindingContext.Builder.AppendMemberAvailability (bindingContext.Changes.SymbolAvailability);
169179
bindingContext.Builder.AppendGeneratedCodeAttribute ();
170-
var extensionClassDeclaration =
171-
bindingContext.Changes.ToSmartEnumExtensionDeclaration (GetSymbolName (bindingContext.Changes));
172180
using (var classBlock = bindingContext.Builder.CreateBlock (extensionClassDeclaration.ToString (), true)) {
173181
classBlock.WriteLine ();
174182
classBlock.WriteLine ($"static IntPtr[] values = new IntPtr [{bindingContext.Changes.EnumMembers.Length}];");
@@ -181,7 +189,7 @@ bool TryEmitSmartEnum (in BindingTypeData<SmartEnum> _, in BindingContext bindin
181189
classBlock.WriteLine ();
182190

183191
// emit the extension methods that will be used to get the values from the enum
184-
EmitExtensionMethods (classBlock, bindingContext.Changes);
192+
EmitExtensionMethods (classBlock, symbolName, bindingContext.Changes);
185193
classBlock.WriteLine ();
186194
}
187195

src/rgen/Microsoft.Macios.Generator/IO/TabbedWriter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ public TabbedWriter<T> WriteRaw (string rawString)
227227
}
228228
#endif
229229

230+
/// <summary>
231+
/// Writes a multi-line documentation comment. Ensure that the raw string used for the docs
232+
/// does not have a new line at the end, this method will add it for you.
233+
/// </summary>
234+
/// <param name="docsRawString">The documentation raw string.</param>
235+
/// <returns>The current writer.</returns>
236+
public TabbedWriter<T> WriteDocumentation (string docsRawString)
237+
{
238+
// user the raw string to write the documentation and add a new line
239+
WriteRaw (docsRawString);
240+
WriteLine ();
241+
return this;
242+
}
243+
230244
/// <summary>
231245
/// Append a new raw literal by prepending the correct indentation.
232246
/// </summary>

tests/cecil-tests/Documentation.KnownFailures.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23148,9 +23148,6 @@ M:AVFoundation.AVCapturePhotoOutputReadinessCoordinator.Dispose(System.Boolean)
2314823148
M:AVFoundation.AVCapturePhotoSettings.Dispose(System.Boolean)
2314923149
M:AVFoundation.AVCapturePhotoSettingsThumbnailFormat.#ctor
2315023150
M:AVFoundation.AVCapturePhotoSettingsThumbnailFormat.#ctor(Foundation.NSDictionary)
23151-
M:AVFoundation.AVCaptureReactionTypeExtensions.GetConstant(AVFoundation.AVCaptureReactionType)
23152-
M:AVFoundation.AVCaptureReactionTypeExtensions.GetValue(Foundation.NSString)
23153-
M:AVFoundation.AVCaptureReactionTypeExtensions.GetValue(ObjCRuntime.NativeHandle)
2315423151
M:AVFoundation.AVCaptureSessionRuntimeErrorEventArgs.#ctor(Foundation.NSNotification)
2315523152
M:AVFoundation.AVCaptureStillImageOutput.CaptureStillImageTaskAsync(AVFoundation.AVCaptureConnection)
2315623153
M:AVFoundation.AVCaptureVideoDataOutput.GetRecommendedVideoSettings(AVFoundation.AVVideoCodecType,AVFoundation.AVFileTypes,Foundation.NSUrl)
@@ -66092,7 +66089,6 @@ T:AVFoundation.AVCapturePrimaryConstituentDeviceRestrictedSwitchingBehaviorCondi
6609266089
T:AVFoundation.AVCapturePrimaryConstituentDeviceSwitchingBehavior
6609366090
T:AVFoundation.AVCaptureReactionType
6609466091
T:AVFoundation.AVCaptureReactionType_Extensions
66095-
T:AVFoundation.AVCaptureReactionTypeExtensions
6609666092
T:AVFoundation.AVCaptureSessionControlsDelegate
6609766093
T:AVFoundation.AVCaptureSessionInterruptionReason
6609866094
T:AVFoundation.AVCaptureSessionRuntimeErrorEventArgs

tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace AVFoundation;
1111

12+
/// <summary>
13+
/// Extension methods for the <see cref="AVCaptureDeviceTypeExtensions" /> enumeration.
14+
/// </summary>
1215
[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
1316
public static partial class AVCaptureDeviceTypeExtensions
1417
{
@@ -125,6 +128,10 @@ internal unsafe static IntPtr AVCaptureDeviceTypeBuiltInLiDARDepthCamera
125128
}
126129
}
127130

131+
/// <summary>
132+
/// Retrieves the <see cref="global::Foundation.NSString" /> constant that describes <paramref name="self" />.
133+
/// </summary>
134+
/// <param name="self">The instance on which this method operates.</param>
128135
public static NSString? GetConstant (this AVCaptureDeviceType self)
129136
{
130137
IntPtr ptr = IntPtr.Zero;
@@ -167,6 +174,10 @@ internal unsafe static IntPtr AVCaptureDeviceTypeBuiltInLiDARDepthCamera
167174
return (NSString?) Runtime.GetNSObject (ptr);
168175
}
169176

177+
/// <summary>
178+
/// Retrieves the <see cref="AVCaptureDeviceTypeExtensions" /> value named by <paramref name="constant" />.
179+
/// </summary>
180+
/// <param name="constant">The name of the constant to retrieve.</param>
170181
public static AVCaptureDeviceType GetValue (NSString constant)
171182
{
172183
if (constant is null)
@@ -196,12 +207,20 @@ public static AVCaptureDeviceType GetValue (NSString constant)
196207
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
197208
}
198209

210+
/// <summary>
211+
/// Retrieves the <see cref="AVCaptureDeviceTypeExtensions" /> value represented by the backing field value in <paramref name="handle" />.
212+
/// </summary>
213+
/// <param name="handle">The native handle with the name of the constant to retrieve.</param>
199214
public static AVCaptureDeviceType GetValue (NativeHandle handle)
200215
{
201216
using var str = Runtime.GetNSObject<NSString> (handle)!;
202217
return GetValue (str);
203218
}
204219

220+
/// <summary>
221+
/// Converts an array of <see cref="AVCaptureDeviceTypeExtensions" /> enum values into an array of their corresponding constants.
222+
/// </summary>
223+
/// <param name="values">The array of enum values to convert.</param>
205224
internal static NSString?[]? ToConstantArray (this AVCaptureDeviceType[]? values)
206225
{
207226
if (values is null)
@@ -214,6 +233,10 @@ public static AVCaptureDeviceType GetValue (NativeHandle handle)
214233
return rv.ToArray ();
215234
}
216235

236+
/// <summary>
237+
/// Converts an array of <see cref="NSString" /> values into an array of their corresponding enum values.
238+
/// </summary>
239+
/// <param name="values">The array if <see cref="NSString" /> values to convert.</param>
217240
internal static AVCaptureDeviceType[]? ToEnumArray (this NSString[]? values)
218241
{
219242
if (values is null)

tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace AVFoundation;
1111

12+
/// <summary>
13+
/// Extension methods for the <see cref="AVCaptureSystemPressureLevelExtensions" /> enumeration.
14+
/// </summary>
1215
[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
1316
public static partial class AVCaptureSystemPressureLevelExtensions
1417
{
@@ -65,6 +68,10 @@ internal unsafe static IntPtr AVCaptureSystemPressureLevelShutdown
6568
}
6669
}
6770

71+
/// <summary>
72+
/// Retrieves the <see cref="global::Foundation.NSString" /> constant that describes <paramref name="self" />.
73+
/// </summary>
74+
/// <param name="self">The instance on which this method operates.</param>
6875
public static NSString? GetConstant (this AVCaptureSystemPressureLevel self)
6976
{
7077
IntPtr ptr = IntPtr.Zero;
@@ -89,6 +96,10 @@ internal unsafe static IntPtr AVCaptureSystemPressureLevelShutdown
8996
return (NSString?) Runtime.GetNSObject (ptr);
9097
}
9198

99+
/// <summary>
100+
/// Retrieves the <see cref="AVCaptureSystemPressureLevelExtensions" /> value named by <paramref name="constant" />.
101+
/// </summary>
102+
/// <param name="constant">The name of the constant to retrieve.</param>
92103
public static AVCaptureSystemPressureLevel GetValue (NSString constant)
93104
{
94105
if (constant is null)
@@ -106,12 +117,20 @@ public static AVCaptureSystemPressureLevel GetValue (NSString constant)
106117
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
107118
}
108119

120+
/// <summary>
121+
/// Retrieves the <see cref="AVCaptureSystemPressureLevelExtensions" /> value represented by the backing field value in <paramref name="handle" />.
122+
/// </summary>
123+
/// <param name="handle">The native handle with the name of the constant to retrieve.</param>
109124
public static AVCaptureSystemPressureLevel GetValue (NativeHandle handle)
110125
{
111126
using var str = Runtime.GetNSObject<NSString> (handle)!;
112127
return GetValue (str);
113128
}
114129

130+
/// <summary>
131+
/// Converts an array of <see cref="AVCaptureSystemPressureLevelExtensions" /> enum values into an array of their corresponding constants.
132+
/// </summary>
133+
/// <param name="values">The array of enum values to convert.</param>
115134
internal static NSString?[]? ToConstantArray (this AVCaptureSystemPressureLevel[]? values)
116135
{
117136
if (values is null)
@@ -124,6 +143,10 @@ public static AVCaptureSystemPressureLevel GetValue (NativeHandle handle)
124143
return rv.ToArray ();
125144
}
126145

146+
/// <summary>
147+
/// Converts an array of <see cref="NSString" /> values into an array of their corresponding enum values.
148+
/// </summary>
149+
/// <param name="values">The array if <see cref="NSString" /> values to convert.</param>
127150
internal static AVCaptureSystemPressureLevel[]? ToEnumArray (this NSString[]? values)
128151
{
129152
if (values is null)

tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
namespace CustomLibrary;
1212

13+
/// <summary>
14+
/// Extension methods for the <see cref="CustomLibraryEnumExtensions" /> enumeration.
15+
/// </summary>
1316
[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
1417
public static partial class CustomLibraryEnumExtensions
1518
{
@@ -46,6 +49,10 @@ internal unsafe static IntPtr High
4649
}
4750
}
4851

52+
/// <summary>
53+
/// Retrieves the <see cref="global::Foundation.NSString" /> constant that describes <paramref name="self" />.
54+
/// </summary>
55+
/// <param name="self">The instance on which this method operates.</param>
4956
public static NSString? GetConstant (this CustomLibraryEnum self)
5057
{
5158
IntPtr ptr = IntPtr.Zero;
@@ -64,6 +71,10 @@ internal unsafe static IntPtr High
6471
return (NSString?) Runtime.GetNSObject (ptr);
6572
}
6673

74+
/// <summary>
75+
/// Retrieves the <see cref="CustomLibraryEnumExtensions" /> value named by <paramref name="constant" />.
76+
/// </summary>
77+
/// <param name="constant">The name of the constant to retrieve.</param>
6778
public static CustomLibraryEnum GetValue (NSString constant)
6879
{
6980
if (constant is null)
@@ -77,12 +88,20 @@ public static CustomLibraryEnum GetValue (NSString constant)
7788
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
7889
}
7990

91+
/// <summary>
92+
/// Retrieves the <see cref="CustomLibraryEnumExtensions" /> value represented by the backing field value in <paramref name="handle" />.
93+
/// </summary>
94+
/// <param name="handle">The native handle with the name of the constant to retrieve.</param>
8095
public static CustomLibraryEnum GetValue (NativeHandle handle)
8196
{
8297
using var str = Runtime.GetNSObject<NSString> (handle)!;
8398
return GetValue (str);
8499
}
85100

101+
/// <summary>
102+
/// Converts an array of <see cref="CustomLibraryEnumExtensions" /> enum values into an array of their corresponding constants.
103+
/// </summary>
104+
/// <param name="values">The array of enum values to convert.</param>
86105
internal static NSString?[]? ToConstantArray (this CustomLibraryEnum[]? values)
87106
{
88107
if (values is null)
@@ -95,6 +114,10 @@ public static CustomLibraryEnum GetValue (NativeHandle handle)
95114
return rv.ToArray ();
96115
}
97116

117+
/// <summary>
118+
/// Converts an array of <see cref="NSString" /> values into an array of their corresponding enum values.
119+
/// </summary>
120+
/// <param name="values">The array if <see cref="NSString" /> values to convert.</param>
98121
internal static CustomLibraryEnum[]? ToEnumArray (this NSString[]? values)
99122
{
100123
if (values is null)

0 commit comments

Comments
 (0)