C#: Generate signal event with the same accessibility as the delegate

This commit is contained in:
Raul Santos 2022-11-03 20:10:11 +01:00
parent 2d6af000e7
commit 445e822bcf
No known key found for this signature in database
GPG Key ID: B532473AE3A803E4
2 changed files with 27 additions and 1 deletions

View File

@ -155,6 +155,32 @@ namespace Godot.SourceGenerators
}; };
} }
public static string GetAccessibilityKeyword(this INamedTypeSymbol namedTypeSymbol)
{
if (namedTypeSymbol.DeclaredAccessibility == Accessibility.NotApplicable)
{
// Accessibility not specified. Get the default accessibility.
return namedTypeSymbol.ContainingSymbol switch
{
null or INamespaceSymbol => "internal",
ITypeSymbol { TypeKind: TypeKind.Class or TypeKind.Struct } => "private",
ITypeSymbol { TypeKind: TypeKind.Interface } => "public",
_ => "",
};
}
return namedTypeSymbol.DeclaredAccessibility switch
{
Accessibility.Private => "private",
Accessibility.Protected => "protected",
Accessibility.Internal => "internal",
Accessibility.ProtectedAndInternal => "private",
Accessibility.ProtectedOrInternal => "private",
Accessibility.Public => "public",
_ => "",
};
}
public static string NameWithTypeParameters(this INamedTypeSymbol symbol) public static string NameWithTypeParameters(this INamedTypeSymbol symbol)
{ {
return symbol.IsGenericType ? return symbol.IsGenericType ?

View File

@ -276,7 +276,7 @@ namespace Godot.SourceGenerators
source.Append( source.Append(
$" /// <inheritdoc cref=\"{signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()}\"/>\n"); $" /// <inheritdoc cref=\"{signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()}\"/>\n");
source.Append(" public event ") source.Append($" {signalDelegate.DelegateSymbol.GetAccessibilityKeyword()} event ")
.Append(signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()) .Append(signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal())
.Append(" @") .Append(" @")
.Append(signalName) .Append(signalName)