Skip to content

Commit b464639

Browse files
author
akravtsova
committed
+ new tag: removeSystemCollectionsAssociations
1 parent cd9eed2 commit b464639

File tree

6 files changed

+95
-26
lines changed

6 files changed

+95
-26
lines changed

src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator/ClassDiagramGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public partial class ClassDiagramGenerator(
1515
bool createAssociation = true,
1616
bool attributeRequired = false,
1717
bool excludeUmlBeginEndTags = false,
18-
bool addPackageTags = false) : CSharpSyntaxWalker
18+
bool addPackageTags = false,
19+
bool removeSystemCollectionsAssociations = false) : CSharpSyntaxWalker
1920
{
2021
private readonly HashSet<string> types = [];
2122
private readonly List<SyntaxNode> additionalTypeDeclarationNodes = [];
@@ -28,6 +29,7 @@ public partial class ClassDiagramGenerator(
2829
private readonly bool attributeRequired = attributeRequired;
2930
private readonly bool excludeUmlBeginEndTags = excludeUmlBeginEndTags;
3031
private readonly bool addPackageTags = addPackageTags;
32+
private readonly bool removeSystemCollectionsAssociations = removeSystemCollectionsAssociations;
3133
private readonly Dictionary<string, string> escapeDictionary = new()
3234
{
3335
{@"(?<before>[^{]){(?<after>{[^{])", "${before}&#123;${after}"},
Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System;
12
using System.Linq;
23
using System.Text.RegularExpressions;
34
using Microsoft.CodeAnalysis;
45
using Microsoft.CodeAnalysis.CSharp;
56
using Microsoft.CodeAnalysis.CSharp.Syntax;
7+
using PlantUmlClassDiagramGenerator.Attributes;
68

79
namespace PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator;
810

@@ -32,33 +34,59 @@ public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
3234
|| typeIgnoringNullable is PredefinedTypeSyntax
3335
|| isTypeParameterProp)
3436
{
35-
var modifiers = GetMemberModifiersText(node.Modifiers,
36-
isInterfaceMember: node.Parent.IsKind(SyntaxKind.InterfaceDeclaration));
37-
var name = node.Identifier.ToString();
38-
//Property does not have an accessor is an expression-bodied property. (get only)
39-
var accessorStr = "<<get>>";
40-
if (node.AccessorList != null)
41-
{
42-
var accessor = node.AccessorList.Accessors
43-
.Where(x => !x.Modifiers.Select(y => y.Kind()).Contains(SyntaxKind.PrivateKeyword))
44-
.Select(x => $"<<{(x.Modifiers.ToString() == "" ? "" : (x.Modifiers.ToString() + " "))}{x.Keyword}>>");
45-
accessorStr = string.Join(" ", accessor);
46-
}
47-
var useLiteralInit = node.Initializer?.Value?.Kind().ToString().EndsWith("LiteralExpression") ?? false;
48-
var initValue = useLiteralInit
49-
? (" = " + escapeDictionary.Aggregate(node.Initializer.Value.ToString(),
50-
(n, e) => Regex.Replace(n, e.Key, e.Value)))
51-
: "";
52-
53-
WriteLine($"{modifiers}{name} : {type} {accessorStr}{initValue}");
37+
FillAssociatedProperty(node, type);
5438
}
5539
else
5640
{
5741
if (type.GetType() == typeof(GenericNameSyntax))
5842
{
59-
additionalTypeDeclarationNodes.Add(type);
60-
}
61-
relationships.AddAssociationFrom(node, typeIgnoringNullable);
43+
if (this.removeSystemCollectionsAssociations)
44+
{
45+
var t = node.Type.ToString().Split('<')[0];
46+
if (!Enum.TryParse(t, out SystemCollectionsTypes _))
47+
additionalTypeDeclarationNodes.Add(type);
48+
else
49+
{
50+
FillAssociatedProperty(node, type);
51+
var s = node.Type.ToString();
52+
relationships.AddAssociationFrom(node, new PlantUmlAssociationAttribute()
53+
{
54+
Association = "o--",
55+
Name = s.Substring(s.IndexOf('<') + 1,s.LastIndexOf('>') - s.IndexOf('<') - 1)
56+
});
57+
}
58+
}
59+
else
60+
{
61+
additionalTypeDeclarationNodes.Add(type);
62+
relationships.AddAssociationFrom(node, typeIgnoringNullable);
63+
}
64+
} else
65+
relationships.AddAssociationFrom(node, typeIgnoringNullable);
6266
}
6367
}
68+
69+
private void FillAssociatedProperty(PropertyDeclarationSyntax node, TypeSyntax type)
70+
{
71+
var modifiers = GetMemberModifiersText(node.Modifiers,
72+
isInterfaceMember: node.Parent.IsKind(SyntaxKind.InterfaceDeclaration));
73+
var name = node.Identifier.ToString();
74+
//Property does not have an accessor is an expression-bodied property. (get only)
75+
var accessorStr = "<<get>>";
76+
if (node.AccessorList != null)
77+
{
78+
var accessor = node.AccessorList.Accessors
79+
.Where(x => !x.Modifiers.Select(y => y.Kind()).Contains(SyntaxKind.PrivateKeyword))
80+
.Select(x => $"<<{(x.Modifiers.ToString() == "" ? "" : (x.Modifiers.ToString() + " "))}{x.Keyword}>>");
81+
accessorStr = string.Join(" ", accessor);
82+
}
83+
84+
var useLiteralInit = node.Initializer?.Value?.Kind().ToString().EndsWith("LiteralExpression") ?? false;
85+
var initValue = useLiteralInit
86+
? (" = " + escapeDictionary.Aggregate(node.Initializer.Value.ToString(),
87+
(n, e) => Regex.Replace(n, e.Key, e.Value)))
88+
: "";
89+
90+
WriteLine($"{modifiers}{name} : {type} {accessorStr}{initValue}");
91+
}
6492
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace PlantUmlClassDiagramGenerator.Library;
2+
3+
public enum SystemCollectionsTypes
4+
{
5+
// classes
6+
ArrayList,
7+
BitArray,
8+
CaseInsensitiveComparer,
9+
CaseInsensitiveHashCodeProvider,
10+
CollectionBase,
11+
Comparer,
12+
DictionaryBase,
13+
Hashtable,
14+
Queue,
15+
ReadOnlyCollectionBase,
16+
SortedList,
17+
Stack,
18+
StructuralComparisons,
19+
20+
// structs
21+
DictionaryEntry,
22+
23+
// interfaces
24+
ICollection,
25+
IComparer,
26+
IDictionary,
27+
IDictionaryEnumerator,
28+
IEnumerable,
29+
IEnumerator,
30+
IEqualityComparer,
31+
IHashCodeProvider,
32+
IList,
33+
IStructuralComparable,
34+
IStructuralEquatable
35+
}

