From ee8e5146a4ea287963fca3d1e50e62945da2aea8 Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Sat, 13 Mar 2021 01:04:55 +0100 Subject: [PATCH] C#: Fix ScriptPathAttribute generator with none or nested namespaces The following two bugs were fixed: - For classes without namespace we were still generating `namespace {` without a namespace identifier, causing a syntax error. - For classes with nested namespaces we were generating only the innermost part of the namespace was being generated, e.g.: for `Foo.Bar` we were generating `namespace Bar {` instead of `namespace Foo.Bar {`. This wasn't causing any build error, but because of the wrong namespace Godot wasn't able to find the class associated with the script. --- modules/mono/SdkPackageVersions.props | 4 +- .../Godot.NET.Sdk/Godot.NET.Sdk.csproj | 9 +-- .../ExtensionMethods.cs | 3 + .../ScriptPathAttributeGenerator.cs | 62 +++++++++++++------ 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/modules/mono/SdkPackageVersions.props b/modules/mono/SdkPackageVersions.props index 396443f30e..df3ebe581c 100644 --- a/modules/mono/SdkPackageVersions.props +++ b/modules/mono/SdkPackageVersions.props @@ -1,6 +1,6 @@ - 4.0.0-dev4 - 4.0.0-dev1 + 4.0.0-dev5 + 4.0.0-dev2 diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Godot.NET.Sdk.csproj b/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Godot.NET.Sdk.csproj index ef8add0ba8..4e9e7184da 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Godot.NET.Sdk.csproj +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Godot.NET.Sdk.csproj @@ -23,11 +23,12 @@ - - + + - - + + Sdk\SdkPackageVersions.props + diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs index c3e74822d5..e16f72f43a 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs @@ -82,5 +82,8 @@ namespace Godot.SourceGenerators public static string FullQualifiedName(this INamedTypeSymbol symbol) => symbol.ToDisplayString(NullableFlowState.NotNull, FullyQualifiedFormatOmitGlobal); + + public static string FullQualifiedName(this INamespaceSymbol namespaceSymbol) + => namespaceSymbol.ToDisplayString(FullyQualifiedFormatOmitGlobal); } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs index 150e59e414..a51728e221 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs @@ -69,7 +69,7 @@ namespace Godot.SourceGenerators IEnumerable classDeclarations ) { - var attributesBuilder = new StringBuilder(); + var attributes = new StringBuilder(); // Remember syntax trees for which we already added an attribute, to prevent unnecessary duplicates. var attributedTrees = new List(); @@ -81,28 +81,54 @@ namespace Godot.SourceGenerators attributedTrees.Add(cds.SyntaxTree); - if (attributesBuilder.Length != 0) - attributesBuilder.Append("\n "); + if (attributes.Length != 0) + attributes.Append("\n"); - attributesBuilder.Append(@"[ScriptPathAttribute(""res://"); - attributesBuilder.Append(RelativeToDir(cds.SyntaxTree.FilePath, godotProjectDir)); - attributesBuilder.Append(@""")]"); + attributes.Append(@"[ScriptPathAttribute(""res://"); + attributes.Append(RelativeToDir(cds.SyntaxTree.FilePath, godotProjectDir)); + attributes.Append(@""")]"); } - string classNs = symbol.ContainingNamespace.Name; string className = symbol.Name; - var source = $@"using Godot; -namespace {classNs} -{{ - {attributesBuilder} - partial class {className} - {{ - }} -}} -"; - context.AddSource(classNs + "." + className + "_ScriptPath_Generated", - SourceText.From(source, Encoding.UTF8)); + INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace; + string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ? + namespaceSymbol.FullQualifiedName() : + string.Empty; + bool hasNamespace = classNs.Length != 0; + + string uniqueName = hasNamespace ? + classNs + "." + className + "_ScriptPath_Generated" : + className + "_ScriptPath_Generated"; + + var source = new StringBuilder(); + + // using Godot; + // namespace {classNs} { + // {attributesBuilder} + // partial class {className} { } + // } + + source.Append("using Godot;\n"); + + if (hasNamespace) + { + source.Append("namespace "); + source.Append(classNs); + source.Append(" {\n\n"); + } + + source.Append(attributes); + source.Append("\n partial class "); + source.Append(className); + source.Append("\n{\n}\n"); + + if (hasNamespace) + { + source.Append("\n}\n"); + } + + context.AddSource(uniqueName, SourceText.From(source.ToString(), Encoding.UTF8)); } private static void AddScriptTypesAssemblyAttr(GeneratorExecutionContext context,