Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Sample/Common/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

public record Product(ProductId Id, string Name);

public readonly partial record struct ProductId : IStructId<Guid>;
public readonly partial record struct ProductId : IStructId<Guid>;
54 changes: 50 additions & 4 deletions src/StructId.Analyzer/AnalysisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ namespace StructId;

public static class AnalysisExtensions
{
public static SymbolDisplayFormat FullName { get; } = new(
public static SymbolDisplayFormat TypeName { get; } = new(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameOnly,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);

public static SymbolDisplayFormat NamespacedTypeName { get; } = new(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);

public static SymbolDisplayFormat FullNameNullable { get; } = new(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.ExpandNullable);

public static string ToFullName(this ISymbol symbol) => symbol.ToDisplayString(FullName);
public static string ToFullName(this ISymbol symbol) => symbol.ToDisplayString(FullNameNullable);

/// <summary>
/// Checks whether the <paramref name="this"/> type inherits or implements the
Expand Down Expand Up @@ -50,6 +58,44 @@ @this is INamedTypeSymbol namedActual &&
public static string GetStructIdNamespace(this AnalyzerConfigOptions options)
=> options.TryGetValue("build_property.StructIdNamespace", out var ns) && !string.IsNullOrEmpty(ns) ? ns : "StructId";

public static IncrementalValueProvider<string> GetStructIdNamespace(this IncrementalValueProvider<AnalyzerConfigOptionsProvider> options)
=> options.Select((x, _) => x.GlobalOptions.TryGetValue("build_property.StructIdNamespace", out var ns) ? ns : "StructId");

public static bool ImplementsExplicitly(this INamedTypeSymbol namedTypeSymbol, INamedTypeSymbol interfaceTypeSymbol)
{
if (interfaceTypeSymbol.IsUnboundGenericType && interfaceTypeSymbol.TypeParameters.Length == 1)
{
try
{
interfaceTypeSymbol = interfaceTypeSymbol.ConstructedFrom.Construct(namedTypeSymbol);
}
catch { }
}

foreach (var interfaceMember in interfaceTypeSymbol.GetMembers())
{
foreach (var classMember in namedTypeSymbol.GetMembers())
{
switch (classMember)
{
case IMethodSymbol methodSymbol:
if (methodSymbol.ExplicitInterfaceImplementations.Contains(interfaceMember, SymbolEqualityComparer.Default))
return true;
break;
case IPropertySymbol propertySymbol:
if (propertySymbol.ExplicitInterfaceImplementations.Contains(interfaceMember, SymbolEqualityComparer.Default))
return true;
break;
case IEventSymbol eventSymbol:
if (eventSymbol.ExplicitInterfaceImplementations.Contains(interfaceMember, SymbolEqualityComparer.Default))
return true;
break;
}
}
}
return false;
}

public static IEnumerable<INamedTypeSymbol> GetAllTypes(this Compilation compilation, bool includeReferenced = true)
=> compilation.Assembly
.GetAllTypes()
Expand Down Expand Up @@ -82,7 +128,7 @@ static IEnumerable<INamedTypeSymbol> GetAllTypes(INamespaceSymbol namespaceSymbo

public static string GetTypeName(this ITypeSymbol type, string? containingNamespace)
{
var typeName = type.ToDisplayString(FullName);
var typeName = type.ToDisplayString(FullNameNullable);
if (containingNamespace == null)
return typeName;

Expand All @@ -92,7 +138,7 @@ public static string GetTypeName(this ITypeSymbol type, string? containingNamesp
return typeName;
}

public static string ToFileName(this ITypeSymbol type) => type.ToDisplayString(FullName).Replace('+', '.');
public static string ToFileName(this ITypeSymbol type) => type.ToDisplayString(FullNameNullable).Replace('+', '.');

public static bool IsStructId(this ITypeSymbol type) => type.AllInterfaces.Any(x => x.Name == "IStructId");

Expand Down
3 changes: 1 addition & 2 deletions src/StructId.Analyzer/BaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ protected record struct TemplateArgs(string StructIdNamespace, INamedTypeSymbol

public virtual void Initialize(IncrementalGeneratorInitializationContext context)
{
var targetNamespace = context.AnalyzerConfigOptionsProvider
.Select((x, _) => x.GlobalOptions.TryGetValue("build_property.StructIdNamespace", out var ns) ? ns : "StructId");
var targetNamespace = context.AnalyzerConfigOptionsProvider.GetStructIdNamespace();

// Locate the required types
var types = context.CompilationProvider
Expand Down
3 changes: 1 addition & 2 deletions src/StructId.Analyzer/NewtonsoftJsonGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public override void Initialize(IncrementalGeneratorInitializationContext contex
context.RegisterSourceOutput(
context.CompilationProvider
.Select((x, _) => x.GetTypeByMetadataName("Newtonsoft.Json.JsonConverter`1"))
.Combine(context.AnalyzerConfigOptionsProvider
.Select((x, _) => x.GlobalOptions.TryGetValue("build_property.StructIdNamespace", out var ns) ? ns : "StructId")),
.Combine(context.AnalyzerConfigOptionsProvider.GetStructIdNamespace()),
(context, source) =>
{
if (source.Left == null)
Expand Down
Loading
Loading