Skip to content

Commit 6d771e1

Browse files
committed
Cleanup. Selection: positions() -> range(). Snapshot: indices(at:) -> range(at:), distances(to:) -> range(to:)
1 parent 1fc8b60 commit 6d771e1

File tree

12 files changed

+67
-66
lines changed

12 files changed

+67
-66
lines changed

Sources/DiffableTextKit/Models/Context.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public struct Context<Style: DiffableTextStyle> {
4848
self.unique()
4949
self.storage.status = remote.status
5050
self.storage.layout!.merge(
51-
snapshot: remote.commit!.snapshot,
52-
preference: remote.commit!.selection)
51+
snapshot:/**/remote.commit!.snapshot,
52+
preference: remote.commit!.selection)
5353
//=--------------------------------------=
5454
// Inactive
5555
//=--------------------------------------=
@@ -128,12 +128,12 @@ extension Context {
128128
}
129129

130130
@inlinable public var selection: Range<String.Index> {
131-
let selection = layout?.selection.map(\.character).positions()
132-
return selection ?? Selection(backup! .startIndex).positions()
131+
let selection = layout?.selection.map(\.character).range()
132+
return selection ?? Selection(backup! .startIndex).range()
133133
}
134134

135135
@inlinable public func selection<T>(as type: T.Type = T.self) -> Range<Offset<T>> {
136-
layout.map({$0.snapshot.distances(to:$0.selection.positions())}) ?? 0 ..< 0
136+
layout.map({ $0.snapshot.range(to: $0.selection .range()) }) ?? 0 ..< 0
137137
}
138138
}
139139

