1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-06 17:25:19 +00:00

Merge pull request #55597 from raulsntos/csharp-source-generators-generics

Fix C# `Godot.SourceGenerators` for generic classes
This commit is contained in:
Ignacio Roldán Etcheverry
2021-12-06 00:04:36 +01:00
committed by GitHub
4 changed files with 34 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<PackageVersion_Godot_NET_Sdk>4.0.0-dev5</PackageVersion_Godot_NET_Sdk> <PackageFloatingVersion_Godot>4.0.*-*</PackageFloatingVersion_Godot>
<PackageVersion_Godot_SourceGenerators>4.0.0-dev2</PackageVersion_Godot_SourceGenerators> <PackageVersion_Godot_NET_Sdk>4.0.0-dev6</PackageVersion_Godot_NET_Sdk>
<PackageVersion_Godot_SourceGenerators>4.0.0-dev3</PackageVersion_Godot_SourceGenerators>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -17,6 +17,6 @@
<!-- C# source generators --> <!-- C# source generators -->
<ItemGroup Condition=" '$(DisableImplicitGodotGeneratorReferences)' != 'true' "> <ItemGroup Condition=" '$(DisableImplicitGodotGeneratorReferences)' != 'true' ">
<PackageReference Include="Godot.SourceGenerators" Version="$(PackageVersion_Godot_SourceGenerators)" /> <PackageReference Include="Godot.SourceGenerators" Version="$(PackageFloatingVersion_Godot)" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,16 @@
namespace Godot.SourceGenerators.Sample
{
partial class Generic<T> : Godot.Object
{
}
// Generic again but different generic parameters
partial class Generic<T, R> : Godot.Object
{
}
// Generic again but without generic parameters
partial class Generic : Godot.Object
{
}
}

View File

@@ -97,9 +97,13 @@ namespace Godot.SourceGenerators
string.Empty; string.Empty;
bool hasNamespace = classNs.Length != 0; bool hasNamespace = classNs.Length != 0;
string uniqueName = hasNamespace ? var uniqueName = new StringBuilder();
classNs + "." + className + "_ScriptPath_Generated" : if (hasNamespace)
className + "_ScriptPath_Generated"; uniqueName.Append($"{classNs}.");
uniqueName.Append(className);
if (symbol.IsGenericType)
uniqueName.Append($"Of{string.Join(string.Empty, symbol.TypeParameters)}");
uniqueName.Append("_ScriptPath_Generated");
var source = new StringBuilder(); var source = new StringBuilder();
@@ -121,6 +125,8 @@ namespace Godot.SourceGenerators
source.Append(attributes); source.Append(attributes);
source.Append("\n partial class "); source.Append("\n partial class ");
source.Append(className); source.Append(className);
if (symbol.IsGenericType)
source.Append($"<{string.Join(", ", symbol.TypeParameters)}>");
source.Append("\n{\n}\n"); source.Append("\n{\n}\n");
if (hasNamespace) if (hasNamespace)
@@ -128,7 +134,7 @@ namespace Godot.SourceGenerators
source.Append("\n}\n"); source.Append("\n}\n");
} }
context.AddSource(uniqueName, SourceText.From(source.ToString(), Encoding.UTF8)); context.AddSource(uniqueName.ToString(), SourceText.From(source.ToString(), Encoding.UTF8));
} }
private static void AddScriptTypesAssemblyAttr(GeneratorExecutionContext context, private static void AddScriptTypesAssemblyAttr(GeneratorExecutionContext context,
@@ -145,12 +151,15 @@ namespace Godot.SourceGenerators
foreach (var godotClass in godotClasses) foreach (var godotClass in godotClasses)
{ {
var qualifiedName = godotClass.Key.ToDisplayString( var qualifiedName = godotClass.Key.ToDisplayString(
NullableFlowState.NotNull, SymbolDisplayFormat.FullyQualifiedFormat); NullableFlowState.NotNull, SymbolDisplayFormat.FullyQualifiedFormat
.WithGenericsOptions(SymbolDisplayGenericsOptions.None));
if (!first) if (!first)
sourceBuilder.Append(", "); sourceBuilder.Append(", ");
first = false; first = false;
sourceBuilder.Append("typeof("); sourceBuilder.Append("typeof(");
sourceBuilder.Append(qualifiedName); sourceBuilder.Append(qualifiedName);
if (godotClass.Key.IsGenericType)
sourceBuilder.Append($"<{new string(',', godotClass.Key.TypeParameters.Count() - 1)}>");
sourceBuilder.Append(")"); sourceBuilder.Append(")");
} }