Skip to content

Commit

Permalink
concept of source builder
Browse files Browse the repository at this point in the history
  • Loading branch information
beakona committed Nov 14, 2020
1 parent e1d98f2 commit 5b74f4c
Show file tree
Hide file tree
Showing 15 changed files with 1,108 additions and 1,027 deletions.
4 changes: 0 additions & 4 deletions AutoInterfaceSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public partial class Person : IPrintable
{
[BeaKona.AutoInterface(typeof(IPrintable))]
private readonly IPrintable aspect1 = new PersonPrinterV1();

void IPrintable.Print()
{
}
}

internal class PersonPrinterV1 : IPrintable
Expand Down
2 changes: 1 addition & 1 deletion BeaKona.AutoInterfaceGenerator/AutoInterfaceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace BeaKona.AutoInterfaceGenerator
{
internal sealed class AutoInterfaceInfo
internal sealed class AutoInterfaceInfo : IMemberInfo
{
public AutoInterfaceInfo(ISymbol member, ITypeSymbol receiverType, AttributeData attribute, INamedTypeSymbol interfaceType)
{
Expand Down
84 changes: 41 additions & 43 deletions BeaKona.AutoInterfaceGenerator/AutoInterfaceSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ void Process(ISymbol symbol, ITypeSymbol receiverType)
}
}

CSharpBuildContext buildContext = new CSharpBuildContext(compilation);

// group the elements by class, and generate the source
foreach (IGrouping<INamedTypeSymbol, AutoInterfaceInfo> group in symbols.GroupBy(i => i.Member.ContainingType))
{
Expand Down Expand Up @@ -188,12 +186,12 @@ void Process(ISymbol symbol, ITypeSymbol receiverType)
continue;
}

ISourceTextBuilder builder = new CSharpSourceTextBuilder(buildContext);
if (this.ProcessClass(builder, context, group.Key, group))
string? code = AutoInterfaceSourceGenerator.ProcessClass(compilation, group.Key, group);
if(code != null)
{
string name = group.Key.Arity> 0 ? $"{group.Key.Name}_{group.Key.Arity}" : group.Key.Name;
//GeneratePreview(context, name, builder.ToString());
context.AddSource($"{name}_AutoInterface.cs", SourceText.From(builder.ToString(), Encoding.UTF8));
//GeneratePreview(context, name, code);
context.AddSource($"{name}_AutoInterface.cs", SourceText.From(code, Encoding.UTF8));
}
}
}
Expand All @@ -213,21 +211,21 @@ void Process(ISymbol symbol, ITypeSymbol receiverType)
// context.AddSource($"Output_Debug_{name}.cs", SourceText.From(output.ToString(), Encoding.UTF8));
//}

