Skip to content

Commit f730e03

Browse files
committed
Fixed: Prefix & Suffix when base is empty. Removed: wrapper == default. Removed: NullableTextStyle wrapper conformances.
1 parent 9fe47b6 commit f730e03

File tree

7 files changed

+54
-88
lines changed

7 files changed

+54
-88
lines changed

Sources/DiffableTextKit/DiffableTextStyle+Nullable.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ public extension NullableTextStyle {
3535
//=------------------------------------------------------------------------=
3636

3737
@inlinable func format(optional value: Value?, with cache: inout Cache) -> String {
38-
guard let value else { return "" }
39-
return format(value, with: &cache)
38+
guard let value else { return String() }; return format(value, with: &cache)
4039
}
4140

4241
@inlinable func interpret(optional value: Value?, with cache: inout Cache) -> Commit<Value?> {
43-
guard let value else { return Commit() }
44-
return Commit(interpret(value, with: &cache))
42+
guard let value else { return Commit() }; return Commit(interpret(value, with: &cache))
4543
}
4644

4745
@inlinable func resolve(optional proposal: Proposal, with cache: inout Cache) throws -> Commit<Value?> {
@@ -86,8 +84,7 @@ public extension NullableTextStyle where Cache == Void {
8684
// MARK: + Details where Cache: Style
8785
//=----------------------------------------------------------------------------=
8886

89-
public extension NullableTextStyle where Cache: NullableTextStyle,
90-
Cache.Value == Value, Cache.Cache == Void {
87+
public extension NullableTextStyle where Cache: NullableTextStyle, Cache.Value == Value, Cache.Cache == Void {
9188

9289
//=------------------------------------------------------------------------=
9390
// MARK: Utilities

Sources/DiffableTextKit/DiffableTextStyle+Wrapper.swift

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ public extension WrapperTextStyle {
3737
@inlinable func locale(_ locale: Locale) -> Self {
3838
var S0 = self; S0.base = S0.base.locale(locale); return S0
3939
}
40-
41-
//=------------------------------------------------------------------------=
42-
// MARK: Utilities
43-
//=------------------------------------------------------------------------=
44-
45-
@inlinable static func == (lhs: Self, rhs: Self) -> Bool {
46-
lhs.base == rhs.base
47-
}
4840
}
4941

5042
//=----------------------------------------------------------------------------=
@@ -76,18 +68,15 @@ public extension WrapperTextStyle where Cache == Base.Cache, Value == Base.Value
7668
// MARK: Utilities
7769
//=------------------------------------------------------------------------=
7870

79-
@inlinable func format(_ value: Value,
80-
with cache: inout Cache) -> String {
71+
@inlinable func format(_ value: Value, with cache: inout Cache) -> String {
8172
base.format(value, with: &cache)
8273
}
8374

84-
@inlinable func interpret(_ value: Value,
85-
with cache: inout Cache) -> Commit<Value> {
75+
@inlinable func interpret(_ value: Value, with cache: inout Cache) -> Commit<Value> {
8676
base.interpret(value, with: &cache)
8777
}
8878

89-
@inlinable func resolve(_ proposal: Proposal,
90-
with cache: inout Cache) throws -> Commit<Value> {
79+
@inlinable func resolve(_ proposal: Proposal, with cache: inout Cache) throws -> Commit<Value> {
9180
try base.resolve(proposal, with: &cache)
9281
}
9382
}
@@ -96,52 +85,21 @@ public extension WrapperTextStyle where Cache == Base.Cache, Value == Base.Value
9685
// MARK: + Details where Base: Nullable
9786
//=----------------------------------------------------------------------------=
9887

99-
public extension WrapperTextStyle where Cache == Base.Cache, Value == Base.Value,
100-
Self: NullableTextStyle, Base: NullableTextStyle {
101-
102-
//=------------------------------------------------------------------------=
103-
// MARK: Utilities
104-
//=------------------------------------------------------------------------=
105-
106-
@inlinable func format(optional value: Value?,
107-
with cache: inout Cache) -> String {
108-
base.format(optional: value, with: &cache)
109-
}
110-
111-
@inlinable func interpret(optional value: Value?,
112-
with cache: inout Cache) -> Commit<Value?> {
113-
base.interpret(optional: value, with: &cache)
114-
}
115-
116-
@inlinable func resolve(optional proposal: Proposal,
117-
with cache: inout Cache) throws -> Commit<Value?> {
118-
try base.resolve(optional: proposal, with: &cache)
119-
}
120-
}
121-
122-
//=----------------------------------------------------------------------------=
123-
// MARK: + Details where Base: Nullable
124-
//=----------------------------------------------------------------------------=
125-
126-
public extension WrapperTextStyle where Cache == Base.Cache, Value == Base.Value?,
127-
Base: NullableTextStyle {
88+
public extension WrapperTextStyle where Cache == Base.Cache, Value == Base.Value?, Base: NullableTextStyle {
12889

12990
//=------------------------------------------------------------------------=
13091
// MARK: Utilities
13192
//=------------------------------------------------------------------------=
13293

133-
@inlinable func format(_ value: Value,
134-
with cache: inout Cache) -> String {
94+
@inlinable func format(_ value: Value, with cache: inout Cache) -> String {
13595
base.format(optional: value, with: &cache)
13696
}
13797

138-
@inlinable func interpret(_ value: Value,
139-
with cache: inout Cache) -> Commit<Value> {
98+
@inlinable func interpret(_ value: Value, with cache: inout Cache) -> Commit<Value> {
14099
base.interpret(optional: value, with: &cache)
141100
}
142101

143-
@inlinable func resolve(_ proposal: Proposal,
144-
with cache: inout Cache) throws -> Commit<Value> {
102+
@inlinable func resolve(_ proposal: Proposal, with cache: inout Cache) throws -> Commit<Value> {
145103
try base.resolve(optional: proposal, with: &cache)
146104
}
147105
}

Sources/DiffableTextKit/Styles/Constant.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ public struct ConstantTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
4141
@inlinable public func locale(_ locale: Locale) -> Self { self }
4242
}
4343

44-
//=----------------------------------------------------------------------------=
45-
// MARK: + Conditionals
46-
//=----------------------------------------------------------------------------=
47-
48-
extension ConstantTextStyle: NullableTextStyle where Base: NullableTextStyle { }
49-
5044
//*============================================================================*
5145
// MARK: * Constant x Style
5246
//*============================================================================*

Sources/DiffableTextKit/Styles/Equals.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ where Base: DiffableTextStyle, Equatable: Swift.Equatable {
4545
}
4646
}
4747

48-
//=----------------------------------------------------------------------------=
49-
// MARK: + Conditionals
50-
//=----------------------------------------------------------------------------=
51-
52-
extension EqualsTextStyle: NullableTextStyle where Base: NullableTextStyle { }
53-
5448
//*============================================================================*
5549
// MARK: * Equals x Style
5650
//*============================================================================*

Sources/DiffableTextKit/Styles/Prefix.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,42 @@ public struct PrefixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
4949
}
5050

5151
//=------------------------------------------------------------------------=
52-
// MARK: Helpers
52+
// MARK: Utilities
5353
//=------------------------------------------------------------------------=
5454

5555
@inlinable func label(_ text: inout String) {
56+
//=--------------------------------------=
57+
// None
58+
//=--------------------------------------=
5659
guard !prefix.isEmpty else { return }
60+
//=--------------------------------------=
61+
// Some
62+
//=--------------------------------------=
5763
text = prefix + text
5864
}
5965

6066
@inlinable func label(_ snapshot: inout Snapshot) {
67+
//=--------------------------------------=
68+
// None
69+
//=--------------------------------------=
6170
guard !prefix.isEmpty else { return }
71+
//=--------------------------------------=
72+
// Some
73+
//=--------------------------------------=
6274
let base = snapshot
6375
snapshot = Snapshot(prefix, as: .phantom)
76+
//=--------------------------------------=
77+
// Base x None
78+
//=--------------------------------------=
79+
guard !base.isEmpty else { return snapshot.select(snapshot.endIndex) }
80+
//=--------------------------------------=
81+
// Base x Some
82+
//=--------------------------------------=
6483
let size = snapshot.count
6584
snapshot.append(contentsOf: base)
66-
85+
//=--------------------------------------=
86+
// Base x Some x Selection
87+
//=--------------------------------------=
6788
guard let selection = base.selection else { return }
6889
let offsets = selection.map({ size + $0.attribute })
6990
let lower = snapshot.index(snapshot.startIndex, offsetBy: offsets.lower)
@@ -72,12 +93,6 @@ public struct PrefixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
7293
}
7394
}
7495

75-
//=----------------------------------------------------------------------------=
76-
// MARK: + Conditionals
77-
//=----------------------------------------------------------------------------=
78-
79-
extension PrefixTextStyle: NullableTextStyle where Base: NullableTextStyle { }
80-
8196
//*============================================================================*
8297
// MARK: * Prefix x Style
8398
//*============================================================================*

Sources/DiffableTextKit/Styles/Standalone.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public struct StandaloneTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
5050
base.update(&shared.cache); return try base.resolve(proposal, with: &shared.cache)
5151
}
5252

53+
//=------------------------------------------------------------------------=
54+
// MARK: Utilities
55+
//=------------------------------------------------------------------------=
56+
57+
@inlinable public static func == (lhs: Self, rhs: Self) -> Bool {
58+
lhs.base == rhs.base
59+
}
60+
5361
//*========================================================================*
5462
// MARK: * Storage [...]
5563
//*========================================================================*
@@ -66,12 +74,6 @@ public struct StandaloneTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
6674
}
6775
}
6876

69-
//=----------------------------------------------------------------------------=
70-
// MARK: + Conditionals
71-
//=----------------------------------------------------------------------------=
72-
73-
extension StandaloneTextStyle: NullableTextStyle where Base: NullableTextStyle { }
74-
7577
//*============================================================================*
7678
// MARK: * Standalone x Style
7779
//*============================================================================*

Sources/DiffableTextKit/Styles/Suffix.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,30 @@ public struct SuffixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
4949
}
5050

5151
//=------------------------------------------------------------------------=
52-
// MARK: Helpers
52+
// MARK: Utilities
5353
//=------------------------------------------------------------------------=
5454

5555
@inlinable func label(_ text: inout String) {
56+
//=--------------------------------------=
57+
// None | Some
58+
//=--------------------------------------=
5659
text.append(contentsOf: suffix)
5760
}
5861

5962
@inlinable func label(_ snapshot: inout Snapshot) {
63+
//=--------------------------------------=
64+
// Base x None
65+
//=--------------------------------------=
66+
if snapshot.isEmpty {
67+
snapshot.select(snapshot.endIndex)
68+
}
69+
//=--------------------------------------=
70+
// None | Some
71+
//=--------------------------------------=
6072
snapshot.append(contentsOf: suffix, as: .phantom)
6173
}
6274
}
6375

64-
//=----------------------------------------------------------------------------=
65-
// MARK: + Conditionals
66-
//=----------------------------------------------------------------------------=
67-
68-
extension SuffixTextStyle: NullableTextStyle where Base: NullableTextStyle { }
69-
7076
//*============================================================================*
7177
// MARK: * Suffix x Style
7278
//*============================================================================*

0 commit comments

Comments
 (0)