diff --git a/Nebula.Launcher/GlobalUsing.cs b/Nebula.Launcher/GlobalUsing.cs index 895122c..2fcd408 100644 --- a/Nebula.Launcher/GlobalUsing.cs +++ b/Nebula.Launcher/GlobalUsing.cs @@ -1,2 +1,2 @@ -global using SourceGen; +global using Nebula.Shared.Attributes; global using Nebula.Launcher.ViewHelper; \ No newline at end of file diff --git a/Nebula.Launcher/ViewModels/ServerEntryModelView.cs b/Nebula.Launcher/ViewModels/ServerEntryModelView.cs index 765056b..f82a416 100644 --- a/Nebula.Launcher/ViewModels/ServerEntryModelView.cs +++ b/Nebula.Launcher/ViewModels/ServerEntryModelView.cs @@ -110,7 +110,7 @@ public partial class ServerEntryModelView : ViewModelBase Process.Exited += OnExited; } - catch (TaskCanceledException _) + catch (TaskCanceledException) { PopupMessageService.Popup("Task canceled"); } diff --git a/Nebula.Launcher/Views/MainView.axaml b/Nebula.Launcher/Views/MainView.axaml index e51dfc0..898ff4f 100644 --- a/Nebula.Launcher/Views/MainView.axaml +++ b/Nebula.Launcher/Views/MainView.axaml @@ -65,8 +65,8 @@ Classes="ViewSelectButton" Command="{Binding TriggerPaneCommand}" Grid.Row="1" - HorizontalAlignment="Left" - Padding="15,0,15,0" + HorizontalAlignment="Stretch" + Padding="5,0,5,0" VerticalAlignment="Stretch"> diff --git a/Nebula.Launcher/Views/Pages/ContentBrowserView.axaml b/Nebula.Launcher/Views/Pages/ContentBrowserView.axaml index 7c5bdec..859edc3 100644 --- a/Nebula.Launcher/Views/Pages/ContentBrowserView.axaml +++ b/Nebula.Launcher/Views/Pages/ContentBrowserView.axaml @@ -15,7 +15,7 @@ - + @@ -42,7 +43,7 @@ @@ -166,10 +167,10 @@ - + s is ClassDeclarationSyntax, - (ctx, _) => GetClassDeclarationForSourceGen(ctx)) + (ctx, _) => SourceHelper.GetClassDeclarationForSourceGen(ctx,ConstructGeneratorAttributeName)) .Where(t => t.reportAttributeFound) .Select((t, _) => t.Item1); @@ -30,44 +27,6 @@ public class DependencyAutoGenerator : IIncrementalGenerator (ctx, t) => GenerateCode(ctx, t.Left, t.Right)); } - private static void GenerateAttribute(string attributeName, string usage, - IncrementalGeneratorInitializationContext context) - { - var attributeSourceCode = $@"// - -namespace SourceGen -{{ - [System.AttributeUsage(System.AttributeTargets.{usage})] - public class {attributeName} : System.Attribute - {{ - }} -}}"; - - context.RegisterPostInitializationOutput(ctx => ctx.AddSource( - $"{attributeName}.g.cs", - SourceText.From(attributeSourceCode, Encoding.UTF8))); - } - - private static (ClassDeclarationSyntax, bool reportAttributeFound) GetClassDeclarationForSourceGen( - GeneratorSyntaxContext context) - { - var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node; - - foreach (var attributeListSyntax in classDeclarationSyntax.AttributeLists) - foreach (var attributeSyntax in attributeListSyntax.Attributes) - { - if (context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) - continue; - - var attributeName = attributeSymbol.ContainingType.ToDisplayString(); - - if (attributeName == $"SourceGen.{ConstructGeneratorAttributeName}") - return (classDeclarationSyntax, true); - } - - return (classDeclarationSyntax, false); - } - private void GenerateCode(SourceProductionContext context, Compilation compilation, ImmutableArray classDeclarations) { @@ -118,15 +77,6 @@ partial class {className} private static IEnumerable GetProperties(INamedTypeSymbol classSymbol) { return classSymbol.GetMembers().OfType().Where(a => - HasAttribute(a, $"SourceGen.{GeneratePropertyAttributeName}")); - } - - private static bool HasAttribute(ISymbol type, string attributeName) - { - foreach (var attribute in type.GetAttributes()) - if (attribute.AttributeClass?.ToDisplayString() == attributeName) - return true; - - return false; + SourceHelper.HasAttribute(a, GeneratePropertyAttributeName)); } } \ No newline at end of file diff --git a/Nebula.SourceGenerators/SourceHelper.cs b/Nebula.SourceGenerators/SourceHelper.cs new file mode 100644 index 0000000..dc880a7 --- /dev/null +++ b/Nebula.SourceGenerators/SourceHelper.cs @@ -0,0 +1,56 @@ +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; + +namespace Nebula.SourceGenerators; + +public static class SourceHelper +{ + public static void GenerateAttribute(string attributeName, string usage, + IncrementalGeneratorInitializationContext context) + { + var attributeSourceCode = $@"// + +namespace SourceGen +{{ + [System.AttributeUsage(System.AttributeTargets.{usage})] + public class {attributeName} : System.Attribute + {{ + }} +}}"; + + context.RegisterPostInitializationOutput(ctx => ctx.AddSource( + $"{attributeName}.g.cs", + SourceText.From(attributeSourceCode, Encoding.UTF8))); + } + + public static bool HasAttribute(ISymbol type, string attributeName) + { + foreach (var attribute in type.GetAttributes()) + if (attribute.AttributeClass?.ToDisplayString() == attributeName) + return true; + + return false; + } + + public static (ClassDeclarationSyntax, bool reportAttributeFound) GetClassDeclarationForSourceGen( + GeneratorSyntaxContext context, string AttributeName) + { + var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node; + + foreach (var attributeListSyntax in classDeclarationSyntax.AttributeLists) + foreach (var attributeSyntax in attributeListSyntax.Attributes) + { + if (context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) + continue; + + var attributeName = attributeSymbol.ContainingType.ToDisplayString(); + + if (attributeName == AttributeName) + return (classDeclarationSyntax, true); + } + + return (classDeclarationSyntax, false); + } +} \ No newline at end of file diff --git a/Nebula.SourceGenerators/ViewConstructGenerator.cs b/Nebula.SourceGenerators/ViewConstructGenerator.cs new file mode 100644 index 0000000..3e34aea --- /dev/null +++ b/Nebula.SourceGenerators/ViewConstructGenerator.cs @@ -0,0 +1,58 @@ +using System.Collections.Immutable; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; + +namespace Nebula.SourceGenerators; + +//[Generator] +public class ViewConstructGenerator : IIncrementalGenerator +{ + public static string ViewForModelAttributeName = "ViewForModelAttribute"; + + public void Initialize(IncrementalGeneratorInitializationContext context) + { + var provider = context.SyntaxProvider + .CreateSyntaxProvider( + (s, _) => s is ClassDeclarationSyntax, (ctx, _) => + SourceHelper.GetClassDeclarationForSourceGen(ctx,ViewForModelAttributeName) + ) + .Where(t => t.reportAttributeFound) + .Select((t,_) => t.Item1); + + context.RegisterSourceOutput(context.CompilationProvider.Combine(provider.Collect()), (ctx,t)=> GenerateCode(ctx, t.Left, t.Right)); + } + + private void GenerateCode(SourceProductionContext context, Compilation compilation, ImmutableArray syntaxes) + { + foreach (var classDeclarationSyntax in syntaxes) + { + var semanticModel = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree); + if(semanticModel.GetDeclaredSymbol(classDeclarationSyntax) is not INamedTypeSymbol classSymbol) + continue; + + var namespaceName = classSymbol.ContainingNamespace.ToDisplayString(); + var className = classDeclarationSyntax.Identifier.Text; + + var code = $@"// + +using System; +using System.Collections.Generic; + +namespace {namespaceName}; + +partial class {className} +{{ + public {className}() : base(){{ + + + }} +}} + "; + + // Add the source code to the compilation. + context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8)); + } + } +} \ No newline at end of file