Files
SwiftLint/Source/SwiftLintFramework/Extensions/StringView+SwiftSyntax.swift
T
JP Simard a773c3ec21 Apply minor changes to the syntactic sugar rewrite (#3915)
* Improve docstrings for `StringView+SwiftSyntax.swift`
* Move changelog entry to correct section & reword
* Change parameter type from `Int` to `ByteCount`
* Move AbsolutePosition / ByteCount conversion to internal API
* Only warn once if syntax tree cannot be parsed
* Move Syntactic Sugar examples to a dedicated file
* Change SyntacticSugarRuleVisitor from SyntaxAnyVisitor to SyntaxVisitor
* Add `SugaredType` enum to help with the implement `SyntacticSugarRule`
2022-03-24 10:27:05 -04:00

28 lines
1.1 KiB
Swift

import Foundation
import SourceKittenFramework
import SwiftSyntax
extension StringView {
/// Converts two absolute positions from SwiftSyntax to a valid `NSRange` if possible.
///
/// - parameter start: Starting position.
/// - parameter end: End position.
///
/// - returns: `NSRange` or nil in case of empty string.
func NSRange(start: AbsolutePosition, end: AbsolutePosition) -> NSRange? {
precondition(end >= start, "End position should be bigger than the start position")
return NSRange(start: start, length: ByteCount(end.utf8Offset - start.utf8Offset))
}
/// Converts absolute position with length from SwiftSyntax to a valid `NSRange` if possible.
///
/// - parameter start: Starting position.
/// - parameter length: Length in bytes.
///
/// - returns: `NSRange` or nil in case of empty string.
private func NSRange(start: AbsolutePosition, length: ByteCount) -> NSRange? {
let byteRange = ByteRange(location: ByteCount(start), length: length)
return byteRangeToNSRange(byteRange)
}
}