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
This commit is contained in:
Thaddeus Crews 2025-08-24 11:04:31 -05:00
commit 37a48c89f9
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
12 changed files with 57 additions and 27 deletions

View file

@ -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"
);
}
}

View file

@ -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
}
}

View file

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

View file

@ -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(">", ")");

View file

@ -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();

View file

@ -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)

View file

@ -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();

View file

@ -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>();

View file

@ -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();

View file

@ -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();

View file

@ -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(">", ")");

View file

@ -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");
}
}