Skip to content

Commit

Permalink
feat: use ToArray, rename variables
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Oct 10, 2024
1 parent 84881b7 commit eab1de1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
39 changes: 19 additions & 20 deletions InterfaceStubGenerator.Shared/Emitter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.Text;

namespace Refit.Generator;
Expand Down Expand Up @@ -116,24 +117,24 @@ partial class {model.Ns}{model.ClassDeclaration}
// Handle Refit Methods
foreach (var method in model.RefitMethods)
{
EmitRefitMethod(source, method, true, memberNames);
WriteRefitMethod(source, method, true, memberNames);
}

foreach (var method in model.DerivedRefitMethods)
{
EmitRefitMethod(source, method, false, memberNames);
WriteRefitMethod(source, method, false, memberNames);
}

// Handle non-refit Methods that aren't static or properties or have a method body
foreach (var method in model.NonRefitMethods)
{
ProcessNonRefitMethod(source, method);
WriteNonRefitMethod(source, method);
}

// Handle Dispose
if (model.DisposeMethod)
{
ProcessDisposableMethod(source);
WriteDisposableMethod(source);
}

source.Append(
Expand All @@ -155,7 +156,7 @@ partial class {model.Ns}{model.ClassDeclaration}
/// <param name="methodModel"></param>
/// <param name="isTopLevel">True if directly from the type we're generating for, false for methods found on base interfaces</param>
/// <param name="memberNames">Contains the unique member names in the interface scope.</param>
private static void EmitRefitMethod(
private static void WriteRefitMethod(
StringBuilder source,
MethodModel methodModel,
bool isTopLevel,
Expand Down Expand Up @@ -185,26 +186,24 @@ HashSet<string> memberNames
WriteMethodOpening(source, methodModel, !isTopLevel, isAsync);

// Build the list of args for the array
var argList = new List<string>();
foreach (var param in methodModel.Parameters)
{
argList.Add($"@{param.MetadataName}");
}
var argList = methodModel
.Parameters.AsArray()
.Select(static param => $"@{param.MetadataName}")
.ToArray();

// List of generic arguments
var genericList = new List<string>();
foreach (var typeParam in methodModel.Constraints)
{
genericList.Add($"typeof({typeParam.DeclaredName})");
}
var genericList = methodModel
.Constraints.AsArray()
.Select(static typeParam => $"typeof({typeParam.DeclaredName})")
.ToArray();

var argumentsArrayString =
argList.Count == 0
argList.Length == 0
? "global::System.Array.Empty<object>()"
: $"new object[] {{ {string.Join(", ", argList)} }}";

var genericString =
genericList.Count > 0
genericList.Length > 0
? $", new global::System.Type[] {{ {string.Join(", ", genericList)} }}"
: string.Empty;

Expand All @@ -220,7 +219,7 @@ HashSet<string> memberNames
WriteMethodClosing(source);
}

private static void ProcessNonRefitMethod(StringBuilder source, MethodModel methodModel)
private static void WriteNonRefitMethod(StringBuilder source, MethodModel methodModel)
{
WriteMethodOpening(source, methodModel, true);

Expand All @@ -236,7 +235,7 @@ private static void ProcessNonRefitMethod(StringBuilder source, MethodModel meth
// TODO: This assumes that the Dispose method is a void that takes no parameters.
// The previous version did not.
// Does the bool overload cause an issue here.
private static void ProcessDisposableMethod(StringBuilder source)
private static void WriteDisposableMethod(StringBuilder source)
{
source.Append(
"""
Expand Down
6 changes: 4 additions & 2 deletions InterfaceStubGenerator.Shared/ImmutableEquatableArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Refit.Generator;

public static class ImmutableEquatableArray
internal static class ImmutableEquatableArray
{
public static ImmutableEquatableArray<T> Empty<T>()
where T : IEquatable<T> => ImmutableEquatableArray<T>.Empty;
Expand All @@ -17,7 +17,7 @@ this IEnumerable<T>? values
/// Provides an immutable list implementation which implements sequence equality.
/// TODO: Convert to struct
/// </summary>
public sealed class ImmutableEquatableArray<T>
internal sealed class ImmutableEquatableArray<T>
: IEquatable<ImmutableEquatableArray<T>>,
IReadOnlyList<T>
where T : IEquatable<T>
Expand All @@ -32,6 +32,8 @@ public sealed class ImmutableEquatableArray<T>

public ImmutableEquatableArray(IEnumerable<T> values) => _values = values.ToArray();

public T[] AsArray() => _values;

public bool Equals(ImmutableEquatableArray<T>? other) =>
other != null && ((ReadOnlySpan<T>)_values).SequenceEqual(other._values);

Expand Down
38 changes: 24 additions & 14 deletions InterfaceStubGenerator.Shared/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ CancellationToken cancellationToken
foreach (var method in group)
{
// Get the symbol being declared by the method
var methodSymbol = model.GetDeclaredSymbol(method, cancellationToken: cancellationToken);
var methodSymbol = model.GetDeclaredSymbol(
method,
cancellationToken: cancellationToken
);
if (!IsRefitMethod(methodSymbol, httpMethodBaseAttributeSymbol))
continue;

Expand All @@ -89,11 +92,11 @@ CancellationToken cancellationToken
m => m.ContainingType,
SymbolEqualityComparer.Default
)
.ToDictionary<IGrouping<INamedTypeSymbol, IMethodSymbol>, INamedTypeSymbol, List<IMethodSymbol>>(
g => g.Key,
v => [.. v],
SymbolEqualityComparer.Default
);
.ToDictionary<
IGrouping<INamedTypeSymbol, IMethodSymbol>,
INamedTypeSymbol,
List<IMethodSymbol>
>(g => g.Key, v => [.. v], SymbolEqualityComparer.Default);

// Look through the candidate interfaces
var interfaceSymbols = new List<INamedTypeSymbol>();
Expand All @@ -103,7 +106,10 @@ CancellationToken cancellationToken
foreach (var iface in group)
{
// get the symbol belonging to the interface
var ifaceSymbol = model.GetDeclaredSymbol(iface, cancellationToken: cancellationToken);
var ifaceSymbol = model.GetDeclaredSymbol(
iface,
cancellationToken: cancellationToken
);

// See if we already know about it, might be a dup
if (ifaceSymbol is null || interfaces.ContainsKey(ifaceSymbol))
Expand Down Expand Up @@ -170,7 +176,11 @@ sealed class PreserveAttribute : global::System.Attribute
// Is it needed in order to get the preserve attribute display name.
// Will the compilation ever change this.
compilation = compilation.AddSyntaxTrees(
CSharpSyntaxTree.ParseText(SourceText.From(attributeText, Encoding.UTF8), options, cancellationToken: cancellationToken)
CSharpSyntaxTree.ParseText(
SourceText.From(attributeText, Encoding.UTF8),
options,
cancellationToken: cancellationToken
)
);

// get the newly bound attribute
Expand Down Expand Up @@ -266,7 +276,7 @@ bool nullableEnabled
.OfType<IMethodSymbol>()
.Except(refitMethods, SymbolEqualityComparer.Default)
.Cast<IMethodSymbol>()
.ToList();
.ToArray();

// get methods for all inherited
var derivedMethods = interfaceSymbol
Expand All @@ -288,11 +298,11 @@ bool nullableEnabled
// Pull out the refit methods from the derived types
var derivedRefitMethods = derivedMethods
.Where(m => IsRefitMethod(m, httpMethodBaseAttributeSymbol))
.ToList();
.ToArray();
var derivedNonRefitMethods = derivedMethods
.Except(derivedMethods, SymbolEqualityComparer.Default)
.Cast<IMethodSymbol>()
.ToList();
.ToArray();

var memberNames = interfaceSymbol
.GetMembers()
Expand Down Expand Up @@ -369,7 +379,7 @@ List<Diagnostic> diagnostics
diagnostics.Add(diagnostic);
}

return ParseMethod(methodSymbol, true);
return ParseMethod(methodSymbol, false);
}

private static bool IsRefitMethod(
Expand Down Expand Up @@ -476,7 +486,7 @@ private static bool ContainsTypeParameter(ITypeSymbol symbol)
return false;
}

private static MethodModel ParseMethod(IMethodSymbol methodSymbol, bool isExplicitInterface)
private static MethodModel ParseMethod(IMethodSymbol methodSymbol, bool isImplicitInterface)
{
var returnType = methodSymbol.ReturnType.ToDisplayString(
SymbolDisplayFormat.FullyQualifiedFormat
Expand All @@ -495,7 +505,7 @@ private static MethodModel ParseMethod(IMethodSymbol methodSymbol, bool isExplic

var parameters = methodSymbol.Parameters.Select(ParseParameter).ToImmutableEquatableArray();

var constraints = GenerateConstraints(methodSymbol.TypeParameters, !isExplicitInterface);
var constraints = GenerateConstraints(methodSymbol.TypeParameters, !isImplicitInterface);

return new MethodModel(
methodSymbol.Name,
Expand Down

0 comments on commit eab1de1

Please sign in to comment.