Skip to content

Commit f27d87d

Browse files
committed
Improved: Prefix edge case. Tests: Prefix & Suffix.
1 parent 8f9a628 commit f27d87d

File tree

5 files changed

+54
-34
lines changed

5 files changed

+54
-34
lines changed

β€ŽSources/DiffableTextKit/Models/Offset.swiftβ€Ž

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ AdditiveArithmetic, ExpressibleByIntegerLiteral {
9292
// MARK: * Offset x Int [...]
9393
//*============================================================================*
9494

95-
public extension Int { @inlinable init<T>(_ offset: Offset<T>) { self = offset.distance } }
95+
public extension Int {
96+
97+
@inlinable @inline(__always)
98+
init<T>(_ offset: Offset<T>) {
99+
self = offset.distance
100+
}
101+
}
96102

97103
//*============================================================================*
98104
// MARK: * Offsets [...]

β€ŽSources/DiffableTextKit/Styles/Prefix.swiftβ€Ž

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public struct PrefixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
1717
public typealias Cache = Base.Cache
1818
public typealias Value = Base.Value
1919

20-
@usableFromInline typealias Characters = Offset<Character>
21-
2220
//=------------------------------------------------------------------------=
2321
// MARK: State
2422
//=------------------------------------------------------------------------=
@@ -62,12 +60,15 @@ public struct PrefixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
6260
@inlinable func label(_ commit: inout Commit<Value>) {
6361
guard !prefix.isEmpty else { return }
6462
let selection = commit.snapshot.selection
63+
let prefix = Snapshot(prefix,as:.phantom)
6564

66-
commit.snapshot = Snapshot(prefix, as: .phantom) + commit.snapshot
65+
commit.snapshot = prefix + commit.snapshot
6766

6867
guard let selection else { return }
69-
let offsets = selection.map({ Characters(prefix.count + $0.attribute) })
70-
commit.snapshot.select(commit.snapshot.indices(at: offsets.positions()))
68+
let offsets = selection.map({ prefix.count + $0.attribute })
69+
let lower = commit.snapshot.index(commit.snapshot.startIndex, offsetBy: offsets.lower)
70+
let upper = commit.snapshot.index(lower/*-*/, offsetBy: offsets.upper - offsets.lower)
71+
commit.snapshot.select(Range(uncheckedBounds: (lower, upper)))
7172
}
7273
}
7374

