Skip to content

Commit 2dda10f

Browse files
Support a shift interface for state adjustment for non-mutations
1 parent 7ae4be8 commit 2dda10f

13 files changed

Lines changed: 78 additions & 4 deletions

Sources/TextFormation/ClosePairFilter.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Rearrange
66
///
77
/// The logic of this operation is *extremely* complicated.
88
public struct ClosePairFilter<Interface: TextSystemInterface> {
9-
private var locationAfterSkippedClose: Int?
109
private let processAfterTrigger: Bool
1110
private var recognizer: ConsecutiveCharacterRecognizer<Interface>
1211
private var triggerPosition: Interface.Position?
@@ -161,7 +160,15 @@ extension ClosePairFilter: Filter {
161160
self.triggerPosition = mutation.postApplyRange?.upperBound
162161
}
163162
}
164-
163+
164+
public mutating func processShift(by offset: Int, interface: Interface) throws {
165+
try recognizer.processShift(by: offset, interface: interface)
166+
167+
if let pos = triggerPosition {
168+
self.triggerPosition = interface.position(from: pos, offset: offset)
169+
}
170+
}
171+
165172
public mutating func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
166173
guard let pos = triggeringPosition(with: mutation) else {
167174
try recognizerCheck(mutation)

Sources/TextFormation/CompositeFilter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public struct CompositeFilter<Interface: TextSystemInterface> {
1414

1515
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
1616
extension CompositeFilter: Filter {
17+
public mutating func processShift(by offset: Int, interface: Interface) throws {
18+
for index in filters.indices {
19+
try filters[index].processShift(by: offset, interface: interface)
20+
}
21+
}
22+
1723
public mutating func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
1824
for index in filters.indices {
1925
if let output = try filters[index].processMutation(mutation) {

Sources/TextFormation/ConsecutiveCharacterRecognizer.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,27 @@ public struct ConsecutiveCharacterRecognizer<Interface: TextSystemInterface> {
8080
}
8181
}
8282

83+
public mutating func processShift(by offset: Int, interface: Interface) throws {
84+
switch state {
85+
case .idle:
86+
break
87+
case .tracking(let position, let int):
88+
guard let newPos = interface.position(from: position, offset: offset) else {
89+
self.state = .idle
90+
break
91+
}
92+
93+
self.state = .tracking(newPos, int)
94+
case .triggered:
95+
break
96+
}
97+
}
98+
8399
@discardableResult
84100
public mutating func processMutation(_ mutation: TextMutation<Interface>) throws -> Bool {
85101
try updateState(mutation)
86102

103+
print(state)
87104
switch state {
88105
case .triggered:
89106
return true

Sources/TextFormation/DeleteCloseFilter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public struct DeleteCloseFilter<Interface: TextSystemInterface> {
1919
}
2020

2121
extension DeleteCloseFilter: Filter {
22+
public mutating func processShift(by offset: Int, interface: Interface) throws {
23+
}
24+
2225
public func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
2326
let interface = mutation.interface
2427
let range = mutation.range

Sources/TextFormation/Filter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public protocol Filter<Interface> {
2525
associatedtype Interface: TextSystemInterface
2626
typealias Mutation = TextMutation<Interface>
2727

28+
mutating func processShift(by offset: Int, interface: Interface) throws
2829
mutating func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output?
2930
}
3031

Sources/TextFormation/LineLeadingWhitespaceFilter.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ extension LineLeadingWhitespaceFilter: Filter {
4343
delta: mutationOuput.delta + whitespaceOutput.delta
4444
)
4545
}
46-
46+
47+
public mutating func processShift(by offset: Int, interface: Interface) throws {
48+
try recognizer.processShift(by: offset, interface: interface)
49+
}
50+
4751
public mutating func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
4852
if try recognizer.processMutation(mutation) {
4953
if let value = try matchHandler(mutation) {

Sources/TextFormation/NewlineProcessingFilter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public struct NewlineProcessingFilter<Interface: TextSystemInterface> {
99
}
1010

1111
extension NewlineProcessingFilter: Filter {
12+
public mutating func processShift(by offset: Int, interface: Interface) throws {
13+
}
14+
1215
public func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
1316
if mutation.string != lineEndingSequence {
1417
return nil

Sources/TextFormation/NewlineWithinPairFilter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public struct NewlineWithinPairFilter<Interface: TextSystemInterface> {
1313
}
1414

1515
extension NewlineWithinPairFilter: Filter {
16+
public mutating func processShift(by offset: Int, interface: Interface) throws {
17+
}
18+
1619
public func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
1720
let interface = mutation.interface
1821
let pos = mutation.range.lowerBound

Sources/TextFormation/OpenPairReplacementFilter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public struct OpenPairReplacementFilter<Interface: TextSystemInterface> {
1111
}
1212

1313
extension OpenPairReplacementFilter: Filter {
14+
public mutating func processShift(by offset: Int, interface: Interface) throws {
15+
}
16+
1417
public func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
1518
let interface = mutation.interface
1619
let start = mutation.range.lowerBound

Sources/TextFormation/SkipFilter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public struct SkipFilter<Interface: TextSystemInterface> {
99
}
1010

1111
extension SkipFilter: Filter {
12+
public mutating func processShift(by offset: Int, interface: Interface) throws {
13+
}
14+
1215
public func processMutation(_ mutation: TextMutation<Interface>) throws -> Interface.Output? {
1316
let string = mutation.string
1417
let interface = mutation.interface

0 commit comments

Comments
 (0)