Skip to content

Commit 362b567

Browse files
committed
Improved: Layout with Resolve options.
1 parent 78c55eb commit 362b567

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Sources/DiffableTextKit/Models/Context.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ extension Context {
253253
self.unique()
254254
self.storage.layout!.merge(
255255
selection: selection,
256-
momentums: momentums)
256+
resolve: [.max, .momentums(momentums)])
257257

258258
update += .selection(layout!.selection != selection)
259259
//=--------------------------------------=

Sources/DiffableTextKit/Models/Layout.swift

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

3737
/// Use this method to defer autocorrection.
3838
@inlinable init(deferring:(snapshot: Snapshot, preference: Selection<Index>?)) {
39-
self.snapshot = deferring.snapshot
39+
self.snapshot = deferring.snapshot
4040
self.preference = deferring.preference
41-
self.selection = Selection(snapshot.endIndex)
41+
self.selection = Selection(snapshot.endIndex)
4242
}
4343

4444
//=------------------------------------------------------------------------=
@@ -50,7 +50,7 @@
5050
//=--------------------------------------=
5151
// Values
5252
//=--------------------------------------=
53-
let selection = selection.map(
53+
let selection = preference ?? selection.map(
5454
lower: { Mismatches .forwards(from: self.snapshot[..<$0], to: snapshot).next },
5555
upper: { Mismatches.backwards(from: self.snapshot[$0...], to: snapshot).next })
5656
//=--------------------------------------=
@@ -65,7 +65,7 @@
6565
// MARK: Transformations
6666
//=------------------------------------------------------------------------=
6767

68-
/// Use this method to resolve a custom or deferred selection.
68+
/// Use this method to resolve selection.
6969
@inlinable mutating func autocorrect() {
7070
self.merge(selection: self.selection)
7171
}
@@ -75,11 +75,11 @@
7575
//=------------------------------------------------------------------------=
7676

7777
/// Use this method on changes to selection.
78-
@inlinable mutating func merge(selection: Selection<Index>, momentums: Bool = false) {
78+
@inlinable mutating func merge(selection: Selection<Index>, resolve: Resolve = []) {
7979
//=--------------------------------------=
8080
// Accept Max Selection
8181
//=--------------------------------------=
82-
if selection == .max(snapshot) {
82+
if resolve.contains(.max), selection == .max(snapshot) {
8383
self.selection = selection; return
8484
}
8585
//=--------------------------------------=
@@ -93,7 +93,7 @@
9393
//=--------------------------------------=
9494
var carets = selection.carets().detached()
9595

96-
if momentums {
96+
if resolve.contains(.momentums) {
9797
carets.lower.momentum = Direction(from: self.selection.lower, to: selection.lower)
9898
carets.upper.momentum = Direction(from: self.selection.upper, to: selection.upper)
9999
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//=----------------------------------------------------------------------------=
2+
// This source file is part of the DiffableTextViews open source project.
3+
//
4+
// Copyright (c) 2022 Oscar Byström Ericsson
5+
// Licensed under Apache License, Version 2.0
6+
//
7+
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
8+
//=----------------------------------------------------------------------------=
9+
10+
//*========================================================================*
11+
// MARK: * Resolve [...]
12+
//*========================================================================*
13+
14+
/// A message describing selection behavior.
15+
@usableFromInline struct Resolve: OptionSet {
16+
17+
//=--------------------------------------------------------------------=
18+
19+
/// Resolve max selection.
20+
public static let max = Self(rawValue: 1 << 0)
21+
22+
/// Resolve selection by using caret momentums.
23+
public static let momentums = Self(rawValue: 1 << 1)
24+
25+
//=--------------------------------------------------------------------=
26+
27+
@usableFromInline var rawValue: UInt8
28+
29+
//=--------------------------------------------------------------------=
30+
31+
@usableFromInline init(rawValue: UInt8) { self.rawValue = rawValue }
32+
}

0 commit comments

Comments
 (0)