Skip to content

Commit 996299a

Browse files
Moved file canonicalizeSpokenFormTestCommand from generate spoken form pr
1 parent 6013ec6 commit 996299a

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import {
2+
Command,
3+
CommandV5,
4+
PartialPrimitiveTargetDescriptorV5,
5+
PartialRangeTargetDescriptorV5,
6+
PartialTargetDescriptorV5,
7+
} from "@cursorless/common";
8+
import { canonicalizeAndValidateCommand } from "./canonicalizeAndValidateCommand";
9+
import { upgradeV0ToV1 } from "./upgradeV0ToV1";
10+
import { upgradeV1ToV2 } from "./upgradeV1ToV2";
11+
import { upgradeV2ToV3 } from "./upgradeV2ToV3";
12+
import { upgradeV3ToV4 } from "./upgradeV3ToV4";
13+
import { upgradeV4ToV5 } from "./upgradeV4ToV5";
14+
15+
/**
16+
* Temporarily here to support spoken form tests for current(v5) Talon side grammar
17+
* TODO: remove
18+
*/
19+
export function canonicalizeSpokenFormTestCommand(
20+
command: Command,
21+
): Required<CommandV5> | null {
22+
while (command.version < 5) {
23+
switch (command.version) {
24+
case 0:
25+
command = upgradeV0ToV1(command);
26+
break;
27+
case 1:
28+
command = upgradeV1ToV2(command);
29+
break;
30+
case 2:
31+
command = upgradeV2ToV3(command);
32+
break;
33+
case 3:
34+
command = upgradeV3ToV4(command);
35+
break;
36+
case 4:
37+
command = upgradeV4ToV5(command);
38+
break;
39+
}
40+
}
41+
42+
if (command.version !== 5) {
43+
return null;
44+
}
45+
46+
const canonicalCommand: Required<CommandV5> = {
47+
version: 5,
48+
spokenForm: "",
49+
action: {
50+
name: command.action.name,
51+
args: command.action.args ?? [],
52+
},
53+
targets: command.targets.map(canonicalizeTargetsV5),
54+
usePrePhraseSnapshot: true,
55+
};
56+
57+
const commandLatest = canonicalizeAndValidateCommand(canonicalCommand);
58+
const spokenForm = generateSpokenForm(commandLatest);
59+
60+
if (spokenForm == null) {
61+
return null;
62+
}
63+
64+
canonicalCommand.spokenForm = spokenForm;
65+
66+
return canonicalCommand;
67+
}
68+
69+
function canonicalizeTargetsV5(
70+
target: PartialTargetDescriptorV5,
71+
): PartialTargetDescriptorV5 {
72+
switch (target.type) {
73+
case "list":
74+
return {
75+
type: target.type,
76+
elements: target.elements.map(
77+
canonicalizeTargetsV5,
78+
) as PartialPrimitiveTargetDescriptorV5[],
79+
};
80+
case "range": {
81+
const result: PartialRangeTargetDescriptorV5 = {
82+
type: target.type,
83+
anchor: canonicalizeTargetsV5(
84+
target.anchor,
85+
) as PartialPrimitiveTargetDescriptorV5,
86+
active: canonicalizeTargetsV5(
87+
target.active,
88+
) as PartialPrimitiveTargetDescriptorV5,
89+
excludeAnchor: target.excludeAnchor,
90+
excludeActive: target.excludeActive,
91+
};
92+
if (target.rangeType != null) {
93+
result.rangeType = target.rangeType;
94+
}
95+
return result;
96+
}
97+
98+
case "primitive": {
99+
const result: PartialTargetDescriptorV5 = {
100+
type: "primitive",
101+
};
102+
if (target.modifiers != null) {
103+
result.modifiers = target.modifiers;
104+
}
105+
if (target.mark != null) {
106+
result.mark = target.mark;
107+
}
108+
return result;
109+
}
110+
case "implicit":
111+
return target;
112+
}
113+
}

0 commit comments

Comments
 (0)