Files
SwiftLint/Source/SwiftLintCore/Models/SwiftLintSyntaxMap.swift
T
JP Simard 5a2cf4b1fe Remove dead code (#6125)
In particular lots of stuff that used to be needed with SourceKit that
we no longer need to keep around.

Identified using Periphery: https://github.com/peripheryapp/periphery
2025-06-21 15:19:37 -04:00

51 lines
1.7 KiB
Swift

import SourceKittenFramework
/// Represents a Swift file's syntax information.
public struct SwiftLintSyntaxMap {
/// 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.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 {
token.range.intersects(byteRange)
}
func intersectsOrAfter(_ token: SwiftLintSyntaxToken) -> Bool {
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] {
tokens(inByteRange: byteRange).compactMap(\.kind)
}
}