Fix : Format Code

This commit is contained in:
Akeit0 2024-03-03 13:01:00 +09:00
parent 3b4d8ba52e
commit 3089992651

View File

@ -26,26 +26,22 @@ namespace Alchemy.SourceGenerator
{ {
foreach (var typeSyntax in receiver.TargetTypes) foreach (var typeSyntax in receiver.TargetTypes)
{ {
var typeSymbol = context.Compilation.GetSemanticModel(typeSyntax.SyntaxTree) var typeSymbol = context.Compilation.GetSemanticModel(typeSyntax.SyntaxTree).GetDeclaredSymbol(typeSyntax);
.GetDeclaredSymbol(typeSyntax);
if (!IsPartial(typeSyntax)) if (!IsPartial(typeSyntax))
{ {
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MustBePartial, context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MustBePartial, typeSyntax.Identifier.GetLocation(), typeSymbol.Name));
typeSyntax.Identifier.GetLocation(), typeSymbol.Name));
continue; continue;
} }
if (IsNested(typeSyntax)) if (IsNested(typeSyntax))
{ {
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.NestedNotAllow, context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.NestedNotAllow, typeSyntax.Identifier.GetLocation(), typeSymbol.Name));
typeSyntax.Identifier.GetLocation(), typeSymbol.Name));
continue; continue;
} }
var fieldSymbols = new List<IFieldSymbol>(); var fieldSymbols = new List<IFieldSymbol>();
var fields = typeSyntax.Members var fields = typeSyntax.Members.OfType<FieldDeclarationSyntax>();
.OfType<FieldDeclarationSyntax>();
foreach (var field in fields) foreach (var field in fields)
{ {
var model = context.Compilation.GetSemanticModel(field.SyntaxTree); var model = context.Compilation.GetSemanticModel(field.SyntaxTree);
@ -53,26 +49,24 @@ namespace Alchemy.SourceGenerator
{ {
var fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol; var fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol;
var alchemySerializeAttribute = fieldSymbol.GetAttributes() var alchemySerializeAttribute = fieldSymbol.GetAttributes()
.FirstOrDefault(x => .FirstOrDefault(x => x.AttributeClass.Name
x.AttributeClass.Name is "AlchemySerializeField" is "AlchemySerializeField"
or "AlchemySerializeFieldAttribute" or "AlchemySerializeFieldAttribute"
or "Alchemy.Serialization.AlchemySerializeField" or "Alchemy.Serialization.AlchemySerializeField"
or "Alchemy.Serialization.AlchemySerializeFieldAttribute"); or "Alchemy.Serialization.AlchemySerializeFieldAttribute");
var nonSerializedAttribute = fieldSymbol.GetAttributes() var nonSerializedAttribute = fieldSymbol.GetAttributes()
.FirstOrDefault(x => .FirstOrDefault(x => x.AttributeClass.Name
x.AttributeClass.Name is "NonSerialized" is "NonSerialized"
or "NonSerializedAttribute" or "NonSerializedAttribute"
or "System.NonSerialized" or "System.NonSerialized"
or "System.NonSerializedAttribute"); or "System.NonSerializedAttribute");
if (alchemySerializeAttribute != null) if (alchemySerializeAttribute != null)
{ {
if (nonSerializedAttribute == null) if (nonSerializedAttribute == null)
{ {
context.ReportDiagnostic(Diagnostic.Create( context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.ShouldBeNonSerialized, variable.Identifier.GetLocation(), fieldSymbol.Name));
DiagnosticDescriptors.ShouldBeNonSerialized, variable.Identifier.GetLocation(),
fieldSymbol.Name));
} }
fieldSymbols.Add(fieldSymbol); fieldSymbols.Add(fieldSymbol);
@ -91,16 +85,14 @@ namespace Alchemy.SourceGenerator
} }
catch (Exception ex) catch (Exception ex)
{ {
var diagnosticDescriptor = new DiagnosticDescriptor("AlchemySerializeGeneratorError", var diagnosticDescriptor = new DiagnosticDescriptor("AlchemySerializeGeneratorError", "AlchemySerializeGeneratorError", $"Generation failed with:\n {ex}", "AlchemySerializeGeneratorError", DiagnosticSeverity.Error, true);
"AlchemySerializeGeneratorError", $"Generation failed with:\n {ex}", context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, Location.None, DiagnosticSeverity.Error));
"AlchemySerializeGeneratorError", DiagnosticSeverity.Error, true);
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, Location.None,
DiagnosticSeverity.Error));
} }
} }
static string ReplaceGenericsToCount( string typeName,int count)
static string ReplaceGenericsToCount(string typeName, int count)
{ {
if(count == 0) return typeName; if (count == 0) return typeName;
var builder = new StringBuilder(); var builder = new StringBuilder();
bool skip = false; bool skip = false;
foreach (var c in typeName) foreach (var c in typeName)
@ -119,6 +111,7 @@ namespace Alchemy.SourceGenerator
builder.Append(c); builder.Append(c);
} }
} }
return builder.ToString(); return builder.ToString();
} }
@ -148,19 +141,16 @@ namespace Alchemy.SourceGenerator
if (typeSymbol.IsGenericType) if (typeSymbol.IsGenericType)
{ {
genericsCount = typeSymbol.TypeParameters.Length; genericsCount = typeSymbol.TypeParameters.Length;
} }
var typeGenerics = typeSymbol.IsGenericType var typeGenerics = typeSymbol.IsGenericType
? "<" + string.Join(", ", typeSymbol.TypeParameters.Select(x => x.Name)) + ">" ? "<" + string.Join(", ", typeSymbol.TypeParameters.Select(x => x.Name)) + ">"
: ""; : "";
var alchemySerializationDataName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) var alchemySerializationDataName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).Replace("global::", "").Replace(".", "_");
.Replace("global::", "").Replace(".", "_"); alchemySerializationDataName = ReplaceGenericsToCount(alchemySerializationDataName, genericsCount) + "_alchemySerializationData";
alchemySerializationDataName = ReplaceGenericsToCount(alchemySerializationDataName,genericsCount) + "_alchemySerializationData";
var inheritedSerializationCallback = hasInheritedImplementation var inheritedSerializationCallback = hasInheritedImplementation ? "base.SerializationCallback_AlchemyImpl(isBeforeSerialize);" : string.Empty;
? "base.SerializationCallback_AlchemyImpl(isBeforeSerialize);"
: string.Empty;
var hasShowSerializationData = typeSymbol.GetAttributes().Any(x => x.AttributeClass.Name var hasShowSerializationData = typeSymbol.GetAttributes().Any(x => x.AttributeClass.Name
is "ShowAlchemySerializationData" is "ShowAlchemySerializationData"
@ -168,19 +158,14 @@ namespace Alchemy.SourceGenerator
or "Alchemy.Serialization.ShowAlchemySerializationData" or "Alchemy.Serialization.ShowAlchemySerializationData"
or "Alchemy.Serialization.ShowAlchemySerializationDataAttribute"); or "Alchemy.Serialization.ShowAlchemySerializationDataAttribute");
var serializationDataAttributesCode = hasShowSerializationData var serializationDataAttributesCode = hasShowSerializationData ? "[global::Alchemy.Inspector.ReadOnly, global::UnityEngine.TextArea(3, 999), global::UnityEngine.SerializeField]" : "[global::UnityEngine.HideInInspector, global::UnityEngine.SerializeField]";
? "[global::Alchemy.Inspector.ReadOnly, global::UnityEngine.TextArea(3, 999), global::UnityEngine.SerializeField]"
: "[global::UnityEngine.HideInInspector, global::UnityEngine.SerializeField]";
// target class namespace // target class namespace
var ns = typeSymbol.ContainingNamespace.IsGlobalNamespace var ns = typeSymbol.ContainingNamespace.IsGlobalNamespace ? string.Empty : $"namespace {typeSymbol.ContainingNamespace} {{";
? string.Empty
: $"namespace {typeSymbol.ContainingNamespace} {{";
foreach (var field in fieldSymbols) foreach (var field in fieldSymbols)
{ {
var serializeCode = var serializeCode = @$"try
@$"try
{{ {{
{alchemySerializationDataName}.{field.Name}.data = global::Alchemy.Serialization.Internal.SerializationHelper.ToJson(this.{field.Name} , {alchemySerializationDataName}.UnityObjectReferences); {alchemySerializationDataName}.{field.Name}.data = global::Alchemy.Serialization.Internal.SerializationHelper.ToJson(this.{field.Name} , {alchemySerializationDataName}.UnityObjectReferences);
{alchemySerializationDataName}.{field.Name}.isCreated = true; {alchemySerializationDataName}.{field.Name}.isCreated = true;
@ -190,8 +175,7 @@ catch (global::System.Exception ex)
global::UnityEngine.Debug.LogException(ex); global::UnityEngine.Debug.LogException(ex);
}}"; }}";
var deserializeCode = var deserializeCode = @$"try
@$"try
{{ {{
if ({alchemySerializationDataName}.{field.Name}.isCreated) if ({alchemySerializationDataName}.{field.Name}.isCreated)
{{ {{
@ -209,8 +193,7 @@ catch (global::System.Exception ex)
serializationDataCodeBuilder.Append("public Item ").Append(field.Name).Append(" = new();"); serializationDataCodeBuilder.Append("public Item ").Append(field.Name).Append(" = new();");
} }
return return @$"
@$"
// <auto-generated/> // <auto-generated/>
{ns} {ns}