@@ -13,16 +13,20 @@ public final class Interface: Codable {
13
13
14
14
// MARK: -
15
15
16
- private lazy var symbolsByIdentifier : [ Symbol . ID : [ Symbol ] ] = {
16
+ public lazy var symbolsGroupedByIdentifier : [ Symbol . ID : [ Symbol ] ] = {
17
17
return Dictionary ( grouping: symbols, by: { $0. id } )
18
18
} ( )
19
19
20
+ public lazy var symbolsGroupedByName : [ String : [ Symbol ] ] = {
21
+ return Dictionary ( grouping: symbols, by: { $0. name } )
22
+ } ( )
23
+
20
24
public private( set) lazy var topLevelSymbols : [ Symbol ] = {
21
- return symbols. filter { $0. declaration is Type || $0. id. pathComponents. isEmpty }
25
+ return symbols. filter { $0. api is Type || $0. id. pathComponents. isEmpty }
22
26
} ( )
23
27
24
28
public private( set) lazy var baseClasses : [ Symbol ] = {
25
- return symbols. filter { $0. declaration is Class &&
29
+ return symbols. filter { $0. api is Class &&
26
30
typesInherited ( by: $0) . isEmpty }
27
31
} ( )
28
32
@@ -42,6 +46,12 @@ public final class Interface: Codable {
42
46
return classClusters
43
47
} ( )
44
48
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
+
45
55
public private( set) lazy var relationships : [ Relationship ] = {
46
56
var relationships : Set < Relationship > = [ ]
47
57
for symbol in symbols {
@@ -50,9 +60,9 @@ public final class Interface: Codable {
50
60
if let container = symbol. context. compactMap ( { $0 as? Symbol } ) . last {
51
61
let predicate : Relationship . Predicate
52
62
53
- switch container. declaration {
63
+ switch container. api {
54
64
case is Protocol :
55
- if symbol. declaration . modifiers. contains ( where: { $0. name == " optional " } ) {
65
+ if symbol. api . modifiers. contains ( where: { $0. name == " optional " } ) {
56
66
predicate = . optionalRequirementOf
57
67
} else {
58
68
predicate = . requirementOf
@@ -65,9 +75,10 @@ public final class Interface: Codable {
65
75
}
66
76
67
77
if let `extension` = `extension` {
68
- for extended in symbols. filter ( { $0. declaration is Type && $0. id. matches ( `extension`. extendedType) } ) {
78
+ if let extended = symbols. first ( where: { $0. api is Type && $0. id. matches ( `extension`. extendedType) } ) {
79
+
69
80
let predicate : Relationship . Predicate
70
- switch extended. declaration {
81
+ switch extended. api {
71
82
case is Protocol :
72
83
predicate = . defaultImplementationOf
73
84
default :
@@ -78,17 +89,26 @@ public final class Interface: Codable {
78
89
}
79
90
}
80
91
81
- if let type = symbol. declaration as? Type {
82
- let inheritance = Set ( ( type. inheritance + ( `extension`? . inheritance ?? [ ] ) ) . flatMap { $0. split ( separator: " & " ) . map { $0. trimmingCharacters ( in: . whitespaces) } } )
83
- for name in inheritance {
84
- let inheritedTypes = symbols. filter ( { ( $0. declaration is Class || $0. declaration is Protocol ) && $0. id. matches ( name) } )
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
+ } )
96
+
97
+ for `extension` in extensionsByExtendedType [ symbol. id. description] ?? [ ] {
98
+ inheritedTypeNames. formUnion ( `extension`. inheritance)
99
+ }
100
+
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. matches ( name) } )
85
105
if inheritedTypes. isEmpty {
86
- let inherited = Symbol ( declaration : Unknown ( name: name) , context: [ ] , documentation: nil , sourceLocation: nil )
106
+ let inherited = Symbol ( api : Unknown ( name: name) , context: [ ] , declaration : nil , documentation: nil , sourceLocation: nil )
87
107
relationships. insert ( Relationship ( subject: symbol, predicate: . inheritsFrom, object: inherited) )
88
108
} else {
89
109
for inherited in inheritedTypes {
90
110
let predicate : Relationship . Predicate
91
- if symbol. declaration is Class , inherited. declaration is Class {
111
+ if symbol. api is Class , inherited. api is Class {
92
112
predicate = . inheritsFrom
93
113
} else {
94
114
predicate = . conformsTo
@@ -104,26 +124,26 @@ public final class Interface: Codable {
104
124
return Array ( relationships)
105
125
} ( )
106
126
107
- private lazy var relationshipsBySubject : [ Symbol . ID : [ Relationship ] ] = {
127
+ public private( set ) lazy var relationshipsBySubject : [ Symbol . ID : [ Relationship ] ] = {
108
128
Dictionary ( grouping: relationships, by: { $0. subject. id } )
109
129
} ( )
110
130
111
- private lazy var relationshipsByObject : [ Symbol . ID : [ Relationship ] ] = {
131
+ public private( set ) lazy var relationshipsByObject : [ Symbol . ID : [ Relationship ] ] = {
112
132
Dictionary ( grouping: relationships, by: { $0. object. id } )
113
133
} ( )
114
134
115
135
// MARK: -
116
136
117
137
public func members( of symbol: Symbol ) -> [ Symbol ] {
118
- return relationshipsByObject [ symbol. id] ? . filter { $0. predicate == . memberOf } . map { $0. subject } ?? [ ]
138
+ return relationshipsByObject [ symbol. id] ? . filter { $0. predicate == . memberOf } . map { $0. subject } . sorted ( ) ?? [ ]
119
139
}
120
140
121
141
public func requirements( of symbol: Symbol ) -> [ Symbol ] {
122
- return relationshipsByObject [ symbol. id] ? . filter { $0. predicate == . requirementOf } . map { $0. subject } ?? [ ]
142
+ return relationshipsByObject [ symbol. id] ? . filter { $0. predicate == . requirementOf } . map { $0. subject } . sorted ( ) ?? [ ]
123
143
}
124
144
125
145
public func optionalRequirements( of symbol: Symbol ) -> [ Symbol ] {
126
- return relationshipsByObject [ symbol. id] ? . filter { $0. predicate == . optionalRequirementOf } . map { $0. subject } ?? [ ]
146
+ return relationshipsByObject [ symbol. id] ? . filter { $0. predicate == . optionalRequirementOf } . map { $0. subject } . sorted ( ) ?? [ ]
127
147
}
128
148
129
149
public func typesInherited( by symbol: Symbol ) -> [ Symbol ] {
@@ -143,6 +163,6 @@ public final class Interface: Codable {
143
163
}
144
164
145
165
public func conditionalCounterparts( of symbol: Symbol ) -> [ Symbol ] {
146
- return symbolsByIdentifier [ symbol. id] ? . filter { $0 != symbol } ?? [ ]
166
+ return symbolsGroupedByIdentifier [ symbol. id] ? . filter { $0 != symbol } . sorted ( ) ?? [ ]
147
167
}
148
168
}
0 commit comments