1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-15 13:51:40 +00:00

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