Auto-format source code
This commit is contained in:
Родитель
0d81973fc6
Коммит
0f10657df9
|
@ -21,10 +21,10 @@ record FieldData {
|
|||
var count = attributeData.ConstructorArguments.Length;
|
||||
switch (count) {
|
||||
case 1:
|
||||
data = new((string) attributeData.ConstructorArguments [0].Value!);
|
||||
data = new ((string) attributeData.ConstructorArguments [0].Value!);
|
||||
return true;
|
||||
case 2:
|
||||
data = new((string) attributeData.ConstructorArguments [0].Value!,
|
||||
data = new ((string) attributeData.ConstructorArguments [0].Value!,
|
||||
(string) attributeData.ConstructorArguments [1].Value!);
|
||||
return true;
|
||||
default:
|
||||
|
|
|
@ -132,23 +132,23 @@ public class BindingSourceGeneratorGenerator : IIncrementalGenerator {
|
|||
var sb = new TabbedStringBuilder (new ());
|
||||
// let people know this is generated code
|
||||
sb.AppendLine ("// <auto-generated>");
|
||||
|
||||
|
||||
// enable nullable!
|
||||
sb.AppendLine ();
|
||||
sb.AppendFormatLine ("#nullable enable");
|
||||
sb.AppendLine ();
|
||||
|
||||
|
||||
CollectUsingStatements (baseTypeDeclarationSyntax.SyntaxTree, sb);
|
||||
|
||||
// delegate semantic model and syntax tree analysis to the emitter who will generate the code and knows
|
||||
// best
|
||||
if (ContextFactory.TryCreate (rootContext, semanticModel, namedTypeSymbol, baseTypeDeclarationSyntax, out var symbolBindingContext)
|
||||
&& EmitterFactory.TryCreate (symbolBindingContext, sb, out var emitter)) {
|
||||
if (emitter.TryEmit(out var diagnostics)) {
|
||||
// only add file when we do generate code
|
||||
var code = sb.ToString ();
|
||||
context.AddSource ($"{symbolBindingContext.Namespace}/{emitter.SymbolName}.g.cs",
|
||||
SourceText.From (code, Encoding.UTF8));
|
||||
if (emitter.TryEmit (out var diagnostics)) {
|
||||
// only add file when we do generate code
|
||||
var code = sb.ToString ();
|
||||
context.AddSource ($"{symbolBindingContext.Namespace}/{emitter.SymbolName}.g.cs",
|
||||
SourceText.From (code, Encoding.UTF8));
|
||||
} else {
|
||||
// add to the diagnostics and continue to the next possible candidate
|
||||
foreach (Diagnostic diagnostic in diagnostics) {
|
||||
|
|
|
@ -10,7 +10,7 @@ class ClassEmitter (ClassBindingContext context, TabbedStringBuilder builder) :
|
|||
|
||||
public bool TryEmit ([NotNullWhen (false)] out ImmutableArray<Diagnostic>? diagnostics)
|
||||
{
|
||||
|
||||
|
||||
builder.AppendLine ();
|
||||
diagnostics = null;
|
||||
// add the namespace and the class declaration
|
||||
|
|
|
@ -17,7 +17,7 @@ class EnumEmitter (ISymbolBindingContext<EnumDeclarationSyntax> context, TabbedS
|
|||
{
|
||||
var typeNamespace = enumField.Symbol.ContainingType.ContainingNamespace.Name;
|
||||
if (!context.RootBindingContext.TryComputeLibraryName (enumField.FieldData.LibraryName, typeNamespace,
|
||||
out string? libraryName, out string? libraryPath)) {
|
||||
out string? libraryName, out string? libraryPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ class EnumEmitter (ISymbolBindingContext<EnumDeclarationSyntax> context, TabbedS
|
|||
using (var propertyBlock = classBlock.CreateBlock ($"internal unsafe static IntPtr {enumField.FieldData.SymbolName}", true))
|
||||
using (var getterBlock = propertyBlock.CreateBlock ("get", true)) {
|
||||
getterBlock.AppendFormatLine ("fixed (IntPtr *storage = &values [{0}])", index);
|
||||
getterBlock.AppendFormatLine("\treturn Dlfcn.CachePointer (Libraries.{0}.Handle, \"{1}\", storage);",
|
||||
getterBlock.AppendFormatLine ("\treturn Dlfcn.CachePointer (Libraries.{0}.Handle, \"{1}\", storage);",
|
||||
libraryPath ?? libraryName, enumField.FieldData.SymbolName);
|
||||
}
|
||||
}
|
||||
|
@ -108,34 +108,34 @@ class EnumEmitter (ISymbolBindingContext<EnumDeclarationSyntax> context, TabbedS
|
|||
}}");
|
||||
}
|
||||
|
||||
public bool TryEmit ([NotNullWhen (false)] out ImmutableArray<Diagnostic>? diagnostics)
|
||||
{
|
||||
diagnostics = null;
|
||||
if (!context.Symbol.TryGetEnumFields (out var members,
|
||||
out diagnostics) || members.Value.Length == 0) {
|
||||
diagnostics = new ImmutableArray<Diagnostic> ();
|
||||
return false;
|
||||
}
|
||||
// in the old generator we had to copy over the enum, in this new approach the only code
|
||||
// we need to create is the extension class for the enum that is backed by fields
|
||||
builder.AppendLine ();
|
||||
builder.AppendFormatLine ("namespace {0};", context.Namespace);
|
||||
builder.AppendLine ();
|
||||
public bool TryEmit ([NotNullWhen (false)] out ImmutableArray<Diagnostic>? diagnostics)
|
||||
{
|
||||
diagnostics = null;
|
||||
if (!context.Symbol.TryGetEnumFields (out var members,
|
||||
out diagnostics) || members.Value.Length == 0) {
|
||||
diagnostics = new ImmutableArray<Diagnostic> ();
|
||||
return false;
|
||||
}
|
||||
// in the old generator we had to copy over the enum, in this new approach the only code
|
||||
// we need to create is the extension class for the enum that is backed by fields
|
||||
builder.AppendLine ();
|
||||
builder.AppendFormatLine ("namespace {0};", context.Namespace);
|
||||
builder.AppendLine ();
|
||||
|
||||
builder.AppendGeneratedCodeAttribute ();
|
||||
using (var classBlock = builder.CreateBlock ($"static public partial class {SymbolName}", true)) {
|
||||
classBlock.AppendLine ();
|
||||
classBlock.AppendFormatLine ("static IntPtr[] values = new IntPtr [{0}];", members.Value.Length);
|
||||
// foreach member in the enum we need to create a field that holds the value, the property emitter
|
||||
// will take care of generating the property. Do not order by name to keep the order of the enum
|
||||
Emit (classBlock, members.Value);
|
||||
classBlock.AppendLine ();
|
||||
builder.AppendGeneratedCodeAttribute ();
|
||||
using (var classBlock = builder.CreateBlock ($"static public partial class {SymbolName}", true)) {
|
||||
classBlock.AppendLine ();
|
||||
classBlock.AppendFormatLine ("static IntPtr[] values = new IntPtr [{0}];", members.Value.Length);
|
||||
// foreach member in the enum we need to create a field that holds the value, the property emitter
|
||||
// will take care of generating the property. Do not order by name to keep the order of the enum
|
||||
Emit (classBlock, members.Value);
|
||||
classBlock.AppendLine ();
|
||||
|
||||
// emit the extension methods that will be used to get the values from the enum
|
||||
Emit (classBlock, context.Symbol, members);
|
||||
classBlock.AppendLine ();
|
||||
}
|
||||
// emit the extension methods that will be used to get the values from the enum
|
||||
Emit (classBlock, context.Symbol, members);
|
||||
classBlock.AppendLine ();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ static class TypeSymbolExtensions {
|
|||
var boundAttributes = symbol.GetAttributes ();
|
||||
if (boundAttributes.Length == 0) {
|
||||
// return an empty dictionary if there are no attributes
|
||||
return new();
|
||||
return new ();
|
||||
}
|
||||
|
||||
var attributes = new Dictionary<string, AttributeData> ();
|
||||
|
|
|
@ -20,7 +20,7 @@ public class BaseGeneratorTestClass {
|
|||
|
||||
// list of the defines for each platform, this is passed to the parser to ensure that
|
||||
// we are testing the platforms as if they were being compiled.
|
||||
readonly Dictionary<ApplePlatform, string[]> platformDefines = new() {
|
||||
readonly Dictionary<ApplePlatform, string []> platformDefines = new () {
|
||||
{ ApplePlatform.iOS, new [] { "__IOS__" } },
|
||||
{ ApplePlatform.TVOS, new [] { "__TVOS__" } },
|
||||
{ ApplePlatform.MacOSX, new [] { "__MACOS__" } },
|
||||
|
@ -56,7 +56,7 @@ public class BaseGeneratorTestClass {
|
|||
throw new InvalidOperationException ($"Could not find platform dll for {platform}");
|
||||
}
|
||||
|
||||
var parseOptions = new CSharpParseOptions (LanguageVersion.Latest, DocumentationMode.None, preprocessorSymbols: platformDefines[platform]);;
|
||||
var parseOptions = new CSharpParseOptions (LanguageVersion.Latest, DocumentationMode.None, preprocessorSymbols: platformDefines [platform]); ;
|
||||
var trees = sources.Select (s => CSharpSyntaxTree.ParseText (s, parseOptions));
|
||||
|
||||
var options = new CSharpCompilationOptions (OutputKind.NetModule);
|
||||
|
|
|
@ -9,11 +9,9 @@ namespace Microsoft.Macios.Generator.Tests.SmartEnum;
|
|||
/// <summary>
|
||||
/// Test all the field generation code.
|
||||
/// </summary>
|
||||
public class SmartEnumTests : BaseGeneratorTestClass
|
||||
{
|
||||
public class TestDataGenerator : BaseTestDataGenerator, IEnumerable<object[]>
|
||||
{
|
||||
readonly List<(ApplePlatform Platform, string ClassName, string BindingFile, string OutputFile)> _data = new()
|
||||
public class SmartEnumTests : BaseGeneratorTestClass {
|
||||
public class TestDataGenerator : BaseTestDataGenerator, IEnumerable<object []> {
|
||||
readonly List<(ApplePlatform Platform, string ClassName, string BindingFile, string OutputFile)> _data = new ()
|
||||
{
|
||||
(ApplePlatform.iOS, "AVCaptureDeviceTypeExtensions", "AVCaptureDeviceTypeEnum.cs", "ExpectedAVCaptureDeviceTypeEnum.cs" ),
|
||||
(ApplePlatform.iOS, "AVCaptureSystemPressureLevelExtensions", "AVCaptureSystemPressureLevel.cs", "ExpectedAVCaptureSystemPressureLevel.cs" ),
|
||||
|
@ -21,25 +19,24 @@ public class SmartEnumTests : BaseGeneratorTestClass
|
|||
(ApplePlatform.MacOSX, "AVMediaCharacteristicsExtensions", "AVMediaCharacteristics.cs", "ExpectedMacOSAVMediaCharacteristics.cs" ),
|
||||
};
|
||||
|
||||
public IEnumerator<object[]> GetEnumerator()
|
||||
public IEnumerator<object []> GetEnumerator ()
|
||||
{
|
||||
foreach (var testData in _data)
|
||||
{
|
||||
foreach (var testData in _data) {
|
||||
yield return [
|
||||
testData.Platform,
|
||||
testData.ClassName,
|
||||
testData.BindingFile,
|
||||
ReadFileAsString(testData.BindingFile),
|
||||
ReadFileAsString (testData.BindingFile),
|
||||
testData.OutputFile,
|
||||
ReadFileAsString(testData.OutputFile)];
|
||||
ReadFileAsString (testData.OutputFile)];
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[ClassData(typeof(TestDataGenerator))]
|
||||
[ClassData (typeof (TestDataGenerator))]
|
||||
public void ExtensionGenerationTests (ApplePlatform platform, string className, string inputFileName, string inputText, string outputFileName, string expectedOutputText)
|
||||
=> CompareGeneratedCode(platform, className, inputFileName, inputText, outputFileName, expectedOutputText);
|
||||
=> CompareGeneratedCode (platform, className, inputFileName, inputText, outputFileName, expectedOutputText);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче