@@ -32,12 +32,13 @@ enum TypeNamer {
32
32
}
33
33
// Drop any annotation
34
34
var type = type. replacingOccurrences ( of: " any " , with: " " )
35
- let removedCharacters = CharacterSet ( charactersIn: " ?[]():&, " )
35
+ let removedCharacters = CharacterSet ( charactersIn: " ?[]():& " )
36
36
type = type. components ( separatedBy: removedCharacters) . joined ( separator: " " )
37
37
let regex = try ! NSRegularExpression ( pattern: " <.*> " )
38
38
let nsString = type as NSString
39
39
if let match = regex. firstMatch ( in: type, range: . init( location: 0 , length: type. count) ) {
40
40
let range = match. range
41
+ let mainType = nsString. replacingCharacters ( in: match. range, with: " " )
41
42
if keepGenerics {
42
43
var genericName = nsString. substring (
43
44
with: . init( location: range. location + 1 , length: range. length - 2 )
@@ -47,18 +48,38 @@ enum TypeNamer {
47
48
. replacingOccurrences ( of: " , " , with: " _ " )
48
49
. replacingOccurrences ( of: " " , with: " " )
49
50
type = nsString. replacingCharacters ( in: match. range, with: " _ \( genericName) " )
51
+ } else if let suffix = Self . suffixedGenericTypes. first ( where: { mainType. hasSuffix ( $0) } ) {
52
+ let genericName = nsString. substring (
53
+ with: . init( location: range. location + 1 , length: range. length - 2 )
54
+ )
55
+ if let mainGeneric = genericName. components ( separatedBy: . init( charactersIn: " , " ) ) . first {
56
+ type = mainGeneric + suffix
57
+ } else {
58
+ type = mainType
59
+ }
50
60
} else {
51
- type = nsString . replacingCharacters ( in : match . range , with : " " )
61
+ type = mainType
52
62
}
63
+ } else {
64
+ type = type. components ( separatedBy: . init( charactersIn: " , " ) ) . joined ( separator: " " )
53
65
}
54
- if let dotIndex = type. firstIndex ( of: " . " ) {
66
+ if let dotIndex = type. lastIndex ( of: " . " ) {
55
67
let nameStart = type. index ( after: dotIndex)
56
- type = String ( type [ nameStart... ] )
68
+ let lastType = String ( type [ nameStart... ] )
69
+ // Types with a Factory subtype should keep the subject of the factory
70
+ if lastType == " Factory " {
71
+ let components = type. components ( separatedBy: . init( charactersIn: " . " ) )
72
+ type = components. suffix ( 2 ) . joined ( separator: " " )
73
+ } else {
74
+ type = lastType
75
+ }
57
76
}
58
77
59
78
return type
60
79
}
61
80
81
+ private static let suffixedGenericTypes = [ " Publisher " , " Subject " , " Provider " ]
82
+
62
83
static func isClosure( type: String ) -> Bool {
63
84
return type. contains ( " -> " )
64
85
}
0 commit comments