|
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift.org open source project
|
4 | 4 | //
|
5 |
| -// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception
|
7 | 7 | //
|
8 | 8 | // See https://swift.org/LICENSE.txt for license information
|
|
13 | 13 | /// A textual edit to the original source represented by a range and a
|
14 | 14 | /// replacement.
|
15 | 15 | public struct SourceEdit: Equatable, Sendable {
|
16 |
| - /// The half-open range that this edit applies to. |
| 16 | + /// The byte range of the original source buffer that the edit applies to. |
17 | 17 | public let range: Range<AbsolutePosition>
|
18 |
| - /// The text to replace the original range with. Empty for a deletion. |
19 |
| - public let replacement: String |
20 | 18 |
|
21 |
| - /// Length of the original source range that this edit applies to. Zero if |
22 |
| - /// this is an addition. |
23 |
| - public var length: SourceLength { |
24 |
| - return SourceLength(utf8Length: range.upperBound.utf8Offset - range.lowerBound.utf8Offset) |
| 19 | + /// The UTF-8 bytes that should be inserted as part of the edit |
| 20 | + public let replacementBytes: [UInt8] |
| 21 | + |
| 22 | + /// A string representation of the replacement |
| 23 | + public var replacement: String { return String(decoding: replacementBytes, as: UTF8.self) } |
| 24 | + |
| 25 | + /// The length of the edit replacement in UTF8 bytes. |
| 26 | + public var replacementLength: SourceLength { return SourceLength(utf8Length: replacementBytes.count) } |
| 27 | + |
| 28 | + @available(*, deprecated, renamed: "range.lowerBound.utf8Offset") |
| 29 | + public var offset: Int { return range.offset } |
| 30 | + |
| 31 | + @available(*, deprecated, renamed: "replacementLength") |
| 32 | + public var length: Int { return range.length.utf8Length } |
| 33 | + |
| 34 | + @available(*, deprecated, renamed: "range.upperBound.utf8Offset") |
| 35 | + public var endOffset: Int { return range.endOffset } |
| 36 | + |
| 37 | + /// After the edit has been applied the range of the replacement text. |
| 38 | + public var replacementRange: Range<AbsolutePosition> { |
| 39 | + return Range(position: range.lowerBound, length: replacementLength) |
25 | 40 | }
|
26 | 41 |
|
27 | 42 | /// Create an edit to replace `range` in the original source with
|
28 | 43 | /// `replacement`.
|
29 | 44 | public init(range: Range<AbsolutePosition>, replacement: String) {
|
30 | 45 | self.range = range
|
31 |
| - self.replacement = replacement |
| 46 | + self.replacementBytes = Array(replacement.utf8) |
| 47 | + } |
| 48 | + |
| 49 | + public init(range: Range<AbsolutePosition>, replacement: [UInt8]) { |
| 50 | + self.range = range |
| 51 | + self.replacementBytes = replacement |
| 52 | + } |
| 53 | + |
| 54 | + @available(*, deprecated, message: "Use SourceEdit(range:replacement:) instead") |
| 55 | + public init(range: ByteSourceRange, replacementLength: Int) { |
| 56 | + self.range = AbsolutePosition(utf8Offset: range.offset)..<AbsolutePosition(utf8Offset: range.endOffset) |
| 57 | + self.replacementBytes = Array(repeating: UInt8(ascii: " "), count: replacementLength) |
| 58 | + } |
| 59 | + |
| 60 | + @available(*, deprecated, message: "Use SourceEdit(range:replacement:) instead") |
| 61 | + public init(offset: Int, length: Int, replacementLength: Int) { |
| 62 | + self.range = AbsolutePosition(utf8Offset: offset)..<AbsolutePosition(utf8Offset: offset + length) |
| 63 | + self.replacementBytes = Array(repeating: UInt8(ascii: " "), count: replacementLength) |
| 64 | + } |
| 65 | + |
| 66 | + @available(*, deprecated, message: "Use SourceEdit(range:replacement:) instead") |
| 67 | + public init(offset: Int, length: Int, replacement: [UInt8]) { |
| 68 | + self.range = AbsolutePosition(utf8Offset: offset)..<AbsolutePosition(utf8Offset: offset + length) |
| 69 | + self.replacementBytes = replacement |
| 70 | + } |
| 71 | + |
| 72 | + @available(*, deprecated, message: "Use SourceEdit(range:replacement:) instead") |
| 73 | + public init(offset: Int, length: Int, replacement: String) { |
| 74 | + self.init(offset: offset, length: length, replacement: Array(replacement.utf8)) |
| 75 | + } |
| 76 | + |
| 77 | + public func intersectsOrTouchesRange(_ other: Range<AbsolutePosition>) -> Bool { |
| 78 | + return self.range.upperBound.utf8Offset >= other.lowerBound.utf8Offset |
| 79 | + && self.range.lowerBound.utf8Offset <= other.upperBound.utf8Offset |
| 80 | + } |
| 81 | + |
| 82 | + public func intersectsRange(_ other: Range<AbsolutePosition>) -> Bool { |
| 83 | + return self.range.upperBound.utf8Offset > other.lowerBound.utf8Offset |
| 84 | + && self.range.lowerBound.utf8Offset < other.upperBound.utf8Offset |
32 | 85 | }
|
33 | 86 |
|
34 | 87 | /// Convenience function to create a textual addition after the given node
|
|
0 commit comments