Files
SwiftLint/Source/SwiftLintFramework/Models/SwiftLintSyntaxMap.swift
T
JP Simard 3a2bb15ca3 Make some SwiftLintFramework declarations public (#4945)
* Make some SwiftLintFramework declarations public

If built-in rules depend on them.

This will allow separating the rules from the core infrastructure in
separate modules in an upcoming PR.

It also helps formalize the API contract for what should be accessible
to rule implementations.

* Exclude extensions from jazzy
2023-04-26 16:37:25 -04:00

55 lines
1.9 KiB
Swift

import SourceKittenFramework
/// Represents a Swift file's syntax information.
public struct SwiftLintSyntaxMap {
/// The raw `SyntaxMap` obtained by SourceKitten.
public let value: SyntaxMap
/// The SwiftLint-specific syntax tokens for this syntax map.
public let tokens: [SwiftLintSyntaxToken]
/// Creates a `SwiftLintSyntaxMap` from the raw `SyntaxMap` obtained by SourceKitten.
///
/// - parameter value: The raw `SyntaxMap` obtained by SourceKitten.
public init(value: SyntaxMap) {
self.value = value
self.tokens = value.tokens.map(SwiftLintSyntaxToken.init)
}
/// Returns array of syntax tokens intersecting with byte range.
///
/// - parameter byteRange: Byte-based NSRange.
///
/// - returns: The array of syntax tokens intersecting with byte range.
public func tokens(inByteRange byteRange: ByteRange) -> [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)
}
/// Returns the syntax kinds in the specified byte range.
///
/// - parameter byteRange: Byte range.
///
/// - returns: The syntax kinds in the specified byte range.
public func kinds(inByteRange byteRange: ByteRange) -> [SyntaxKind] {
return tokens(inByteRange: byteRange).compactMap { $0.kind }
}
}