@@ -17,12 +17,17 @@ import sourcekitd
17
17
public class SourceKitdResponse : CustomStringConvertible {
18
18
19
19
public struct Dictionary : CustomStringConvertible , CustomReflectable {
20
+ // The lifetime of this sourcekitd_variant_t is tied to the response it came
21
+ // from, so keep a reference to the response too.
20
22
private let dict : sourcekitd_variant_t
23
+ private let context : SourceKitdResponse
21
24
22
- public init ( dict: sourcekitd_variant_t ) {
25
+
26
+ public init ( dict: sourcekitd_variant_t , context: SourceKitdResponse ) {
23
27
assert ( sourcekitd_variant_get_type ( dict) . rawValue ==
24
28
SOURCEKITD_VARIANT_TYPE_DICTIONARY . rawValue)
25
29
self . dict = dict
30
+ self . context = context
26
31
}
27
32
28
33
public func getString( _ key: SourceKitdUID ) -> String {
@@ -47,12 +52,12 @@ public class SourceKitdResponse: CustomStringConvertible {
47
52
48
53
public func getArray( _ key: SourceKitdUID ) -> Array {
49
54
let value = sourcekitd_variant_dictionary_get_value ( dict, key. uid)
50
- return Array ( arr: value)
55
+ return Array ( arr: value, context : context )
51
56
}
52
57
53
58
public func getDictionary( _ key: SourceKitdUID ) -> Dictionary {
54
59
let value = sourcekitd_variant_dictionary_get_value ( dict, key. uid)
55
- return Dictionary ( dict: value)
60
+ return Dictionary ( dict: value, context : context )
56
61
}
57
62
58
63
public func getOptional( _ key: SourceKitdUID ) -> Variant ? {
@@ -61,7 +66,7 @@ public class SourceKitdResponse: CustomStringConvertible {
61
66
SOURCEKITD_VARIANT_TYPE_NULL . rawValue {
62
67
return nil
63
68
}
64
- return Variant ( val: value)
69
+ return Variant ( val: value, context : context )
65
70
}
66
71
67
72
public var description : String {
@@ -74,17 +79,21 @@ public class SourceKitdResponse: CustomStringConvertible {
74
79
}
75
80
76
81
public struct Array : CustomStringConvertible {
82
+ // The lifetime of this sourcekitd_variant_t is tied to the response it came
83
+ // from, so keep a reference to the response too.
77
84
private let arr : sourcekitd_variant_t
85
+ private let context : SourceKitdResponse
78
86
79
87
public var count : Int {
80
88
let count = sourcekitd_variant_array_get_count ( arr)
81
89
return Int ( count)
82
90
}
83
91
84
- public init ( arr: sourcekitd_variant_t ) {
92
+ public init ( arr: sourcekitd_variant_t , context : SourceKitdResponse ) {
85
93
assert ( sourcekitd_variant_get_type ( arr) . rawValue ==
86
94
SOURCEKITD_VARIANT_TYPE_ARRAY . rawValue)
87
95
self . arr = arr
96
+ self . context = context
88
97
}
89
98
90
99
public func getString( _ index: Int ) -> String {
@@ -109,20 +118,21 @@ public class SourceKitdResponse: CustomStringConvertible {
109
118
110
119
public func getArray( _ index: Int ) -> Array {
111
120
let value = sourcekitd_variant_array_get_value ( arr, index)
112
- return Array ( arr: value)
121
+ return Array ( arr: value, context : context )
113
122
}
114
123
115
124
public func getDictionary( _ index: Int ) -> Dictionary {
116
125
let value = sourcekitd_variant_array_get_value ( arr, index)
117
- return Dictionary ( dict: value)
126
+ return Dictionary ( dict: value, context : context )
118
127
}
119
128
120
129
public func enumerate( _ applier: ( _ index: Int , _ value: Variant ) -> Bool ) {
121
130
// The block passed to sourcekit_variant_array_apply() does not actually
122
131
// escape, it's synchronous and not called after returning.
132
+ let context = self . context
123
133
withoutActuallyEscaping ( applier) { escapingApplier in
124
134
_ = sourcekitd_variant_array_apply ( arr) { ( index, elem) -> Bool in
125
- return escapingApplier ( Int ( index) , Variant ( val: elem) )
135
+ return escapingApplier ( Int ( index) , Variant ( val: elem, context : context ) )
126
136
}
127
137
}
128
138
}
@@ -134,10 +144,14 @@ public class SourceKitdResponse: CustomStringConvertible {
134
144
}
135
145
136
146
public struct Variant : CustomStringConvertible {
147
+ // The lifetime of this sourcekitd_variant_t is tied to the response it came
148
+ // from, so keep a reference to the response too.
137
149
private let val : sourcekitd_variant_t
150
+ fileprivate let context : SourceKitdResponse
138
151
139
- fileprivate init ( val: sourcekitd_variant_t ) {
152
+ fileprivate init ( val: sourcekitd_variant_t , context : SourceKitdResponse ) {
140
153
self . val = val
154
+ self . context = context
141
155
}
142
156
143
157
public func getString( ) -> String {
@@ -167,11 +181,11 @@ public class SourceKitdResponse: CustomStringConvertible {
167
181
}
168
182
169
183
public func getArray( ) -> Array {
170
- return Array ( arr: val)
184
+ return Array ( arr: val, context : context )
171
185
}
172
186
173
187
public func getDictionary( ) -> Dictionary {
174
- return Dictionary ( dict: val)
188
+ return Dictionary ( dict: val, context : context )
175
189
}
176
190
177
191
public var description : String {
@@ -182,7 +196,7 @@ public class SourceKitdResponse: CustomStringConvertible {
182
196
private let resp : sourcekitd_response_t
183
197
184
198
public var value : Dictionary {
185
- return Dictionary ( dict: sourcekitd_response_get_value ( resp) )
199
+ return Dictionary ( dict: sourcekitd_response_get_value ( resp) , context : self )
186
200
}
187
201
188
202
/// Copies the raw bytes of the JSON description of this documentation item.
0 commit comments