Skip to content

Commit e510acd

Browse files
committed
Cleanup. Renamed some dev stuff.
1 parent fd5ff46 commit e510acd

File tree

18 files changed

+110
-105
lines changed

18 files changed

+110
-105
lines changed

Sources/DiffableTextKit/DiffableTextStyle.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public protocol DiffableTextStyle: Equatable {
5656
/// - Thrown errors result in input cancellation.
5757
/// - Thrown errors have their descriptions printed in DEBUG mode.
5858
///
59-
@inlinable func merge(_ proposal: Proposal) throws -> Commit<Value>
59+
@inlinable func resolve(_ proposal: Proposal) throws -> Commit<Value>
6060
}
6161

6262
//=----------------------------------------------------------------------------=
@@ -110,20 +110,20 @@ extension DiffableTextStyleWrapper {
110110

111111
@inlinable @inline(__always)
112112
public func format(_ value: Value) -> String
113-
where Style.Value == Value {
113+
where Value == Style.Value {
114114
style.format(value)
115115
}
116116

117117
@inlinable @inline(__always)
118118
public func interpret(_ value: Value) -> Commit<Value>
119-
where Style.Value == Value {
119+
where Value == Style.Value {
120120
style.interpret(value)
121121
}
122122

123123
@inlinable @inline(__always)
124-
public func merge(_ proposal: Proposal) throws -> Commit<Value>
125-
where Style.Value == Value {
126-
try style.merge(proposal)
124+
public func resolve(_ proposal: Proposal) throws -> Commit<Value>
125+
where Value == Style.Value {
126+
try style.resolve(proposal)
127127
}
128128

129129
//=------------------------------------------------------------------------=

Sources/DiffableTextKit/Helpers/Carets.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,75 @@
1616
/// An instance with equal bounds represents a single upper caret.
1717
/// This distinction matters for transformations such as map(lower:upper:).
1818
///
19-
@usableFromInline struct Carets<Bound: Comparable>: Equatable {
19+
@usableFromInline struct Carets<Caret: Comparable>: Equatable {
2020

2121
//=------------------------------------------------------------------------=
2222
// MARK: State
2323
//=------------------------------------------------------------------------=
2424

25-
@usableFromInline let range: Range<Bound>
25+
@usableFromInline let range: Range<Caret>
2626

2727
//=------------------------------------------------------------------------=
2828
// MARK: Initializers
2929
//=------------------------------------------------------------------------=
3030

31-
@inlinable init(_ range: Range<Bound>) {
31+
@inlinable init(_ range: Range<Caret>) {
3232
self.range = range
3333
}
3434

35-
@inlinable init(_ bound: Bound) {
35+
@inlinable init(_ bound: Caret) {
3636
self.init(Range(uncheckedBounds: (bound, bound)))
3737
}
3838

39-
@inlinable static func unchecked(_ bounds: (lower: Bound, upper: Bound)) -> Self {
39+
@inlinable static func unchecked(_ bounds: (Caret, Caret)) -> Self {
4040
Self.init(Range(uncheckedBounds: bounds))
4141
}
4242

4343
//=------------------------------------------------------------------------=
4444
// MARK: Accessors
4545
//=------------------------------------------------------------------------=
4646

47-
@inlinable var lowerBound: Bound {
47+
@inlinable var lower: Caret {
4848
range.lowerBound
4949
}
5050

51-
@inlinable var upperBound: Bound {
51+
@inlinable var upper: Caret {
5252
range.upperBound
5353
}
5454

5555
//=------------------------------------------------------------------------=
5656
// MARK: Transformations
5757
//=------------------------------------------------------------------------=
5858

59-
@inlinable func map<T>(caret: (Bound) -> T) -> Carets<T> {
59+
@inlinable mutating func collapse() {
60+
self = Self(upper)
61+
}
62+
63+
//=------------------------------------------------------------------------=
64+
// MARK: Transformations
65+
//=------------------------------------------------------------------------=
66+
67+
@inlinable func map<T>(caret: (Caret) -> T) -> Carets<T> {
6068
//=--------------------------------------=
6169
// Return
6270
//=--------------------------------------=
6371
self.map(lower: caret, upper: caret)
6472
}
6573

66-
@inlinable func map<T>(lower: (Bound) -> T, upper: (Bound) -> T) -> Carets<T> {
74+
@inlinable func map<T>(lower: (Caret) -> T, upper: (Caret) -> T) -> Carets<T> {
6775
//=--------------------------------------=
6876
// Single
6977
//=--------------------------------------=
70-
let upperBound = upper(range.upperBound)
71-
var lowerBound = upperBound
78+
let max = upper(self.upper); var min = max
7279
//=--------------------------------------=
7380
// Double
7481
//=--------------------------------------=
7582
if !range.isEmpty {
76-
lowerBound = lower(range.lowerBound)
77-
lowerBound = Swift.min(lowerBound, upperBound)
83+
min = Swift.min(lower(self.lower), max)
7884
}
7985
//=--------------------------------------=
8086
// Return
8187
//=--------------------------------------=
82-
return .unchecked((lowerBound, upperBound))
88+
return .unchecked((min, max))
8389
}
8490
}

Sources/DiffableTextKit/Helpers/Directions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}
3737

3838
@inlinable init<T>(from start: Carets<T>, to end: Carets<T>) {
39-
self.lower = Direction(start.lowerBound, to: end.lowerBound)
40-
self.upper = Direction(start.upperBound, to: end.upperBound)
39+
self.lower = Direction(from: start.lower, to: end.lower)
40+
self.upper = Direction(from: start.upper, to: end.upper)
4141
}
4242
}

Sources/DiffableTextKit/Models/Changes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// MARK: Declaration
1212
//*============================================================================*
1313

14-
/// Used to store state comparison results in order to avoid multiple comparisons.
14+
/// A model used to capture comparison results.
1515
public struct Changes: OptionSet {
1616

1717
//=------------------------------------------------------------------------=

Sources/DiffableTextKit/Models/Context.swift

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,42 @@ public extension Context {
163163
// MARK: Transformations
164164
//=----------------------------------------------------------------------------=
165165

166-
public extension Context {
166+
extension Context {
167167

168168
//=------------------------------------------------------------------------=
169169
// MARK: Context
170170
//=------------------------------------------------------------------------=
171171

172-
@inlinable internal mutating func merge(_ other: Self) {
172+
@inlinable mutating func merge(_ other: Self) {
173173
//=--------------------------------------=
174-
// Focused
174+
// Active
175175
//=--------------------------------------=
176176
if other.focus == true {
177177
self.write {
178178
$0.status = other.status
179179
$0.layout.merge(snapshot: other.snapshot)
180180
}
181181
//=--------------------------------------=
182-
// Unfocused
182+
// Inactive
183183
//=--------------------------------------=
184184
} else { self = other }
185185
}
186186

187+
//=------------------------------------------------------------------------=
188+
// MARK: Selection
189+
//=------------------------------------------------------------------------=
190+
191+
@inlinable mutating func collapse() {
192+
self.write({ $0.layout.selection.collapse() })
193+
}
194+
}
195+
196+
//=----------------------------------------------------------------------------=
197+
// MARK: Transformations
198+
//=----------------------------------------------------------------------------=
199+
200+
public extension Context {
201+
187202
//=------------------------------------------------------------------------=
188203
// MARK: Status
189204
//=------------------------------------------------------------------------=
@@ -201,7 +216,7 @@ public extension Context {
201216
//=--------------------------------------=
202217
// Update
203218
//=--------------------------------------=
204-
self.merge(Self.init(next))
219+
self.merge(Self(next))
205220
//=--------------------------------------=
206221
// Return
207222
//=--------------------------------------=
@@ -218,13 +233,13 @@ public extension Context {
218233
//=--------------------------------------=
219234
// Values
220235
//=--------------------------------------=
221-
let carets = layout.indices(at: Carets(range))
222-
let commit = try style.merge(Proposal(
223-
snapshot, with: characters, in: carets.range))
236+
let commit = try style.resolve(Proposal(
237+
update: snapshot, with: characters, in:
238+
layout.indices(at: Carets(range)).range))
224239
//=--------------------------------------=
225240
// Update
226241
//=--------------------------------------=
227-
self.set(selection: carets.upperBound)
242+
self.collapse()
228243
self.merge(Self.focused(style, commit))
229244
//=--------------------------------------=
230245
// Return
@@ -236,12 +251,20 @@ public extension Context {
236251
// MARK: Selection
237252
//=------------------------------------------------------------------------=
238253

239-
@inlinable internal mutating func set(selection: Index) {
240-
self.write({ $0.layout.selection = Carets(selection) })
241-
}
242-
243-
@inlinable mutating func merge<T>(selection: Range<T.Position>, momentums: Bool) -> Update where T: Offset {
244-
self.write({ $0.layout .merge(selection: Carets(selection), momentums: momentums) })
245-
return Update.selection(selection != self.selection())
254+
@inlinable mutating func merge<T>(
255+
selection: Range<T.Position>,
256+
momentums: Bool) -> Update where T: Offset {
257+
//=--------------------------------------=
258+
// Update
259+
//=--------------------------------------=
260+
self.write {
261+
$0.layout.merge(
262+
selection: Carets(selection),
263+
momentums: momentums)
264+
}
265+
//=--------------------------------------=
266+
// Return
267+
//=--------------------------------------=
268+
return .selection(selection != self.selection())
246269
}
247270
}

Sources/DiffableTextKit/Models/Direction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// MARK: Initializers
2626
//=------------------------------------------------------------------------=
2727

28-
@inlinable public init?<T>(_ start: T, to end: T) where T: Comparable {
28+
@inlinable public init?<T>(from start: T, to end: T) where T: Comparable {
2929
if start < end { self = .forwards }
3030
else if start > end { self = .backwards }
3131
else { return nil }

Sources/DiffableTextKit/Models/Position.swift

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,3 @@ public struct Position<Offset: DiffableTextKit.Offset>: Comparable {
4343
lhs.offset < rhs.offset
4444
}
4545
}
46-
47-
//*============================================================================*
48-
// MARK: Range
49-
//*============================================================================*
50-
51-
public extension Range {
52-
53-
//=------------------------------------------------------------------------=
54-
// MARK: Initializers
55-
//=------------------------------------------------------------------------=
56-
57-
@inlinable init<T>(nsrange: NSRange,
58-
as position: Position<T>.Type = Position<T>.self)
59-
where T: Offset, Bound == Position<T> {
60-
self.init(uncheckedBounds: (
61-
Position(nsrange.lowerBound),
62-
Position(nsrange.upperBound)))
63-
}
64-
}

Sources/DiffableTextKit/Models/Proposal.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public struct Proposal {
2626
// MARK: Initializers
2727
//=------------------------------------------------------------------------=
2828

29-
@inlinable init(_ snapshot: Snapshot, with characters: String, in range: Range<Index>) {
30-
self.snapshot = snapshot
31-
self.replacement = Snapshot(characters, as: .content)
32-
self.range = range
29+
@inlinable init(update snapshot: Snapshot, with characters: String, in range: Range<Index>) {
30+
self.snapshot = snapshot; self.replacement = Snapshot(characters); self.range = range
3331
}
3432

3533
//=------------------------------------------------------------------------=
@@ -38,8 +36,6 @@ public struct Proposal {
3836

3937
/// Returns a new snapshot with the proposed change applied to it.
4038
@inlinable public func merged() -> Snapshot {
41-
var result = snapshot
42-
result.replaceSubrange(range, with: replacement)
43-
return result
39+
var result = snapshot; result.replaceSubrange(range, with: replacement); return result
4440
}
4541
}

Sources/DiffableTextKit/Models/Snapshot.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
/// |x|o|o|o|x|o|o|o|o|o|o|o|x|x|x|x|~
1919
/// ```
2020
///
21-
/// Set the anchor to force selection of the caret represented by its index. This is required
22-
/// when a snapshot contains only formatting characters, and you want the caret to appear
23-
/// at a location. As an example, a pattern style bound to an enmpty value may want to set
24-
/// the anchor at the index of the first placeholder character.
21+
/// Set the anchor to select the caret represented by its index. This may be done
22+
/// on snapshots containing only formatting characters. As an example, a pattern text style
23+
/// that is bound to an empty value may anchor at the first placeholder character.
2524
///
2625
public struct Snapshot: BidirectionalCollection, RangeReplaceableCollection {
2726
@usableFromInline typealias Target = (Index) -> Bool
@@ -47,19 +46,19 @@ public struct Snapshot: BidirectionalCollection, RangeReplaceableCollection {
4746
// MARK: Initializers: Attribute
4847
//=------------------------------------------------------------------------=
4948

50-
@inlinable public init(_ characters: String, as attribute: Attribute) {
49+
@inlinable public init(_ characters: String, as attribute: Attribute = []) {
5150
self._characters = characters
5251
self._attributes = Array(repeating: attribute, count: characters.count)
5352
}
5453

55-
@inlinable public init<C>(_ characters: C, as attribute: Attribute) where
56-
C: RandomAccessCollection, C.Element == Character {
54+
@inlinable public init<C>(_ characters: C, as attribute: Attribute = [])
55+
where C: RandomAccessCollection, C.Element == Character {
5756
self._characters = String(characters)
5857
self._attributes = Array(repeating: attribute, count: characters.count)
5958
}
6059

61-
@inlinable public init<S>(_ characters: S, as attribute: Attribute) where
62-
S: Sequence, S.Element == Character { self.init()
60+
@inlinable public init<S>(_ characters: S, as attribute: Attribute = [])
61+
where S: Sequence, S.Element == Character { self.init()
6362
for character in characters {
6463
self._characters.append(character)
6564
self._attributes.append(attribute)

Sources/DiffableTextKit/Models/Status.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,34 @@ public struct Status<Style: DiffableTextStyle> {
3838
//=------------------------------------------------------------------------=
3939

4040
@inlinable public mutating func merge(_ other: Self) -> Changes {
41-
let update = (self .!= other)
41+
let changes = (self .!= other)
4242
//=--------------------------------------=
4343
// Update
4444
//=--------------------------------------=
45-
if update.contains(.style) { self.style = other.style }
46-
if update.contains(.value) { self.value = other.value }
47-
if update.contains(.focus) { self.focus = other.focus }
45+
if changes.contains(.style) { self.style = other.style }
46+
if changes.contains(.value) { self.value = other.value }
47+
if changes.contains(.focus) { self.focus = other.focus }
4848
//=--------------------------------------=
4949
// Return
5050
//=--------------------------------------=
51-
return update
51+
return changes
5252
}
5353

5454
//=------------------------------------------------------------------------=
5555
// MARK: Utilities
5656
//=------------------------------------------------------------------------=
5757

5858
@inlinable static func .!= (lhs: Self, rhs: Self) -> Changes {
59-
var update = Changes()
59+
var changes = Changes()
6060
//=--------------------------------------=
6161
// Compare
6262
//=--------------------------------------=
63-
if lhs.style != rhs.style { update.formUnion(.style) }
64-
if lhs.value != rhs.value { update.formUnion(.value) }
65-
if lhs.focus != rhs.focus { update.formUnion(.focus) }
63+
if lhs.style != rhs.style { changes.formUnion(.style) }
64+
if lhs.value != rhs.value { changes.formUnion(.value) }
65+
if lhs.focus != rhs.focus { changes.formUnion(.focus) }
6666
//=--------------------------------------=
6767
// Return
6868
//=--------------------------------------=
69-
return update
69+
return changes
7070
}
7171
}

0 commit comments

Comments
 (0)