Skip to content

Commit

Permalink
Clarify naming
Browse files Browse the repository at this point in the history
  • Loading branch information
theolampert committed Oct 27, 2024
1 parent a3dcc65 commit 6956b44
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
42 changes: 21 additions & 21 deletions Sources/Quilt/Quilt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public struct Quilt: Codable, Sendable {
private var counter: Int = 0
private let user: UUID

public var operations: ContiguousArray<Operation> = []
public var operationLog: ContiguousArray<Operation> = []

public private(set) var appliedOps: [Operation] = []
public private(set) var currentContent: [Operation] = []

public init(user: UUID) {
self.user = user
Expand All @@ -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)
Expand All @@ -44,36 +44,36 @@ public struct Quilt: Codable, Sendable {
}
}
}
appliedOps = ops
currentContent = ops
}

/// Inserts a character at the specified index in the text
/// - Parameters:
/// - 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()
}

/// 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()
}
Expand All @@ -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()
}
Expand All @@ -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(
Expand All @@ -120,18 +120,18 @@ public struct Quilt: Codable, Sendable {
end: end
)
)
operations.append(operation)
operationLog.append(operation)
counter += 1
applyOperations()
}

/// 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
Expand Down
32 changes: 16 additions & 16 deletions Tests/QuiltTests/QuiltTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)
)
)
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -126,23 +126,23 @@ 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() {
var quilt1 = Quilt(user: user)
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
Expand All @@ -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() {
Expand All @@ -161,13 +161,13 @@ 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() {
var quilt = Quilt(user: user)

// Should still work by inserting at the end
quilt.insert(character: "A", atIndex: 999)
XCTAssertEqual(quilt.operations.count, 1)
XCTAssertEqual(quilt.operationLog.count, 1)
}

0 comments on commit 6956b44

Please sign in to comment.