Files
SwiftLint/Source/SwiftLintFramework/Extensions/SourceKittenDictionary+Swiftlint.swift
JP Simard fe5baca7cd Migrate to use SourceKitten's new ByteCount/ByteRange types (#3037)
New APIs were introduced in SourceKitten to allow for a more typesafe distinction between integers meaning NSString-based distances and byte-based distances.

* https://github.com/jpsim/SourceKitten/pull/639
* https://github.com/jpsim/SourceKitten/pull/642

This PR migrates SwiftLint's use of those APIs.
2020-01-16 15:18:37 -08:00

45 lines
1.5 KiB
Swift

import SourceKittenFramework
extension SourceKittenDictionary {
/// Returns array of tuples containing "key.kind" and "byteRange" from Structure
/// that contains the byte offset. Returns all kinds if no parameter specified.
///
/// - parameter byteOffset: Int?
///
/// - returns: The kinds and byte ranges.
internal func kinds(forByteOffset byteOffset: ByteCount? = nil)
-> [(kind: String, byteRange: ByteRange)] {
var results = [(kind: String, byteRange: ByteRange)]()
func parse(_ dictionary: SourceKittenDictionary) {
guard let range = dictionary.byteRange else {
return
}
if let byteOffset = byteOffset, !range.contains(byteOffset) {
return
}
if let kind = dictionary.kind {
results.append((kind: kind, byteRange: range))
}
dictionary.substructure.forEach(parse)
}
parse(self)
return results
}
internal func structures(forByteOffset byteOffset: ByteCount) -> [SourceKittenDictionary] {
var results = [SourceKittenDictionary]()
func parse(_ dictionary: SourceKittenDictionary) {
guard let byteRange = dictionary.byteRange, byteRange.contains(byteOffset) else {
return
}
results.append(dictionary)
dictionary.substructure.forEach(parse)
}
parse(self)
return results
}
}