Skip to content

Commit 410b1d1

Browse files
authored
documentation updates and swiftformat pass (#194)
1 parent e2bb692 commit 410b1d1

14 files changed

+64
-42
lines changed

Sources/Automerge/Automerge.docc/Automerge.md

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Read <doc:FiveMinuteQuickstart> to get a quick taste of how to use Automerge, or
6868

6969
- ``Automerge/AutomergeText``
7070
- ``Automerge/Cursor``
71+
- ``Automerge/Position``
7172
- ``Automerge/Mark``
7273
- ``Automerge/ExpandMark``
7374

@@ -111,6 +112,7 @@ Read <doc:FiveMinuteQuickstart> to get a quick taste of how to use Automerge, or
111112

112113
### Type Conversion Errors
113114

115+
- ``Automerge/ScalarValueConversionError``
114116
- ``Automerge/BooleanScalarConversionError``
115117
- ``Automerge/BytesScalarConversionError``
116118
- ``Automerge/IntScalarConversionError``

Sources/Automerge/Automerge.docc/Curation/Codable/AutomergeEncoder.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ var myColors = ColorList(colors: ["blue", "red"])
1717
try encoder.encode(myColors)
1818
```
1919

20+
To support cross-platform usage, when provided a optional type to encode, the encoder writes a
21+
``ScalarValue/Null`` into the Document as opposed to not creating the relevant entry in a map or list.
22+
2023
## Topics
2124

2225
### Creating an Encoder

Sources/Automerge/Automerge.docc/Curation/Document.md

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
### Observing Documents
126126

127127
- ``objectWillChange``
128+
- ``objectDidChange``
128129

129130
### Transfering Documents
130131

Sources/Automerge/BoundTypes/AutomergeText.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public final class AutomergeText: Codable, @unchecked Sendable {
224224
/// document.
225225
/// - Parameters:
226226
/// - doc: The Automerge document associated with this reference.
227-
/// - path: A string path that represents a `Text` container within the Automerge document.
227+
/// - id: The Automerge object ID of the text object to bind.
228228
public func bind(doc: Document, id: ObjId) throws {
229229
// this assert runs afoul of the encoder, which doesn't make sense right now, but
230230
// I don't want to second guess it at the moment.

Sources/Automerge/ChangeHash.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public struct ChangeHash: Equatable, Hashable, CustomDebugStringConvertible, Sen
1212
}
1313

1414
public extension Set<ChangeHash> {
15-
1615
/// Transforms each `ChangeHash` in the set into its byte array (`[UInt8]`). This raw byte representation
1716
/// captures the state of the document at a specific point in its history, allowing for efficient storage
1817
/// and retrieval of document states.
@@ -25,16 +24,16 @@ public extension Set<ChangeHash> {
2524
}
2625

2726
public extension Data {
28-
29-
/// Interprets the data to return the data as a set of change hashes that represent a state within an Automerge document. If the data is not a multiple of 32 bytes, returns nil.
27+
/// Interprets the data to return the data as a set of change hashes that represent a state within an Automerge
28+
/// document. If the data is not a multiple of 32 bytes, returns nil.
3029
func heads() -> Set<ChangeHash>? {
3130
let rawBytes: [UInt8] = Array(self)
3231
guard rawBytes.count % 32 == 0 else { return nil }
3332
let totalHashes = rawBytes.count / 32
34-
let heads = (0..<totalHashes).map { index in
33+
let heads = (0 ..< totalHashes).map { index in
3534
let lowerBound = index * 32
3635
let upperBound = (index + 1) * 32
37-
let bytes = rawBytes[lowerBound..<upperBound]
36+
let bytes = rawBytes[lowerBound ..< upperBound]
3837
return ChangeHash(bytes: Array(bytes))
3938
}
4039
return Set(heads)

Sources/Automerge/Codable/AnyCodingKey.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension AnyCodingKey: CodingKey {
6767
///
6868
/// For a non-failable initializer for ``AnyCodingKey``, use ``AnyCodingKey/init(_:)-5azuh``.
6969
///
70-
/// - Parameter stringVal: The key for a keyed container.
70+
/// - Parameter stringValue: The key for a keyed container.
7171
public init?(stringValue: String) {
7272
pathElement = Automerge.Prop.Key(stringValue)
7373
}

Sources/Automerge/Codable/Decoding/AutomergeDecoder.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct AutomergeDecoder {
1414
}
1515

1616
/// Returns the type you specify, decoded from the Automerge document referenced by the decoder.
17-
/// - Parameter _: _ The type of the value to decode from the Automerge document.
17+
/// - Parameter : The type of the value to decode from the Automerge document.
1818
@inlinable public func decode<T: Decodable>(_: T.Type) throws -> T {
1919
if T.self == AutomergeText.self {
2020
// Special case decoding AutomergeText - when it's the top level type being encoded,
@@ -37,8 +37,8 @@ public struct AutomergeDecoder {
3737

3838
/// Returns the type you specify, decoded from the Automerge document referenced by the decoder.
3939
/// - Parameters:
40-
/// - _: _ The type of the value to decode from the Automerge document.
41-
/// - path: The path to the schema location within the Automerge document to attempt to decode into the type you
40+
/// - : The type of the value to decode from the Automerge document.
41+
/// - path: The path to the schema location within the Automerge document to attempt to decode into the type you
4242
/// provide.
4343
///
4444
/// The `path` parameter accepts any type conforming to the `CodingKey` protocol.

Sources/Automerge/Codable/Decoding/AutomergeSingleValueDecodingContainer.swift

-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ struct AutomergeSingleValueDecodingContainer: SingleValueDecodingContainer {
184184
debugDescription: "Expected to decode \(T.self) from \(value), but it wasn't `.text`."
185185
))
186186
}
187-
188187
default:
189188
return try T(from: impl)
190189
}

Sources/Automerge/Codable/Encoding/AutomergeEncoder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// An encoder that stores types that conform to the codable protocol into an Automerge document.
1+
/// An encoder that stores types that conform to the Codable protocol into an Automerge document.
22
public struct AutomergeEncoder {
33
/// The user info dictionary for the encoder.
44
public var userInfo: [CodingUserInfoKey: Any] = [:]

Sources/Automerge/Cursor.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public enum Position {
3333
extension Position {
3434
func toFfi() -> FfiPosition {
3535
switch self {
36-
case .cursor(let cursor):
36+
case let .cursor(cursor):
3737
return .cursor(position: cursor.bytes)
38-
case .index(let index):
38+
case let .index(index):
3939
return .index(position: index)
4040
}
4141
}

Sources/Automerge/Document.swift

+42-24
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ public final class Document: @unchecked Sendable {
2727
try work()
2828
}
2929
#endif
30-
30+
3131
#if canImport(Combine)
32-
public let objectDidChange: PassthroughSubject<(), Never> = .init()
32+
/// A publisher that sends a signal after the document is updated.
33+
///
34+
/// You can use the signal from this publisher to read the and record ``Document/heads()``
35+
/// to get the state indicator of the document after the change is complete.
36+
public let objectDidChange: PassthroughSubject<Void, Never> = .init()
3337
#endif
3438

3539
var reportingLogLevel: LogVerbosity
@@ -129,7 +133,7 @@ public final class Document: @unchecked Sendable {
129133
/// - ty: The type of object to add to the dictionary.
130134
/// - Returns: The object Id that references the object added.
131135
public func putObject(obj: ObjId, key: String, ty: ObjType) throws -> ObjId {
132-
return try lock {
136+
try lock {
133137
sendObjectWillChange()
134138
defer { sendObjectDidChange() }
135139
return try self.doc.wrapErrors {
@@ -149,7 +153,7 @@ public final class Document: @unchecked Sendable {
149153
/// If the index position doesn't yet exist within the array, this method will throw an error.
150154
/// To add an object that extends the array, use the method ``insertObject(obj:index:ty:)``.
151155
public func putObject(obj: ObjId, index: UInt64, ty: ObjType) throws -> ObjId {
152-
return try lock {
156+
try lock {
153157
sendObjectWillChange()
154158
defer { sendObjectDidChange() }
155159
return try self.doc.wrapErrors {
@@ -186,7 +190,7 @@ public final class Document: @unchecked Sendable {
186190
/// If you want to change an existing index, use the ``putObject(obj:index:ty:)`` to put in an object or
187191
/// ``put(obj:index:value:)`` to put in a value.
188192
public func insertObject(obj: ObjId, index: UInt64, ty: ObjType) throws -> ObjId {
189-
return try lock {
193+
try lock {
190194
sendObjectWillChange()
191195
defer { sendObjectDidChange() }
192196
return try self.doc.wrapErrors {
@@ -558,7 +562,7 @@ public final class Document: @unchecked Sendable {
558562
/// - position: The index position in the list, or index of the UTF-8 view in the string for a text object.
559563
/// - Returns: A cursor that references the position you specified.
560564
public func cursor(obj: ObjId, position: UInt64) throws -> Cursor {
561-
return try lock {
565+
try lock {
562566
sendObjectWillChange()
563567
defer { sendObjectDidChange() }
564568
return try Cursor(bytes: self.doc.wrapErrors { try $0.cursor(obj: obj.bytes, position: position) })
@@ -573,7 +577,7 @@ public final class Document: @unchecked Sendable {
573577
/// - heads: The set of ``ChangeHash`` that represents a point of time in the history the document.
574578
/// - Returns: A cursor that references the position and point in time you specified.
575579
public func cursorAt(obj: ObjId, position: UInt64, heads: Set<ChangeHash>) throws -> Cursor {
576-
return try lock {
580+
try lock {
577581
sendObjectWillChange()
578582
defer { sendObjectDidChange() }
579583
return try Cursor(bytes: self.doc.wrapErrors { try $0.cursorAt(
@@ -641,7 +645,7 @@ public final class Document: @unchecked Sendable {
641645
/// deleting.
642646
/// - delete: The number of unicode scalars to delete from the `start` index.
643647
/// If negative, the function deletes characters preceding `start` index, rather than following it.
644-
/// - values: The characters to insert after the `start` index.
648+
/// - value: The characters to insert after the `start` index.
645649
///
646650
/// With `spliceText`, the `start` and `delete` parameters represent integer distances of unicode scalars of the
647651
/// Swift strings, not the counts of Characters (or grapheme clusters).
@@ -797,7 +801,8 @@ public final class Document: @unchecked Sendable {
797801
///
798802
/// - Parameters:
799803
/// - obj: The identifier of the text object, represented by an ``ObjId``.
800-
/// - position: The position within the text, represented by a ``Position`` enum which can be a ``Cursor`` or an `UInt64` as a fixed position.
804+
/// - position: The position within the text, represented by a ``Position`` enum which can be a ``Cursor`` or an
805+
/// `UInt64` as a fixed position.
801806
/// - heads: A set of `ChangeHash` values that represents a point in time in the document's history.
802807
/// - Returns: An array of `Mark` objects for the text object at the specified position.
803808
///
@@ -811,16 +816,22 @@ public final class Document: @unchecked Sendable {
811816
/// ```
812817
///
813818
/// ## Recommendation
819+
///
814820
/// Use this method to query the marks applied to a text object at a specific position.
815-
/// This can be useful for retrieving ``Marks`` related to a character without traversing the full document.
821+
/// This can be useful for retrieving the list of ``Automerge/Mark`` related to a character without
822+
/// traversing the full document.
816823
///
817824
/// ## When to Use Cursor vs. Index
818825
///
819826
/// While you can specify the position either with a `Cursor` or an `Index`, there are important distinctions:
820827
///
821-
/// - **Cursor**: Use a `Cursor` when you need to track a position that might change over time due to edits in the text object. A `Cursor` provides a way to maintain a reference to a logical position within the text even if the text content changes, making it more robust in collaborative or frequently edited documents.
828+
/// - **Cursor**: Use a `Cursor` when you need to track a position that might change over time due to edits in the
829+
/// text object. A `Cursor` provides a way to maintain a reference to a logical position within the text even if the
830+
/// text content changes, making it more robust in collaborative or frequently edited documents.
822831
///
823-
/// - **Index**: Use an `Index` when you have a fixed position and you are sure that the text content will not change, or changes are irrelevant to your current operation. An index is a straightforward approach for static text content.
832+
/// - **Index**: Use an `Index` when you have a fixed position and you are sure that the text content will not
833+
/// change, or changes are irrelevant to your current operation. An index is a straightforward approach for static
834+
/// text content.
824835
///
825836
/// # See Also
826837
/// ``marksAt(obj:position:)``
@@ -845,7 +856,8 @@ public final class Document: @unchecked Sendable {
845856
///
846857
/// - Parameters:
847858
/// - obj: The identifier of the text object, represented by an ``ObjId``.
848-
/// - position: The position within the text, represented by a ``Position`` enum which can be a ``Cursor`` or an `UInt64` as a fixed position.
859+
/// - position: The position within the text, represented by a ``Position`` enum which can be a ``Cursor`` or an
860+
/// `UInt64` as a fixed position.
849861
/// - Returns: An array of `Mark` objects for the text object at the specified position.
850862
/// - Note: This method retrieves marks from the latest version of the document.
851863
/// If you need to specify a point in the document's history, refer to ``marksAt(obj:position:heads:)``.
@@ -861,15 +873,20 @@ public final class Document: @unchecked Sendable {
861873
///
862874
/// ## Recommendation
863875
/// Use this method to query the marks applied to a text object at a specific position.
864-
/// This can be useful for retrieving ``Marks`` related to a character without traversing the full document.
876+
/// This can be useful for retrieving the list of ``Automerge/Mark`` related to a character without
877+
/// traversing the full document.
865878
///
866879
/// ## When to Use Cursor vs. Index
867880
///
868881
/// While you can specify the position either with a `Cursor` or an `Index`, there are important distinctions:
869882
///
870-
/// - **Cursor**: Use a `Cursor` when you need to track a position that might change over time due to edits in the text object. A `Cursor` provides a way to maintain a reference to a logical position within the text even if the text content changes, making it more robust in collaborative or frequently edited documents.
883+
/// - **Cursor**: Use a `Cursor` when you need to track a position that might change over time due to edits in the
884+
/// text object. A `Cursor` provides a way to maintain a reference to a logical position within the text even if the
885+
/// text content changes, making it more robust in collaborative or frequently edited documents.
871886
///
872-
/// - **Index**: Use an `Index` when you have a fixed position and you are sure that the text content will not change, or changes are irrelevant to your current operation. An index is a straightforward approach for static text content.
887+
/// - **Index**: Use an `Index` when you have a fixed position and you are sure that the text content will not
888+
/// change, or changes are irrelevant to your current operation. An index is a straightforward approach for static
889+
/// text content.
873890
///
874891
/// # See Also
875892
/// ``marksAt(obj:position:heads:)``
@@ -901,7 +918,7 @@ public final class Document: @unchecked Sendable {
901918
/// The `save` function also compacts the memory footprint of an Automerge document and increments the result of
902919
/// ``heads()``, which indicates a specific point in time for the history of the document.
903920
public func save() -> Data {
904-
return lock {
921+
lock {
905922
sendObjectWillChange()
906923
defer { sendObjectDidChange() }
907924
return self.doc.wrapErrors {
@@ -956,7 +973,7 @@ public final class Document: @unchecked Sendable {
956973
/// - message: The message from the peer to update this document and sync state.
957974
/// - Returns: An array of ``Patch`` that represent the changes applied from the peer.
958975
public func receiveSyncMessageWithPatches(state: SyncState, message: Data) throws -> [Patch] {
959-
return try lock {
976+
try lock {
960977
sendObjectWillChange()
961978
defer { sendObjectDidChange() }
962979
let patches = try self.doc.wrapErrors {
@@ -1007,7 +1024,7 @@ public final class Document: @unchecked Sendable {
10071024
/// - Parameter other: another ``Document``
10081025
/// - Returns: A list of ``Patch`` the represent the changes applied when merging the other document.
10091026
public func mergeWithPatches(other: Document) throws -> [Patch] {
1010-
return try lock {
1027+
try lock {
10111028
sendObjectWillChange()
10121029
defer { sendObjectDidChange() }
10131030
let patches = try self.doc.wrapErrorsWithOther(other: other.doc) {
@@ -1078,8 +1095,8 @@ public final class Document: @unchecked Sendable {
10781095
/// ```
10791096
///
10801097
/// - Parameters:
1081-
/// - from: The set of heads at beginning point in the documents history.
1082-
/// - to: The set of heads at ending point in the documents history.
1098+
/// - before: The set of heads at beginning point in the documents history.
1099+
/// - after: The set of heads at ending point in the documents history.
10831100
/// - Note: `from` and `to` do not have to be chronological. Document state can move backward.
10841101
/// - Returns: The difference needed to produce a document at `to` when it is set at `from` in history.
10851102
public func difference(from before: Set<ChangeHash>, to after: Set<ChangeHash>) -> [Patch] {
@@ -1100,7 +1117,7 @@ public final class Document: @unchecked Sendable {
11001117
/// ```
11011118
///
11021119
/// - Parameters:
1103-
/// - since: The set of heads at the point in the documents history to compare to.
1120+
/// - lhs: The set of heads at the point in the documents history to compare to.
11041121
/// - Returns: The difference needed to produce current document given an arbitrary
11051122
/// point in the history.
11061123
public func difference(since lhs: Set<ChangeHash>) -> [Patch] {
@@ -1116,7 +1133,7 @@ public final class Document: @unchecked Sendable {
11161133
/// ```
11171134
///
11181135
/// - Parameters:
1119-
/// - to: The set of heads at ending point in the documents history.
1136+
/// - rhs: The set of heads at ending point in the documents history.
11201137
/// - Returns: The difference needed to move current document to a previous point
11211138
/// in the history.
11221139
public func difference(to rhs: Set<ChangeHash>) -> [Patch] {
@@ -1186,7 +1203,7 @@ public final class Document: @unchecked Sendable {
11861203
/// ``encodeNewChanges()``, ``encodeChangesSince(heads:)`` or any
11871204
/// concatenation of those.
11881205
public func applyEncodedChangesWithPatches(encoded: Data) throws -> [Patch] {
1189-
return try lock {
1206+
try lock {
11901207
sendObjectWillChange()
11911208
defer { sendObjectDidChange() }
11921209
let patches = try self.doc.wrapErrors {
@@ -1253,6 +1270,7 @@ extension Document: ObservableObject {
12531270
// #endif
12541271
objectWillChange.send()
12551272
}
1273+
12561274
fileprivate func sendObjectDidChange() {
12571275
objectDidChange.send()
12581276
}

Tests/AutomergeTests/TestChanges.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ChangeSetTests: XCTestCase {
113113
try doc.merge(other: doc2)
114114
try doc.merge(other: doc3)
115115

116-
let rawHashes = (0..<500).map { _ in doc.heads().raw() }
116+
let rawHashes = (0 ..< 500).map { _ in doc.heads().raw() }
117117

118118
XCTAssertEqual(Set(rawHashes).count, 1)
119119
}

Tests/AutomergeTests/TestMarks.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class MarksTestCase: XCTestCase {
7777

7878
XCTAssertEqual(marks, [
7979
Mark(start: 2, end: 2, name: "bold", value: .Boolean(true)),
80-
Mark(start: 2, end: 2, name: "italic", value: .Boolean(true))
80+
Mark(start: 2, end: 2, name: "italic", value: .Boolean(true)),
8181
])
8282
}
8383

@@ -93,7 +93,7 @@ class MarksTestCase: XCTestCase {
9393

9494
XCTAssertEqual(marks, [
9595
Mark(start: 2, end: 2, name: "bold", value: .Boolean(true)),
96-
Mark(start: 2, end: 2, name: "italic", value: .Boolean(true))
96+
Mark(start: 2, end: 2, name: "italic", value: .Boolean(true)),
9797
])
9898
}
9999
}

Tests/AutomergeTests/TestObservableDocument.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ObservableDocumentTestCase: XCTestCase {
4545
stashedHeads = doc.heads()
4646
}
4747
XCTAssertNotNil(willChangeHandle)
48-
48+
4949
let didChangeHandle = doc.objectDidChange.sink {
5050
_ = doc.heads()
5151
}

0 commit comments

Comments
 (0)