Jelajahi Sumber

Merge pull request #106744 from L2750558108/fix-@-error-in-c#

Fix source generator exceptions appearing when use "@+internal keyword" as type or namespace name in C# script
Thaddeus Crews 3 minggu lalu
induk
melakukan
37a48c89f9

+ 15 - 0
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/KeywordClassNameAndNamespaceTest.cs

@@ -0,0 +1,15 @@
+using Xunit;
+
+namespace Godot.SourceGenerators.Tests;
+
+public class KeywordClassAndNamespaceTest
+{
+    [Fact]
+    public async void GenerateScriptMethodsTest()
+    {
+        await CSharpSourceGeneratorVerifier<ScriptMethodsGenerator>.Verify(
+            "KeywordClassNameAndNamespace.cs",
+            "namespace.class_ScriptMethods.generated.cs"
+        );
+    }
+}

+ 17 - 0
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/namespace.class_ScriptMethods.generated.cs

@@ -0,0 +1,17 @@
+using Godot;
+using Godot.NativeInterop;
+
+namespace @namespace {
+
+partial class @class
+{
+#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword
+    /// <summary>
+    /// Cached StringNames for the methods contained in this class, for fast lookup.
+    /// </summary>
+    public new class MethodName : global::Godot.GodotObject.MethodName {
+    }
+#pragma warning restore CS0109
+}
+
+}

+ 8 - 0
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/KeywordClassNameAndNamespace.cs

@@ -0,0 +1,8 @@
+using Godot;
+
+namespace @namespace
+{
+    partial class @class : GodotObject
+    {
+    }
+}

+ 2 - 7
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs

@@ -181,13 +181,6 @@ namespace Godot.SourceGenerators
             };
         }
 
-        public static string NameWithTypeParameters(this INamedTypeSymbol symbol)
-        {
-            return symbol.IsGenericType && symbol.TypeParameters.Length > 0 ?
-                string.Concat(symbol.Name, "<", string.Join(", ", symbol.TypeParameters), ">") :
-                symbol.Name;
-        }
-
         private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
             SymbolDisplayFormat.FullyQualifiedFormat
                 .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
@@ -268,6 +261,8 @@ namespace Godot.SourceGenerators
 
         public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
             => qualifiedName
+                // AddSource() doesn't support @ prefix
+                .Replace("@", "")
                 // AddSource() doesn't support angle brackets
                 .Replace("<", "(Of ")
                 .Replace(">", ")");

+ 2 - 2
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptMethodsGenerator.cs

@@ -114,13 +114,13 @@ namespace Godot.SourceGenerators
                     source.Append("partial ");
                     source.Append(containingType.GetDeclarationKeyword());
                     source.Append(" ");
-                    source.Append(containingType.NameWithTypeParameters());
+                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                     source.Append("\n{\n");
                 }
             }
 
             source.Append("partial class ");
-            source.Append(symbol.NameWithTypeParameters());
+            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
             source.Append("\n{\n");
 
             var members = symbol.GetMembers();

+ 1 - 1
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs

@@ -138,7 +138,7 @@ namespace Godot.SourceGenerators
 
             source.Append(attributes);
             source.Append("\npartial class ");
-            source.Append(symbol.NameWithTypeParameters());
+            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
             source.Append("\n{\n}\n");
 
             if (hasNamespace)

+ 2 - 2
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs

@@ -103,13 +103,13 @@ namespace Godot.SourceGenerators
                     source.Append("partial ");
                     source.Append(containingType.GetDeclarationKeyword());
                     source.Append(" ");
-                    source.Append(containingType.NameWithTypeParameters());
+                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                     source.Append("\n{\n");
                 }
             }
 
             source.Append("partial class ");
-            source.Append(symbol.NameWithTypeParameters());
+            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
             source.Append("\n{\n");
 
             var members = symbol.GetMembers();

+ 2 - 2
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs

@@ -100,13 +100,13 @@ namespace Godot.SourceGenerators
                     source.Append("partial ");
                     source.Append(containingType.GetDeclarationKeyword());
                     source.Append(" ");
-                    source.Append(containingType.NameWithTypeParameters());
+                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                     source.Append("\n{\n");
                 }
             }
 
             source.Append("partial class ");
-            source.Append(symbol.NameWithTypeParameters());
+            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
             source.Append("\n{\n");
 
             var exportedMembers = new List<ExportedPropertyMetadata>();

+ 2 - 2
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSerializationGenerator.cs

@@ -101,13 +101,13 @@ namespace Godot.SourceGenerators
                     source.Append("partial ");
                     source.Append(containingType.GetDeclarationKeyword());
                     source.Append(" ");
-                    source.Append(containingType.NameWithTypeParameters());
+                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                     source.Append("\n{\n");
                 }
             }
 
             source.Append("partial class ");
-            source.Append(symbol.NameWithTypeParameters());
+            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
             source.Append("\n{\n");
 
             var members = symbol.GetMembers();

+ 2 - 2
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs

@@ -103,13 +103,13 @@ namespace Godot.SourceGenerators
                     source.Append("partial ");
                     source.Append(containingType.GetDeclarationKeyword());
                     source.Append(" ");
-                    source.Append(containingType.NameWithTypeParameters());
+                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                     source.Append("\n{\n");
                 }
             }
 
             source.Append("partial class ");
-            source.Append(symbol.NameWithTypeParameters());
+            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
             source.Append("\n{\n");
 
             var members = symbol.GetMembers();

+ 2 - 7
modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/ExtensionMethods.cs

@@ -94,13 +94,6 @@ internal static class ExtensionMethods
         };
     }
 
-    public static string NameWithTypeParameters(this INamedTypeSymbol symbol)
-    {
-        return symbol.IsGenericType && symbol.TypeParameters.Length > 0 ?
-            string.Concat(symbol.Name, "<", string.Join(", ", symbol.TypeParameters), ">") :
-            symbol.Name;
-    }
-
     private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
         SymbolDisplayFormat.FullyQualifiedFormat
             .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
@@ -123,6 +116,8 @@ internal static class ExtensionMethods
 
     public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
         => qualifiedName
+            // AddSource() doesn't support @ prefix
+            .Replace("@", "")
             // AddSource() doesn't support angle brackets
             .Replace("<", "(Of ")
             .Replace(">", ")");

+ 2 - 2
modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/UnmanagedCallbacksGenerator.cs

@@ -140,7 +140,7 @@ using Godot.NativeInterop;
                 source.Append("partial ");
                 source.Append(containingType.GetDeclarationKeyword());
                 source.Append(" ");
-                source.Append(containingType.NameWithTypeParameters());
+                source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                 source.Append("\n{\n");
             }
         }
@@ -319,7 +319,7 @@ using Godot.NativeInterop;
                 source.Append("partial ");
                 source.Append(containingType.GetDeclarationKeyword());
                 source.Append(" ");
-                source.Append(containingType.NameWithTypeParameters());
+                source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                 source.Append("\n{\n");
             }
         }