Skip to content

Commit

Permalink
Derivedfromgenerator add only flat
Browse files Browse the repository at this point in the history
  • Loading branch information
ramoneeza committed Jul 17, 2023
1 parent 5376b75 commit 710fd32
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 24 deletions.
3 changes: 3 additions & 0 deletions Rop.DerivedFrom.Annotations/IDerivedFrom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public interface IDerivedFrom<out T>
public interface IFormDerivedFrom<out T>
{
}
public interface IFormFlat<out T>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageTags>helper-classes;winforms;annotations</PackageTags>
<Description>Annotations for DerivedFrom Generator</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.3</Version>
<Version>1.0.5</Version>
<Authors>Ramon Ordiales Plaza</Authors>
<Company>Ramon Ordiales Plaza</Company>
<Copyright>2022 Ramon Ordiales</Copyright>
Expand Down
24 changes: 16 additions & 8 deletions Rop.DerivedFromGenerator/DerivedFromGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void Execute(GeneratorExecutionContext context)
if (collector==null) return;
var normal=collector.GetNormalClassesToAugment();
var forms=collector.GetFormClassesToAugment();
var formscomplet=collector.GetFormClassesToAugmentComplet();
var namespacenormalflatten = "";
var namespaceformflatten = "";
if (normal.Count != 0)
Expand All @@ -44,7 +45,7 @@ public void Execute(GeneratorExecutionContext context)
generateCode(context, classtoaugment,namespacenormalflatten);
}
}
if (forms.Count != 0 && namespaceformflatten!="")
if (formscomplet.Count != 0 && namespaceformflatten!="")
{
foreach (var classtoaugment in collector.ClassesToAugment)
{
Expand Down Expand Up @@ -118,6 +119,10 @@ private string generateSpecialFlatten(GeneratorExecutionContext context, List<Pr
collector.Ts.TraceEvent(TraceEventType.Error, 1, "build_property.rootnamespace not found");
return "";
}

var finalfile=Path.Combine(Path.GetDirectoryName(fd), "Flatten.cs");
var originalfile=File.ReadAllText(finalfile);

mynamespace = mynamespace + ".FormDerived";
var sb = new StringBuilder();
sb.AppendLine("// Autogenerated code for flatten");
Expand All @@ -130,8 +135,7 @@ private string generateSpecialFlatten(GeneratorExecutionContext context, List<Pr
sb.AppendLine($"public partial class {flatbasename}: {basename} {{}}");
}
var final = sb.ToString();
var finalfile=Path.Combine(Path.GetDirectoryName(fd), "Flatten.cs");
File.WriteAllText(finalfile,final);
if (final != originalfile) File.WriteAllText(finalfile,final);
return mynamespace;
}

Expand Down Expand Up @@ -162,15 +166,14 @@ public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (!(syntaxNode is ClassDeclarationSyntax cds)) return;
// Business logic to decide what we're interested in goes here
var basesof = cds.GenericBasesOfType("IDerivedFrom").Concat(cds.GenericBasesOfType("IFormDerivedFrom")).ToList();
var basesof = cds.GenericBasesOfAny("IDerivedFrom", "IFormDerivedFrom", "IFormFlat");
if (basesof.Any())
{
var ac = new PartialClassToAugment(cds);
foreach (var b in basesof)
{
var isformderivedfrom = b.Identifier.Text == "IFormDerivedFrom";
var t=b.TypeArgumentList.Arguments[0];
ClassesToAugment.Add(new ProxyPartialClassToAugment(ac,t,isformderivedfrom));
ClassesToAugment.Add(new ProxyPartialClassToAugment(ac,t,b.Identifier.Text));
}
}
}
Expand All @@ -181,12 +184,17 @@ public void Clear()

public List<ProxyPartialClassToAugment> GetNormalClassesToAugment()
{
return ClassesToAugment.Where(c=>!c.IsFormDerivedFrom).ToList();
return ClassesToAugment.Where(c=>c.DerivedType=="IDerivedFrom").ToList();
}

public List<ProxyPartialClassToAugment> GetFormClassesToAugment()
{
return ClassesToAugment.Where(c => c.IsFormDerivedFrom).ToList();
return ClassesToAugment.Where(c => c.DerivedType!="IDerivedFrom").ToList();
}

