mirror of
				https://github.com/godotengine/godot.git
				synced 2025-11-03 23:21:15 +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:
		
						commit
						37a48c89f9
					
				
					 12 changed files with 57 additions and 27 deletions
				
			
		| 
						 | 
					@ -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"
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,8 @@
 | 
				
			||||||
 | 
					using Godot;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace @namespace
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    partial class @class : GodotObject
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -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; } =
 | 
					        private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
 | 
				
			||||||
            SymbolDisplayFormat.FullyQualifiedFormat
 | 
					            SymbolDisplayFormat.FullyQualifiedFormat
 | 
				
			||||||
                .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
 | 
					                .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
 | 
				
			||||||
| 
						 | 
					@ -268,6 +261,8 @@ namespace Godot.SourceGenerators
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
 | 
					        public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
 | 
				
			||||||
            => qualifiedName
 | 
					            => qualifiedName
 | 
				
			||||||
 | 
					                // AddSource() doesn't support @ prefix
 | 
				
			||||||
 | 
					                .Replace("@", "")
 | 
				
			||||||
                // AddSource() doesn't support angle brackets
 | 
					                // AddSource() doesn't support angle brackets
 | 
				
			||||||
                .Replace("<", "(Of ")
 | 
					                .Replace("<", "(Of ")
 | 
				
			||||||
                .Replace(">", ")");
 | 
					                .Replace(">", ")");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -114,13 +114,13 @@ namespace Godot.SourceGenerators
 | 
				
			||||||
                    source.Append("partial ");
 | 
					                    source.Append("partial ");
 | 
				
			||||||
                    source.Append(containingType.GetDeclarationKeyword());
 | 
					                    source.Append(containingType.GetDeclarationKeyword());
 | 
				
			||||||
                    source.Append(" ");
 | 
					                    source.Append(" ");
 | 
				
			||||||
                    source.Append(containingType.NameWithTypeParameters());
 | 
					                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
                    source.Append("\n{\n");
 | 
					                    source.Append("\n{\n");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            source.Append("partial class ");
 | 
					            source.Append("partial class ");
 | 
				
			||||||
            source.Append(symbol.NameWithTypeParameters());
 | 
					            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
            source.Append("\n{\n");
 | 
					            source.Append("\n{\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var members = symbol.GetMembers();
 | 
					            var members = symbol.GetMembers();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -138,7 +138,7 @@ namespace Godot.SourceGenerators
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            source.Append(attributes);
 | 
					            source.Append(attributes);
 | 
				
			||||||
            source.Append("\npartial class ");
 | 
					            source.Append("\npartial class ");
 | 
				
			||||||
            source.Append(symbol.NameWithTypeParameters());
 | 
					            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
            source.Append("\n{\n}\n");
 | 
					            source.Append("\n{\n}\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (hasNamespace)
 | 
					            if (hasNamespace)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -103,13 +103,13 @@ namespace Godot.SourceGenerators
 | 
				
			||||||
                    source.Append("partial ");
 | 
					                    source.Append("partial ");
 | 
				
			||||||
                    source.Append(containingType.GetDeclarationKeyword());
 | 
					                    source.Append(containingType.GetDeclarationKeyword());
 | 
				
			||||||
                    source.Append(" ");
 | 
					                    source.Append(" ");
 | 
				
			||||||
                    source.Append(containingType.NameWithTypeParameters());
 | 
					                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
                    source.Append("\n{\n");
 | 
					                    source.Append("\n{\n");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            source.Append("partial class ");
 | 
					            source.Append("partial class ");
 | 
				
			||||||
            source.Append(symbol.NameWithTypeParameters());
 | 
					            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
            source.Append("\n{\n");
 | 
					            source.Append("\n{\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var members = symbol.GetMembers();
 | 
					            var members = symbol.GetMembers();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -100,13 +100,13 @@ namespace Godot.SourceGenerators
 | 
				
			||||||
                    source.Append("partial ");
 | 
					                    source.Append("partial ");
 | 
				
			||||||
                    source.Append(containingType.GetDeclarationKeyword());
 | 
					                    source.Append(containingType.GetDeclarationKeyword());
 | 
				
			||||||
                    source.Append(" ");
 | 
					                    source.Append(" ");
 | 
				
			||||||
                    source.Append(containingType.NameWithTypeParameters());
 | 
					                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
                    source.Append("\n{\n");
 | 
					                    source.Append("\n{\n");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            source.Append("partial class ");
 | 
					            source.Append("partial class ");
 | 
				
			||||||
            source.Append(symbol.NameWithTypeParameters());
 | 
					            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
            source.Append("\n{\n");
 | 
					            source.Append("\n{\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var exportedMembers = new List<ExportedPropertyMetadata>();
 | 
					            var exportedMembers = new List<ExportedPropertyMetadata>();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -101,13 +101,13 @@ namespace Godot.SourceGenerators
 | 
				
			||||||
                    source.Append("partial ");
 | 
					                    source.Append("partial ");
 | 
				
			||||||
                    source.Append(containingType.GetDeclarationKeyword());
 | 
					                    source.Append(containingType.GetDeclarationKeyword());
 | 
				
			||||||
                    source.Append(" ");
 | 
					                    source.Append(" ");
 | 
				
			||||||
                    source.Append(containingType.NameWithTypeParameters());
 | 
					                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
                    source.Append("\n{\n");
 | 
					                    source.Append("\n{\n");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            source.Append("partial class ");
 | 
					            source.Append("partial class ");
 | 
				
			||||||
            source.Append(symbol.NameWithTypeParameters());
 | 
					            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
            source.Append("\n{\n");
 | 
					            source.Append("\n{\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var members = symbol.GetMembers();
 | 
					            var members = symbol.GetMembers();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -103,13 +103,13 @@ namespace Godot.SourceGenerators
 | 
				
			||||||
                    source.Append("partial ");
 | 
					                    source.Append("partial ");
 | 
				
			||||||
                    source.Append(containingType.GetDeclarationKeyword());
 | 
					                    source.Append(containingType.GetDeclarationKeyword());
 | 
				
			||||||
                    source.Append(" ");
 | 
					                    source.Append(" ");
 | 
				
			||||||
                    source.Append(containingType.NameWithTypeParameters());
 | 
					                    source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
                    source.Append("\n{\n");
 | 
					                    source.Append("\n{\n");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            source.Append("partial class ");
 | 
					            source.Append("partial class ");
 | 
				
			||||||
            source.Append(symbol.NameWithTypeParameters());
 | 
					            source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
            source.Append("\n{\n");
 | 
					            source.Append("\n{\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var members = symbol.GetMembers();
 | 
					            var members = symbol.GetMembers();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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; } =
 | 
					    private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
 | 
				
			||||||
        SymbolDisplayFormat.FullyQualifiedFormat
 | 
					        SymbolDisplayFormat.FullyQualifiedFormat
 | 
				
			||||||
            .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
 | 
					            .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
 | 
				
			||||||
| 
						 | 
					@ -123,6 +116,8 @@ internal static class ExtensionMethods
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
 | 
					    public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
 | 
				
			||||||
        => qualifiedName
 | 
					        => qualifiedName
 | 
				
			||||||
 | 
					            // AddSource() doesn't support @ prefix
 | 
				
			||||||
 | 
					            .Replace("@", "")
 | 
				
			||||||
            // AddSource() doesn't support angle brackets
 | 
					            // AddSource() doesn't support angle brackets
 | 
				
			||||||
            .Replace("<", "(Of ")
 | 
					            .Replace("<", "(Of ")
 | 
				
			||||||
            .Replace(">", ")");
 | 
					            .Replace(">", ")");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -140,7 +140,7 @@ using Godot.NativeInterop;
 | 
				
			||||||
                source.Append("partial ");
 | 
					                source.Append("partial ");
 | 
				
			||||||
                source.Append(containingType.GetDeclarationKeyword());
 | 
					                source.Append(containingType.GetDeclarationKeyword());
 | 
				
			||||||
                source.Append(" ");
 | 
					                source.Append(" ");
 | 
				
			||||||
                source.Append(containingType.NameWithTypeParameters());
 | 
					                source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
                source.Append("\n{\n");
 | 
					                source.Append("\n{\n");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -319,7 +319,7 @@ using Godot.NativeInterop;
 | 
				
			||||||
                source.Append("partial ");
 | 
					                source.Append("partial ");
 | 
				
			||||||
                source.Append(containingType.GetDeclarationKeyword());
 | 
					                source.Append(containingType.GetDeclarationKeyword());
 | 
				
			||||||
                source.Append(" ");
 | 
					                source.Append(" ");
 | 
				
			||||||
                source.Append(containingType.NameWithTypeParameters());
 | 
					                source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
 | 
				
			||||||
                source.Append("\n{\n");
 | 
					                source.Append("\n{\n");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue