Skip to content

Commit

Permalink
[pre-commit.ci lite] apply automatic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci-lite[bot] authored Mar 25, 2024
1 parent 3467420 commit 8baf3e5
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 248 deletions.
14 changes: 7 additions & 7 deletions packages/generate-examples/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable */
export default {
displayName: 'generate-examples',
preset: '../../jest.preset.js',
displayName: "generate-examples",
preset: "../../jest.preset.js",
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
"ts-jest": {
tsconfig: "<rootDir>/tsconfig.spec.json",
},
},
transform: {
'^.+\\.[tj]s$': 'ts-jest',
"^.+\\.[tj]s$": "ts-jest",
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/generate-examples',
moduleFileExtensions: ["ts", "js", "html"],
coverageDirectory: "../../coverage/libs/generate-examples",
};
2 changes: 1 addition & 1 deletion packages/generate-examples/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/generate-examples';
export * from "./lib/generate-examples";
21 changes: 11 additions & 10 deletions packages/generate-examples/src/lib/buildDictionary.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { loadFixture } from './loadFixture.js';
import { loadFixture } from "./loadFixture.js";

Promise.all([
loadFixture('actions', 'bringArgMadeAfterLook'),
loadFixture('decorations', 'chuckBlockAirUntilBatt'),
loadFixture('decorations', 'cutFine'),
loadFixture('decorations', 'chuckLineFine'),
loadFixture('actions', 'bringAirAndBatAndCapToAfterItemEach'),
loadFixture("actions", "bringArgMadeAfterLook"),
loadFixture("decorations", "chuckBlockAirUntilBatt"),
loadFixture("decorations", "cutFine"),
loadFixture("decorations", "chuckLineFine"),
loadFixture("actions", "bringAirAndBatAndCapToAfterItemEach"),
]).then((allItems) => {
allItems.forEach((item) => {
if (item)
if (item) {
console.log(`
.wrapper
.before
${item.before.replace(/\n/gi, '\n ')}
${item.before.replace(/\n/gi, "\n ")}
.during
${(item.during || item.before).replace(/\n/gi, '\n ')}
${(item.during || item.before).replace(/\n/gi, "\n ")}
.command ${item.command}
.after
${item.after.replace(/\n/gi, '\n ')}
${item.after.replace(/\n/gi, "\n ")}
`);
}
});
});
133 changes: 75 additions & 58 deletions packages/generate-examples/src/lib/buildSpokenForm.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import path from 'path';
import fs from 'fs-extra';
const cursorlessRoot = path.resolve('../../../src');
import path from "path";
import fs from "fs-extra";
const cursorlessRoot = path.resolve("../../../src");

const commandDictionaryJson = fs.readJSONSync(
path.join(
cursorlessRoot,
'../cursorless-nx/libs/cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json'
)
) as typeof import('../../../cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json');
"../cursorless-nx/libs/cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json",
),
) as typeof import("../../../cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json");

const letters = 'abcdefghijklmnopqrstuvwxyz'.split('');
const letters = "abcdefghijklmnopqrstuvwxyz".split("");
const defaultAlphabet =
'air bat cap drum each fine gust harp sit jury crunch look made near odd pit quench red sun trap urge vest whale plex yank zip'
.split(' ')
"air bat cap drum each fine gust harp sit jury crunch look made near odd pit quench red sun trap urge vest whale plex yank zip"
.split(" ")
.map((word, index) => [letters[index], word] as const);

const defaultDigits = 'zero one two three four five six seven eight nine'
.split(' ')
const defaultDigits = "zero one two three four five six seven eight nine"
.split(" ")
.map((word, index) => [`${index}`, word] as const);

const letterDictionary = defaultAlphabet
Expand All @@ -32,48 +32,48 @@ const commandDictionary = commandDictionaryJson.sections.reduce(
...r,
[id]: variations[0].spokenForm,
}),
{}
{},
),
};
},
{} as Record<
typeof commandDictionaryJson['sections'][number]['id'],
(typeof commandDictionaryJson)["sections"][number]["id"],
Record<
typeof commandDictionaryJson['sections'][number]['items'][number]['id'],
typeof commandDictionaryJson['sections'][number]['items'][number]['variations'][0]['spokenForm']
(typeof commandDictionaryJson)["sections"][number]["items"][number]["id"],
(typeof commandDictionaryJson)["sections"][number]["items"][number]["variations"][0]["spokenForm"]
>
>
>,
);

type CommandDictionary = typeof commandDictionary;

interface Modifier {
type: 'position' | 'containingScope';
position: keyof CommandDictionary['positions'];
type: "position" | "containingScope";
position: keyof CommandDictionary["positions"];
scopeType: {
type: keyof CommandDictionary['scopes'];
type: keyof CommandDictionary["scopes"];
};
}
type PrimitiveTarget = {
type: 'primitive';
type: "primitive";
modifiers?: Modifier[];
position?: keyof CommandDictionary['positions'];
position?: keyof CommandDictionary["positions"];
mark?: {
type: 'decoratedSymbol';
type: "decoratedSymbol";
character: keyof typeof letterDictionary;
};
};