β€ŽTests/DiffableTextKitTests/Mock.swiftβ€Ž

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ struct Mock: DiffableTextStyle {
2121
// MARK: State
2222
//=------------------------------------------------------------------------=
2323

24-
var locale: Locale
24+
var locale: Locale
25+
var selection: Bool
2526

2627
//=------------------------------------------------------------------------=
2728
// MARK: Initializers
2829
//=------------------------------------------------------------------------=
2930

30-
init(locale: Locale = .autoupdatingCurrent) { self.locale = locale }
31+
init(locale: Locale = .autoupdatingCurrent, selection: Bool = false) {
32+
self.locale = locale; self.selection = selection
33+
}
3134

3235
//=------------------------------------------------------------------------=
3336
// MARK: Transformations
@@ -46,7 +49,9 @@ struct Mock: DiffableTextStyle {
4649
}
4750

4851
func interpret(_ value: Value, with cache: inout Void) -> Commit<Value> {
49-
Commit(value, Snapshot(value))
52+
var snapshot = Snapshot(value)
53+
if selection { snapshot.selection = .max(snapshot) }
54+
return Commit(value, snapshot)
5055
}
5156

5257
func resolve(_ proposal: Proposal, with cache: inout Void) throws -> Commit<Value> {

β€ŽTests/DiffableTextKitTests/Styles/Prefix.swiftβ€Ž

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,30 @@ import XCTest
1818
//*============================================================================*
1919

2020
final class PrefixTests: XCTestCase {
21-
21+
22+
typealias C = Offset<Character>
23+
2224
//=------------------------------------------------------------------------=
23-
// MARK: Assertions
25+
// MARK: Tests
2426
//=------------------------------------------------------------------------=
2527

26-
func OK(prefix: String, value: String, format: String) {
27-
let style = Mock().prefix(prefix); XCTAssertEqual(style.format(value), format)
28+
func testFormat() {
29+
XCTAssertEqual(Mock().prefix("πŸ‡ΈπŸ‡ͺ").format("πŸ‡ΊπŸ‡Έ"), "πŸ‡ΈπŸ‡ͺπŸ‡ΊπŸ‡Έ")
2830
}
2931

30-
func OK(prefix: String, value: String, interpret: Commit<String>) {
31-
let style = Mock().prefix(prefix); XCTAssertEqual(style.interpret(value), interpret)
32+
func testInterpret() {
33+
XCTAssertEqual(Mock().prefix("πŸ‡ΈπŸ‡ͺ").interpret("πŸ‡ΊπŸ‡Έ"),
34+
Commit("πŸ‡ΊπŸ‡Έ", Snapshot("πŸ‡ΈπŸ‡ͺ", as: .phantom) + "πŸ‡ΊπŸ‡Έ"))
3235
}
3336

34-
//=------------------------------------------------------------------------=
35-
// MARK: Tests
36-
//=------------------------------------------------------------------------=
37-
38-
func test() {
39-
OK(prefix: "πŸ‡ΈπŸ‡ͺ", value: "πŸ‡ΊπŸ‡Έ", format: "πŸ‡ΈπŸ‡ͺπŸ‡ΊπŸ‡Έ")
40-
OK(prefix: "πŸ‡ΈπŸ‡ͺ", value: "πŸ‡ΊπŸ‡Έ", interpret: Commit("πŸ‡ΊπŸ‡Έ", Snapshot("πŸ‡ΈπŸ‡ͺ", as: .phantom) + "πŸ‡ΊπŸ‡Έ"))
37+
func testSelectionInBaseIsOffsetByPrefixSize() {
38+
let characters = "0123456789"
39+
40+
let normal = Mock(selection: true)/*----------*/.interpret(characters).snapshot
41+
let prefix = Mock(selection: true).prefix("...").interpret(characters).snapshot
42+
43+
XCTAssertEqual(normal.selection!.positions(), normal.indices(at: C(0) ..< C(10)))
44+
XCTAssertEqual(prefix.selection!.positions(), prefix.indices(at: C(3) ..< C(13)))
4145
}
4246
}
4347

β€ŽTests/DiffableTextKitTests/Styles/Suffix.swiftβ€Ž

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,29 @@ import XCTest
1919

2020
final class SuffixTests: XCTestCase {
2121

22+
typealias C = Offset<Character>
23+
2224
//=------------------------------------------------------------------------=
23-
// MARK: Assertions
25+
// MARK: Tests
2426
//=------------------------------------------------------------------------=
2527

26-
func OK(value: String, suffix: String, format: String) {
27-
let style = Mock().suffix(suffix); XCTAssertEqual(style.format(value), format)
28+
func testFormat() {
29+
XCTAssertEqual(Mock().suffix("πŸ‡ΈπŸ‡ͺ").format("πŸ‡ΊπŸ‡Έ"), "πŸ‡ΊπŸ‡ΈπŸ‡ΈπŸ‡ͺ")
2830
}
2931

30-
func OK(value: String, suffix: String, interpret: Commit<String>) {
31-
let style = Mock().suffix(suffix); XCTAssertEqual(style.interpret(value), interpret)
32+
func testInterpret() {
33+
XCTAssertEqual(Mock().suffix("πŸ‡ΈπŸ‡ͺ").interpret("πŸ‡ΊπŸ‡Έ"),
34+
Commit("πŸ‡ΊπŸ‡Έ", "πŸ‡ΊπŸ‡Έ" + Snapshot("πŸ‡ΈπŸ‡ͺ", as: .phantom)))
3235
}
3336

34-
//=------------------------------------------------------------------------=
35-
// MARK: Tests
36-
//=------------------------------------------------------------------------=
37-
38-
func test() {
39-
OK(value: "πŸ‡ΈπŸ‡ͺ", suffix: "πŸ‡ΊπŸ‡Έ", format: "πŸ‡ΈπŸ‡ͺπŸ‡ΊπŸ‡Έ")
40-
OK(value: "πŸ‡ΈπŸ‡ͺ", suffix: "πŸ‡ΊπŸ‡Έ", interpret: Commit("πŸ‡ΈπŸ‡ͺ", "πŸ‡ΈπŸ‡ͺ" + Snapshot("πŸ‡ΊπŸ‡Έ", as: .phantom)))
37+
func testSelectionIsSame() {
38+
let characters = "0123456789"
39+
40+
let normal = Mock(selection: true)/*----------*/.interpret(characters).snapshot
41+
let suffix = Mock(selection: true).suffix("...").interpret(characters).snapshot
42+
43+
XCTAssertEqual(normal.selection!.positions(), normal.indices(at: C(0) ..< C(10)))
44+
XCTAssertEqual(suffix.selection!.positions(), suffix.indices(at: C(0) ..< C(10)))
4145
}
4246
}
4347

0 commit comments

Comments
Β (0)