@@ -2,135 +2,124 @@ import Foundation
2
2
import SwiftSemantics
3
3
import struct SwiftSemantics. Protocol
4
4
5
- public final class Interface : Codable {
5
+ public final class Interface {
6
6
public let imports : [ Import ]
7
7
public let symbols : [ Symbol ]
8
8
9
9
public required init ( imports: [ Import ] , symbols: [ Symbol ] ) {
10
10
self . imports = imports
11
11
self . symbols = symbols. filter { $0. isPublic }
12
- }
13
-
14
- // MARK: -
15
-
16
- public lazy var symbolsGroupedByIdentifier : [ Symbol . ID : [ Symbol ] ] = {
17
- return Dictionary ( grouping: symbols, by: { $0. id } )
18
- } ( )
19
12
20
- public lazy var symbolsGroupedByQualifiedName : [ String : [ Symbol ] ] = {
21
- return Dictionary ( grouping: symbols, by: { $0. id. description } )
22
- } ( )
13
+ self . symbolsGroupedByIdentifier = Dictionary ( grouping : symbols , by : { $0 . id } )
14
+ self . symbolsGroupedByQualifiedName = Dictionary ( grouping: symbols, by: { $0. id. description } )
15
+ self . topLevelSymbols = symbols . filter { $0 . api is Type || $0 . id . pathComponents . isEmpty }
23
16
24
- public private( set) lazy var topLevelSymbols : [ Symbol ] = {
25
- return symbols. filter { $0. api is Type || $0. id. pathComponents. isEmpty }
26
- } ( )
17
+ self . relationships = {
18
+ let extensionsByExtendedType : [ String : [ Extension ] ] = Dictionary ( grouping: symbols. flatMap { $0. context. compactMap { $0 as? Extension } } , by: { $0. extendedType } )
27
19
28
- public private( set) lazy var baseClasses : [ Symbol ] = {
29
- return symbols. filter { $0. api is Class &&
30
- typesInherited ( by: $0) . isEmpty }
31
- } ( )
20
+ var relationships : Set < Relationship > = [ ]
21
+ for symbol in symbols {
22
+ let lastDeclarationScope = symbol. context. last ( where: { $0 is Extension || $0 is Symbol } )
23
+
24
+ if let container = lastDeclarationScope as? Symbol {
25
+ let predicate : Relationship . Predicate
32
26
33
- public private( set) lazy var classHierarchies : [ Symbol : Set < Symbol > ] = {
34
- var classClusters : [ Symbol : Set < Symbol > ] = [ : ]
27
+ switch container. api {
28
+ case is Protocol :
29
+ if symbol. api. modifiers. contains ( where: { $0. name == " optional " } ) {
30
+ predicate = . optionalRequirementOf
31
+ } else {
32
+ predicate = . requirementOf
33
+ }
34
+ default :
35
+ predicate = . memberOf
36
+ }
35
37
36
- for baseClass in baseClasses {
37
- var superclasses = Set ( CollectionOfOne ( baseClass ) )
38
+ relationships . insert ( Relationship ( subject : symbol , predicate : predicate , object : container ) )
39
+ }
38
40
39
- while !superclasses. isEmpty {
40
- let subclasses = Set ( superclasses. flatMap { typesInheriting ( from: $0) } . filter { $0. isPublic } )
41
- defer { superclasses = subclasses }
42
- classClusters [ baseClass, default: [ ] ] . formUnion ( subclasses)
43
- }
44
- }
41
+ if let `extension` = lastDeclarationScope as? Extension {
42
+ if let extended = symbols. first ( where: { $0. api is Type && $0. id. matches ( `extension`. extendedType) } ) {
45
43
46
- return classClusters
47
- } ( )
44
+ let predicate : Relationship . Predicate
45
+ switch extended. api {
46
+ case is Protocol :
47
+ predicate = . defaultImplementationOf
48
+ default :
49
+ predicate = . memberOf
50
+ }
48
51
49
- private lazy var extensionsByExtendedType : [ String : [ Extension ] ] = {
50
- return Dictionary ( grouping: symbols. flatMap { $0. context. compactMap { $0 as? Extension } } ) {
51
- $0. extendedType
52
- }
53
- } ( )
54
-
55
- public private( set) lazy var relationships : [ Relationship ] = {
56
- var relationships : Set < Relationship > = [ ]
57
- for symbol in symbols {
58
- let lastDeclarationScope = symbol. context. last ( where: { $0 is Extension || $0 is Symbol } )
59
-
60
- if let container = lastDeclarationScope as? Symbol {
61
- let predicate : Relationship . Predicate
62
-
63
- switch container. api {
64
- case is Protocol :
65
- if symbol. api. modifiers. contains ( where: { $0. name == " optional " } ) {
66
- predicate = . optionalRequirementOf
67
- } else {
68
- predicate = . requirementOf
52
+ relationships. insert ( Relationship ( subject: symbol, predicate: predicate, object: extended) )
69
53
}
70
- default :
71
- predicate = . memberOf
72
54
}
73
55
74
- relationships. insert ( Relationship ( subject: symbol, predicate: predicate, object: container) )
75
- }
76
-
77
- if let `extension` = lastDeclarationScope as? Extension {
78
- if let extended = symbols. first ( where: { $0. api is Type && $0. id. matches ( `extension`. extendedType) } ) {
56
+ if let type = symbol. api as? Type {
57
+ var inheritedTypeNames : Set < String > = [ ]
58
+ inheritedTypeNames. formUnion ( type. inheritance. flatMap { $0. split ( separator: " & " ) . map { $0. trimmingCharacters ( in: . whitespaces) }
59
+ } )
79
60
80
- let predicate : Relationship . Predicate
81
- switch extended. api {
82
- case is Protocol :
83
- predicate = . defaultImplementationOf
84
- default :
85
- predicate = . memberOf
61
+ for `extension` in extensionsByExtendedType [ symbol. id. description] ?? [ ] {
62
+ inheritedTypeNames. formUnion ( `extension`. inheritance)
86
63
}
87
64
88
- relationships. insert ( Relationship ( subject: symbol, predicate: predicate, object: extended) )
65
+ inheritedTypeNames = Set ( inheritedTypeNames. flatMap { $0. split ( separator: " & " ) . map { $0. trimmingCharacters ( in: . whitespaces) } } )
66
+
67
+ for name in inheritedTypeNames {
68
+ let inheritedTypes = symbols. filter ( { ( $0. api is Class || $0. api is Protocol ) && $0. id. description == name } )
69
+ if inheritedTypes. isEmpty {
70
+ let inherited = Symbol ( api: Unknown ( name: name) , context: [ ] , declaration: nil , documentation: nil , sourceLocation: nil )
71
+ relationships. insert ( Relationship ( subject: symbol, predicate: . conformsTo, object: inherited) )
72
+ } else {
73
+ for inherited in inheritedTypes {
74
+ let predicate : Relationship . Predicate
75
+ if symbol. api is Class , inherited. api is Class {
76
+ predicate = . inheritsFrom
77
+ } else {
78
+ predicate = . conformsTo
79
+ }
80
+
81
+ relationships. insert ( Relationship ( subject: symbol, predicate: predicate, object: inherited) )
82
+ }
83
+ }
84
+ }
89
85
}
90
86
}
91
87
92
- if let type = symbol. api as? Type {
93
- var inheritedTypeNames : Set < String > = [ ]
94
- inheritedTypeNames. formUnion ( type. inheritance. flatMap { $0. split ( separator: " & " ) . map { $0. trimmingCharacters ( in: . whitespaces) }
95
- } )
88
+ return Array ( relationships)
89
+ } ( )
96
90
97
- for `extension` in extensionsByExtendedType [ symbol . id . description ] ?? [ ] {
98
- inheritedTypeNames . formUnion ( `extension` . inheritance )
99
- }
91
+ self . relationshipsBySubject = Dictionary ( grouping : relationships , by : { $0 . subject . id } )
92
+ self . relationshipsByObject = Dictionary ( grouping : relationships , by : { $0 . object . id } )
93
+ }
100
94
101
- inheritedTypeNames = Set ( inheritedTypeNames. flatMap { $0. split ( separator: " & " ) . map { $0. trimmingCharacters ( in: . whitespaces) } } )
102
-
103
- for name in inheritedTypeNames {
104
- let inheritedTypes = symbols. filter ( { ( $0. api is Class || $0. api is Protocol ) && $0. id. description == name } )
105
- if inheritedTypes. isEmpty {
106
- let inherited = Symbol ( api: Unknown ( name: name) , context: [ ] , declaration: nil , documentation: nil , sourceLocation: nil )
107
- relationships. insert ( Relationship ( subject: symbol, predicate: . conformsTo, object: inherited) )
108
- } else {
109
- for inherited in inheritedTypes {
110
- let predicate : Relationship . Predicate
111
- if symbol. api is Class , inherited. api is Class {
112
- predicate = . inheritsFrom
113
- } else {
114
- predicate = . conformsTo
115
- }
95
+ // MARK: -
116
96
117
- relationships. insert ( Relationship ( subject: symbol, predicate: predicate, object: inherited) )
118
- }
119
- }
120
- }
97
+ public let symbolsGroupedByIdentifier : [ Symbol . ID : [ Symbol ] ]
98
+ public let symbolsGroupedByQualifiedName : [ String : [ Symbol ] ]
99
+ public let topLevelSymbols : [ Symbol ]
100
+ public var baseClasses : [ Symbol ] {
101
+ symbols. filter { $0. api is Class && typesInherited ( by: $0) . isEmpty }
102
+ }
103
+ public var classHierarchies : [ Symbol : Set < Symbol > ] {
104
+ var classClusters : [ Symbol : Set < Symbol > ] = [ : ]
105
+
106
+ for baseClass in baseClasses {
107
+ var superclasses = Set ( CollectionOfOne ( baseClass) )
108
+
109
+ while !superclasses. isEmpty {
110
+ let subclasses = Set ( superclasses. flatMap { typesInheriting ( from: $0) } . filter { $0. isPublic } )
111
+ defer { superclasses = subclasses }
112
+ classClusters [ baseClass, default: [ ] ] . formUnion ( subclasses)
121
113
}
122
114
}
123
115
124
- return Array ( relationships)
125
- } ( )
116
+ return classClusters
126
117
127
- public private( set) lazy var relationshipsBySubject : [ Symbol . ID : [ Relationship ] ] = {
128
- Dictionary ( grouping: relationships, by: { $0. subject. id } )
129
- } ( )
118
+ }
130
119
131
- public private ( set ) lazy var relationshipsByObject : [ Symbol . ID : [ Relationship ] ] = {
132
- Dictionary ( grouping : relationships , by : { $0 . object . id } )
133
- } ( )
120
+ public let relationships : [ Relationship ]
121
+ public let relationshipsBySubject : [ Symbol . ID : [ Relationship ] ]
122
+ public let relationshipsByObject : [ Symbol . ID : [ Relationship ] ]
134
123
135
124
// MARK: -
136
125
0 commit comments