-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
moveLeft.ts
31 lines (29 loc) · 1.49 KB
/
moveLeft.ts
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
import { firstBeforeOrNull } from '../../../helpers/arrayhelpers/firstBeforeOrNull';
import { last } from '../../../helpers/arrayhelpers/last';
import { TreeNode } from '../../../SyntaxTreeComponents/Nodes/Base/TreeNode';
import { BranchingNode } from '../../../SyntaxTreeComponents/Nodes/Base/BranchingNode';
import { Placeholder } from '../../../SyntaxTreeComponents/Placeholder/Placeholder';
import { KeyboardMemory } from '../../KeyboardMemory';
import { lastOrNull } from '../../../helpers/arrayhelpers/lastOrNull';
export function moveLeft(k: KeyboardMemory): void {
if (k.current instanceof Placeholder) {
if (k.current.parentNode == null) {
return;
}
const previousPlaceholder: Placeholder | null = firstBeforeOrNull(k.current.parentNode.placeholders, k.current);
if (previousPlaceholder !== null) {
k.current = lastOrNull(previousPlaceholder.nodes) ?? previousPlaceholder;
} else {
const ancestorPlaceholder = k.current.parentNode.parentPlaceholder;
const nodePreviousToParentOfCurrent: TreeNode | null = firstBeforeOrNull(ancestorPlaceholder.nodes, k.current.parentNode);
k.current = nodePreviousToParentOfCurrent ?? ancestorPlaceholder;
}
} else {
if (k.current instanceof BranchingNode) {
const placeholder = last(k.current.placeholders);
k.current = lastOrNull(placeholder.nodes) ?? placeholder;
} else {
k.current = firstBeforeOrNull(k.current.parentPlaceholder.nodes, k.current) ?? k.current.parentPlaceholder;
}
}
}