Skip to content

Commit

Permalink
tutorial: Handle case where user has disabled a hat (#2603)
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey authored Jul 30, 2024
1 parent 27220ee commit a3982a9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/cursorless-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export * from "./spokenForms/defaultSpokenFormMap";
export * from "./testUtil/extractTargetKeys";
export * from "./testUtil/plainObjectToTarget";
export * from "./util/getPartialTargetDescriptors";
export * from "./util/getPrimitiveTargets";
export * from "./util/grammarHelpers";
4 changes: 4 additions & 0 deletions packages/cursorless-tutorial/src/TutorialImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CharacterRange,
Debouncer,
Disposable,
Hats,
HatTokenMap,
IDE,
Notifier,
Expand Down Expand Up @@ -68,6 +69,7 @@ export class TutorialImpl implements Tutorial, CommandRunnerDecorator {
private hatTokenMap: HatTokenMap,
private customSpokenFormGenerator: CustomSpokenFormGenerator,
private contentProvider: TutorialContentProvider,
private hats: Hats,
) {
this.setupStep = this.setupStep.bind(this);
this.reparseCurrentTutorial = this.reparseCurrentTutorial.bind(this);
Expand Down Expand Up @@ -170,6 +172,7 @@ export class TutorialImpl implements Tutorial, CommandRunnerDecorator {
this.customSpokenFormGenerator,
this.getRawTutorial(tutorialId),
this.ide.keyValueStore,
this.hats,
);

this.currentTutorial = tutorialContent;
Expand Down Expand Up @@ -204,6 +207,7 @@ export class TutorialImpl implements Tutorial, CommandRunnerDecorator {
this.customSpokenFormGenerator,
this.getRawTutorial(tutorialId),
this.ide.keyValueStore,
this.hats,
);

this.currentTutorial = tutorialContent;
Expand Down
3 changes: 3 additions & 0 deletions packages/cursorless-tutorial/src/TutorialStepParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Hats,
TutorialContentProvider,
TutorialId,
TutorialStepFragment,
Expand Down Expand Up @@ -41,13 +42,15 @@ export class TutorialStepParser {
contentProvider: TutorialContentProvider,
tutorialId: TutorialId,
customSpokenFormGenerator: CustomSpokenFormGenerator,
hats: Hats,
) {
this.parseTutorialStep = this.parseTutorialStep.bind(this);

const cursorlessCommandParser = new CursorlessCommandComponentParser(
contentProvider,
tutorialId,
customSpokenFormGenerator,
hats,
);

const actionParser = new ActionComponentParser(customSpokenFormGenerator);
Expand Down
3 changes: 3 additions & 0 deletions packages/cursorless-tutorial/src/loadTutorial.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Hats,
RawTutorialContent,
TutorialContentProvider,
TutorialId,
Expand All @@ -16,11 +17,13 @@ export async function loadTutorial(
customSpokenFormGenerator: CustomSpokenFormGenerator,
rawContent: RawTutorialContent,
keyValueStore: KeyValueStore,
hats: Hats,
) {
const parser = new TutorialStepParser(
contentProvider,
tutorialId,
customSpokenFormGenerator,
hats,
);

let tutorialContent: TutorialContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {
CommandComplete,
getKey,
Hats,
splitKey,
TutorialContentProvider,
TutorialId,
} from "@cursorless/common";
import {
CustomSpokenFormGenerator,
canonicalizeAndValidateCommand,
getPartialTargetDescriptors,
transformPartialPrimitiveTargets,
} from "@cursorless/cursorless-engine";
import { TutorialError } from "../TutorialError";
import { StepComponent, StepComponentParser } from "../types/StepComponent";
import { cloneDeep, mapKeys } from "lodash-es";

/**
* Parses components of the form `{command:takeNear.yml}`. The argument
Expand All @@ -19,14 +25,48 @@ export class CursorlessCommandComponentParser implements StepComponentParser {
private contentProvider: TutorialContentProvider,
private tutorialId: TutorialId,
private customSpokenFormGenerator: CustomSpokenFormGenerator,
private hats: Hats,
) {}

async parse(arg: string): Promise<StepComponent> {
const fixture = await this.contentProvider.loadFixture(
this.tutorialId,
arg,
);
const command = canonicalizeAndValidateCommand(fixture.command);
const command = cloneDeep(canonicalizeAndValidateCommand(fixture.command));

transformPartialPrimitiveTargets(
getPartialTargetDescriptors(command.action),
(target) => {
if (target.mark?.type !== "decoratedSymbol") {
return target;
}

const color = target.mark.symbolColor;

if (this.hats.enabledHatStyles[color] === undefined) {
target.mark.symbolColor = Object.keys(this.hats.enabledHatStyles)[0];
}

return target;
},
);

if (fixture.initialState.marks != null) {
fixture.initialState.marks = mapKeys(
fixture.initialState.marks,
(_value, key) => {
const { hatStyle, character } = splitKey(key);
if (this.hats.enabledHatStyles[hatStyle] === undefined) {
return getKey(
Object.keys(this.hats.enabledHatStyles)[0],
character,
);
}
return key;
},
);
}

return {
initialState: fixture.initialState,
Expand Down
4 changes: 3 additions & 1 deletion packages/cursorless-vscode/src/createTutorial.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HatTokenMap, IDE } from "@cursorless/common";
import { Hats, HatTokenMap, IDE } from "@cursorless/common";
import {
CommandRunnerDecorator,
CustomSpokenFormGenerator,
Expand All @@ -21,6 +21,7 @@ export function createTutorial(
) => void,
hatTokenMap: HatTokenMap,
customSpokenFormGenerator: CustomSpokenFormGenerator,
hats: Hats,
) {
const contentProvider = new FileSystemTutorialContentProvider(ide.assetsRoot);

Expand All @@ -29,6 +30,7 @@ export function createTutorial(
hatTokenMap,
customSpokenFormGenerator,
contentProvider,
hats,
);
ide.disposeOnExit(tutorial);
addCommandRunnerDecorator(tutorial);
Expand Down
1 change: 1 addition & 0 deletions packages/cursorless-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export async function activate(
addCommandRunnerDecorator,
hatTokenMap,
customSpokenFormGenerator,
hats,
);

registerCommands(
Expand Down

0 comments on commit a3982a9

Please sign in to comment.