- tweak: Some view change
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
global using SourceGen;
|
||||
global using Nebula.Shared.Attributes;
|
||||
global using Nebula.Launcher.ViewHelper;
|
||||
@@ -110,7 +110,7 @@ public partial class ServerEntryModelView : ViewModelBase
|
||||
|
||||
Process.Exited += OnExited;
|
||||
}
|
||||
catch (TaskCanceledException _)
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
PopupMessageService.Popup("Task canceled");
|
||||
}
|
||||
|
||||
@@ -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">
|
||||
<Label HorizontalAlignment="Center" VerticalAlignment="Center">|||</Label>
|
||||
</Button>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
<Grid
|
||||
ColumnDefinitions="*,2*,40,40"
|
||||
Margin="10"
|
||||
Margin="5,5,5,0"
|
||||
RowDefinitions="40,*">
|
||||
<Border
|
||||
BorderThickness="2,0,0,0"
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
|
||||
<Grid
|
||||
ColumnDefinitions="*"
|
||||
Margin="5"
|
||||
Margin="5,0,5,5"
|
||||
RowDefinitions="*,40">
|
||||
<ScrollViewer Margin="0,0,0,10" Padding="0,0,8,0">
|
||||
<ScrollViewer Margin="0,0,0,10" Padding="0,0,10,0" Grid.RowSpan="2">
|
||||
<ItemsControl
|
||||
Background="#00000000"
|
||||
ItemsSource="{Binding ServerInfos}"
|
||||
@@ -33,6 +33,7 @@
|
||||
RowDefinitions="30,*">
|
||||
<Border
|
||||
BorderThickness="2,0,0,0"
|
||||
CornerRadius="10,0,0,0"
|
||||
Grid.Column="0"
|
||||
Grid.Row="0"
|
||||
Padding="10,0,0,0">
|
||||
@@ -42,7 +43,7 @@
|
||||
</Border>
|
||||
<Border
|
||||
BorderThickness="2,0,2,0"
|
||||
CornerRadius="0"
|
||||
CornerRadius="0,10,0,0"
|
||||
Grid.Column="1"
|
||||
Grid.Row="0"
|
||||
Padding="5">
|
||||
@@ -166,10 +167,10 @@
|
||||
</ScrollViewer>
|
||||
|
||||
<Border
|
||||
BorderThickness="2,0,0,0"
|
||||
BorderThickness="2,2,2,0"
|
||||
CornerRadius="10"
|
||||
Grid.Row="1">
|
||||
<Grid ColumnDefinitions="*,40,40" RowDefinitions="*">
|
||||
<Grid ColumnDefinitions="*,40,40" RowDefinitions="*" Margin="5,0,0,0">
|
||||
<TextBox
|
||||
Margin="0"
|
||||
Text="{Binding SearchText}"
|
||||
|
||||
7
Nebula.Shared/Attributes/ConstructGeneratorAttribute.cs
Normal file
7
Nebula.Shared/Attributes/ConstructGeneratorAttribute.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Nebula.Shared.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class ConstructGeneratorAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
7
Nebula.Shared/Attributes/GeneratePropertyAttribute.cs
Normal file
7
Nebula.Shared/Attributes/GeneratePropertyAttribute.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Nebula.Shared.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class GeneratePropertyAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -11,18 +11,15 @@ namespace Nebula.SourceGenerators;
|
||||
[Generator]
|
||||
public class DependencyAutoGenerator : IIncrementalGenerator
|
||||
{
|
||||
private static readonly string ConstructGeneratorAttributeName = "ConstructGeneratorAttribute";
|
||||
private static readonly string GeneratePropertyAttributeName = "GeneratePropertyAttribute";
|
||||
private static readonly string ConstructGeneratorAttributeName = "Nebula.Shared.Attributes.ConstructGeneratorAttribute";
|
||||
private static readonly string GeneratePropertyAttributeName = "Nebula.Shared.Attributes.GeneratePropertyAttribute";
|
||||
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
GenerateAttribute(ConstructGeneratorAttributeName, "Class", context);
|
||||
GenerateAttribute(GeneratePropertyAttributeName, "Property", context);
|
||||
|
||||
var provider = context.SyntaxProvider
|
||||
.CreateSyntaxProvider(
|
||||
(s, _) => 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 = $@"// <auto-generated/>
|
||||
|
||||
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<ClassDeclarationSyntax> classDeclarations)
|
||||
{
|
||||
@@ -118,15 +77,6 @@ partial class {className}
|
||||
private static IEnumerable<IPropertySymbol> GetProperties(INamedTypeSymbol classSymbol)
|
||||
{
|
||||
return classSymbol.GetMembers().OfType<IPropertySymbol>().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));
|
||||
}
|
||||
}
|
||||
56
Nebula.SourceGenerators/SourceHelper.cs
Normal file
56
Nebula.SourceGenerators/SourceHelper.cs
Normal file
@@ -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 = $@"// <auto-generated/>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
58
Nebula.SourceGenerators/ViewConstructGenerator.cs
Normal file
58
Nebula.SourceGenerators/ViewConstructGenerator.cs
Normal file
@@ -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<ClassDeclarationSyntax> 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 = $@"// <auto-generated/>
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user