@@ -206,8 +206,8 @@ extension Context {
206206
self.storage.status.value = commit.value
207207
self.storage.layout!.selection.collapse()
208208
self.storage.layout!.merge(
209-
snapshot: commit.snapshot,
210-
preference: commit.selection)
209+
snapshot:/**/commit.snapshot,
210+
preference: commit.selection)
211211
//=--------------------------------------=
212212
// Return
213213
//=--------------------------------------=
@@ -230,7 +230,8 @@ extension Context {
230230
//=--------------------------------------=
231231
// Values
232232
//=--------------------------------------=
233-
let selection = Selection(layout!.snapshot.indices(at: selection))
233+
let selection = Selection(
234+
layout!.snapshot.range(at: selection))
234235
//=--------------------------------------=
235236
// Update
236237
//=--------------------------------------=

Sources/DiffableTextKit/Models/Offset.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ public extension Offsets {
141141
distance(from: start, to: end)
142142
}
143143

144-
@inlinable @inline(__always) func distances<T>(
145-
to indices: Range<Index>, as type: T.Type = T.self) -> Range<Offset<T>> {
146-
let lower: Offset<T> = distance(from: startIndex, to: indices.lowerBound)
147-
let count: Offset<T> = distance(from: indices.lowerBound, to: indices.upperBound)
144+
@inlinable @inline(__always) func range<T>(
145+
to positions: Range<Index>, as type: T.Type = T.self) -> Range<Offset<T>> {
146+
let lower: Offset<T> = distance(from: /*-----*/ startIndex, to: positions.lowerBound)
147+
let count: Offset<T> = distance(from: positions.lowerBound, to: positions.upperBound)
148148
return lower ..< lower + count
149149
}
150150

@@ -158,12 +158,12 @@ public extension Offsets {
158158
}
159159

160160
@inlinable @inline(__always) func index<T>(
161-
at distances: Offset<T>, as type: T.Type = T.self) -> Index {
162-
index(from: startIndex, move: distances)
161+
at distance: Offset<T>, as type: T.Type = T.self) -> Index {
162+
index(from: startIndex, move: distance)
163163
}
164164

165-
@inlinable @inline(__always) func indices<T>(
166-
at distances: Range<Offset<T>>, as type: T.Type = T.self) -> Range<Index> {
165+
@inlinable @inline(__always) func range<T>(
166+
at distances: Range<Offset<T>>, as type: T.Type = T.self) -> Range<Index> {
167167
let lower = index(from: startIndex, move: distances.lowerBound)
168168
return lower ..< index(from: lower, move: Offset<T>(distances.count))
169169
}
@@ -257,13 +257,13 @@ public extension Encoding {
257257

258258
@inlinable @inline(__always) static func distance(
259259
from start: Snapshot.Index, to end: Snapshot.Index,
260-
in collection: Snapshot) -> Offset<Self> {
260+
in collection: Snapshot) -> Offset<Self> {
261261
distance(from: start.character, to: end.character, in: collection.characters)
262262
}
263263

264264
@inlinable static func index(
265265
from start: Snapshot.Index, move distance: Offset<Self>,
266-
in collection: Snapshot) -> Snapshot.Index {
266+
in collection: Snapshot) -> Snapshot.Index {
267267
var character = index(from: start.character, move: distance, in: collection.characters)
268268
if character != collection.characters.endIndex {
269269
character = collection.characters.rangeOfComposedCharacterSequence(at: character).lowerBound
@@ -288,13 +288,13 @@ public extension Character {
288288

289289
@inlinable @inline(__always) static func distance(
290290
from start: String.Index, to end: String.Index,
291-
in collection: some StringProtocol) -> Offset<Self> {
291+
in collection: some StringProtocol) -> Offset<Self> {
292292
Offset(collection.distance(from: start, to: end))
293293
}
294294

295295
@inlinable @inline(__always) static func index(
296296
from start: String.Index, move distance: Offset<Self>,
297-
in collection: some StringProtocol) -> String.Index {
297+
in collection: some StringProtocol) -> String.Index {
298298
collection.index(start, offsetBy: Int(distance))
299299
}
300300

@@ -304,13 +304,13 @@ public extension Character {
304304

305305
@inlinable @inline(__always) static func distance(
306306
from start: Snapshot.Index, to end: Snapshot.Index,
307-
in collection: Snapshot) -> Offset<Self> {
307+
in collection: Snapshot) -> Offset<Self> {
308308
Offset(collection.distance(from: start, to: end))
309309
}
310310

311311
@inlinable @inline(__always) static func index(
312312
from start: Snapshot.Index, move distance: Offset<Self>,
313-
in collection: Snapshot) -> Snapshot.Index {
313+
in collection: Snapshot) -> Snapshot.Index {
314314
collection.index(start, offsetBy: Int(distance))
315315
}
316316
}
@@ -328,13 +328,13 @@ public extension Unicode.Scalar {
328328

329329
@inlinable @inline(__always) static func distance(
330330
from start: String.Index, to end: String.Index,
331-
in collection: some StringProtocol) -> Offset<Self> {
331+
in collection: some StringProtocol) -> Offset<Self> {
332332
Offset(collection.unicodeScalars.distance(from: start, to: end))
333333
}
334334

335335
@inlinable @inline(__always) static func index(
336336
from start: String.Index, move distance: Offset<Self>,
337-
in collection: some StringProtocol) -> String.Index {
337+
in collection: some StringProtocol) -> String.Index {
338338
collection.unicodeScalars.index(start, offsetBy: Int(distance))
339339
}
340340
}
@@ -352,13 +352,13 @@ public extension UTF16 {
352352

353353
@inlinable @inline(__always) static func distance(
354354
from start: String.Index, to end: String.Index,
355-
in collection: some StringProtocol) -> Offset<Self> {
355+
in collection: some StringProtocol) -> Offset<Self> {
356356
Offset(collection.utf16.distance(from: start, to: end))
357357
}
358358

359359
@inlinable @inline(__always) static func index(
360360
from start: String.Index, move distance: Offset<Self>,
361-
in collection: some StringProtocol) -> String.Index {
361+
in collection: some StringProtocol) -> String.Index {
362362
collection.utf16.index(start, offsetBy: Int(distance))
363363
}
364364
}
@@ -376,13 +376,13 @@ public extension UTF8 {
376376

377377
@inlinable @inline(__always) static func distance(
378378
from start: String.Index, to end: String.Index,
379-
in collection: some StringProtocol) -> Offset<Self> {
379+
in collection: some StringProtocol) -> Offset<Self> {
380380
Offset(collection.utf8.distance(from: start, to: end))
381381
}
382382

383383
@inlinable @inline(__always) static func index(
384384
from start: String.Index, move distance: Offset<Self>,
385-
in collection: some StringProtocol) -> String.Index {
385+
in collection: some StringProtocol) -> String.Index {
386386
collection.utf8.index(start, offsetBy: Int(distance))
387387
}
388388
}

Sources/DiffableTextKit/Models/Proposal.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ public struct Proposal {
2020

2121
public let base: Snapshot
2222
public var replacement: Snapshot
23-
public var range: Range<Snapshot.Index>
23+
public var range: Range<Index>
2424

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

29-
@inlinable public init(_ base: Snapshot, with replacement: Snapshot, in range: Range<Snapshot.Index>) {
29+
@inlinable public init(_ base: Snapshot, with replacement: Snapshot, in range: Range<Index>) {
3030
self.base = base; self.replacement = replacement; self.range = range
3131
}
3232

3333
@inlinable public init<T>(_ base: Snapshot, with replacement: Snapshot, in range: Range<Offset<T>>) {
34-
self.base = base; self.replacement = replacement; self.range = base.indices(at: range)
34+
self.base = base; self.replacement = replacement; self.range = base.range(at: range)
3535
}
3636

3737
//=------------------------------------------------------------------------=

Sources/DiffableTextKit/Models/Selection.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ public struct Selection<Bound: Comparable>: CustomStringConvertible, Equatable {
2828
// MARK: Initializers
2929
//=------------------------------------------------------------------------=
3030

31-
@inlinable public init(unchecked:(lower: Bound, upper: Bound)) {
31+
@inlinable public init(unchecked: (lower: Bound, upper: Bound)){
3232
(self.lower, self.upper) = unchecked; assert(lower <= upper)
3333
}
3434

35-
@inlinable public init(_ position: Bound) {
36-
self.init(unchecked:(position, position))
35+
@inlinable public init(_ bound: Bound){
36+
self.init(unchecked:(bound, bound))
3737
}
3838

39-
@inlinable public init(_ positions: Range<Bound>) {
40-
self.init(unchecked:(positions.lowerBound, positions.upperBound))
39+
@inlinable public init(_ range: Range<Bound>) {
40+
self.init(unchecked:(range.lowerBound, range.upperBound))
4141
}
4242

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

47-
@inlinable public static func max<T>(_ collection: T) -> Self where T: Collection, T.Index == Bound {
47+
@inlinable public static func max<T>(_ collection: T) -> Self where T: Collection, Bound == T.Index {
4848
Self(unchecked: (collection.startIndex, collection.endIndex))
4949
}
5050

@@ -91,14 +91,14 @@ public struct Selection<Bound: Comparable>: CustomStringConvertible, Equatable {
9191
//=------------------------------------------------------------------------=
9292

9393
@inlinable public func carets() -> Selection<Caret<Bound>> {
94-
Selection<Caret>(unchecked: (.lower(lower), .upper(upper)))
94+
Selection<Caret>(unchecked:(.lower(lower), .upper(upper)))
9595
}
9696

9797
@inlinable public func detached() -> (lower: Bound, upper: Bound) {
9898
(lower, upper)
9999
}
100100

101-
@inlinable public func positions() -> Range<Bound> {
102-
Range(uncheckedBounds: (lower, upper))
101+
@inlinable public func range() -> Range<Bound> {
102+
Range(uncheckedBounds:(lower, upper))
103103
}
104104
}

Sources/DiffableTextKit/Models/Snapshot.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ extension Snapshot {
335335
//=--------------------------------------=
336336
// Inspect Initial Index
337337
//=--------------------------------------=
338-
let positions = self.indices
338+
let indices = self.indices
339339

340-
if let adjacent = positions.index(
340+
if let adjacent = indices.index(
341341
from: caret.position,
342342
towards: caret.affinity,
343343
jumping: caret.affinity == .forwards ? .to : .through,
@@ -348,7 +348,7 @@ extension Snapshot {
348348
//=--------------------------------------=
349349
var direction = caret.momentum ?? caret.affinity
350350

351-
if let index = positions.index(
351+
if let index = indices.index(
352352
from: caret.position,
353353
towards: direction,
354354
jumping: direction == caret.affinity ? .to : .through,
@@ -358,7 +358,7 @@ extension Snapshot {
358358
//=--------------------------------------=
359359
direction = direction.reversed()
360360

361-
if let index = positions.index(
361+
if let index = indices.index(
362362
from: caret.position,
363363
towards: direction,
364364
jumping: Jump.to, // direction independent

Sources/DiffableTextKitXUIKit/Models/Downstream.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import UIKit
3737
}
3838

3939
@inlinable var selection: Range<Offset> {
40-
get { view.offsets(at: view.selectedTextRange!) }
41-
set { view.selectedTextRange = view.positions(at: newValue) }
40+
get { view.range(to: view.selectedTextRange!) /*-----*/ }
41+
set { view.selectedTextRange = view.range(at: newValue) }
4242
}
4343

4444
@inlinable var focus: Focus {

Sources/DiffableTextKitXUIKit/Support/UITextField.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ extension UITextField {
2424
// MARK: Utilities
2525
//=------------------------------------------------------------------------=
2626

27-
@inlinable func offset(at position: UITextPosition) -> Offset {
27+
@inlinable func distance(to position: UITextPosition) -> Offset {
2828
Offset(offset(from: beginningOfDocument, to: position))
2929
}
3030

31-
@inlinable func offsets(at positions: UITextRange) -> Range<Offset> {
32-
let lower = offset(at: positions.start)
31+
@inlinable func range(to positions: UITextRange) -> Range<Offset> {
32+
let lower = distance(to: positions.start)
3333
let count = offset(from: positions.start, to: positions.end)
3434
return lower ..< lower + Offset(count)
3535
}
3636

37-
@inlinable func position(at offset: Offset) -> UITextPosition {
38-
position(from: beginningOfDocument, offset: Int(offset))!
37+
@inlinable func position(at distance: Offset) -> UITextPosition {
38+
position(from: beginningOfDocument, offset: Int(distance))!
3939
}
4040

41-
@inlinable func positions(at offsets: Range<Offset>) -> UITextRange {
42-
let lower = position(at: offsets.lowerBound)
43-
let upper = position(from: lower, offset: offsets.count)!
41+
@inlinable func range(at distances: Range<Offset>) -> UITextRange {
42+
let lower = position(at: distances.lowerBound)
43+
let upper = position(from: lower, offset: distances.count)!
4444
return textRange(from: lower, to: upper)!
4545
}
4646
}

Tests/DiffableTextKitTests/Models/Selection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final class SelectionTests: XCTestCase {
6565
//=------------------------------------------------------------------------=
6666

6767
func testPositionsAreLowerToUpperAsRange() {
68-
XCTAssertEqual(Selection(3 ..< 7).positions(), 3 ..< 7)
68+
XCTAssertEqual(Selection(3 ..< 7).range(), 3 ..< 7)
6969
}
7070

7171
func testbMapUpperLowerMapsBothWhenUnequal() {

Tests/DiffableTextKitTests/Models/Snapshot.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,17 @@ final class SnapshotTests: XCTestCase {
155155
//=------------------------------------------------------------------------=
156156

157157
func testReplaceSubrangeWithSymbols() {
158-
snapshot = Snapshot(repeating: " ", count: 4)
159-
snapshot.replaceSubrange(snapshot.indices(at: C(0) ..< 2), with: Snapshot("AA", as: .content))
160-
snapshot.replaceSubrange(snapshot.indices(at: C(2) ..< 4), with: Snapshot("BB", as: .phantom))
158+
snapshot = Snapshot(repeating: " ", count: 4)
159+
snapshot.replaceSubrange(snapshot.range(at: C(0) ..< 2), with: Snapshot("AA", as: .content))
160+
snapshot.replaceSubrange(snapshot.range(at: C(2) ..< 4), with: Snapshot("BB", as: .phantom))
161161

162162
Assert("AABB", [.content, .content, .phantom, .phantom])
163163
}
164164

165165
func testReplaceSubrangeWithCharacters() {
166-
snapshot = Snapshot(repeating: " ", count: 4)
167-
snapshot.replaceSubrange(snapshot.indices(at: C(0) ..< 2), with: "AA", as: .content)
168-
snapshot.replaceSubrange(snapshot.indices(at: C(2) ..< 4), with: "BB", as: .phantom)
166+
snapshot = Snapshot(repeating: " ", count: 4)
167+
snapshot.replaceSubrange(snapshot.range(at: C(0) ..< 2), with: "AA", as: .content)
168+
snapshot.replaceSubrange(snapshot.range(at: C(2) ..< 4), with: "BB", as: .phantom)
169169

170170
Assert("AABB", [.content, .content, .phantom, .phantom])
171171
}

Tests/DiffableTextKitTests/Styles/Prefix.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ final class PrefixTextStyleTests: XCTestCase {
5353
let normal = Mock(selection: true)/*----------*/.interpret(characters)
5454
let prefix = Mock(selection: true).prefix("...").interpret(characters)
5555

56-
XCTAssertEqual(normal.selection!.positions(), normal.snapshot.indices(at: C(0) ..< 10))
57-
XCTAssertEqual(prefix.selection!.positions(), prefix.snapshot.indices(at: C(3) ..< 13))
56+
XCTAssertEqual(normal.selection!.range(), normal.snapshot.range(at: C(0) ..< 10))
57+
XCTAssertEqual(prefix.selection!.range(), prefix.snapshot.range(at: C(3) ..< 13))
5858
}
5959
}
6060

0 commit comments

Comments
 (0)