|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | +import XCTest |
| 15 | + |
| 16 | +/// Get `ConcurrentEdits` in source whose edited zones are marked with markers |
| 17 | +/// Also extract the markers from source to get original source and edited source |
| 18 | +public func getConcurrentEditsAndSources(_ source: String) -> (edits: ConcurrentEdits, orignialSource: String, editedSource: String) { |
| 19 | + var editedSource = source |
| 20 | + |
| 21 | + var sequentialEdits: [SourceEdit] = [] |
| 22 | + while let (edit, afterSource) = nextMarkedEdits(editedSource) { |
| 23 | + sequentialEdits.append(edit) |
| 24 | + editedSource = afterSource |
| 25 | + } |
| 26 | + |
| 27 | + return (ConcurrentEdits(fromSequential: sequentialEdits), extractOriginalSource(from: source), editedSource) |
| 28 | +} |
| 29 | + |
| 30 | +fileprivate func nextMarkedEdits(_ source: String) -> (edit: SourceEdit, editedSource: String)? { |
| 31 | + guard let startIndex = source.firstIndex(where: { $0.isStartMarker }), |
| 32 | + let separateIndex = source.firstIndex(where: { $0.isSeperateMarker }), |
| 33 | + let endIndex = source.firstIndex(where: { $0.isEndMarker }) |
| 34 | + else { |
| 35 | + return nil |
| 36 | + } |
| 37 | + |
| 38 | + let edit = SourceEdit(offset: source.distance(from: source.startIndex, to: startIndex), length: source.distance(from: source.index(after: startIndex), to: separateIndex), replacementLength: source.distance(from: source.index(after: separateIndex), to: endIndex)) |
| 39 | + |
| 40 | + let editedSource = String(source[source.startIndex..<startIndex] + source[source.index(after: separateIndex)..<endIndex] + source[source.index(after: endIndex)...]) |
| 41 | + |
| 42 | + return (edit, editedSource) |
| 43 | +} |
| 44 | + |
| 45 | +fileprivate func extractOriginalSource(from source: String) -> String { |
| 46 | + var text = source |
| 47 | + while let startIdx = text.firstIndex(where: { $0.isStartMarker }), |
| 48 | + let separateIdx = text.firstIndex(where: { $0.isSeperateMarker }), |
| 49 | + let endIdx = text.firstIndex(where: { $0.isEndMarker }) |
| 50 | + { |
| 51 | + text = String(text[text.startIndex..<startIdx] + text[text.index(after: startIdx)..<separateIdx] + text[text.index(after: endIdx)...]) |
| 52 | + } |
| 53 | + |
| 54 | + return text |
| 55 | +} |
| 56 | + |
| 57 | +/// Apply the given edits to `testString` and return the resulting string. |
| 58 | +/// `concurrent` specifies whether the edits should be interpreted as being |
| 59 | +/// applied sequentially or concurrently. |
| 60 | +public func applyEdits( |
| 61 | + _ edits: [SourceEdit], |
| 62 | + concurrent: Bool, |
| 63 | + to testString: String, |
| 64 | + replacementChar: Character = "?" |
| 65 | +) -> String { |
| 66 | + guard let replacementAscii = replacementChar.asciiValue else { |
| 67 | + fatalError("replacementChar must be an ASCII character") |
| 68 | + } |
| 69 | + var edits = edits |
| 70 | + if concurrent { |
| 71 | + XCTAssert(ConcurrentEdits._isValidConcurrentEditArray(edits)) |
| 72 | + |
| 73 | + // If the edits are concurrent, sorted and not overlapping (as guaranteed by |
| 74 | + // the check above, we can apply them sequentially to the string in reverse |
| 75 | + // order because later edits don't affect earlier edits. |
| 76 | + edits = edits.reversed() |
| 77 | + } |
| 78 | + var bytes = Array(testString.utf8) |
| 79 | + for edit in edits { |
| 80 | + assert(edit.endOffset <= bytes.count) |
| 81 | + bytes.removeSubrange(edit.offset..<edit.endOffset) |
| 82 | + bytes.insert(contentsOf: [UInt8](repeating: replacementAscii, count: edit.replacementLength), at: edit.offset) |
| 83 | + } |
| 84 | + return String(bytes: bytes, encoding: .utf8)! |
| 85 | +} |
| 86 | + |
| 87 | +fileprivate extension Character { |
| 88 | + var isStartMarker: Bool { |
| 89 | + self == "⏩️" |
| 90 | + } |
| 91 | + |
| 92 | + var isSeperateMarker: Bool { |
| 93 | + self == "⏸️" |
| 94 | + } |
| 95 | + |
| 96 | + var isEndMarker: Bool { |
| 97 | + self == "⏪️" |
| 98 | + } |
| 99 | +} |
0 commit comments