Skip to content

Commit 1d914ea

Browse files
committed
Mini-rework: cleanup.
1 parent 28d9e08 commit 1d914ea

File tree

20 files changed

+115
-121
lines changed

20 files changed

+115
-121
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ An open source package for as-you-type formatting in SwiftUI.
1818
| :desert_island: | Standalone | Uses no remote dependencies |
1919
| :open_book: | Open source | 100% transparent, as it should be |
2020

21-
### [Algorithms](Sources/DiffableTextKit/Models)
22-
23-
It uses three main algorithms to determine text selection.
21+
### [Algorithms](Sources/DiffableTextKit)
2422

2523
| | Algorithm | Description | Complexity |
2624
|---|-----------|-------------|------------|
27-
| :book: | Text | Determines selection when text changes | ≤ Linear |
28-
| :left_right_arrow: | Positions | Determines selection by positions/offsets | ≤ Linear |
25+
| :book: | Text | Determines selection when text changes | ≤ Linear |
26+
| :left_right_arrow: | ∆Selection | Determines selection when selection changes | ≤ Linear |
2927
| :star: | Attributes | Determines selection based on attributes | ≤ Linaer |
3028

3129
# Installation

Sources/DiffableTextKit/DiffableTextStyle.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ public protocol DiffableTextStyle: Equatable {
2121
//=------------------------------------------------------------------------=
2222

2323
associatedtype Value: Equatable
24-
25-
//=------------------------------------------------------------------------=
26-
// MARK: Aliases
27-
//=------------------------------------------------------------------------=
28-
29-
typealias Commit = DiffableTextKit.Commit<Value>
30-
typealias Context = DiffableTextKit.Context<Self>
31-
typealias Remote = DiffableTextKit.Remote<Self>
3224

3325
//=------------------------------------------------------------------------=
3426
// MARK: Transformations
@@ -55,7 +47,7 @@ public protocol DiffableTextStyle: Equatable {
5547
///
5648
/// This method is called in response to changes upstream while the view is focused.
5749
///
58-
@inlinable func interpret(_ value: Value) -> Commit
50+
@inlinable func interpret(_ value: Value) -> Commit<Value>
5951

6052
/// Returns a value and a snapshot.
6153
///
@@ -64,7 +56,7 @@ public protocol DiffableTextStyle: Equatable {
6456
/// - Thrown errors result in input cancellation.
6557
/// - Thrown errors have their descriptions printed in DEBUG mode.
6658
///
67-
@inlinable func merge(_ changes: Changes) throws -> Commit
59+
@inlinable func merge(_ changes: Changes) throws -> Commit<Value>
6860

6961
//*========================================================================*
7062
// MARK: * UIKit

Sources/DiffableTextKit/Helpers/Field.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@
3737
// MARK: Accessors
3838
//=------------------------------------------------------------------------=
3939

40-
@inlinable func positions<T>(as offset: T.Type = T.self) -> Range<T.Position> where T: Offset {
40+
@inlinable func positions<T>(as type: Position<T>.Type =
41+
Position<T>.self) -> Range<T.Position> where T: Offset {
4142
snapshot.positions(at: selection)
42-
}
43-
43+
}
44+
}
45+
46+
//=----------------------------------------------------------------------------=
47+
// MARK: + Transformations
48+
//=----------------------------------------------------------------------------=
49+
50+
extension Field {
51+
4452
//=------------------------------------------------------------------------=
45-
// MARK: Transformations
53+
// MARK: Snapshot
4654
//=------------------------------------------------------------------------=
4755

4856
@inlinable mutating func update(snapshot: Snapshot) {
@@ -51,9 +59,13 @@
5159
upper: { Mismatches.backwards(from: self.snapshot[$0...], to: snapshot).next })
5260
self.snapshot = snapshot; self.update(selection: selection)
5361
}
62+
63+
//=------------------------------------------------------------------------=
64+
// MARK: Selection
65+
//=------------------------------------------------------------------------=
5466

5567
@inlinable mutating func update<T>(selection: Range<T.Position>, momentum: Bool) where T: Offset {
56-
let selection = snapshot.indices(at: selection) // translate positions
68+
let selection = snapshot.indices(at: selection) // translate positions as indices
5769
let momentum = momentum ? Directions(from: self.selection, to: selection) : .none
5870
self.update(selection: selection, momentum: momentum)
5971
}

Sources/DiffableTextKit/Helpers/Mismatches.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Next: BidirectionalCollection, Next.Element == Symbol {
2424
@usableFromInline typealias Reversed = Mismatches<ReversedCollection<Prev>, ReversedCollection<Next>>
2525

2626
//=------------------------------------------------------------------------=
27-
// MARK: Prefix
27+
// MARK: Forwards
2828
//=------------------------------------------------------------------------=
2929

3030
@inlinable static func forwards(to next: Next, from prev: Prev) -> Indices {
@@ -65,7 +65,7 @@ Next: BidirectionalCollection, Next.Element == Symbol {
6565
}
6666

6767
//=------------------------------------------------------------------------=
68-
// MARK: Suffix
68+
// MARK: Backwards
6969
//=------------------------------------------------------------------------=
7070

7171
@inlinable static func backwards(to next: Next, from prev: Prev) -> Indices {
@@ -77,3 +77,4 @@ Next: BidirectionalCollection, Next.Element == Symbol {
7777
return (reversed.prev.base, reversed.next.base)
7878
}
7979
}
80+

Sources/DiffableTextKit/Helpers/Range.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
88
//=----------------------------------------------------------------------------=
99

10-
//*============================================================================*
11-
// MARK: * Range
12-
//*============================================================================*
10+
//=----------------------------------------------------------------------------=
11+
// MARK: + Initializers
12+
//=----------------------------------------------------------------------------=
1313

1414
extension Range {
1515

1616
//=------------------------------------------------------------------------=
17-
// MARK: Convenience
17+
// MARK: Convenient
1818
//=------------------------------------------------------------------------=
1919

2020
@inlinable static func empty(_ location: Bound) -> Self {
@@ -26,9 +26,9 @@ extension Range {
2626
}
2727

2828
//=------------------------------------------------------------------------=
29-
// MARK: Map
29+
// MARK: Transformative
3030
//=------------------------------------------------------------------------=
31-
31+
3232
@inlinable static func from<T>(_ source: Range<T>,
3333
map bound: (T) -> Bound) -> Range<Bound> {
3434
//=--------------------------------------=

Sources/DiffableTextKit/Models/Changes.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,25 @@ public struct Changes {
1818
// MARK: State
1919
//=------------------------------------------------------------------------=
2020

21-
public let snapshot: Snapshot
21+
public let snapshot: Snapshot
2222
public var replacement: Snapshot
23-
public var range: Range<Snapshot.Index>
23+
public var indices: Range<Index>
2424

2525
//=------------------------------------------------------------------------=
2626
// MARK: Initializers
2727
//=------------------------------------------------------------------------=
2828

29-
@inlinable public init(to snapshot: Snapshot, as replacement: String, in range: Range<Snapshot.Index>) {
30-
self.snapshot = snapshot; self.replacement = Snapshot(replacement, as: .content); self.range = range
29+
@inlinable init(_ snapshot: Snapshot, with characters: String, at indices: Range<Index>) {
30+
self.snapshot = snapshot; self.indices = indices
31+
self.replacement = Snapshot(characters, as: .content)
3132
}
3233

3334
//=------------------------------------------------------------------------=
34-
// MARK: Utilities
35+
// MARK: Transformations
3536
//=------------------------------------------------------------------------=
3637

3738
/// Returns a new snapshot with the proposed change applied to it.
3839
@inlinable public func proposal() -> Snapshot {
39-
var result = snapshot; result.replaceSubrange(range, with: replacement); return result
40+
var result = snapshot; result.replaceSubrange(indices, with: replacement); return result
4041
}
4142
}

Sources/DiffableTextKit/Models/Context.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
/// - Copy-on-write.
1717
///
1818
public struct Context<Style: DiffableTextStyle> {
19+
public typealias Commit = DiffableTextKit.Commit<Value>
20+
public typealias Remote = DiffableTextKit.Remote<Style>
1921
public typealias Value = Style.Value
20-
public typealias Commit = Style.Commit
21-
public typealias Remote = Style.Remote
2222

2323
//=------------------------------------------------------------------------=
2424
// MARK: State
@@ -92,7 +92,7 @@ public struct Context<Style: DiffableTextStyle> {
9292
public extension Context {
9393

9494
//=------------------------------------------------------------------------=
95-
// MARK: 1st
95+
// MARK: Primary
9696
//=------------------------------------------------------------------------=
9797

9898
@inlinable var focus: Focus {
@@ -112,7 +112,7 @@ public extension Context {
112112
}
113113

114114
//=------------------------------------------------------------------------=
115-
// MARK: 2nd
115+
// MARK: Secondary
116116
//=------------------------------------------------------------------------=
117117

118118
@inlinable var snapshot: Snapshot {
@@ -123,8 +123,9 @@ public extension Context {
123123
field.snapshot.characters
124124
}
125125

126-
@inlinable func selection<T>(as offset: T.Type = T.self) -> Range<T.Position> where T: Offset {
127-
field.positions(as: offset)
126+
@inlinable func selection<T>(as type: Position<T>.Type =
127+
Position<T>.self) -> Range<T.Position> where T: Offset {
128+
field.positions(as: type)
128129
}
129130
}
130131

@@ -213,30 +214,23 @@ public extension Context {
213214
}
214215

215216
//=------------------------------------------------------------------------=
216-
// MARK: Replacement
217+
// MARK: Characters
217218
//=------------------------------------------------------------------------=
218219

219-
@inlinable mutating func merge<T>(_ input: String,
220+
@inlinable mutating func merge<T>(_ characters: String,
220221
in range: Range<T.Position>) throws where T: Offset {
221222
//=--------------------------------------=
222223
// MARK: Values
223224
//=--------------------------------------=
224225
let indices = snapshot.indices(at: range)
225226
let commit = try style.merge(Changes(
226-
to: snapshot, as: input, in: indices))
227+
snapshot, with: characters, at: indices))
227228
//=--------------------------------------=
228229
// MARK: Update
229230
//=--------------------------------------=
230231
self.set(selection: indices.upperBound)
231232
self.merge(Self.focused(style, commit))
232233
}
233-
}
234-
235-
//=----------------------------------------------------------------------------=
236-
// MARK: + Transformations
237-
//=----------------------------------------------------------------------------=
238-
239-
public extension Context {
240234

241235
//=------------------------------------------------------------------------=
242236
// MARK: Selection

Sources/DiffableTextKit/Models/Position.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Foundation
1313
// MARK: * Position
1414
//*============================================================================*
1515

16+
/// A text offset measured in code units.
1617
public struct Position<Offset: DiffableTextKit.Offset>: Comparable {
1718
@inlinable public static var offset: Offset.Type { Offset.self }
1819

@@ -42,18 +43,3 @@ public struct Position<Offset: DiffableTextKit.Offset>: Comparable {
4243
lhs.offset < rhs.offset
4344
}
4445
}
45-
46-
//=----------------------------------------------------------------------------=
47-
// MARK: + Utilities
48-
//=----------------------------------------------------------------------------=
49-
50-
extension Position {
51-
52-
//=------------------------------------------------------------------------=
53-
// MARK: Range
54-
//=------------------------------------------------------------------------=
55-
56-
@inlinable public static func range(_ range: NSRange) -> Range<Self> {
57-
Self(range.lowerBound) ..< Self(range.upperBound)
58-
}
59-
}

Sources/DiffableTextKit/Models/Snapshot.swift

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,6 @@ public struct Snapshot: BidirectionalCollection, RangeReplaceableCollection {
139139
}
140140
}
141141

142-
//=----------------------------------------------------------------------------=
143-
// MARK: + Conversions
144-
//=----------------------------------------------------------------------------=
145-
146-
extension Snapshot: CustomStringConvertible {
147-
148-
//=------------------------------------------------------------------------=
149-
// MARK: Description
150-
//=------------------------------------------------------------------------=
151-
152-
@inlinable public var description: String {
153-
"\(Self.self)(\"\(_characters)\", \(_attributes))"
154-
}
155-
}
156-
157142
//=----------------------------------------------------------------------------=
158143
// MARK: + Transformations
159144
//=----------------------------------------------------------------------------=
@@ -239,9 +224,26 @@ public extension Snapshot {
239224
}
240225
}
241226

227+
//=----------------------------------------------------------------------------=
228+
// MARK: + Conversions
229+
//=----------------------------------------------------------------------------=
230+
231+
extension Snapshot: CustomStringConvertible {
232+
233+
//=------------------------------------------------------------------------=
234+
// MARK: Description
235+
//=------------------------------------------------------------------------=
236+
237+
@inlinable public var description: String {
238+
"\(Self.self)(\"\(_characters)\", \(_attributes))"
239+
}
240+
}
241+
242242
//=----------------------------------------------------------------------------=
243243
// MARK: + Accessors
244244
//=----------------------------------------------------------------------------=
245+
// All extensions below this line are internal implementation details.
246+
//=----------------------------------------------------------------------------=
245247

246248
extension Snapshot {
247249

@@ -328,6 +330,17 @@ extension Snapshot {
328330
// MARK: Forwards / Backwards / To / Through
329331
//=--------------------------------------------------------------------=
330332

333+
334+
@inlinable func caret(from index: Index, towards direction: Direction,
335+
jumping distance: Jump, targeting target: Target) -> Index? {
336+
switch (direction, distance) {
337+
case (.forwards, .to ): return caret(from: index, forwardsTo: target)
338+
case (.forwards, .through): return caret(from: index, forwardsThrough: target)
339+
case (.backwards, .to ): return caret(from: index, backwardsTo: target)
340+
case (.backwards, .through): return caret(from: index, backwardsThrough: target)
341+
}
342+
}
343+
331344
@inlinable func caret(from index: Index, forwardsTo target: Target) -> Index? {
332345
indices[index...].first(where: target)
333346
}
@@ -344,16 +357,6 @@ extension Snapshot {
344357
indices[..<index].last(where: target)
345358
}
346359

347-
@inlinable func caret(from index: Index, towards direction: Direction,
348-
jumping distance: Jump, targeting target: Target) -> Index? {
349-
switch (direction, distance) {
350-
case (.forwards, .to ): return caret(from: index, forwardsTo: target)
351-
case (.forwards, .through): return caret(from: index, forwardsThrough: target)
352-
case (.backwards, .to ): return caret(from: index, backwardsTo: target)
353-
case (.backwards, .through): return caret(from: index, backwardsThrough: target)
354-
}
355-
}
356-
357360
//=------------------------------------------------------------------------=
358361
// MARK: Peek
359362
//=------------------------------------------------------------------------=
@@ -371,7 +374,7 @@ extension Snapshot {
371374
}
372375

373376
//=------------------------------------------------------------------------=
374-
// MARK: Accessors
377+
// MARK: Passthrough
375378
//=------------------------------------------------------------------------=
376379

377380
@inlinable func nonpassthrough(at index: Index) -> Bool {

Sources/DiffableTextStylesXNumeric/Models/Precision.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extension Precision {
105105
integer: integer .upperBound,
106106
fraction: fraction.upperBound)
107107
//=--------------------------------------=
108-
// MARK: Instantiate
108+
// MARK: Return
109109
//=--------------------------------------=
110110
return Self(unchecked: (lower, upper))
111111
}

0 commit comments

Comments
 (0)