-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteLeft.swift
65 lines (63 loc) · 3.75 KB
/
deleteLeft.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
public extension KeyboardMemory {
func deleteLeft() -> Void {
if let current: Placeholder = self.current as? Placeholder {
guard current.nodes.isEmpty, let currentParentNode : BranchingNode = current.parentNode else {
return
}
if let nonEmptyPlaceholderOnLeft = currentParentNode.placeholders.getFirstNonEmptyOnLeftOf(current) {
if currentParentNode.placeholders.count == 2 && current === currentParentNode.placeholders[1] && current.nodes.count == 0 {
deleteOuterBranchingNodeButNotItsContents(nonEmptyPlaceholderOnLeft)
self.current = nonEmptyPlaceholderOnLeft.nodes.last!
} else {
nonEmptyPlaceholderOnLeft.nodes.removeLast()
self.current = nonEmptyPlaceholderOnLeft.nodes.last ?? nonEmptyPlaceholderOnLeft
}
} else if currentParentNode.placeholders.asValueTypeArray.allSatisfy({ $0.nodes.isEmpty }) {
let ancestorPlaceholder = currentParentNode.parentPlaceholder!
let previousNode = ancestorPlaceholder.nodes.firstBeforeOrNil(currentParentNode)
ancestorPlaceholder.nodes.remove(currentParentNode)
self.current = previousNode ?? ancestorPlaceholder
} else if currentParentNode.placeholders[0] === current && current.nodes.isEmpty && currentParentNode.placeholders.contains(where: { $0.nodes.count > 0 }) {
if let previousNode = currentParentNode.parentPlaceholder.nodes.firstBeforeOrNil(currentParentNode) {
encapsulatePreviousInto(previousNode, current)
self.current = current.nodes.last!
} else {
let nonEmptySiblingPlaceholders = currentParentNode.placeholders.asValueTypeArray.filter{ $0.nodes.count > 0 }
if (nonEmptySiblingPlaceholders.count == 1) {
let nodes = nonEmptySiblingPlaceholders[0].nodes
let ancestorPlaceholder : Placeholder = currentParentNode.parentPlaceholder!
let indexOfParentNode : Int = ancestorPlaceholder.nodes.indexOf(currentParentNode)!
for node in nodes.asValueTypeArray {
node.parentPlaceholder = ancestorPlaceholder
}
ancestorPlaceholder.nodes.replaceSubrange(indexOfParentNode...indexOfParentNode, with: nodes)
self.current = nodes.last!
}
}
}
} else {
if let current = self.current as? BranchingNode, current.placeholders[0].nodes.count > 0 && current.placeholders.asValueTypeArray[1..<current.placeholders.count].allSatisfy({ $0.nodes.isEmpty }) {
let nonEmptyPlaceholder = current.placeholders[0]
deleteOuterBranchingNodeButNotItsContents(nonEmptyPlaceholder)
self.current = nonEmptyPlaceholder.nodes.last!
} else if let current = self.current as? BranchingNode, current.placeholders.contains(where: { $0.nodes.count > 0 }) {
self.current = current.placeholders.asValueTypeArray.flatMap{ $0.nodes.asValueTypeArray }.last!
self.deleteLeft()
} else {
let current = self.current as! TreeNode
let previousNode: TreeNode? = current.parentPlaceholder.nodes.firstBeforeOrNil(current)
current.parentPlaceholder.nodes.remove(current)
self.current = previousNode ?? current.parentPlaceholder
}
}
}
func encapsulatePreviousInto(_ previousNode: TreeNode, _ targetPlaceholder: Placeholder) {
targetPlaceholder.parentNode!.parentPlaceholder.nodes.remove(previousNode)
targetPlaceholder.nodes.append(previousNode)
let previousNodeOldParentPlaceholder : Placeholder = previousNode.parentPlaceholder!
previousNode.parentPlaceholder = targetPlaceholder
if previousNode is PartOfNumberWithDigits {
encapsulateAllPartsOfNumberWithDigitsLeftOfIndex(previousNodeOldParentPlaceholder.nodes.count - 1, previousNodeOldParentPlaceholder.nodes, targetPlaceholder)
}
}
}