Skip to content

Commit 394452a

Browse files
committed
Merge branch 'main' into noFloatingPromises
2 parents 38e9e95 + c29b5cf commit 394452a

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed
Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import {
22
CommandComplete,
3+
CommandLatest,
34
getKey,
45
Hats,
6+
HatStyleMap,
57
splitKey,
8+
TestCaseSnapshot,
69
TutorialContentProvider,
710
TutorialId,
811
} from "@cursorless/common";
912
import {
10-
CustomSpokenFormGenerator,
1113
canonicalizeAndValidateCommand,
14+
CustomSpokenFormGenerator,
1215
getPartialTargetDescriptors,
1316
transformPartialPrimitiveTargets,
1417
} from "@cursorless/cursorless-engine";
1518
import { TutorialError } from "../TutorialError";
1619
import { StepComponent, StepComponentParser } from "../types/StepComponent";
1720
import { cloneDeep, mapKeys } from "lodash-es";
21+
import { produce } from "immer";
1822

1923
/**
2024
* Parses components of the form `{command:takeNear.yml}`. The argument
@@ -33,43 +37,15 @@ export class CursorlessCommandComponentParser implements StepComponentParser {
3337
this.tutorialId,
3438
arg,
3539
);
36-
const command = cloneDeep(canonicalizeAndValidateCommand(fixture.command));
37-
38-
transformPartialPrimitiveTargets(
39-
getPartialTargetDescriptors(command.action),
40-
(target) => {
41-
if (target.mark?.type !== "decoratedSymbol") {
42-
return target;
43-
}
44-
45-
const color = target.mark.symbolColor;
46-
47-
if (this.hats.enabledHatStyles[color] === undefined) {
48-
target.mark.symbolColor = Object.keys(this.hats.enabledHatStyles)[0];
49-
}
5040

51-
return target;
52-
},
41+
const { command, initialState } = substituteMissingHats(
42+
this.hats.enabledHatStyles,
43+
canonicalizeAndValidateCommand(fixture.command),
44+
fixture.initialState,
5345
);
5446

55-
if (fixture.initialState.marks != null) {
56-
fixture.initialState.marks = mapKeys(
57-
fixture.initialState.marks,
58-
(_value, key) => {
59-
const { hatStyle, character } = splitKey(key);
60-
if (this.hats.enabledHatStyles[hatStyle] === undefined) {
61-
return getKey(
62-
Object.keys(this.hats.enabledHatStyles)[0],
63-
character,
64-
);
65-
}
66-
return key;
67-
},
68-
);
69-
}
70-
7147
return {
72-
initialState: fixture.initialState,
48+
initialState,
7349
languageId: fixture.languageId,
7450
trigger: {
7551
type: "command",
@@ -96,3 +72,60 @@ export class CursorlessCommandComponentParser implements StepComponentParser {
9672
return spokenForm.spokenForms[0];
9773
}
9874
}
75+
76+
/**
77+
* If the user has particular hats disabled, substitute them with hats that the
78+
* user actually has enabled.
79+
*
80+
* We just pick the first hat in the list of available hats, but it would
81+
* probably be better to pick a similar hat, eg if the hat is a colored hat,
82+
* pick a different color.
83+
*
84+
* @param hats The IDE hats
85+
* @param command The command to substitute hats in
86+
* @param initialState The initial state snapshot to substitute hats in
87+
* @returns A new command and initial state snapshot with hats substituted
88+
*/
89+
function substituteMissingHats(
90+
enabledHatStyles: HatStyleMap,
91+
command: CommandLatest,
92+
initialState: TestCaseSnapshot,
93+
) {
94+
command = cloneDeep(command);
95+
96+
// Update the hats in the command
97+
transformPartialPrimitiveTargets(
98+
getPartialTargetDescriptors(command.action),
99+
(target) => {
100+
if (target.mark?.type !== "decoratedSymbol") {
101+
return target;
102+
}
103+
104+
const color = target.mark.symbolColor;
105+
106+
if (enabledHatStyles[color] === undefined) {
107+
target.mark.symbolColor = Object.keys(enabledHatStyles)[0];
108+
}
109+
110+
return target;
111+
},
112+
);
113+
114+
// Update the hats in the initial state snapshot
115+
if (initialState.marks != null) {
116+
initialState = produce(initialState, (draft) => {
117+
draft.marks = mapKeys(draft.marks, (_value, key) => {
118+
const { hatStyle, character } = splitKey(key);
119+
if (enabledHatStyles[hatStyle] === undefined) {
120+
return getKey(Object.keys(enabledHatStyles)[0], character);
121+
}
122+
return key;
123+
});
124+
});
125+
}
126+
127+
return {
128+
command,
129+
initialState,
130+
};
131+
}

0 commit comments

Comments
 (0)