SwiftLint/Source/SwiftLintFramework/Models/SwiftLintSyntaxMap.swift

42 lines
1.3 KiB
Swift

import Foundation
import SourceKittenFramework
public struct SwiftLintSyntaxMap {
public let value: SyntaxMap
public let tokens: [SwiftLintSyntaxToken]
public init(value: SyntaxMap) {
self.value = value
self.tokens = value.tokens.map(SwiftLintSyntaxToken.init)
}
/// Returns array of SyntaxTokens intersecting with byte range
///
/// - Parameter byteRange: byte based NSRange
internal func tokens(inByteRange byteRange: NSRange) -> [SwiftLintSyntaxToken] {
func intersect(_ token: SwiftLintSyntaxToken) -> Bool {
return token.range.intersects(byteRange)
}
func intersectsOrAfter(_ token: SwiftLintSyntaxToken) -> Bool {
return token.offset + token.length > byteRange.location
}
guard let startIndex = tokens.firstIndexAssumingSorted(where: intersectsOrAfter) else {
return []
}
let tokensAfterFirstIntersection = tokens
.lazy
.suffix(from: startIndex)
.prefix(while: { $0.offset < byteRange.upperBound })
.filter(intersect)
return Array(tokensAfterFirstIntersection)
}
internal func kinds(inByteRange byteRange: NSRange) -> [SyntaxKind] {
return tokens(inByteRange: byteRange).compactMap { $0.kind }
}
}