From 445e822bcf2dc2cbc4391ce3df18803bd26c0f79 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Thu, 3 Nov 2022 20:10:11 +0100 Subject: [PATCH] C#: Generate signal event with the same accessibility as the delegate --- .../ExtensionMethods.cs | 26 +++++++++++++++++++ .../ScriptSignalsGenerator.cs | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs index 957d5789df8..62fa7b0a367 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs @@ -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) { return symbol.IsGenericType ? diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs index 54f2212339c..23253524ab7 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs @@ -276,7 +276,7 @@ namespace Godot.SourceGenerators source.Append( $" /// \n"); - source.Append(" public event ") + source.Append($" {signalDelegate.DelegateSymbol.GetAccessibilityKeyword()} event ") .Append(signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()) .Append(" @") .Append(signalName)