Skip to content

Commit 60f933f

Browse files
committed
Added: NormalTextStyle
1 parent 2978a4e commit 60f933f

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

Sources/DiffableTextKit/Models/Proposal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public struct Proposal {
7070
/// Returns a lazy snapshot with the proposed change applied to it.
7171
@inlinable public func merged() -> LazySequence<FlattenSequence<[Slice<Snapshot>]>> {[
7272
proposal.base[..<proposal.range.lowerBound],
73-
proposal.replacement[...], /*-------------*/
73+
proposal.replacement[...],
7474
proposal.base[proposal.range.upperBound...]].lazy.joined()
7575
}
7676
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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: * Normal
12+
//*============================================================================*
13+
14+
/// A normal text style, without bells and whistles.
15+
public struct NormalTextStyle: DiffableTextStyle {
16+
17+
//=------------------------------------------------------------------------=
18+
// MARK: Initializers
19+
//=------------------------------------------------------------------------=
20+
21+
@inlinable @inline(__always) public init() { }
22+
23+
//=------------------------------------------------------------------------=
24+
// MARK: Utilities
25+
//=------------------------------------------------------------------------=
26+
27+
@inlinable @inline(__always) public func format(_ value: String, with cache: inout Void) -> String {
28+
value
29+
}
30+
31+
@inlinable public func interpret(_ value: String, with cache: inout Void) -> Commit<String> {
32+
Commit(value, Snapshot(value))
33+
}
34+
35+
@inlinable public func resolve(_ proposal: Proposal, with cache: inout Void) throws -> Commit<String> {
36+
let S0 = Snapshot(proposal.lazy.merged().nonvirtuals()); return Commit(S0.characters, S0)
37+
}
38+
}
39+
40+
//*============================================================================*
41+
// MARK: * Normal x Init
42+
//*============================================================================*
43+
44+
extension DiffableTextStyle where Self == NormalTextStyle {
45+
46+
//=------------------------------------------------------------------------=
47+
// MARK: Initializers
48+
//=------------------------------------------------------------------------=
49+
50+
/// A normal text style, without bells and whistles.
51+
@inlinable @inline(__always) public static var normal: Self { .init() }
52+
}

Sources/DiffableTextKitXPattern/Style.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ extension PatternTextStyle {
210210
//=----------------------------------=
211211
// Position == Placeholder
212212
//=----------------------------------=
213-
if let predicate = placeholders[pattern[qIndex]] {
213+
if let predicate = placeholders[pattern[qIndex]] {
214214
guard cIndex != content.endIndex else { break matches }
215215
let nonvirtual = content[cIndex]
216216
guard predicate(nonvirtual) /**/ else { break matches }

Sources/DiffableTextViews/Exports.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
@_exported import struct DiffableTextKit.EqualsTextStyle
2020

21+
@_exported import struct DiffableTextKit.NormalTextStyle
22+
2123
@_exported import struct DiffableTextKit.StandaloneTextStyle
2224

2325
@_exported import struct DiffableTextKit.PrefixTextStyle
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
#if DEBUG
11+
12+
@testable import DiffableTextKit
13+
14+
import XCTest
15+
16+
//*============================================================================*
17+
// MARK: * Normal x Tests
18+
//*============================================================================*
19+
20+
final class NormalTests: XCTestCase {
21+
22+
//=------------------------------------------------------------------------=
23+
// MARK: State
24+
//=------------------------------------------------------------------------=
25+
26+
let normal = NormalTextStyle()
27+
28+
//=------------------------------------------------------------------------=
29+
// MARK: Tests
30+
//=------------------------------------------------------------------------=
31+
32+
func testFormat() {
33+
XCTAssertEqual(normal.format("2"), "2")
34+
}
35+
36+
func testInterpret() {
37+
XCTAssertEqual(normal.interpret("3"), Commit("3", "3"))
38+
}
39+
40+
func testResolve() {
41+
let base = Snapshot("13") + Snapshot("o(><)o", as: .phantom)
42+
let middle = base.index(base.startIndex, offsetBy: 1)
43+
let proposal = Proposal(base, with: "2", in: middle ..< middle)
44+
45+
let resolved = try! normal.resolve(proposal)
46+
47+
XCTAssertEqual(resolved.value, "123")
48+
XCTAssertEqual(resolved.snapshot, "123")
49+
}
50+
}
51+
52+
#endif

0 commit comments

Comments
 (0)