Skip to content

Commit 347cc4b

Browse files
committed
Add __sourcekitd_variant_*_apply
It is hard to pass multiple Swift objects in context parameter on calling sourcekitd's `*_apply_f` functions. So, I added `*_apply` compatible functions that passing Swift closure as context and calling them in C function.
1 parent 7a45891 commit 347cc4b

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

Source/SourceKittenFramework/SourceKitVariant.swift

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,19 @@ extension SourceKitVariant {
200200
switch sourcekitd_variant_get_type(sourcekitObject) {
201201
case SOURCEKITD_VARIANT_TYPE_ARRAY:
202202
var array = [SourceKitVariant]()
203-
_ = sourcekitd_variant_array_apply(sourcekitObject) { index, value in
203+
_ = __sourcekitd_variant_array_apply(sourcekitObject) { index, value in
204204
array.insert(SourceKitVariant(variant: value, response: response), at:Int(index))
205205
return true
206206
}
207207
self = .array(array)
208208
case SOURCEKITD_VARIANT_TYPE_DICTIONARY:
209209
var count: Int = 0
210-
_ = sourcekitd_variant_dictionary_apply(sourcekitObject) { _, _ in
210+
_ = __sourcekitd_variant_dictionary_apply(sourcekitObject) { _, _ in
211211
count += 1
212212
return true
213213
}
214214
var dictionary = [String:SourceKitVariant](minimumCapacity: count)
215-
_ = sourcekitd_variant_dictionary_apply(sourcekitObject) { key, value in
215+
_ = __sourcekitd_variant_dictionary_apply(sourcekitObject) { key, value in
216216
if let key = String(sourceKitUID: key!) {
217217
dictionary[key] = SourceKitVariant(variant: value, response: response)
218218
}
@@ -445,3 +445,40 @@ extension SourceKitVariant {
445445
var attributes: [SourceKitVariant]? { return self["key.attributes"]?.array }
446446
var attribute: SourceKitVariant? { return self["key.attribute"] }
447447
}
448+
449+
// MARK: - sourcekitd_variant_*_apply
450+
// It is hard to pass multiple Swift objects in context parameter on calling
451+
// sourcekitd's `*_apply_f` functions.
452+
// So, I added `*_apply` compatible functions that passing Swift closure as
453+
// context and calling them in C function.
454+
func __sourcekitd_variant_array_apply(
455+
_ array: sourcekitd_variant_t,
456+
_ applier: @escaping (Int, sourcekitd_variant_t) -> Bool) -> Bool {
457+
typealias array_applier = (Int, sourcekitd_variant_t) -> Bool
458+
var applier = applier
459+
return withUnsafeMutablePointer(to: &applier) { context in
460+
sourcekitd_variant_array_apply_f(array, { index, value, context in
461+
if let context = context {
462+
let applier = context.assumingMemoryBound(to: array_applier.self).pointee
463+
return applier(index, value)
464+
}
465+
return true
466+
}, context)
467+
}
468+
}
469+
470+
func __sourcekitd_variant_dictionary_apply(
471+
_ dict: sourcekitd_variant_t,
472+
_ applier: @escaping (sourcekitd_uid_t?, sourcekitd_variant_t) -> Bool) -> Bool {
473+
typealias dictionary_applier = (sourcekitd_uid_t?, sourcekitd_variant_t) -> Bool
474+
var applier = applier
475+
return withUnsafeMutablePointer(to: &applier) { context in
476+
sourcekitd_variant_dictionary_apply_f(dict, { key, value, context in
477+
if let context = context {
478+
let applier = context.assumingMemoryBound(to: dictionary_applier.self).pointee
479+
return applier(key, value)
480+
}
481+
return true
482+
}, context)
483+
}
484+
}

0 commit comments

Comments
 (0)