8
8
using Microsoft . CodeAnalysis . CSharp . Syntax ;
9
9
using PlantUmlClassDiagramGenerator . Attributes ;
10
10
11
- namespace PlantUmlClassDiagramGenerator . Library ;
11
+ namespace PlantUmlClassDiagramGenerator . Library . ClassDiagramGenerator ;
12
12
13
13
public class ClassDiagramGenerator (
14
14
TextWriter writer ,
15
15
string indent ,
16
16
Accessibilities ignoreMemberAccessibilities = Accessibilities . None ,
17
17
bool createAssociation = true ,
18
18
bool attributeRequired = false ,
19
- bool excludeUmlBeginEndTags = false ) : CSharpSyntaxWalker
19
+ bool excludeUmlBeginEndTags = false ,
20
+ bool addPackageTags = false ) : CSharpSyntaxWalker
20
21
{
21
22
private readonly HashSet < string > types = [ ] ;
22
23
private readonly List < SyntaxNode > additionalTypeDeclarationNodes = [ ] ;
23
24
private readonly Accessibilities ignoreMemberAccessibilities = ignoreMemberAccessibilities ;
24
- private readonly RelationshipCollection relationships = new ( ) ;
25
+ public readonly RelationshipCollection relationships = new ( ) ;
25
26
private readonly TextWriter writer = writer ;
26
27
private readonly string indent = indent ;
27
28
private int nestingDepth = 0 ;
28
29
private readonly bool createAssociation = createAssociation ;
29
30
private readonly bool attributeRequired = attributeRequired ;
30
31
private readonly bool excludeUmlBeginEndTags = excludeUmlBeginEndTags ;
32
+ private readonly bool addPackageTags = addPackageTags ;
31
33
private readonly Dictionary < string , string > escapeDictionary = new ( )
32
34
{
33
35
{ @"(?<before>[^{]){(?<after>{[^{])" , "${before}{${after}" } ,
@@ -45,7 +47,24 @@ public void GenerateInternal(SyntaxNode root)
45
47
{
46
48
Visit ( root ) ;
47
49
GenerateAdditionalTypeDeclarations ( ) ;
48
- GenerateRelationships ( ) ;
50
+ if ( ! this . addPackageTags )
51
+ GenerateRelationships ( ) ;
52
+ }
53
+
54
+ public override void VisitFileScopedNamespaceDeclaration ( FileScopedNamespaceDeclarationSyntax node )
55
+ {
56
+ if ( this . addPackageTags )
57
+ VisitFileScopedNamespaceDeclaration ( node , ( ) => base . VisitFileScopedNamespaceDeclaration ( node ) ) ;
58
+ else
59
+ base . VisitFileScopedNamespaceDeclaration ( node ) ;
60
+ }
61
+
62
+ public override void VisitNamespaceDeclaration ( NamespaceDeclarationSyntax node )
63
+ {
64
+ if ( this . addPackageTags )
65
+ VisitNamespaceDeclaration ( node , ( ) => base . VisitNamespaceDeclaration ( node ) ) ;
66
+ else
67
+ base . VisitNamespaceDeclaration ( node ) ;
49
68
}
50
69
51
70
public override void VisitInterfaceDeclaration ( InterfaceDeclarationSyntax node )
@@ -371,7 +390,7 @@ private void WriteLine(string line)
371
390
private bool SkipInnerTypeDeclaration ( SyntaxNode node )
372
391
{
373
392
if ( nestingDepth <= 0 ) return false ;
374
-
393
+ if ( nestingDepth == 1 && addPackageTags ) return false ;
375
394
additionalTypeDeclarationNodes . Add ( node ) ;
376
395
return true ;
377
396
}
@@ -411,6 +430,47 @@ private void GenerateRelationships()
411
430
WriteLine ( relationship . ToString ( ) ) ;
412
431
}
413
432
}
433
+
434
+ public static string [ ] GenerateRelationships ( RelationshipCollection relationshipCollection )
435
+ {
436
+ List < string > strings = new List < string > ( ) ;
437
+ foreach ( var relationship in relationshipCollection )
438
+ {
439
+ strings . AddRange ( relationshipCollection . Select ( r => r . ToString ( ) ) ) ;
440
+ }
441
+
442
+ return strings . ToArray ( ) ;
443
+ }
444
+
445
+ private void VisitFileScopedNamespaceDeclaration ( FileScopedNamespaceDeclarationSyntax node , Action visitBase )
446
+ {
447
+ if ( attributeRequired && ! node . AttributeLists . HasDiagramAttribute ( ) ) { return ; }
448
+ if ( node . AttributeLists . HasIgnoreAttribute ( ) ) { return ; }
449
+ if ( SkipInnerTypeDeclaration ( node ) ) { return ; }
450
+
451
+ var typeName = NamespaceNameText . From ( node ) ;
452
+
453
+ WriteLine ( $ "package \" { typeName . Identifier } \" {{") ;
454
+ nestingDepth ++ ;
455
+ visitBase ( ) ;
456
+ nestingDepth -- ;
457
+ WriteLine ( "}" ) ;
458
+ }
459
+
460
+ private void VisitNamespaceDeclaration ( NamespaceDeclarationSyntax node , Action visitBase )
461
+ {
462
+ if ( attributeRequired && ! node . AttributeLists . HasDiagramAttribute ( ) ) { return ; }
463
+ if ( node . AttributeLists . HasIgnoreAttribute ( ) ) { return ; }
464
+ if ( SkipInnerTypeDeclaration ( node ) ) { return ; }
465
+
466
+ var typeName = NamespaceNameText . From ( node ) ;
467
+
468
+ WriteLine ( $ "package \" { typeName . Identifier } \" {{") ;
469
+ nestingDepth ++ ;
470
+ visitBase ( ) ;
471
+ nestingDepth -- ;
472
+ WriteLine ( "}" ) ;
473
+ }
414
474
415
475
private void VisitTypeDeclaration ( TypeDeclarationSyntax node , Action visitBase )
416
476
{
0 commit comments