Skip to content

Commit 7b3d1ed

Browse files
authored
Merge pull request #109 from arthurward/feature/filterRecords
Suppress the compiler-generated members of record types in source generator.
2 parents 87cdd74 + 4a6a830 commit 7b3d1ed

File tree

6 files changed

+28
-49
lines changed

6 files changed

+28
-49
lines changed

src/PlantUmlClassDiagramGenerator.SourceGenerator/Extensions/IMethodSymbolExtensions.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public static string GetName(this IMethodSymbol symbol)
1717
}
1818
public static string GetReturnTypeString(this IMethodSymbol symbol)
1919
{
20-
if(symbol.MethodKind is MethodKind.Constructor
21-
or MethodKind.SharedConstructor
20+
if (symbol.MethodKind is MethodKind.Constructor
21+
or MethodKind.SharedConstructor
2222
or MethodKind.StaticConstructor)
2323
{ return ""; }
2424
return symbol.ReturnsVoid ? " : void" : $" : {symbol.ReturnType.GetTypeName()}";
@@ -29,7 +29,7 @@ public static string GetModifiersString(this IMethodSymbol symbol)
2929
var modifiers = string.Join(" ",
3030
new[]
3131
{
32-
symbol.ContainingType.TypeKind is not TypeKind.Interface
32+
symbol.ContainingType.TypeKind is not TypeKind.Interface
3333
&& symbol.IsAbstract ? "{abstract}" : "",
3434
symbol.IsStatic ? "{static}" : "",
3535
symbol.IsSealed ? "<<sealed>>" : "",
@@ -49,4 +49,21 @@ public static string GetParametersString(this IMethodSymbol symbol)
4949
return string.Join(", ", symbol.Parameters
5050
.Select(param => $"{param.Name} : {param.Type.GetTypeName()}"));
5151
}
52+
53+
/// <summary>
54+
/// Determine if the given method is the sole explicit constructor for a
55+
/// record type.
56+
/// </summary>
57+
/// <param name="method">The method to inspect.</param>
58+
/// <returns>True if there are no other explicit constructors in the method's type.</returns>
59+
public static bool IsSoleRecordConstructor(this IMethodSymbol method)
60+
{
61+
var containingType = method.ContainingType;
62+
var explicitConstructors = containingType.GetMembers()
63+
.OfType<IMethodSymbol>()
64+
.Where(m => !m.IsImplicitlyDeclared && m.MethodKind is MethodKind.Constructor or MethodKind.SharedConstructor or MethodKind.StaticConstructor);
65+
66+
return containingType.IsRecord
67+
&& explicitConstructors.Count() == 1;
68+
}
5269
}

src/PlantUmlClassDiagramGenerator.SourceGenerator/PlantUmlDiagramBuilder.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ private void ProcessMembersSymbol(IImmutableSet<INamedTypeSymbol> symbols)
6262
}
6363
break;
6464
case IPropertySymbol propertySymbol:
65-
SetPropertyDeclaration(propertySymbol);
66-
SetPropertyAssociation(propertySymbol, symbols);
65+
// Skip compiler-generated properties.
66+
if (!propertySymbol.IsImplicitlyDeclared)
67+
{
68+
SetPropertyDeclaration(propertySymbol);
69+
SetPropertyAssociation(propertySymbol, symbols);
70+
}
6771
break;
6872
case IMethodSymbol methodSymbol:
69-
if (methodSymbol.MethodKind is not MethodKind.PropertyGet
73+
if (!methodSymbol.IsSoleRecordConstructor() // Only include constructor when there is more than one.
74+
&& methodSymbol.MethodKind is not MethodKind.PropertyGet
7075
and not MethodKind.PropertySet
7176
and not MethodKind.EventAdd
7277
and not MethodKind.EventRemove
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
@startuml Item
22
class Item <<record>> {
3-
+ Item(Name : string, Value : double)
4-
# <<readonly>> <<virtual>> EqualityContract : Type <<get>>
53
+ Name : string <<get>> <<set>>
64
+ Value : double <<get>> <<set>>
7-
+ <<override>> ToString() : string
8-
# <<virtual>> PrintMembers(builder : StringBuilder) : bool
9-
+ {static} operator !=(left : Item?, right : Item?) : bool
10-
+ {static} operator ==(left : Item?, right : Item?) : bool
11-
+ <<override>> GetHashCode() : int
12-
+ <<override>> Equals(obj : object?) : bool
13-
+ <<virtual>> Equals(other : Item?) : bool
14-
# Item(original : Item)
15-
+ Deconstruct(Name : string, Value : double) : void
165
}
176
@enduml
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
@startuml Parameters
22
class Parameters <<record>> {
3-
# <<readonly>> <<virtual>> EqualityContract : Type <<get>>
43
+ <<readonly>> X : int <<get>>
54
+ <<readonly>> Y : int <<get>>
6-
+ Parameters(x : int, y : int)
7-
+ Area() : int
8-
+ <<override>> ToString() : string
9-
# <<virtual>> PrintMembers(builder : StringBuilder) : bool
10-
+ {static} operator !=(left : Parameters?, right : Parameters?) : bool
11-
+ {static} operator ==(left : Parameters?, right : Parameters?) : bool
12-
+ <<override>> GetHashCode() : int
13-
+ <<override>> Equals(obj : object?) : bool
14-
+ <<virtual>> Equals(other : Parameters?) : bool
15-
# Parameters(original : Parameters)
165
}
176
"IEquatable`1" "<Parameters>" <|.. Parameters
187
@enduml
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
@startuml RecordA
22
class RecordA <<record>> {
3-
+ RecordA(Name : string, Value : int)
4-
# <<readonly>> <<virtual>> EqualityContract : Type <<get>>
53
+ Name : string <<get>> <<set>>
64
+ Value : int <<get>> <<set>>
7-
+ <<override>> ToString() : string
8-
# <<virtual>> PrintMembers(builder : StringBuilder) : bool
9-
+ {static} operator !=(left : RecordA?, right : RecordA?) : bool
10-
+ {static} operator ==(left : RecordA?, right : RecordA?) : bool
11-
+ <<override>> GetHashCode() : int
12-
+ <<override>> Equals(obj : object?) : bool
13-
+ <<virtual>> Equals(other : RecordA?) : bool
14-
# RecordA(original : RecordA)
15-
+ Deconstruct(Name : string, Value : int) : void
165
}
176
@enduml
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
@startuml RecordStruct
22
struct RecordStruct <<sealed>> <<record>> {
3-
+ RecordStruct(X : float, Y : float, Z : float)
43
+ X : float <<get>> <<set>>
54
+ Y : float <<get>> <<set>>
65
+ Z : float <<get>> <<set>>
7-
+ <<readonly>> <<override>> ToString() : string
8-
- <<readonly>> PrintMembers(builder : StringBuilder) : bool
9-
+ {static} operator !=(left : RecordStruct, right : RecordStruct) : bool
10-
+ {static} operator ==(left : RecordStruct, right : RecordStruct) : bool
11-
+ <<readonly>> <<override>> GetHashCode() : int
12-
+ <<readonly>> <<override>> Equals(obj : object) : bool
13-
+ <<readonly>> Equals(other : RecordStruct) : bool
14-
+ <<readonly>> Deconstruct(X : float, Y : float, Z : float) : void
15-
+ RecordStruct()
166
}
177
@enduml

0 commit comments

Comments
 (0)