public List<ProxyPartialClassToAugment> GetFormClassesToAugmentComplet()
{
return ClassesToAugment.Where(c => c.DerivedType!="IFormDerivedFrom").ToList();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Rop.DerivedFromGenerator/ProxyPartialClassToAugment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class ProxyPartialClassToAugment
public PartialClassToAugment Original { get; }
public TypeSyntax BaseToFlat { get; }
public string FlatBaseName { get; }
public bool IsFormDerivedFrom { get; }
public ProxyPartialClassToAugment(PartialClassToAugment original, TypeSyntax baseToFlat, bool isFormDerivedFrom)
public string DerivedType { get; }
public ProxyPartialClassToAugment(PartialClassToAugment original, TypeSyntax baseToFlat, string derivedtype)
{
Original = original;
BaseToFlat = baseToFlat;
IsFormDerivedFrom = isFormDerivedFrom;
DerivedType = derivedtype;
FlatBaseName = new string(baseToFlat.ToString().Select(c => char.IsLetterOrDigit(c) ? c : '_').ToArray());
}
}
Expand Down
2 changes: 1 addition & 1 deletion Rop.DerivedFromGenerator/Rop.DerivedFromGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageTags>extensions;generics;winforms;source-generator;roslyn</PackageTags>
<Description>Source generator to derive form from generic</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.6</Version>
<Version>1.0.7</Version>
<Authors>Ramon Ordiales Plaza</Authors>
<Company>Ramon Ordiales Plaza</Company>
<Copyright>2023 Ramon Ordiales</Copyright>
Expand Down
20 changes: 20 additions & 0 deletions Rop.Generators.Shared/SyntaxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ public static List<GenericNameSyntax> GenericBasesOfType(this ClassDeclarationSy
}
return res;
}
public static List<GenericNameSyntax> GenericBasesOfAny(this ClassDeclarationSyntax cds,params string[] typename)
{
var res=new List<GenericNameSyntax>();
if (cds.BaseList is null) return res;
foreach (var type in cds.BaseList.Types)
{
if (type.Type is GenericNameSyntax gns)
{
var id=gns.Identifier.ToString();
if (typename.Contains(id))
{
res.Add(gns);
}
}
}
return res;
}



public static string ToStringValue(this ExpressionSyntax expression)
{
switch (expression)
Expand Down
7 changes: 4 additions & 3 deletions Rop.Generators.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.DerivedFrom.SubForms",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.DerivedFrom.FormDerived", "Test\Test.DerivedFrom.FormDerived\Test.DerivedFrom.FormDerived.csproj", "{BECC3846-2D46-4586-84C4-7E1AE321A96C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rop.ObservableGenerator", "Rop.ObservableGenerator\Rop.ObservableGenerator.csproj", "{0D404D6A-D05F-4163-BD7B-4DAA7D85F81D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rop.ObservableGenerator", "Rop.ObservableGenerator\Rop.ObservableGenerator.csproj", "{0D404D6A-D05F-4163-BD7B-4DAA7D85F81D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rop.ObservableGenerator.Annotations", "Rop.ObservableGenerator.Annotations\Rop.ObservableGenerator.Annotations.csproj", "{5EFA3272-F68D-41D3-93AE-BCAD2C41C7DB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rop.ObservableGenerator.Annotations", "Rop.ObservableGenerator.Annotations\Rop.ObservableGenerator.Annotations.csproj", "{5EFA3272-F68D-41D3-93AE-BCAD2C41C7DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.ObservableGenerator", "Test\Test.ObservableGenerator\Test.ObservableGenerator.csproj", "{DFA586D4-8B90-4F2B-BED1-2263BB5DDCFA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.ObservableGenerator", "Test\Test.ObservableGenerator\Test.ObservableGenerator.csproj", "{DFA586D4-8B90-4F2B-BED1-2263BB5DDCFA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -186,6 +186,7 @@ Global
Rop.Generators.Shared\Rop.Generators.Shared.projitems*{10ed2d2d-6d3d-4570-b78c-f61b52328596}*SharedItemsImports = 5
Rop.Generators.Shared\Rop.Generators.Shared.projitems*{568ee803-f01b-4f45-ba81-70642128a797}*SharedItemsImports = 5
Rop.Generators.Shared\Rop.Generators.Shared.projitems*{59cbdaf7-857a-47d6-ae57-0d069f5d925f}*SharedItemsImports = 5
Rop.Generators.Shared\Rop.Generators.Shared.projitems*{5a2bdd1d-9a9b-42bf-b00c-4104ceb957f6}*SharedItemsImports = 5
Rop.Generators.Shared\Rop.Generators.Shared.projitems*{86231b22-cb3a-4de0-af41-c26710435cc9}*SharedItemsImports = 5
Rop.Generators.Shared\Rop.Generators.Shared.projitems*{8f79abde-e5ad-421d-be65-0042f27b104b}*SharedItemsImports = 5
Rop.Generators.Shared\Rop.Generators.Shared.projitems*{b6fa2a36-63eb-4074-b174-3e85b96e4265}*SharedItemsImports = 13
Expand Down
4 changes: 2 additions & 2 deletions Rop.ProxyGenerator/ProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ void MethodBody(bool prebase,bool postbase, string pnames , string retstr)
$"base.{name}({pnames});");
if (!postbase)
sb.AppendLines(3,
$"{retstr}{field}.{name}({pnames});");
$"{retstr}{field}?.{name}({pnames});");
else
sb.AppendLines(3,
$"{field}.{name}({pnames});",
$"{field}?.{name}({pnames});",
$"{retstr}base.{name}({pnames});");
}

