Skip to content

Commit

Permalink
Added: NormalTextStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Aug 14, 2022
1 parent 2978a4e commit 60f933f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/DiffableTextKit/Models/Proposal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public struct Proposal {
/// Returns a lazy snapshot with the proposed change applied to it.
@inlinable public func merged() -> LazySequence<FlattenSequence<[Slice<Snapshot>]>> {[
proposal.base[..<proposal.range.lowerBound],
proposal.replacement[...], /*-------------*/
proposal.replacement[...],
proposal.base[proposal.range.upperBound...]].lazy.joined()
}
}
Expand Down
52 changes: 52 additions & 0 deletions Sources/DiffableTextKit/Styles/Normal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//=----------------------------------------------------------------------------=
// This source file is part of the DiffableTextViews open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

//*============================================================================*
// MARK: * Normal
//*============================================================================*

/// A normal text style, without bells and whistles.
public struct NormalTextStyle: DiffableTextStyle {

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

@inlinable @inline(__always) public init() { }

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

@inlinable @inline(__always) public func format(_ value: String, with cache: inout Void) -> String {
value
}

@inlinable public func interpret(_ value: String, with cache: inout Void) -> Commit<String> {
Commit(value, Snapshot(value))
}

@inlinable public func resolve(_ proposal: Proposal, with cache: inout Void) throws -> Commit<String> {
let S0 = Snapshot(proposal.lazy.merged().nonvirtuals()); return Commit(S0.characters, S0)
}
}

//*============================================================================*
// MARK: * Normal x Init
//*============================================================================*

extension DiffableTextStyle where Self == NormalTextStyle {

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

/// A normal text style, without bells and whistles.
@inlinable @inline(__always) public static var normal: Self { .init() }
}
2 changes: 1 addition & 1 deletion Sources/DiffableTextKitXPattern/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ extension PatternTextStyle {
//=----------------------------------=
// Position == Placeholder
//=----------------------------------=
if let predicate = placeholders[pattern[qIndex]] {
if let predicate = placeholders[pattern[qIndex]] {
guard cIndex != content.endIndex else { break matches }
let nonvirtual = content[cIndex]
guard predicate(nonvirtual) /**/ else { break matches }
Expand Down
2 changes: 2 additions & 0 deletions Sources/DiffableTextViews/Exports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

@_exported import struct DiffableTextKit.EqualsTextStyle

@_exported import struct DiffableTextKit.NormalTextStyle

@_exported import struct DiffableTextKit.StandaloneTextStyle

@_exported import struct DiffableTextKit.PrefixTextStyle
Expand Down
52 changes: 52 additions & 0 deletions Tests/DiffableTextKitTests/Styles/Normal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//=----------------------------------------------------------------------------=
// This source file is part of the DiffableTextViews open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

#if DEBUG

@testable import DiffableTextKit

import XCTest

//*============================================================================*
// MARK: * Normal x Tests
//*============================================================================*

final class NormalTests: XCTestCase {

//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=

let normal = NormalTextStyle()

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testFormat() {
XCTAssertEqual(normal.format("2"), "2")
}

func testInterpret() {
XCTAssertEqual(normal.interpret("3"), Commit("3", "3"))
}

func testResolve() {
let base = Snapshot("13") + Snapshot("o(><)o", as: .phantom)
let middle = base.index(base.startIndex, offsetBy: 1)
let proposal = Proposal(base, with: "2", in: middle ..< middle)

let resolved = try! normal.resolve(proposal)

XCTAssertEqual(resolved.value, "123")
XCTAssertEqual(resolved.snapshot, "123")
}
}

#endif

0 comments on commit 60f933f

Please sign in to comment.