type RangeTarget = {
type: 'range';
type: "range";
excludeAnchor?: boolean;
excludeActive?: boolean;
anchor: Target;
active: Target;
};

type ListTarget = {
type: 'list';
type: "list";
elements: Target[];
};

Expand All @@ -82,7 +82,7 @@ type Command = { action: { name: string }; targets: Target[] };

function interpolate(
template: string,
handleAction: (counter: number) => string
handleAction: (counter: number) => string,
) {
let counter = -1;
return template.replace(/<[a-z0-9]+>/gi, () => handleAction(++counter));
Expand All @@ -97,22 +97,24 @@ class SpokenForm {

public build() {
return interpolate(this.getTemplate(), (counter) =>
this.parseTarget(this.command.targets[counter])
this.parseTarget(this.command.targets[counter]),
);
}

private getTemplate() {
return commandDictionary['actions'][this.command.action.name];
return commandDictionary["actions"][this.command.action.name];
}

private parseTarget(target: Target): string {
if (!target) throw new Error(`Excess mark`);
if (!target) {
throw new Error(`Excess mark`);
}
switch (target.type) {
case 'primitive':
case "primitive":
return this.parsePrimitiveTarget(target);
case 'range':
case "range":
return this.parseRangeTarget(target);
case 'list':
case "list":
return this.parseListTarget(target);
default: {
// @ts-expect-error - if this is hit we need to add new cases and types
Expand All @@ -126,58 +128,73 @@ class SpokenForm {
?.filter((v): v is Modifier => !!v)
?.map((mod) => {
switch (mod.type) {
case 'position':
return commandDictionary['positions'][mod.position];
case 'containingScope':
return commandDictionary['scopes'][mod.scopeType.type];
case "position":
return commandDictionary["positions"][mod.position];
case "containingScope":
return commandDictionary["scopes"][mod.scopeType.type];
}
throw new Error(`Unknown modifier type ${mod.type}`);
})
.join(' ') || '';
.join(" ") || "";
if (target.position) {
prefix = `${commandDictionary['positions'][target.position]} ${prefix}`;
prefix = `${commandDictionary["positions"][target.position]} ${prefix}`;
}
if (target.mark) {
if (target.mark.type !== 'decoratedSymbol')
if (target.mark.type !== "decoratedSymbol") {
throw new Error(`Unknown target type ${target.mark.type}`);
}
return (
(prefix ? prefix + ' ' : '') + letterDictionary[target.mark.character]
(prefix ? prefix + " " : "") + letterDictionary[target.mark.character]
);
}
if (!prefix) throw new Error(`Unknown mark`);
if (!prefix) {
throw new Error(`Unknown mark`);
}
return prefix;
}

parseRangeTarget(target: RangeTarget) {
let compoundTargetKey;
if (target.excludeAnchor && target.excludeActive)
compoundTargetKey = 'rangeExclusive';
else if (!target.excludeAnchor && !target.excludeActive)
compoundTargetKey = 'rangeInclusive';
else if (!target.excludeAnchor && target.excludeActive)
compoundTargetKey = 'rangeExcludingEnd';
else throw new Error(`Bad inclusion range`);
if (target.excludeAnchor && target.excludeActive) {
compoundTargetKey = "rangeExclusive";
} else if (!target.excludeAnchor && !target.excludeActive) {
compoundTargetKey = "rangeInclusive";
} else if (!target.excludeAnchor && target.excludeActive) {
compoundTargetKey = "rangeExcludingEnd";
} else {
throw new Error(`Bad inclusion range`);
}
return interpolate(
commandDictionary['compoundTargets'][compoundTargetKey],
commandDictionary["compoundTargets"][compoundTargetKey],
(index) => {
if (index === 0) return this.parseTarget(target.anchor);
if (index === 1) return this.parseTarget(target.active);
return '';
}
if (index === 0) {
return this.parseTarget(target.anchor);
}
if (index === 1) {
return this.parseTarget(target.active);
}
return "";
},
);
}
parseListTarget(target: ListTarget) {
return target.elements.reduce((result, element) => {
if (!result) return this.parseTarget(element);
if (!result) {
return this.parseTarget(element);
}
return interpolate(
commandDictionary['compoundTargets']['listConnective'],
commandDictionary["compoundTargets"]["listConnective"],
(index) => {
if (index === 0) return result;
if (index === 1) return this.parseTarget(element);
if (index === 0) {
return result;
}
if (index === 1) {
return this.parseTarget(element);
}
throw Error(`Invalid List`);
}
},
);
}, '');
}, "");
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/generate-examples/src/lib/generate-examples.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function generateExamples(): string {
return 'generate-examples';
return "generate-examples";
}
Loading

0 comments on commit 8baf3e5

Please sign in to comment.