private bool ProcessClass(ISourceTextBuilder builder, GeneratorExecutionContext context, INamedTypeSymbol type, IEnumerable<AutoInterfaceInfo> infos)
private static string? ProcessClass(Compilation compilation, INamedTypeSymbol type, IEnumerable<IMemberInfo> infos)
{
Compilation compilation = context.Compilation;

ScopeInfo scope = new ScopeInfo(compilation, type);
ScopeInfo scope = new ScopeInfo(type);

SourceBuilder sb = new SourceBuilder();
ICodeTextBuilder output = new CSharpCodeTextBuilder(compilation, sb);
bool anyReasonToEmitSourceFile = false;

//bool isNullable = compilation.Options.NullableContextOptions == NullableContextOptions.Enable;
builder.AppendLine("#nullable enable");
output.Builder.AppendLine("#nullable enable");

ImmutableArray<INamespaceSymbol> namespaceParts = type.ContainingNamespace != null && type.ContainingNamespace.IsGlobalNamespace == false ? builder.Context.GetNamespaceParts(type.ContainingNamespace) : new ImmutableArray<INamespaceSymbol>();
ImmutableArray<INamespaceSymbol> namespaceParts = type.ContainingNamespace != null && type.ContainingNamespace.IsGlobalNamespace == false ? SemanticFacts.GetNamespaceParts(type.ContainingNamespace) : new ImmutableArray<INamespaceSymbol>();
if (namespaceParts.Length > 0)
{
builder.AppendNamespaceBegin(string.Join(".", namespaceParts.Select(i => builder.Context.GetSourceIdentifier(i))));
output.AppendNamespaceBeginning(string.Join(".", namespaceParts.Select(i => output.GetSourceIdentifier(i))));
}

List<INamedTypeSymbol> containingTypes = new List<INamedTypeSymbol>();
Expand All @@ -238,26 +236,26 @@ private bool ProcessClass(ISourceTextBuilder builder, GeneratorExecutionContext

foreach (INamedTypeSymbol ct in containingTypes)
{
builder.AppendIndentation();
builder.AppendTypeDeclarationBegin(ct, new ScopeInfo(compilation, ct));
builder.AppendLine();
builder.AppendIndentation();
builder.AppendLine("{");
builder.IncrementIndentation();
output.Builder.AppendIndentation();
output.AppendTypeDeclarationBeginning(ct, new ScopeInfo(ct));
output.Builder.AppendLine();
output.Builder.AppendIndentation();
output.Builder.AppendLine('{');
output.Builder.IncrementIndentation();
}

builder.AppendIndentation();
builder.AppendTypeDeclarationBegin(type, scope);
builder.AppendLine();
builder.AppendIndentation();
builder.AppendLine("{");
builder.IncrementIndentation();
output.Builder.AppendIndentation();
output.AppendTypeDeclarationBeginning(type, scope);
output.Builder.AppendLine();
output.Builder.AppendIndentation();
output.Builder.AppendLine('{');
output.Builder.IncrementIndentation();

bool separatorRequired = false;

foreach (IGrouping<INamedTypeSymbol, AutoInterfaceInfo> group in infos.GroupBy(i => i.InterfaceType))
foreach (IGrouping<INamedTypeSymbol, IMemberInfo> group in infos.GroupBy(i => i.InterfaceType))
{
List<AutoInterfaceInfo> items = group.ToList();
List<IMemberInfo> items = group.ToList();

foreach (ISymbol member in group.Key.GetMembers())
{
Expand All @@ -272,9 +270,9 @@ private bool ProcessClass(ISourceTextBuilder builder, GeneratorExecutionContext

if (separatorRequired)
{
builder.AppendLine();
output.Builder.AppendLine();
}
builder.AppendMethodDefinition(compilation, method, scope, group.Key, items);
output.AppendMethodDefinition(method, scope, group.Key, items);
separatorRequired = true;
}
}
Expand All @@ -284,10 +282,10 @@ private bool ProcessClass(ISourceTextBuilder builder, GeneratorExecutionContext

if (separatorRequired)
{
builder.AppendLine();
output.Builder.AppendLine();
}

builder.AppendPropertyDefinition(property, scope, group.Key, items);
output.AppendPropertyDefinition(property, scope, group.Key, items);
separatorRequired = true;
}
else if (member is IEventSymbol @event)
Expand All @@ -296,10 +294,10 @@ private bool ProcessClass(ISourceTextBuilder builder, GeneratorExecutionContext

if (separatorRequired)
{
builder.AppendLine();
output.Builder.AppendLine();
}

builder.AppendEventDefinition(@event, scope, group.Key, items);
output.AppendEventDefinition(@event, scope, group.Key, items);
separatorRequired = true;
}
else
Expand All @@ -310,25 +308,25 @@ private bool ProcessClass(ISourceTextBuilder builder, GeneratorExecutionContext
}
}

builder.DecrementIndentation();
builder.AppendIndentation();
builder.AppendLine("}");
output.Builder.DecrementIndentation();
output.Builder.AppendIndentation();
output.Builder.AppendLine('}');

for (int i = 0; i < containingTypes.Count; i++)
{
builder.DecrementIndentation();
builder.AppendIndentation();
builder.AppendLine("}");
output.Builder.DecrementIndentation();
output.Builder.AppendIndentation();
output.Builder.AppendLine('}');
}

if (namespaceParts.Length > 0)
{
builder.DecrementIndentation();
builder.AppendIndentation();
builder.AppendLine("}");
output.Builder.DecrementIndentation();
output.Builder.AppendIndentation();
output.Builder.AppendLine('}');
}

return anyReasonToEmitSourceFile;
return anyReasonToEmitSourceFile ? output.ToString() : null;
}

private static void ReportDiagnostic(GeneratorExecutionContext context, string id, string title, string message, string description, DiagnosticSeverity severity, SyntaxNode? node, params object?[] messageArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/beakona/AutoInterface</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>
<Version>1.0.6</Version>
<Version>1.0.7</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
136 changes: 0 additions & 136 deletions BeaKona.AutoInterfaceGenerator/CSharpBuildContext.cs

This file was deleted.

Loading

0 comments on commit 5b74f4c

Please sign in to comment.