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`
This commit is contained in:
JP Simard
2022-03-24 10:27:05 -04:00
committed by GitHub
parent 9ace4bbb73
commit a773c3ec21
7 changed files with 213 additions and 192 deletions
@@ -3,23 +3,25 @@ import SourceKittenFramework
import SwiftSyntax
extension StringView {
/// Converts two absolute positions from SwiftSyntax to valid NSRange if possible
/// - Parameters:
/// - start: starting poisiition
/// - end: end position
/// - Returns: NSRange or nil in case of empty string
/// 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.utf8Offset >= start.utf8Offset, "End position should be beigger than start position")
return NSRange(start: start, length: end.utf8Offset - start.utf8Offset)
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 valid NSRange if possible
/// - Parameters:
/// - start: starting position
/// - length: length in bytes
/// - Returns: NSRange or nil in case of empty string
private func NSRange(start: AbsolutePosition, length: Int) -> NSRange? {
let byteRange = ByteRange(location: ByteCount(start.utf8Offset), length: ByteCount(length))
/// 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)
}
}