Skip to content

Commit

Permalink
Use generator context extensions for source generation project
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed Mar 5, 2024
1 parent c5e2a43 commit 255dd95
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void AppendConverterCreatorBody()
for (var i = 0; i < elements.Length; i++)
{
var element = elements[i];
AppendAssignConverterExplicit(element, $"cvt{i}", GetConverterTypeFullName(i), GetTypeFullName(i));
AppendAssignConverterExplicit(element, $"cvt{i}", GetTypeFullName(i));
CancellationToken.ThrowIfCancellationRequested();
}
Output.AppendIndent(3, $"var converter = new {OutputConverterTypeName}(", ");", elements.Length, x => $"cvt{x}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void AppendConverterCreatorBody()
for (var i = 0; i < elements.Length; i++)
{
types.Add(GetTypeFullName(i));
AppendAssignConverterExplicit(elements[i], $"cvt{i}", GetConverterTypeFullName(i), GetTypeFullName(i));
AppendAssignConverterExplicit(elements[i], $"cvt{i}", GetTypeFullName(i));
CancellationToken.ThrowIfCancellationRequested();
}
Output.AppendIndent(3, $"var converter = Mikodev.Binary.Generator.Get{info.TypeName}Converter<{string.Join(", ", types)}>(", ");", elements.Length, x => $"cvt{x}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void AppendConverterCreatorBody()
{
var info = this.info;
var element = info.ElementType;
AppendAssignConverterExplicit(element, "cvt0", GetConverterTypeFullName(0), GetTypeFullName(0));
AppendAssignConverterExplicit(element, "cvt0", GetTypeFullName(0));
Output.AppendIndent(3, $"var converter = new {OutputConverterTypeName}(cvt0);");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void AppendConverterCreatorBody()
var members = this.members;
Output.AppendIndent(3, $"var names = new string[] {{ ", $" }};", members.Length, x => members[x].NamedKeyLiteral);
Output.AppendIndent(3, $"var optional = new bool[] {{ ", $" }};", members.Length, x => members[x].IsOptional ? "true" : "false");
Output.AppendIndent(3, $"var encoding = (Mikodev.Binary.Converter<string>)context.GetConverter(typeof(string));");
Output.AppendIndent(3, $"var encoding = Mikodev.Binary.GeneratorContextExtensions.GetConverter<string>(context);");
for (var i = 0; i < members.Length; i++)
{
var member = members[i];
Expand Down
8 changes: 4 additions & 4 deletions code/Binary.SourceGeneration/SymbolConverterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected SymbolConverterContext(SourceGeneratorContext context, SourceGenerator
var output = Symbols.GetOutputFullName(symbol);
this.context = context;
this.tracker = tracker;
this.fullNameCache = new Dictionary<int, (string, string)>();
this.fullNameCache = [];
Symbol = symbol;
SymbolTypeFullName = context.GetTypeFullName(symbol);
SymbolConverterTypeFullName = context.GetConverterTypeFullName(symbol);
Expand Down Expand Up @@ -90,9 +90,9 @@ protected void AppendAssignConverterExplicitConverterCreator(ITypeSymbol creator
CancellationToken.ThrowIfCancellationRequested();
}

protected void AppendAssignConverterExplicit(ITypeSymbol member, string variableName, string converterTypeAlias, string memberTypeAlias)
protected void AppendAssignConverterExplicit(ITypeSymbol member, string variableName, string memberTypeAlias)
{
Output.AppendIndent(3, $"var {variableName} = ({converterTypeAlias})context.GetConverter(typeof({memberTypeAlias}));");
Output.AppendIndent(3, $"var {variableName} = Mikodev.Binary.GeneratorContextExtensions.GetConverter<{memberTypeAlias}>(context);");
this.tracker.Invoke(member);
CancellationToken.ThrowIfCancellationRequested();
}
Expand All @@ -104,7 +104,7 @@ protected void AppendAssignConverter(SymbolMemberInfo member, string variableNam
else if (Symbols.GetConverterCreatorType(this.context, member.Symbol) is { } creator)
AppendAssignConverterExplicitConverterCreator(creator, variableName, converterTypeAlias, memberTypeAlias);
else
AppendAssignConverterExplicit(member.Type, variableName, converterTypeAlias, memberTypeAlias);
AppendAssignConverterExplicit(member.Type, variableName, memberTypeAlias);
}

protected abstract void Handle();
Expand Down

0 comments on commit 255dd95

Please sign in to comment.