@@ -2,135 +2,124 @@ import Foundation
22import SwiftSemantics
33import struct SwiftSemantics. Protocol
44
5- public final class Interface : Codable {
5+ public final class Interface {
66 public let imports : [ Import ]
77 public let symbols : [ Symbol ]
88
99 public required init ( imports: [ Import ] , symbols: [ Symbol ] ) {
1010 self . imports = imports
1111 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- } ( )
1912
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 }
2316
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 } )
2719
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
3226
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+ }
3537
36- for baseClass in baseClasses {
37- var superclasses = Set ( CollectionOfOne ( baseClass ) )
38+ relationships . insert ( Relationship ( subject : symbol , predicate : predicate , object : container ) )
39+ }
3840
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) } ) {
4543
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+ }
4851
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) )
6953 }
70- default :
71- predicate = . memberOf
7254 }
7355
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+ } )
7960
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)
8663 }
8764
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+ }
8985 }
9086 }
9187
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+ } ( )
9690
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+ }
10094
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: -
11696
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)
121113 }
122114 }
123115
124- return Array ( relationships)
125- } ( )
116+ return classClusters
126117
127- public private( set) lazy var relationshipsBySubject : [ Symbol . ID : [ Relationship ] ] = {
128- Dictionary ( grouping: relationships, by: { $0. subject. id } )
129- } ( )
118+ }
130119
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 ] ]
134123
135124 // MARK: -
136125
0 commit comments