Expand Down
2 changes: 1 addition & 1 deletion Rop.ProxyGenerator/Rop.ProxyGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageTags>extensions;proxy;source-generator;roslyn</PackageTags>
<Description>Source generator to create ProxyClasses</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.27</Version>
<Version>1.0.28</Version>
<Authors>Ramon Ordiales Plaza</Authors>
<Company>Ramon Ordiales Plaza</Company>
<Copyright>2022 Ramon Ordiales</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageTags>extensions;generics;static;winforms;source-generator;roslyn</PackageTags>
<Description>Source generator to create static extensions</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.9</Version>
<Version>1.0.11</Version>
<Authors>Ramon Ordiales Plaza</Authors>
<Company>Ramon Ordiales Plaza</Company>
<Copyright>2023 Ramon Ordiales</Copyright>
Expand Down
10 changes: 6 additions & 4 deletions Rop.StaticExtensionGenerator/StaticExtensionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ private void generateCode(GeneratorExecutionContext context, PartialClassToAugme
var baseclase=classmodel.BaseType;
if (baseclase is null||baseclase.Name=="Object")
{
var iderived=classmodel.Interfaces.FirstOrDefault(i=>i.Name=="IDerived");
var iderived=classmodel.Interfaces.FirstOrDefault(i=>i.Name=="IDerivedFrom" || i.Name=="IFormDerivedFrom");
if (iderived != null)
{
baseclase= iderived.TypeArguments[0] as INamedTypeSymbol;
}
}

var (staticMethods,path)=GetStaticMethods(baseclase,classmodel,model,receiver);
var sb = new StringBuilder();
sb.AppendLine("// Autogenerated code for StaticMethods");
Expand All @@ -59,13 +58,16 @@ private void generateCode(GeneratorExecutionContext context, PartialClassToAugme
sb.AppendLines(classtoaugment.GetHeader(new string []{}));
foreach (var symbol in staticMethods)
{
var name = symbol.Name;
var prename = symbol.Name;
var name = prename;
if (name.StartsWith("_")) name = name.Substring(1);
if (char.IsLower(name[0])) name = char.ToUpper(name[0]) + name.Substring(1);
var returntype = symbol.ReturnType.ToString();
var par=symbol.Parameters;
var pardef=string.Join(",",par.Select(p=>p.ToString()));
var parnames =string.Join(",",par.Select(p => p.Name));
var clasebelongs=symbol.ContainingType;
sb.AppendLines($"\t\tpublic static {returntype} {name}({pardef})=>{clasebelongs.ToDisplayString()}.{name}<{formname}>({parnames});");
sb.AppendLines($"\t\tpublic static {returntype} {name}({pardef})=>{clasebelongs.ToDisplayString()}.{prename}<{formname}>({parnames});");
}
sb.AppendLines(classtoaugment.GetFooter());
var final = sb.ToString();
Expand Down

0 comments on commit 710fd32

Please sign in to comment.