minor code cleanup
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
internal static class AttributeDataExtensions
|
||||
{
|
||||
public static string? GetNamedParamValue(this AttributeData attributeData, string paramName)
|
||||
{
|
||||
var pair = attributeData.NamedArguments.FirstOrDefault(x => x.Key == paramName);
|
||||
return pair.Value.Value?.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
@@ -47,7 +46,9 @@ namespace InterfaceGenerator
|
||||
private void GenerateInterfaces(GeneratorExecutionContext context)
|
||||
{
|
||||
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var compilation = GetCompilation(context);
|
||||
InitAttributes(compilation);
|
||||
@@ -67,45 +68,35 @@ namespace InterfaceGenerator
|
||||
}
|
||||
}
|
||||
|
||||
private string GetVisibilityModifier(INamedTypeSymbol implTypeSymbol, AttributeData attributeData)
|
||||
private static string InferVisibilityModifier(ISymbol implTypeSymbol, AttributeData attributeData)
|
||||
{
|
||||
var pair = attributeData.NamedArguments.FirstOrDefault(x => x.Key == Attributes.VisibilityModifierPropName);
|
||||
string? result = pair.Value.Value?.ToString();
|
||||
|
||||
string? result = attributeData.GetNamedParamValue(Attributes.VisibilityModifierPropName);
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
return result;
|
||||
return result!;
|
||||
}
|
||||
|
||||
return implTypeSymbol.DeclaredAccessibility switch
|
||||
{
|
||||
Accessibility.Public => "public",
|
||||
var _ => "internal",
|
||||
_ => "internal",
|
||||
};
|
||||
}
|
||||
|
||||
private string GetInterfaceName(INamedTypeSymbol implTypeSymbol, AttributeData attributeData)
|
||||
private static string InferInterfaceName(ISymbol implTypeSymbol, AttributeData attributeData)
|
||||
{
|
||||
var pair = attributeData.NamedArguments.FirstOrDefault(x => x.Key == Attributes.InterfaceNamePropName);
|
||||
string? result = pair.Value.Value?.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return "I" + implTypeSymbol.Name;
|
||||
return attributeData.GetNamedParamValue(Attributes.InterfaceNamePropName) ?? $"I{implTypeSymbol.Name}";
|
||||
}
|
||||
|
||||
private string GenerateInterfaceCode(INamedTypeSymbol implTypeSymbol, AttributeData attributeData)
|
||||
{
|
||||
using var stream = new MemoryStream();
|
||||
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
|
||||
var codeWriter = new IndentedTextWriter(streamWriter, GeneratorConsts.Indent);
|
||||
var codeWriter = new IndentedTextWriter(streamWriter, " ");
|
||||
|
||||
var namespaceName = implTypeSymbol.ContainingNamespace.ToDisplayString();
|
||||
var interfaceName = GetInterfaceName(implTypeSymbol, attributeData);
|
||||
var visibilityModifier = GetVisibilityModifier(implTypeSymbol, attributeData);
|
||||
var interfaceName = InferInterfaceName(implTypeSymbol, attributeData);
|
||||
var visibilityModifier = InferVisibilityModifier(implTypeSymbol, attributeData);
|
||||
|
||||
codeWriter.WriteLine("namespace {0}", namespaceName);
|
||||
codeWriter.WriteLine("{");
|
||||
@@ -132,7 +123,7 @@ namespace InterfaceGenerator
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
private void WriteTypeGenericsIfNeeded(IndentedTextWriter writer, INamedTypeSymbol implTypeSymbol)
|
||||
private static void WriteTypeGenericsIfNeeded(TextWriter writer, INamedTypeSymbol implTypeSymbol)
|
||||
{
|
||||
if (!implTypeSymbol.IsGenericType)
|
||||
{
|
||||
@@ -146,16 +137,12 @@ namespace InterfaceGenerator
|
||||
WriteTypeParameterConstraints(writer, implTypeSymbol.TypeParameters);
|
||||
}
|
||||
|
||||
private void GenerateInterfaceMemberDefinitions(IndentedTextWriter writer, INamedTypeSymbol implTypeSymbol)
|
||||
private void GenerateInterfaceMemberDefinitions(TextWriter writer, INamespaceOrTypeSymbol implTypeSymbol)
|
||||
{
|
||||
foreach (var member in implTypeSymbol.GetMembers())
|
||||
{
|
||||
if (member.DeclaredAccessibility != Accessibility.Public)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (member.HasAttribute(_ignoreAttribute))
|
||||
if (member.DeclaredAccessibility != Accessibility.Public ||
|
||||
member.HasAttribute(_ignoreAttribute))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -164,7 +151,7 @@ namespace InterfaceGenerator
|
||||
}
|
||||
}
|
||||
|
||||
private static void GenerateInterfaceMemberDefinition(IndentedTextWriter writer, ISymbol member)
|
||||
private static void GenerateInterfaceMemberDefinition(TextWriter writer, ISymbol member)
|
||||
{
|
||||
switch (member)
|
||||
{
|
||||
@@ -177,7 +164,7 @@ namespace InterfaceGenerator
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteSymbolDocsIfPresent(IndentedTextWriter writer, ISymbol symbol)
|
||||
private static void WriteSymbolDocsIfPresent(TextWriter writer, ISymbol symbol)
|
||||
{
|
||||
var xml = symbol.GetDocumentationCommentXml();
|
||||
if (string.IsNullOrWhiteSpace(xml))
|
||||
@@ -208,13 +195,12 @@ namespace InterfaceGenerator
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsPublicOrInternal(IMethodSymbol methodSymbol)
|
||||
private static bool IsPublicOrInternal(ISymbol symbol)
|
||||
{
|
||||
return methodSymbol.DeclaredAccessibility == Accessibility.Public ||
|
||||
methodSymbol.DeclaredAccessibility == Accessibility.Internal;
|
||||
return symbol.DeclaredAccessibility is Accessibility.Public or Accessibility.Internal;
|
||||
}
|
||||
|
||||
private static void GeneratePropertyDefinition(IndentedTextWriter writer, IPropertySymbol propertySymbol)
|
||||
private static void GeneratePropertyDefinition(TextWriter writer, IPropertySymbol propertySymbol)
|
||||
{
|
||||
if (propertySymbol.IsStatic)
|
||||
{
|
||||
@@ -267,7 +253,7 @@ namespace InterfaceGenerator
|
||||
writer.WriteLine("}");
|
||||
}
|
||||
|
||||
private static void GenerateMethodDefinition(IndentedTextWriter writer, IMethodSymbol methodSymbol)
|
||||
private static void GenerateMethodDefinition(TextWriter writer, IMethodSymbol methodSymbol)
|
||||
{
|
||||
if (methodSymbol.MethodKind != MethodKind.Ordinary || methodSymbol.IsStatic)
|
||||
{
|
||||
@@ -374,7 +360,7 @@ namespace InterfaceGenerator
|
||||
}
|
||||
|
||||
private static void WriteTypeParameterConstraints(
|
||||
IndentedTextWriter writer,
|
||||
TextWriter writer,
|
||||
IEnumerable<ITypeParameterSymbol> typeParameters)
|
||||
{
|
||||
foreach (var typeParameter in typeParameters)
|
||||
@@ -404,10 +390,10 @@ namespace InterfaceGenerator
|
||||
return receiver.CandidateTypes.Select(candidate => GetTypeSymbol(compilation, candidate));
|
||||
}
|
||||
|
||||
private static INamedTypeSymbol GetTypeSymbol(Compilation compilation, TypeDeclarationSyntax type)
|
||||
private static INamedTypeSymbol GetTypeSymbol(Compilation compilation, SyntaxNode type)
|
||||
{
|
||||
var model = compilation.GetSemanticModel(type.SyntaxTree);
|
||||
var typeSymbol = ModelExtensions.GetDeclaredSymbol(model, type)!;
|
||||
var typeSymbol = model.GetDeclaredSymbol(type)!;
|
||||
return (INamedTypeSymbol)typeSymbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
internal class GeneratorConsts
|
||||
{
|
||||
public const string Indent = " ";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user