Skip to content

Commit

Permalink
bug with dotted namespaces..
Browse files Browse the repository at this point in the history
  • Loading branch information
beakona committed Jun 18, 2021
1 parent 097ad55 commit 986e699
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 135 deletions.
4 changes: 2 additions & 2 deletions AutoInterfaceSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ private void LogDebug(string name)
}

[BeaKona.AutoInterface]
[BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.PropertyGetter, Filter = "Length", Language = "scriban", Body = "return 1;")]
[BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.Method, Filter = "Print(\\d)?", Body = "LogDebug(nameof({{interface}}.{{name}})); {{expression}};")]
//[BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.PropertyGetter, Filter = "Length", Language = "scriban", Body = "return 1;")]
//[BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.Method, Filter = "Print(\\d)?", Body = "LogDebug(nameof({{interface}}.{{name}})); {{expression}};")]
private readonly IPrintable? aspect1 = new PrinterV1();

//[BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.Method, Filter = "Print2", Body = "/* */")]
Expand Down
42 changes: 19 additions & 23 deletions BeaKona.AutoInterfaceGenerator/AutoInterfaceSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Initialize(GeneratorInitializationContext context)
public void Execute(GeneratorExecutionContext context)
{
using Stream icStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BeaKona.AutoInterfaceGenerator.InjectedCode.cs");
using StreamReader icReader = new StreamReader(icStream);
using StreamReader icReader = new(icStream);

SourceText txt = SourceText.From(icReader.ReadToEnd(), Encoding.UTF8);

Expand All @@ -50,7 +50,7 @@ public void Execute(GeneratorExecutionContext context)
if (compilation.GetTypeByMetadataName("BeaKona.AutoInterfaceAttribute") is INamedTypeSymbol autoInterfaceAttributeSymbol && compilation.GetTypeByMetadataName("BeaKona.AutoInterfaceTemplateAttribute") is INamedTypeSymbol autoInterfaceTemplateAttributeSymbol)
{
// loop over the candidates, and keep the ones that are actually annotated
List<AutoInterfaceRecord> records = new List<AutoInterfaceRecord>();
List<AutoInterfaceRecord> records = new();

foreach (MemberDeclarationSyntax candidate in receiver.Candidates)
{
Expand Down Expand Up @@ -125,7 +125,7 @@ public void Execute(GeneratorExecutionContext context)

if (referencesWithMissingInterface.Count > 0)
{
HashSet<INamedTypeSymbol> emitted = new HashSet<INamedTypeSymbol>();
HashSet<INamedTypeSymbol> emitted = new();
foreach (AutoInterfaceRecord itemWithMissingInterface in referencesWithMissingInterface)
{
if (emitted.Add(itemWithMissingInterface.InterfaceType))
Expand Down Expand Up @@ -161,7 +161,7 @@ public void Execute(GeneratorExecutionContext context)

//private static void GeneratePreview(GeneratorExecutionContext context, string name, string code)
//{
// StringBuilder output = new StringBuilder();
// StringBuilder output = new();
// output.AppendLine("namespace BeaKona.Output {");
// output.AppendLine($"public static class Debug_{name}");
// output.AppendLine("{");
Expand All @@ -173,8 +173,8 @@ public void Execute(GeneratorExecutionContext context)

private static List<AutoInterfaceRecord> CollectRecords(GeneratorExecutionContext context, ISymbol symbol, ITypeSymbol receiverType, INamedTypeSymbol autoInterfaceAttributeSymbol, INamedTypeSymbol autoInterfaceTemplateAttributeSymbol)
{
List<PartialTemplate> templateParts = new List<PartialTemplate>();
Dictionary<ISymbol, HashSet<INamedTypeSymbol>> danglingInterfaceTypesBySymbols = new Dictionary<ISymbol, HashSet<INamedTypeSymbol>>();
List<PartialTemplate> templateParts = new();
Dictionary<ISymbol, HashSet<INamedTypeSymbol>> danglingInterfaceTypesBySymbols = new();

foreach (AttributeData attribute in symbol.GetAttributes())
{
Expand Down Expand Up @@ -353,7 +353,7 @@ private static List<AutoInterfaceRecord> CollectRecords(GeneratorExecutionContex
}
}

List<AutoInterfaceRecord> records = new List<AutoInterfaceRecord>();
List<AutoInterfaceRecord> records = new();

foreach (AttributeData attribute in symbol.GetAttributes())
{
Expand Down Expand Up @@ -513,24 +513,20 @@ private static List<AutoInterfaceRecord> CollectRecords(GeneratorExecutionContex

private static string? ProcessClass(GeneratorExecutionContext context, Compilation compilation, INamedTypeSymbol type, IEnumerable<IMemberInfo> infos)
{
ScopeInfo scope = new ScopeInfo(type);
ScopeInfo scope = new(type);

SourceBuilder builder = new SourceBuilder();
SourceBuilder builder = new();

ICodeTextWriter writer = new CSharpCodeTextWriter(context, compilation);
bool anyReasonToEmitSourceFile = false;
bool error = false;

//bool isNullable = compilation.Options.NullableContextOptions == NullableContextOptions.Enable;
builder.AppendLine("#nullable enable");
builder.AppendLine();
writer.WriteNamespaceBeginning(builder, type.ContainingNamespace);

ImmutableArray<INamespaceSymbol> namespaceParts = type.ContainingNamespace != null && type.ContainingNamespace.IsGlobalNamespace == false ? SemanticFacts.GetNamespaceParts(type.ContainingNamespace) : new ImmutableArray<INamespaceSymbol>();
if (namespaceParts.Length > 0)
{
writer.WriteNamespaceBeginning(builder, namespaceParts);
}

List<INamedTypeSymbol> containingTypes = new List<INamedTypeSymbol>();
List<INamedTypeSymbol> containingTypes = new();
for (INamedTypeSymbol? ct = type.ContainingType; ct != null; ct = ct.ContainingType)
{
containingTypes.Insert(0, ct);
Expand Down Expand Up @@ -584,35 +580,35 @@ private static List<AutoInterfaceRecord> CollectRecords(GeneratorExecutionContex
{
INamedTypeSymbol @interface = group.Key;

StandaloneModel model = new StandaloneModel();
StandaloneModel model = new();

model.Load(writer, builder, @interface, scope, references);

MethodModel CreateMethod(IMethodSymbol method)
{
MethodModel m = new MethodModel();
MethodModel m = new();
m.Load(writer, builder, method, scope, references);
return m;
}


PropertyModel CreateProperty(IPropertySymbol property)
{
PropertyModel m = new PropertyModel();
PropertyModel m = new();
m.Load(writer, builder, property, scope, references);
return m;
}

IndexerModel CreateIndexer(IPropertySymbol indexer)
{
IndexerModel m = new IndexerModel();
IndexerModel m = new();
m.Load(writer, builder, indexer, scope, references);
return m;
}

EventModel CreateEvent(IEventSymbol @event)
{
EventModel m = new EventModel();
EventModel m = new();
m.Load(writer, builder, @event, scope, references);
return m;
}
Expand Down Expand Up @@ -697,7 +693,7 @@ EventModel CreateEvent(IEventSymbol @event)
builder.AppendLine('}');
}

if (namespaceParts.Length > 0)
if (type.ContainingNamespace != null && type.ContainingNamespace.ConstituentNamespaces.Length > 0)
{
builder.DecrementIndentation();
builder.AppendIndentation();
Expand All @@ -713,7 +709,7 @@ EventModel CreateEvent(IEventSymbol @event)
/// </summary>
private class SyntaxReceiver : ISyntaxReceiver
{
public List<MemberDeclarationSyntax> Candidates { get; } = new List<MemberDeclarationSyntax>();
public List<MemberDeclarationSyntax> Candidates { get; } = new();

/// <summary>
/// Called for every syntax node in the compilation, we can inspect the nodes and save any information useful for generation
Expand Down
Loading

0 comments on commit 986e699

Please sign in to comment.