Skip to content

Commit 51a820b

Browse files
committed
Mini-rework: cleanup.
1 parent cdd437b commit 51a820b

File tree

3 files changed

+61
-51
lines changed

3 files changed

+61
-51
lines changed

Sources/DiffableTextKit/Helpers/Field.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension Field {
5454
//=------------------------------------------------------------------------=
5555

5656
@inlinable mutating func update(snapshot: Snapshot) {
57-
let selection = Range.from(selection, // proposed selection calculated first
57+
let selection = Range.map(selection, // proposed selection is calculated first
5858
lower: { Mismatches .forwards(from: self.snapshot[..<$0], to: snapshot).next },
5959
upper: { Mismatches.backwards(from: self.snapshot[$0...], to: snapshot).next })
6060
self.snapshot = snapshot; self.update(selection: selection)
@@ -71,8 +71,8 @@ extension Field {
7171
}
7272

7373
@inlinable mutating func update(selection: Range<Index>, momentum: Directions = .none) {
74-
if selection == snapshot.range { return self.selection = selection } // accept max
75-
self.selection = Range.from(selection, // set preferred carets based on attributes
74+
if selection == snapshot.range { return self.selection = selection } // accept max value
75+
self.selection = Range.map(selection, // set preferred carets based on snapshot attributes
7676
lower: { snapshot.caret(from: $0, towards: momentum.lowerBound, preferring: .forwards) },
7777
upper: { snapshot.caret(from: $0, towards: momentum.upperBound, preferring: .backwards) })
7878
}

Sources/DiffableTextKit/Helpers/Range.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,34 @@
1414
extension Range {
1515

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

20+
/// Creates an empty instance with its lower and upper bound set at the location.
2021
@inlinable static func empty(_ location: Bound) -> Self {
2122
Self(uncheckedBounds: (location, location))
2223
}
2324

25+
/// Creates an instance with the given bounds.
2426
@inlinable static func unchecked(_ bounds: (lower: Bound, upper: Bound)) -> Self {
2527
Self(uncheckedBounds: bounds)
2628
}
2729

2830
//=------------------------------------------------------------------------=
29-
// MARK: Transformative
31+
// MARK: Map
3032
//=------------------------------------------------------------------------=
3133

32-
@inlinable static func from<T>(_ source: Range<T>,
33-
map bound: (T) -> Bound) -> Range<Bound> {
34+
/// Maps the bounds of another instance.
35+
@inlinable static func map<T>(_ source: Range<T>,
36+
bound: (T) -> Bound) -> Range<Bound> {
3437
//=--------------------------------------=
3538
// MARK: Return
3639
//=--------------------------------------=
37-
Self.from(source, lower: bound, upper: bound)
40+
Self.map(source, lower: bound, upper: bound)
3841
}
3942

40-
@inlinable static func from<T>(_ source: Range<T>,
43+
/// Maps the bounds of another instance. It maps the upper bound when it is empty.
44+
@inlinable static func map<T>(_ source: Range<T>,
4145
lower: (T) -> Bound, upper: (T) -> Bound) -> Range<Bound> {
4246
//=--------------------------------------=
4347
// MARK: Single

Sources/DiffableTextKit/Models/Snapshot.swift

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
/// ```
2020
///
2121
/// Set the anchor to force selection of the caret represented by its index. This is required
22-
/// when the snapshot contains only formatting characters, and you want the user's caret to appear
23-
/// at a specific location. As an example, a pattern text style with an empty value may anchor at the
24-
/// index of the first placeholder character.
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.
2525
///
2626
public struct Snapshot: BidirectionalCollection, RangeReplaceableCollection {
2727
@usableFromInline typealias Target = (Index) -> Bool
@@ -158,7 +158,7 @@ public extension Snapshot {
158158
}
159159

160160
//=------------------------------------------------------------------------=
161-
// MARK: Attributes
161+
// MARK: Update
162162
//=------------------------------------------------------------------------=
163163

164164
@inlinable mutating func update(attributes index: Index,
@@ -181,7 +181,7 @@ public extension Snapshot {
181181
}
182182

183183
//=------------------------------------------------------------------------=
184-
// MARK: Replacements
184+
// MARK: Replace
185185
//=------------------------------------------------------------------------=
186186

187187
@inlinable mutating func replaceSubrange<C: Collection>(
@@ -239,10 +239,41 @@ extension Snapshot: CustomStringConvertible {
239239
}
240240
}
241241

242+
//=----------------------------------------------------------------------------=
243+
// All extensions below this line are internal implementation details.
242244
//=----------------------------------------------------------------------------=
243245
// MARK: + Accessors
244246
//=----------------------------------------------------------------------------=
245-
// All extensions below this line are internal implementation details.
247+
248+
extension Snapshot {
249+
250+
//=------------------------------------------------------------------------=
251+
// MARK: Attributes
252+
//=------------------------------------------------------------------------=
253+
254+
@inlinable func nonpassthrough(at index: Index) -> Bool {
255+
!attributes[index.attribute].contains(.passthrough)
256+
}
257+
258+
//=------------------------------------------------------------------------=
259+
// MARK: Peek
260+
//=------------------------------------------------------------------------=
261+
262+
@inlinable func peek(from index: Index, towards direction: Direction) -> Index? {
263+
direction == .forwards ? peek(ahead: index) : peek(behind: index)
264+
}
265+
266+
@inlinable func peek(ahead index: Index) -> Index? {
267+
index != endIndex ? index : nil
268+
}
269+
270+
@inlinable func peek(behind index: Index) -> Index? {
271+
index != startIndex ? self.index(before: index) : nil
272+
}
273+
}
274+
275+
//=----------------------------------------------------------------------------=
276+
// MARK: + Positions
246277
//=----------------------------------------------------------------------------=
247278

248279
extension Snapshot {
@@ -256,27 +287,27 @@ extension Snapshot {
256287
}
257288

258289
//=------------------------------------------------------------------------=
259-
// MARK: Index
290+
// MARK: Single
260291
//=------------------------------------------------------------------------=
261292

262293
@inlinable func index<T>(at position: T.Position) -> Index where T: Offset {
263294
T.index(at: position, in: characters)
264295
}
265296

266-
@inlinable func indices<T>(at positions: Range<T.Position>) -> Range<Index> where T: Offset {
267-
Range.from(positions, map: index(at:))
297+
@inlinable func position<T>(at index: Index) -> T.Position where T: Offset {
298+
T.position(at: index, in: characters)
268299
}
269300

270301
//=------------------------------------------------------------------------=
271-
// MARK: Position
302+
// MARK: Double
272303
//=------------------------------------------------------------------------=
273-
274-
@inlinable func position<T>(at index: Index) -> T.Position where T: Offset {
275-
T.position(at: index, in: characters)
304+
305+
@inlinable func indices<T>(at positions: Range<T.Position>) -> Range<Index> where T: Offset {
306+
Range.map(positions, bound: index(at:))
276307
}
277308

278309
@inlinable func positions<T>(at indices: Range<Index>) -> Range<T.Position> where T: Offset {
279-
Range.from(indices, map: position(at:))
310+
Range.map(indices, bound: position(at:))
280311
}
281312
}
282313

@@ -307,29 +338,28 @@ extension Snapshot {
307338
//=--------------------------------------=
308339
let direction = direction ?? preference
309340
//=--------------------------------------=
310-
// MARK: Search In The Direction
341+
// MARK: Search In Direction
311342
//=--------------------------------------=
312343
if let caret = caret(from: index,
313344
towards: direction,
314345
jumping: direction == preference ? .to : .through,
315346
targeting: nonpassthrough(at:)) { return caret }
316347
//=--------------------------------------=
317-
// MARK: Search In The Other Direction
348+
// MARK: Search In Other Direction
318349
//=--------------------------------------=
319350
if let caret = caret(from: index,
320351
towards: direction.reversed(),
321352
jumping: Jump.to, // use Jump.to on each direction
322353
targeting: nonpassthrough(at:)) { return caret }
323354
//=--------------------------------------=
324-
// MARK: Default To Instance End Index
355+
// MARK: Default To Instance's End Index
325356
//=--------------------------------------=
326357
return self.endIndex
327358
}
328359

329360
//=--------------------------------------------------------------------=
330361
// MARK: Forwards / Backwards / To / Through
331362
//=--------------------------------------------------------------------=
332-
333363

334364
@inlinable func caret(from index: Index, towards direction: Direction,
335365
jumping distance: Jump, targeting target: Target) -> Index? {
@@ -356,28 +386,4 @@ extension Snapshot {
356386
@inlinable func caret(from index: Index, backwardsThrough target: Target) -> Index? {
357387
indices[..<index].last(where: target)
358388
}
359-
360-
//=------------------------------------------------------------------------=
361-
// MARK: Peek
362-
//=------------------------------------------------------------------------=
363-
364-
@inlinable func peek(from index: Index, towards direction: Direction) -> Index? {
365-
direction == .forwards ? peek(ahead: index) : peek(behind: index)
366-
}
367-
368-
@inlinable func peek(ahead index: Index) -> Index? {
369-
index != endIndex ? index : nil
370-
}
371-
372-
@inlinable func peek(behind index: Index) -> Index? {
373-
index != startIndex ? self.index(before: index) : nil
374-
}
375-
376-
//=------------------------------------------------------------------------=
377-
// MARK: Passthrough
378-
//=------------------------------------------------------------------------=
379-
380-
@inlinable func nonpassthrough(at index: Index) -> Bool {
381-
!attributes[index.attribute].contains(.passthrough)
382-
}
383389
}

0 commit comments

Comments
 (0)