Skip to content

Commit f192430

Browse files
committed
Fix source generator exceptions appearing when use "@+internal keyword" as type or namespace name in C# script
1 parent 7a0ab9d commit f192430

File tree

12 files changed

+57
-27
lines changed

12 files changed

+57
-27
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Xunit;
2+
3+
namespace Godot.SourceGenerators.Tests;
4+
5+
public class KeywordClassAndNamespaceTest
6+
{
7+
[Fact]
8+
public async void GenerateScriptMethodsTest()
9+
{
10+
await CSharpSourceGeneratorVerifier<ScriptMethodsGenerator>.Verify(
11+
"KeywordClassNameAndNamespace.cs",
12+
"namespace.class_ScriptMethods.generated.cs"
13+
);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Godot;
2+
using Godot.NativeInterop;
3+
4+
namespace @namespace {
5+
6+
partial class @class
7+
{
8+
#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword
9+
/// <summary>
10+
/// Cached StringNames for the methods contained in this class, for fast lookup.
11+
/// </summary>
12+
public new class MethodName : global::Godot.GodotObject.MethodName {
13+
}
14+
#pragma warning restore CS0109
15+
}
16+
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Godot;
2+
3+
namespace @namespace
4+
{
5+
partial class @class : GodotObject
6+
{
7+
}
8+
}

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,6 @@ public static string GetAccessibilityKeyword(this INamedTypeSymbol namedTypeSymb
181181
};
182182
}
183183

184-
public static string NameWithTypeParameters(this INamedTypeSymbol symbol)
185-
{
186-
return symbol.IsGenericType && symbol.TypeParameters.Length > 0 ?
187-
string.Concat(symbol.Name, "<", string.Join(", ", symbol.TypeParameters), ">") :
188-
symbol.Name;
189-
}
190-
191184
private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
192185
SymbolDisplayFormat.FullyQualifiedFormat
193186
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
@@ -268,6 +261,8 @@ static void ParenEnclosedFullQualifiedSyntax(SyntaxNode node, SemanticModel sm,
268261

269262
public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
270263
=> qualifiedName
264+
// AddSource() doesn't support @ prefix
265+
.Replace("@", "")
271266
// AddSource() doesn't support angle brackets
272267
.Replace("<", "(Of ")
273268
.Replace(">", ")");

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptMethodsGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
114114
source.Append("partial ");
115115
source.Append(containingType.GetDeclarationKeyword());
116116
source.Append(" ");
117-
source.Append(containingType.NameWithTypeParameters());
117+
source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
118118
source.Append("\n{\n");
119119
}
120120
}
121121

122122
source.Append("partial class ");
123-
source.Append(symbol.NameWithTypeParameters());
123+
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
124124
source.Append("\n{\n");
125125

126126
var members = symbol.GetMembers();

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ IEnumerable<ClassDeclarationSyntax> classDeclarations
138138

139139
source.Append(attributes);
140140
source.Append("\npartial class ");
141-
source.Append(symbol.NameWithTypeParameters());
141+
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
142142
source.Append("\n{\n}\n");
143143

144144
if (hasNamespace)

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
103103
source.Append("partial ");
104104
source.Append(containingType.GetDeclarationKeyword());
105105
source.Append(" ");
106-
source.Append(containingType.NameWithTypeParameters());
106+
source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
107107
source.Append("\n{\n");
108108
}
109109
}
110110

111111
source.Append("partial class ");
112-
source.Append(symbol.NameWithTypeParameters());
112+
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
113113
source.Append("\n{\n");
114114

115115
var members = symbol.GetMembers();

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
100100
source.Append("partial ");
101101
source.Append(containingType.GetDeclarationKeyword());
102102
source.Append(" ");
103-
source.Append(containingType.NameWithTypeParameters());
103+
source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
104104
source.Append("\n{\n");
105105
}
106106
}
107107

108108
source.Append("partial class ");
109-
source.Append(symbol.NameWithTypeParameters());
109+
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
110110
source.Append("\n{\n");
111111

112112
var exportedMembers = new List<ExportedPropertyMetadata>();

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSerializationGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
101101
source.Append("partial ");
102102
source.Append(containingType.GetDeclarationKeyword());
103103
source.Append(" ");
104-
source.Append(containingType.NameWithTypeParameters());
104+
source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
105105
source.Append("\n{\n");
106106
}
107107
}
108108

109109
source.Append("partial class ");
110-
source.Append(symbol.NameWithTypeParameters());
110+
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
111111
source.Append("\n{\n");
112112

113113
var members = symbol.GetMembers();

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
103103
source.Append("partial ");
104104
source.Append(containingType.GetDeclarationKeyword());
105105
source.Append(" ");
106-
source.Append(containingType.NameWithTypeParameters());
106+
source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
107107
source.Append("\n{\n");
108108
}
109109
}
110110

111111
source.Append("partial class ");
112-
source.Append(symbol.NameWithTypeParameters());
112+
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
113113
source.Append("\n{\n");
114114

115115
var members = symbol.GetMembers();

0 commit comments

Comments
 (0)