From 6956b44b0142ad3a88c1eb030f2f509814c935bc Mon Sep 17 00:00:00 2001 From: Theo Date: Sun, 27 Oct 2024 14:04:59 +0100 Subject: [PATCH] Clarify naming --- Sources/Quilt/Quilt.swift | 42 +++++++++++++++---------------- Tests/QuiltTests/QuiltTests.swift | 32 +++++++++++------------ 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/Sources/Quilt/Quilt.swift b/Sources/Quilt/Quilt.swift index 193697d..7063974 100644 --- a/Sources/Quilt/Quilt.swift +++ b/Sources/Quilt/Quilt.swift @@ -4,9 +4,9 @@ public struct Quilt: Codable, Sendable { private var counter: Int = 0 private let user: UUID - public var operations: ContiguousArray = [] + public var operationLog: ContiguousArray = [] - public private(set) var appliedOps: [Operation] = [] + public private(set) var currentContent: [Operation] = [] public init(user: UUID) { self.user = user @@ -21,7 +21,7 @@ public struct Quilt: Codable, Sendable { */ var lastIdx: (OpID, Int)? - for operation in operations { + for operation in operationLog { if case .insert = operation.type { if operation.afterId == nil { ops.insert(operation, at: 0) @@ -44,7 +44,7 @@ public struct Quilt: Codable, Sendable { } } } - appliedOps = ops + currentContent = ops } /// Inserts a character at the specified index in the text @@ -52,13 +52,13 @@ public struct Quilt: Codable, Sendable { /// - character: The character to insert /// - atIndex: The position at which to insert the character public mutating func insert(character: Character, atIndex: Int) { - // Use appliedOps since we're interested in the index of a character not action + // Use currentContent since we're interested in the index of a character not action let operation = Operation( opId: .init(counter: counter, id: user), type: .insert(character), - afterId: appliedOps[safeIndex: atIndex - 1]?.opId + afterId: currentContent[safeIndex: atIndex - 1]?.opId ) - operations.append(operation) + operationLog.append(operation) counter += 1 applyOperations() } @@ -66,14 +66,14 @@ public struct Quilt: Codable, Sendable { /// Removes the character at the specified index /// - Parameter atIndex: The position of the character to remove public mutating func remove(atIndex: Int) { - // Use appliedOps since we're interested in the index of a character not action - guard let opID = appliedOps[safeIndex: atIndex]?.opId - ?? appliedOps.first?.opId else { return } + // Use currentContent since we're interested in the index of a character not action + guard let opID = currentContent[safeIndex: atIndex]?.opId + ?? currentContent.first?.opId else { return } let operation = Operation( opId: .init(counter: counter, id: user), type: .remove(opID) ) - operations.append(operation) + operationLog.append(operation) counter += 1 applyOperations() } @@ -88,14 +88,14 @@ public struct Quilt: Codable, Sendable { fromIndex: Int, toIndex: Int ) { - // Use appliedOps since we're interested in the index of a character not action - let start: SpanMarker = .before(appliedOps[fromIndex].opId) - let end: SpanMarker = .before(appliedOps[toIndex].opId) + // Use currentContent since we're interested in the index of a character not action + let start: SpanMarker = .before(currentContent[fromIndex].opId) + let end: SpanMarker = .before(currentContent[toIndex].opId) let operation = Operation( opId: .init(counter: counter, id: user), type: .addMark(type: mark, start: start, end: end) ) - operations.append(operation) + operationLog.append(operation) counter += 1 applyOperations() } @@ -110,8 +110,8 @@ public struct Quilt: Codable, Sendable { fromIndex: Int, toIndex: Int ) { - let start: SpanMarker = .before(appliedOps[fromIndex].opId) - let end: SpanMarker = .before(appliedOps[toIndex].opId) + let start: SpanMarker = .before(currentContent[fromIndex].opId) + let end: SpanMarker = .before(currentContent[toIndex].opId) let operation = Operation( opId: .init(counter: counter, id: user), type: .removeMark( @@ -120,7 +120,7 @@ public struct Quilt: Codable, Sendable { end: end ) ) - operations.append(operation) + operationLog.append(operation) counter += 1 applyOperations() } @@ -128,10 +128,10 @@ public struct Quilt: Codable, Sendable { /// Merges another Quilt document into this one /// - Parameter quilt: The Quilt document to merge public mutating func merge(_ quilt: Quilt) { - operations += quilt.operations.filter { operation in - !self.operations.contains(where: { operation.opId == $0.opId }) + operationLog += quilt.operationLog.filter { operation in + !self.operationLog.contains(where: { operation.opId == $0.opId }) } - if let max = operations.max(by: { + if let max = operationLog.max(by: { $0.opId.counter < $1.opId.counter })?.opId.counter { counter = max + 1 diff --git a/Tests/QuiltTests/QuiltTests.swift b/Tests/QuiltTests/QuiltTests.swift index b64dce6..f285e1e 100644 --- a/Tests/QuiltTests/QuiltTests.swift +++ b/Tests/QuiltTests/QuiltTests.swift @@ -20,7 +20,7 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! quilt.insert(character: "T", atIndex: 0) XCTAssertEqual( - quilt.operations[0], + quilt.operationLog[0], Operation( opId: OpID(counter: 0, id: user), type: .insert("T"), @@ -31,21 +31,21 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! quilt.insert(character: "H", atIndex: 1) XCTAssertEqual( - quilt.operations[1], + quilt.operationLog[1], Operation( opId: OpID(counter: 1, id: user), type: .insert("H"), - afterId: quilt.operations[0].id + afterId: quilt.operationLog[0].id ) ) quilt.remove(atIndex: 1) XCTAssertEqual( - quilt.operations[2], + quilt.operationLog[2], Operation( opId: OpID(counter: 2, id: user), - type: .remove(quilt.operations[1].id) + type: .remove(quilt.operationLog[1].id) ) ) } @@ -60,7 +60,7 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! quilt.addMark(mark: .bold, fromIndex: 0, toIndex: 4) - XCTAssertEqual(quilt.operations[5], Operation( + XCTAssertEqual(quilt.operationLog[5], Operation( opId: OpID(counter: 5, id: user), type: .addMark( type: .bold, @@ -75,7 +75,7 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! toIndex: 4 ) - XCTAssertEqual(quilt.operations[6], Operation( + XCTAssertEqual(quilt.operationLog[6], Operation( opId: OpID(counter: 6, id: user), type: .removeMark( type: .bold, @@ -126,8 +126,8 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! quilt1.merge(quilt2) - XCTAssertEqual(quilt1.operations.count, 2) - XCTAssertEqual(quilt1.appliedOps.count, 2) + XCTAssertEqual(quilt1.operationLog.count, 2) + XCTAssertEqual(quilt1.currentContent.count, 2) } @Test func testMergeDuplicateOperations() { @@ -135,14 +135,14 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! var quilt2 = Quilt(user: otherUser) // Changed to different user quilt1.insert(character: "A", atIndex: 0) - quilt2.operations = quilt1.operations + quilt2.operationLog = quilt1.operationLog quilt2.insert(character: "B", atIndex: 1) quilt1.merge(quilt2) - // Should add both operations since they're from different users - XCTAssertEqual(quilt1.operations.count, 2) - XCTAssertEqual(quilt1.appliedOps.count, 2) + // Should add both operationLog since they're from different users + XCTAssertEqual(quilt1.operationLog.count, 2) + XCTAssertEqual(quilt1.currentContent.count, 2) } // MARK: - Edge Cases Tests @@ -152,7 +152,7 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! // Should not crash quilt.remove(atIndex: 0) - XCTAssertEqual(quilt.operations.count, 0) + XCTAssertEqual(quilt.operationLog.count, 0) } @Test func testRemoveFromInvalidIndex() { @@ -161,7 +161,7 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! // Should not crash and should add the remove operation quilt.remove(atIndex: 1) - XCTAssertEqual(quilt.operations.count, 2) + XCTAssertEqual(quilt.operationLog.count, 2) } @Test func testInsertAtInvalidIndex() { @@ -169,5 +169,5 @@ let otherUser = UUID(uuidString: "F3DFDB75-A3D9-4B55-9312-111EF297D567")! // Should still work by inserting at the end quilt.insert(character: "A", atIndex: 999) - XCTAssertEqual(quilt.operations.count, 1) + XCTAssertEqual(quilt.operationLog.count, 1) }