|
| 1 | +import { CommandComplete, TutorialId, loadFixture } from "@cursorless/common"; |
| 2 | +import path from "path"; |
| 3 | +import { CustomSpokenFormGeneratorImpl } from "../generateSpokenForm/CustomSpokenFormGeneratorImpl"; |
| 4 | +import { canonicalizeAndValidateCommand } from "./commandVersionUpgrades/canonicalizeAndValidateCommand"; |
| 5 | +import { StepComponent, StepComponentParser } from "./StepComponent"; |
| 6 | +import { TutorialError } from "./TutorialError"; |
| 7 | + |
| 8 | +export class CursorlessCommandHandler implements StepComponentParser { |
| 9 | + constructor( |
| 10 | + private tutorialRootDir: string, |
| 11 | + private tutorialId: TutorialId, |
| 12 | + private customSpokenFormGenerator: CustomSpokenFormGeneratorImpl, |
| 13 | + ) {} |
| 14 | + |
| 15 | + async parse(arg: string): Promise<StepComponent> { |
| 16 | + const fixture = await loadFixture( |
| 17 | + path.join(this.tutorialRootDir, this.tutorialId, arg), |
| 18 | + ); |
| 19 | + const command = canonicalizeAndValidateCommand(fixture.command); |
| 20 | + |
| 21 | + return { |
| 22 | + initialState: fixture.initialState, |
| 23 | + languageId: fixture.languageId, |
| 24 | + trigger: { |
| 25 | + type: "command", |
| 26 | + command, |
| 27 | + }, |
| 28 | + content: { |
| 29 | + type: "command", |
| 30 | + value: this.getCommandSpokenForm(command), |
| 31 | + }, |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Handle the argument of a "{step:cloneStateInk.yml}"" |
| 37 | + */ |
| 38 | + private getCommandSpokenForm(command: CommandComplete) { |
| 39 | + // command to be said for moving to the next step |
| 40 | + const spokenForm = |
| 41 | + this.customSpokenFormGenerator.commandToSpokenForm(command); |
| 42 | + |
| 43 | + if (spokenForm.type === "error") { |
| 44 | + throw new TutorialError( |
| 45 | + `Error while processing spoken form for command: ${spokenForm.reason}`, |
| 46 | + { requiresTalonUpdate: spokenForm.requiresTalonUpdate }, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + return spokenForm.spokenForms[0]; |
| 51 | + } |
| 52 | +} |
0 commit comments