mirror of
https://github.com/AlexMacocian/WpfExtended.git
synced 2026-07-15 14:59:30 +00:00
Assign generated class modifiers based on declared modifiers.
This commit is contained in:
@@ -102,8 +102,8 @@ namespace System.Extensions
|
||||
.WithNamespace(
|
||||
new NamespaceTemplate()
|
||||
.WithNamespace(namespaceName))
|
||||
.WithModifiers(Modifier.Public, Modifier.Partial)
|
||||
.WithName(classSymbol.Name);
|
||||
AssignModifiers(source, classSymbol);
|
||||
foreach (var fieldSymbol in fields)
|
||||
{
|
||||
ProcessField(source, classSymbol, attributeSymbol, fieldSymbol);
|
||||
@@ -112,6 +112,15 @@ namespace System.Extensions
|
||||
return source.GenerateString();
|
||||
}
|
||||
|
||||
private static void AssignModifiers(ClassTemplate source, INamedTypeSymbol classSymbol)
|
||||
{
|
||||
//Find class declaration syntax and parse modifiers.
|
||||
if (classSymbol.DeclaringSyntaxReferences.Select(sr => sr.GetSyntax()).OfType<ClassDeclarationSyntax>().First() is ClassDeclarationSyntax classDeclarationSyntax)
|
||||
{
|
||||
source.WithModifiers(classDeclarationSyntax.Modifiers.Select(m => Modifier.Parse(m.ValueText)).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static void ProcessField(ClassTemplate source, INamedTypeSymbol classSymbol, INamedTypeSymbol attributeSymbol, IFieldSymbol fieldSymbol)
|
||||
{
|
||||
var fieldName = fieldSymbol.Name;
|
||||
|
||||
@@ -43,6 +43,11 @@ namespace System.Extensions.Templates
|
||||
this.Modifiers.AddRange(modifiers);
|
||||
return this;
|
||||
}
|
||||
public ClassTemplate WithModifier(Modifier modifier)
|
||||
{
|
||||
this.Modifiers.Add(modifier);
|
||||
return this;
|
||||
}
|
||||
public ClassTemplate WithMethods(params MethodTemplate[] methodTemplates)
|
||||
{
|
||||
this.Methods.Clear();
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
namespace System.Extensions.Templates
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace System.Extensions.Templates
|
||||
{
|
||||
internal sealed class Modifier : AbstractTemplate
|
||||
{
|
||||
public static Modifier Protected { get; } = new Modifier { Value = "protected" };
|
||||
public static Modifier Readonly { get; } = new Modifier { Value = "readonly" };
|
||||
public static Modifier Static { get; } = new Modifier { Value = "static" };
|
||||
public static Modifier Public { get; } = new Modifier { Value = "public" };
|
||||
@@ -22,5 +26,32 @@
|
||||
{
|
||||
codeWriter.Append(this.Value);
|
||||
}
|
||||
|
||||
public static bool TryParse(string value, out Modifier modifier)
|
||||
{
|
||||
foreach(var supportedModifier in SupportedModifiers)
|
||||
{
|
||||
if (supportedModifier.Value.Equals(value, StringComparison.Ordinal))
|
||||
{
|
||||
modifier = supportedModifier;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
modifier = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Modifier Parse(string value)
|
||||
{
|
||||
if (TryParse(value, out var modifier))
|
||||
{
|
||||
return modifier;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Could not find a modifier with value {value}");
|
||||
}
|
||||
|
||||
private static IEnumerable<Modifier> SupportedModifiers { get; } = new List<Modifier> { Protected, Readonly, Static, Internal, Public, Private, Abstract, Virtual, Partial, Sealed };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user