Add support for Property and Method attributes (#49)

* .

* .

* P en E

* .
This commit is contained in:
Stef Heyenrath
2022-12-17 13:36:41 +01:00
committed by GitHub
parent a1a283c8bb
commit e22cec1b62
19 changed files with 535 additions and 31 deletions
@@ -3,4 +3,6 @@ namespace ProxyInterfaceSourceGenerator.Constants;
internal static class InternalClassNames
{
public const string NullableAttribute = "System.Runtime.CompilerServices.NullableAttribute";
public const string AsyncStateMachineAttribute = "System.Runtime.CompilerServices.AsyncStateMachineAttribute";
}
@@ -7,11 +7,6 @@ internal static class MethodSymbolExtensions
public static string GetMethodNameWithOptionalTypeParameters(this IMethodSymbol method) =>
!method.IsGenericMethod ? method.Name : $"{method.Name}<{string.Join(", ", method.TypeParameters.Select(tp => tp.Name))}>";
public static bool IsPublic(this IMethodSymbol? methodSymbol)
{
return methodSymbol is { DeclaredAccessibility: Accessibility.Public };
}
//public static string GetWhereStatement(this IMethodSymbol method) =>
// !method.IsGenericMethod ? string.Empty : string.Join("", method.TypeParameters.Select(tp => tp.GetWhereConstraints()));
}
@@ -1,6 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using ProxyInterfaceSourceGenerator.Constants;
using ProxyInterfaceSourceGenerator.Enums;
namespace ProxyInterfaceSourceGenerator.Extensions;
@@ -9,16 +8,6 @@ internal static class ParameterSymbolExtensions
{
private const string ParameterValueNull = "null";
public static string GetAttributesPrefix(this IParameterSymbol ps)
{
var attributes = ps.GetAttributes()
.Where(a => !string.Equals(a.AttributeClass?.GetFullType(), InternalClassNames.NullableAttribute, StringComparison.OrdinalIgnoreCase))
.Select(a => $"[{a}]")
.ToArray();
return attributes.Any() ? $"{string.Join(" ", attributes)} " : string.Empty;
}
public static string GetRefPrefix(this IParameterSymbol ps)
{
return ps.RefKind switch
@@ -24,6 +24,4 @@ internal static class PropertySymbolExtensions
return (type!, property.GetSanitizedName(), $"{{ {get}{set}}}");
}
}
@@ -1,10 +1,36 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using ProxyInterfaceSourceGenerator.Constants;
namespace ProxyInterfaceSourceGenerator.Extensions;
internal static class SymbolExtensions
{
private static readonly string[] ExcludedAttributes =
{
InternalClassNames.AsyncStateMachineAttribute ,
InternalClassNames.NullableAttribute
};
public static string GetAttributesPrefix(this ISymbol symbol)
{
var attributes = symbol.GetAttributesAsList();
return attributes.Any() ? $"{string.Join(" ", attributes)} " : string.Empty;
}
public static IReadOnlyList<string> GetAttributesAsList(this ISymbol symbol)
{
return symbol
.GetAttributes()
.Where(a => a.AttributeClass.IsPublic() && !ExcludedAttributes.Contains(a.AttributeClass?.ToString(), StringComparer.OrdinalIgnoreCase))
.Select(a => $"[{a}]")
.ToArray();
}
public static bool IsPublic(this ISymbol? symbol) =>
symbol is { DeclaredAccessibility: Accessibility.Public };
public static bool IsKeywordOrReserved(this ISymbol symbol) =>
SyntaxFacts.GetKeywordKind(symbol.Name) != SyntaxKind.None || SyntaxFacts.GetContextualKeywordKind(symbol.Name) != SyntaxKind.None;
@@ -110,6 +110,11 @@ using System;
propertyName = $"this[{string.Join(", ", methodParameters)}]";
}
foreach (var attribute in property.GetAttributesAsList())
{
str.AppendLine($" {attribute}");
}
str.AppendLine($" {getterSetter.Value.PropertyType} {propertyName} {getterSetter.Value.GetSet}");
str.AppendLine();
}
@@ -125,6 +130,11 @@ using System;
var methodParameters = GetMethodParameters(method.Parameters, true);
var whereStatement = GetWhereStatementFromMethod(method);
foreach (var attribute in method.GetAttributesAsList())
{
str.AppendLine($" {attribute}");
}
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){whereStatement};");
str.AppendLine();
}
@@ -139,6 +149,12 @@ using System;
{
var ps = @event.First().Parameters.First();
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
foreach (var attribute in ps.GetAttributesAsList())
{
str.AppendLine($" {attribute}");
}
str.AppendLine($" event {type} {@event.Key.GetSanitizedName()};");
str.AppendLine();
}
@@ -177,6 +177,11 @@ using System;
set = setIsPublic ? $"set => {instancePropertyName} = value; " : string.Empty;
}
foreach (var attribute in property.GetAttributesAsList())
{
str.AppendLine($" {attribute}");
}
str.AppendLine($" public {overrideOrVirtual}{type} {propertyName} {{ {get}{set}}}");
str.AppendLine();
}
@@ -189,11 +194,6 @@ using System;
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol, proxyBaseClasses))
{
if (method.Name == "TryParse")
{
int y = 0;
}
var methodParameters = new List<string>();
var invokeParameters = new List<string>();
@@ -223,6 +223,11 @@ using System;
var whereStatement = GetWhereStatementFromMethod(method);
foreach (var attribute in method.GetAttributesAsList())
{
str.AppendLine($" {attribute}");
}
str.AppendLine($" public {overrideOrVirtual}{returnTypeAsString} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){whereStatement}");
str.AppendLine(" {");
foreach (var ps in method.Parameters)
@@ -247,9 +252,7 @@ using System;
var methodName = method.GetMethodNameWithOptionalTypeParameters();
var alternateReturnVariableName = $"result_{methodName.GetDeterministicHashCodeAsString()}";
string instance = !method.IsStatic ?
"_Instance" :
$"{targetClassSymbol.Symbol}";
string instance = !method.IsStatic ? "_Instance" : $"{targetClassSymbol.Symbol}";
if (returnTypeAsString == "void")
{
@@ -302,6 +305,12 @@ using System;
var name = @event.Key.GetSanitizedName();
var ps = @event.First().Parameters.First();
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
foreach (var attribute in ps.GetAttributesAsList())
{
str.AppendLine($" {attribute}");
}
str.Append($" public event {type} {name} {{");
if (@event.Any(e => e.MethodKind == MethodKind.EventAdd))