From 5b30f01e46d25f7b59e37d6d96b2030d6c96ac87 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 15 Nov 2021 02:53:40 +0700 Subject: [PATCH] Code style tweaks --- Sources/Defaults/Defaults+Extensions.swift | 8 +++++-- Sources/Defaults/Defaults+Protocol.swift | 20 ++++++++++++---- Sources/Defaults/Defaults.swift | 11 ++++++--- Sources/Defaults/Observation.swift | 8 +++++-- Sources/Defaults/SwiftUI.swift | 24 +++++++++++++------ Sources/Defaults/Utilities.swift | 15 ++++++++---- .../DefaultsMigrationTests.swift | 8 +++++-- migration.md | 4 +++- 8 files changed, 72 insertions(+), 26 deletions(-) diff --git a/Sources/Defaults/Defaults+Extensions.swift b/Sources/Defaults/Defaults+Extensions.swift index a010b90..c2da474 100644 --- a/Sources/Defaults/Defaults+Extensions.swift +++ b/Sources/Defaults/Defaults+Extensions.swift @@ -144,9 +144,13 @@ extension Color: Defaults.Serializable { } #if os(macOS) -/// `NSColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`. +/** +`NSColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`. +*/ extension NSColor: Defaults.Serializable {} #else -/// `UIColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`. +/** +`UIColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`. +*/ extension UIColor: Defaults.Serializable {} #endif diff --git a/Sources/Defaults/Defaults+Protocol.swift b/Sources/Defaults/Defaults+Protocol.swift index c767df9..2cc086f 100644 --- a/Sources/Defaults/Defaults+Protocol.swift +++ b/Sources/Defaults/Defaults+Protocol.swift @@ -21,10 +21,14 @@ public protocol DefaultsSerializable { typealias Serializable = Bridge.Serializable associatedtype Bridge: DefaultsBridge - /// Static bridge for the `Value` which cannot be stored natively. + /** + Static bridge for the `Value` which cannot be stored natively. + */ static var bridge: Bridge { get } - /// A flag to determine whether `Value` can be stored natively or not. + /** + A flag to determine whether `Value` can be stored natively or not. + */ static var isNativelySupportedType: Bool { get } } @@ -89,16 +93,22 @@ public protocol DefaultsBridge { } public protocol DefaultsCollectionSerializable: Collection, Defaults.Serializable { - /// `Collection` does not have a initializer, but we need a initializer to convert an array into the `Value`. + /** + `Collection` does not have a initializer, but we need a initializer to convert an array into the `Value`. + */ init(_ elements: [Element]) } public protocol DefaultsSetAlgebraSerializable: SetAlgebra, Defaults.Serializable { - /// Since `SetAlgebra` protocol does not conform to `Sequence`, we cannot convert a `SetAlgebra` to an `Array` directly. + /** + Since `SetAlgebra` protocol does not conform to `Sequence`, we cannot convert a `SetAlgebra` to an `Array` directly. + */ func toArray() -> [Element] } -/// Convenience protocol for `Codable`. +/** +Convenience protocol for `Codable`. +*/ public protocol DefaultsCodableBridge: Defaults.Bridge where Serializable == String, Value: Codable {} /** diff --git a/Sources/Defaults/Defaults.swift b/Sources/Defaults/Defaults.swift index d19f19a..a2d8d86 100644 --- a/Sources/Defaults/Defaults.swift +++ b/Sources/Defaults/Defaults.swift @@ -7,7 +7,9 @@ public protocol DefaultsBaseKey: Defaults.AnyKey { } extension DefaultsBaseKey { - /// Reset the item back to its default value. + /** + Reset the item back to its default value. + */ public func reset() { suite.removeObject(forKey: name) } @@ -39,8 +41,11 @@ public enum Defaults { public final class Key: AnyKey { public let defaultValue: Value - /// Create a defaults key. - /// The `default` parameter can be left out if the `Value` type is an optional. + /** + Create a defaults key. + + The `default` parameter can be left out if the `Value` type is an optional. + */ public init(_ key: String, default defaultValue: Value, suite: UserDefaults = .standard) { self.defaultValue = defaultValue diff --git a/Sources/Defaults/Observation.swift b/Sources/Defaults/Observation.swift index ac68287..2d674c6 100644 --- a/Sources/Defaults/Observation.swift +++ b/Sources/Defaults/Observation.swift @@ -28,10 +28,14 @@ extension Defaults { public typealias Observation = DefaultsObservation public enum ObservationOption { - /// Whether a notification should be sent to the observer immediately, before the observer registration method even returns. + /** + Whether a notification should be sent to the observer immediately, before the observer registration method even returns. + */ case initial - /// Whether separate notifications should be sent to the observer before and after each change, instead of a single notification after the change. + /** + Whether separate notifications should be sent to the observer before and after each change, instead of a single notification after the change. + */ case prior } diff --git a/Sources/Defaults/SwiftUI.swift b/Sources/Defaults/SwiftUI.swift index bec3503..ff291fe 100644 --- a/Sources/Defaults/SwiftUI.swift +++ b/Sources/Defaults/SwiftUI.swift @@ -33,7 +33,9 @@ extension Defaults { } } - /// Reset the key back to its default value. + /** + Reset the key back to its default value. + */ func reset() { key.reset() } @@ -47,7 +49,7 @@ public struct Default: DynamicProperty { private let key: Defaults.Key - // Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamicaly changed. + // Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamically changed. @ObservedObject private var observable: Defaults.Observable /** @@ -87,10 +89,14 @@ public struct Default: DynamicProperty { public var projectedValue: Binding { $observable.value } - /// The default value of the key. + /** + The default value of the key. + */ public var defaultValue: Value { key.defaultValue } - /// Combine publisher that publishes values when the `Defaults` item changes. + /** + Combine publisher that publishes values when the `Defaults` item changes. + */ public var publisher: Publisher { Defaults.publisher(key) } public mutating func update() { @@ -123,7 +129,9 @@ public struct Default: DynamicProperty { @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension Default where Value: Equatable { - /// Indicates whether the value is the same as the default value. + /** + Indicates whether the value is the same as the default value. + */ public var isDefaultValue: Bool { wrappedValue == defaultValue } } @@ -165,7 +173,7 @@ extension Defaults { private let label: () -> Label - // Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamicaly changed. + // Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamically changed. @ObservedObject private var observable: Defaults.Observable public init(key: Key, @ViewBuilder label: @escaping () -> Label) { @@ -192,7 +200,9 @@ extension Defaults.Toggle where Label == Text { @available(macOS 11, iOS 14, tvOS 14, watchOS 7, *) extension Defaults.Toggle { - /// Do something when the value changes to a different value. + /** + Do something when the value changes to a different value. + */ public func onChange(_ action: @escaping (Bool) -> Void) -> Self { onChange = action return self diff --git a/Sources/Defaults/Utilities.swift b/Sources/Defaults/Utilities.swift index 798c2af..b4fe795 100644 --- a/Sources/Defaults/Utilities.swift +++ b/Sources/Defaults/Utilities.swift @@ -119,10 +119,15 @@ final class LifetimeAssociation { } -/// A protocol for making generic type constraints of optionals. -/// - Note: It's intentionally not including `associatedtype Wrapped` as that limits a lot of the use-cases. +/** +A protocol for making generic type constraints of optionals. + +- Note: It's intentionally not including `associatedtype Wrapped` as that limits a lot of the use-cases. +*/ public protocol _DefaultsOptionalType: ExpressibleByNilLiteral { - /// This is useful as you can't compare `_OptionalType` to `nil`. + /** + This is useful as you cannot compare `_OptionalType` to `nil`. + */ var isNil: Bool { get } } @@ -146,7 +151,9 @@ extension DispatchQueue { extension Sequence { - /// Returns an array containing the non-nil elements. + /** + Returns an array containing the non-nil elements. + */ func compact() -> [T] where Element == T? { // TODO: Make this `compactMap(\.self)` when https://bugs.swift.org/browse/SR-12897 is fixed. compactMap { $0 } diff --git a/Tests/DefaultsTests/DefaultsMigrationTests.swift b/Tests/DefaultsTests/DefaultsMigrationTests.swift index 60d678c..c1dc0c9 100644 --- a/Tests/DefaultsTests/DefaultsMigrationTests.swift +++ b/Tests/DefaultsTests/DefaultsMigrationTests.swift @@ -25,7 +25,9 @@ private struct TimeZone: Hashable { } extension TimeZone: Defaults.NativeType { - /// Associated `CodableForm` to `CodableTimeZone`. + /** + Associated `CodableForm` to `CodableTimeZone`. + */ typealias CodableForm = CodableTimeZone static let bridge = TimeZoneBridge() @@ -37,7 +39,9 @@ private struct CodableTimeZone { } extension CodableTimeZone: Defaults.CodableType { - /// Convert from `Codable` to `Native`. + /** + Convert from `Codable` to `Native`. + */ func toNative() -> TimeZone { TimeZone(id: id, name: name) } diff --git a/migration.md b/migration.md index 59b23d8..742ad04 100644 --- a/migration.md +++ b/migration.md @@ -380,7 +380,9 @@ private struct CodableTimeZone { } extension CodableTimeZone: Defaults.CodableType { - /// Convert from `Codable` to native type. + /** + Convert from `Codable` to native type. + */ func toNative() -> TimeZone { TimeZone(id: id, name: name) }