-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
229 additions
and
10 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/Extensions/GObject-2.0.Integration/GObject-2.0.Integration.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>GirCore.GObject-2.0.Integration</PackageId> | ||
<RootNamespace>GObject.Integration</RootNamespace> | ||
<Description>Source Generators to make it easy to integrate C# with the GObject type system.</Description> | ||
|
||
<IncludeBuildOutput>false</IncludeBuildOutput> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.12.0" /> | ||
</ItemGroup> | ||
|
||
<!--TODO VERIFY THIS --> | ||
<!-- This ensures the library will be packaged as a source generator when we use `dotnet pack` --> | ||
<ItemGroup> | ||
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" | ||
PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
</ItemGroup> | ||
</Project> |
12 changes: 12 additions & 0 deletions
12
src/Extensions/GObject-2.0.Integration/Integration/Generator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace GObject.Integration; | ||
|
||
[Generator] | ||
public class Generator : IIncrementalGenerator | ||
{ | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
context.EnableSubclassSupport(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Extensions/GObject-2.0.Integration/Integration/Subclass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace GObject.Integration; | ||
|
||
public static class Subclass | ||
{ | ||
public static void EnableSubclassSupport(this IncrementalGeneratorInitializationContext context) | ||
{ | ||
//https://andrewlock.net/creating-a-source-generator-part-1-creating-an-incremental-source-generator/ | ||
|
||
context.RegisterPostInitializationOutput(ctx => ctx.AddSource( | ||
hintName: SubclassAttribute.FileName, | ||
source: SubclassAttribute.Code | ||
)); | ||
|
||
context.RegisterImplementationSourceOutput( | ||
source: context.GetSubclassValuesProvider(), | ||
action: SubclassCode.Generate | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Extensions/GObject-2.0.Integration/Integration/Subclass/SubclassAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace GObject.Integration; | ||
|
||
public static class SubclassAttribute | ||
{ | ||
public const string FullyQualifiedName = "GObject.SubclassAttribute"; | ||
|
||
public const string FileName = "SubclassAttribute.g.cs"; | ||
|
||
public const string Code = """ | ||
namespace GObject; | ||
[System.AttributeUsage(System.AttributeTargets.Class)] | ||
public class SubclassAttribute : System.Attribute { } | ||
"""; | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Extensions/GObject-2.0.Integration/Integration/Subclass/SubclassCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace GObject.Integration; | ||
|
||
public static class SubclassCode | ||
{ | ||
public static void Generate(SourceProductionContext context, SubclassData subclassData) | ||
{ | ||
context.AddSource( | ||
hintName:$"{subclassData.Name}.Subclass.g.cs", | ||
source: ToCode(subclassData) | ||
); | ||
} | ||
private static string ToCode(SubclassData subclassData) | ||
{ | ||
return $$""" | ||
namespace GridViewSample; | ||
//TODO: HIER MUSS EIN Primary Constructor rein, der ein Handle braucht und das an die Base weitergibt | ||
//public class My(AboutDialogHandle handle) : AboutDialog(handle) | ||
//{ | ||
// public My(params GObject.ConstructArgument[] constructArguments) : this(Gtk.Internal.AboutDialogHandle.For<My>(false, constructArguments)) { } | ||
// Dann kann ein User den Constructor so schreiben: | ||
// public My(int a) : this() | ||
public partial class {{subclassData.Name}} : GObject.GTypeProvider, GObject.InstanceFactory | ||
{ | ||
private static readonly GObject.Type GType = GObject.Internal.SubclassRegistrar.Register<{{subclassData.Name}}, GObject.Object>(); | ||
public static new GObject.Type GetGType() => GType; | ||
static object GObject.InstanceFactory.Create(System.IntPtr handle, bool ownsHandle) | ||
{ | ||
return new {{subclassData.Name}}(handle, ownsHandle); | ||
} | ||
private {{subclassData.Name}}(System.IntPtr ptr, bool ownsHandle) : base(new GObject.Internal.ObjectHandle(ptr, ownsHandle)) { } | ||
} | ||
"""; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Extensions/GObject-2.0.Integration/Integration/Subclass/SubclassData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace GObject.Integration; | ||
|
||
public record SubclassData(string Name, string Parent); |
28 changes: 28 additions & 0 deletions
28
src/Extensions/GObject-2.0.Integration/Integration/Subclass/SubclassValuesProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace GObject.Integration; | ||
|
||
public static class SubclassValuesProvider | ||
{ | ||
public static IncrementalValuesProvider<SubclassData> GetSubclassValuesProvider(this IncrementalGeneratorInitializationContext context) | ||
{ | ||
return context.SyntaxProvider | ||
.ForAttributeWithMetadataName( | ||
fullyQualifiedMetadataName: SubclassAttribute.FullyQualifiedName, | ||
predicate: static (_, _) => true, | ||
transform: GetSubclassData) | ||
.Where(data => data is not null)!; | ||
} | ||
|
||
private static SubclassData? GetSubclassData(GeneratorAttributeSyntaxContext context, CancellationToken cancellationToken) | ||
{ | ||
var semanticModel = context.SemanticModel; | ||
var enumDeclarationSyntax = context.TargetNode; | ||
|
||
if (semanticModel.GetDeclaredSymbol(enumDeclarationSyntax, cancellationToken) is not INamedTypeSymbol symbol) | ||
return null; | ||
|
||
return new SubclassData(Name: symbol.Name, "Test"); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Extensions/GObject-2.0.Integration/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"GObject-2.0.Integration": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "../../Samples/Gtk-4.0/GridView/GridView.csproj" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Tests/Libs/GirTest-0.1.Tests/SubclassIntegrationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
|
||
namespace GirTest.Tests; | ||
|
||
[TestClass, TestCategory("BindingTest")] | ||
public class SubclassIntegrationTest : Test | ||
{ | ||
[TestMethod] | ||
public void GeneratesCode() | ||
{ | ||
var result = RunGeneratorOn(""" | ||
[GObject.Subclass] | ||
public partial class CustomSubclassOfObject{ } | ||
"""); | ||
var sources = result.Results.First().GeneratedSources; | ||
|
||
sources[0].HintName.Should().Be("SubclassAttribute.g.cs"); | ||
sources[0].SourceText.ToString().Should().Be(""" | ||
namespace GObject; | ||
[System.AttributeUsage(System.AttributeTargets.Class)] | ||
public class SubclassAttribute : System.Attribute { } | ||
"""); | ||
|
||
sources[1].HintName.Should().Be("CustomSubclassOfObject.Subclass.g.cs"); | ||
sources[1].SourceText.ToString().Should().Be(""" | ||
namespace GridViewSample; | ||
public partial class CustomSubclassOfObject : GObject.GTypeProvider, GObject.InstanceFactory | ||
{ | ||
private static readonly GObject.Type GType = GObject.Internal.SubclassRegistrar.Register<CustomSubclassOfObject, GObject.Object>(); | ||
public static new GObject.Type GetGType() => GType; | ||
static object GObject.InstanceFactory.Create(System.IntPtr handle, bool ownsHandle) | ||
{ | ||
return new CustomSubclassOfObject(handle, ownsHandle); | ||
} | ||
} | ||
"""); | ||
} | ||
|
||
private static GeneratorDriverRunResult RunGeneratorOn(string source) | ||
{ | ||
var inputCompilation = CSharpCompilation.Create("compilation", [CSharpSyntaxTree.ParseText(source)]); | ||
var generator = new GObject.Integration.Generator(); | ||
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); | ||
driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out _, out _); | ||
|
||
return driver.GetRunResult(); | ||
} | ||
} |