-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertWithEncapsulateCurrent.swift
26 lines (26 loc) · 1.25 KB
/
insertWithEncapsulateCurrent.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
public extension KeyboardMemory {
func insertWithEncapsulateCurrent(_ newNode: BranchingNode, deleteOuterRoundBracketsIfAny: Bool = false) -> Void {
let encapsulatingPlaceholder = newNode.placeholders[0]
if let current = self.current as? TreeNode {
let siblingNodes : ReferenceArray<TreeNode> = current.parentPlaceholder.nodes
let currentIndex = siblingNodes.indexOf(current)!
siblingNodes[currentIndex] = newNode
newNode.parentPlaceholder = current.parentPlaceholder
if let current = current as? RoundBracketsNode, deleteOuterRoundBracketsIfAny {
current.placeholders[0].nodes.encapsulate(encapsulatingPlaceholder)
self.current = newNode.placeholders.firstAfterOrNil(encapsulatingPlaceholder) ?? newNode
} else if current is PartOfNumberWithDigits {
encapsulatingPlaceholder.nodes.append(current)
current.parentPlaceholder = encapsulatingPlaceholder
encapsulateAllPartsOfNumberWithDigitsLeftOfIndex(currentIndex, siblingNodes, encapsulatingPlaceholder)
self.moveRight()
} else {
encapsulatingPlaceholder.nodes.append(current)
current.parentPlaceholder = encapsulatingPlaceholder
self.moveRight()
}
} else {
self.insert(newNode)
}
}
}