From 948b3750659501b36b016e3c5b0d734c0a0749df Mon Sep 17 00:00:00 2001 From: badcel <1218031+badcel@users.noreply.github.com> Date: Wed, 12 Feb 2025 17:59:32 +0100 Subject: [PATCH] GObject-2.0.Integration: Support global namespace --- .../SourceGenerator/Subclass/SubclassCode.cs | 11 ++++++----- .../SourceGenerator/Subclass/SubclassData.cs | 1 + .../Subclass/SubclassValuesProvider.cs | 1 + .../SubclassIntegrationTest.GlobalNamespace.cs | 2 ++ .../Libs/GirTest-0.1.Tests/SubclassIntegrationTest.cs | 8 ++++++++ 5 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.GlobalNamespace.cs diff --git a/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassCode.cs b/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassCode.cs index a438de630..e06d6d48d 100644 --- a/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassCode.cs +++ b/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassCode.cs @@ -15,11 +15,12 @@ public static void Generate(SourceProductionContext context, SubclassData subcla } private static string ToCode(SubclassData subclassData) { - return $$""" - namespace {{subclassData.Namespace}}; - - {{RenderClassHierarchy(subclassData)}} - """; + return subclassData.IsGlobalNamespace + ? RenderClassHierarchy(subclassData) + : $""" + namespace {subclassData.Namespace}; + {RenderClassHierarchy(subclassData)} + """; } private static string RenderClassHierarchy(SubclassData subclassData) diff --git a/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassData.cs b/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassData.cs index f142c051e..b14f826b2 100644 --- a/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassData.cs +++ b/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassData.cs @@ -8,6 +8,7 @@ internal sealed record SubclassData( string Parent, string ParentHandle, string Namespace, + bool IsGlobalNamespace, string Accessibility, string FileName, Stack UpperNestedClasses diff --git a/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassValuesProvider.cs b/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassValuesProvider.cs index 8fb6d24e7..782a3d41c 100644 --- a/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassValuesProvider.cs +++ b/src/Extensions/GObject-2.0.Integration/SourceGenerator/Subclass/SubclassValuesProvider.cs @@ -49,6 +49,7 @@ public static IncrementalValuesProvider GetSubclassValuesProvider( Parent: parentType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), ParentHandle: parentHandle, Namespace: context.TargetSymbol.ContainingNamespace.ToDisplayString(), + IsGlobalNamespace: context.TargetSymbol.ContainingNamespace.IsGlobalNamespace, Accessibility: accessibility, FileName: GetFileName(subclass), UpperNestedClasses: upperNestedClasses diff --git a/src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.GlobalNamespace.cs b/src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.GlobalNamespace.cs new file mode 100644 index 000000000..540bc2073 --- /dev/null +++ b/src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.GlobalNamespace.cs @@ -0,0 +1,2 @@ +[GObject.Subclass] +internal partial class SomeGlobalSubClass; diff --git a/src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.cs b/src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.cs index 9ca26f540..b23cb1fdf 100644 --- a/src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.cs +++ b/src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.cs @@ -46,26 +46,34 @@ public void GenericSubclassesShouldBePossible() var type4 = SomeGenericSubclass>.GetGType(); var type5 = SomeSubSubClass.GetGType(); var type6 = SomeContainingClass.SomeNestedGenericSubSubSubClass.GetGType(); + var type7 = SomeGlobalSubClass.GetGType(); type1.Should().NotBe(type2); type1.Should().NotBe(type3); type1.Should().NotBe(type4); type1.Should().NotBe(type5); type1.Should().NotBe(type6); + type1.Should().NotBe(type7); type2.Should().NotBe(type3); type2.Should().NotBe(type4); type2.Should().NotBe(type5); type2.Should().NotBe(type6); + type2.Should().NotBe(type7); type3.Should().NotBe(type4); type3.Should().NotBe(type5); type3.Should().NotBe(type6); + type3.Should().NotBe(type7); type4.Should().NotBe(type5); type4.Should().NotBe(type6); + type4.Should().NotBe(type7); type5.Should().NotBe(type6); + type5.Should().NotBe(type7); + + type6.Should().NotBe(type7); } }