-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrefix.swift
121 lines (99 loc) · 4.72 KB
/
Prefix.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//=----------------------------------------------------------------------------=
// 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: * Prefix
//*============================================================================*
/// Adds a prefix to another style.
public struct PrefixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
public typealias Cache = Base.Cache
public typealias Value = Base.Value
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
public var base: Base
public var prefix: String
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@inlinable public init(_ base: Base, prefix: String) {
self.base = base; self.prefix = prefix
}
//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
@inlinable public func format(_ value: Value, with cache: inout Cache) -> String {
var S0 = base.format(value, with: &cache); label(&S0); return S0
}
@inlinable public func interpret(_ value: Value, with cache: inout Cache) -> Commit<Value> {
var S0 = base.interpret(value, with: &cache); label(&S0); return S0
}
@inlinable public func resolve(_ proposal: Proposal, with cache: inout Cache) throws -> Commit<Value> {
var S0 = try base.resolve(proposal, with: &cache); label(&S0); return S0
}
//=------------------------------------------------------------------------=
// MARK: Utilities x Private
//=------------------------------------------------------------------------=
@inlinable func label(_ text: inout String) {
//=--------------------------------------=
// None
//=--------------------------------------=
guard !prefix.isEmpty else { return }
//=--------------------------------------=
// Some
//=--------------------------------------=
text = prefix + text
}
/// This transformation assumes that the base style
/// provides a manual selection when all attributes
/// are passthrough to avoid duplicate computations.
@inlinable func label(_ commit: inout Commit<Value>) {
//=--------------------------------------=
// None
//=--------------------------------------=
guard !prefix.isEmpty else { return }
//=--------------------------------------=
// Some
//=--------------------------------------=
let base = commit.snapshot
commit.snapshot = Snapshot(prefix, as: .phantom)
//=--------------------------------------=
// Base x None
//=--------------------------------------=
guard !base.isEmpty else {
commit.select({ $0.endIndex }); return
}
//=--------------------------------------=
// Base x Some
//=--------------------------------------=
let start = commit.snapshot.endIndex
commit.snapshot.append(contentsOf: base)
//=--------------------------------------=
// Base x Selection
//=--------------------------------------=
guard let selection = commit.selection else { return }
let min = selection.lower.attribute
let max = selection.upper.attribute
let lower = commit.snapshot.index(start, offsetBy: min)
let upper = commit.snapshot.index(lower, offsetBy: max - min)
commit.selection = Selection(unchecked: (lower, upper))
}
}
//*============================================================================*
// MARK: * Prefix x Style
//*============================================================================*
public extension DiffableTextStyle {
typealias Prefix = PrefixTextStyle<Self>
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
/// Adds a prefix to the style.
@inlinable func prefix(_ prefix: String) -> Prefix {
Prefix(self, prefix: prefix)
}
}