src/PlantUmlClassDiagramGenerator/Generator/PlantUmlFromDirGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public bool GeneratePlantUml(Dictionary<string, string> parameters)
8787
parameters.ContainsKey("-createAssociation"),
8888
parameters.ContainsKey("-attributeRequired"),
8989
excludeUmlBeginEndTags,
90-
parameters.ContainsKey("-addPackageTags"));
90+
parameters.ContainsKey("-addPackageTags"),
91+
parameters.ContainsKey("-removeSystemCollectionsAssociations"));
9192
gen.Generate(root);
9293
relationships.AddAll(gen.relationships);
9394
}

src/PlantUmlClassDiagramGenerator/Generator/PlantUmlFromFileGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public bool GeneratePlantUml(Dictionary<string, string> parameters)
5353
ignoreAcc,
5454
parameters.ContainsKey("-createAssociation"),
5555
parameters.ContainsKey("-attributeRequired"),
56-
parameters.ContainsKey("-excludeUmlBeginEndTags"));
56+
parameters.ContainsKey("-excludeUmlBeginEndTags"),
57+
false,
58+
parameters.ContainsKey("-removeSystemCollectionsAssociations"));
5759
gen.Generate(root);
5860
}
5961
catch (Exception e)

src/PlantUmlClassDiagramGenerator/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ enum OptionType
3232
["-allInOne"] = OptionType.Switch,
3333
["-attributeRequired"] = OptionType.Switch,
3434
["-excludeUmlBeginEndTags"] = OptionType.Switch,
35-
["-addPackageTags"] = OptionType.Switch
35+
["-addPackageTags"] = OptionType.Switch,
36+
["-removeSystemCollectionsAssociations"] = OptionType.Switch
3637
};
3738

3839
static int Main(string[] args)

0 commit comments

Comments
 (0)