Skip to content

Commit 8b341fa

Browse files
committed
Cleanup
1 parent 8ff6bc3 commit 8b341fa

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

Diff for: cursorless-talon/src/spoken_forms_output.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from talon import app
66

7+
from .command import CursorlessCommand
8+
79
SPOKEN_FORMS_OUTPUT_PATH = Path.home() / ".cursorless" / "spokenForms.json"
810

911

@@ -31,7 +33,11 @@ def init(self):
3133
def write(self, spoken_forms: list[SpokenFormEntry]):
3234
with open(SPOKEN_FORMS_OUTPUT_PATH, "w") as out:
3335
try:
34-
out.write(json.dumps({"version": 0, "entries": spoken_forms}))
36+
out.write(
37+
json.dumps(
38+
{"version": CursorlessCommand.version, "entries": spoken_forms}
39+
)
40+
)
3541
except Exception:
3642
error_message = (
3743
f"Error writing spoken form json {SPOKEN_FORMS_OUTPUT_PATH}"

Diff for: packages/cursorless-engine/src/CustomSpokenForms.ts

+20-33
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import {
66
} from "@cursorless/common";
77
import { homedir } from "os";
88
import * as path from "path";
9-
import {
10-
CustomRegexSpokenFormEntry,
11-
PairedDelimiterSpokenFormEntry,
12-
SimpleScopeTypeTypeSpokenFormEntry,
13-
getSpokenFormEntries,
14-
} from "./scopeProviders/getSpokenFormEntries";
9+
import { getSpokenFormEntries } from "./scopeProviders/getSpokenFormEntries";
1510
import { SpokenFormMap } from "./SpokenFormMap";
1611
import { defaultSpokenFormMap } from "./DefaultSpokenFormMap";
1712

@@ -21,6 +16,12 @@ export const spokenFormsPath = path.join(
2116
"spokenForms.json",
2217
);
2318

19+
const ENTRY_TYPES = [
20+
"simpleScopeTypeType",
21+
"customRegex",
22+
"pairedDelimiter",
23+
] as const;
24+
2425
/**
2526
* Maintains a list of all scope types and notifies listeners when it changes.
2627
*/
@@ -66,36 +67,22 @@ export class CustomSpokenForms implements SpokenFormMap {
6667
onDidChangeCustomSpokenForms = this.notifier.registerListener;
6768

6869
private async updateSpokenFormMaps(): Promise<void> {
69-
console.log("updateSpokenFormMaps before getSpokenFormEntries");
7070
const entries = await getSpokenFormEntries();
71-
console.log("updateSpokenFormMaps after getSpokenFormEntries");
7271

73-
this.simpleScopeTypeType = Object.fromEntries(
74-
entries
75-
.filter(
76-
(entry): entry is SimpleScopeTypeTypeSpokenFormEntry =>
77-
entry.type === "simpleScopeTypeType",
78-
)
79-
.map(({ id, spokenForms }) => [id, spokenForms] as const),
80-
);
81-
this.customRegex = Object.fromEntries(
82-
entries
83-
.filter(
84-
(entry): entry is CustomRegexSpokenFormEntry =>
85-
entry.type === "customRegex",
86-
)
87-
.map(({ id, spokenForms }) => [id, spokenForms] as const),
88-
);
89-
this.pairedDelimiter = Object.fromEntries(
90-
entries
91-
.filter(
92-
(entry): entry is PairedDelimiterSpokenFormEntry =>
93-
entry.type === "pairedDelimiter",
94-
)
95-
.map(({ id, spokenForms }) => [id, spokenForms] as const),
96-
);
72+
for (const entryType of ENTRY_TYPES) {
73+
// TODO: Handle case where we've added a new scope type but they haven't yet
74+
// updated their talon files. In that case we want to indicate in tree view
75+
// that the scope type exists but they need to update their talon files to
76+
// be able to speak it. We could just detect that there's no entry for it in
77+
// the spoken forms file, but that feels a bit brittle.
78+
// FIXME: How to avoid the type assertion?
79+
this[entryType] = Object.fromEntries(
80+
entries
81+
.filter((entry) => entry.type === entryType)
82+
.map(({ id, spokenForms }) => [id, spokenForms]),
83+
) as any;
84+
}
9785

98-
console.log("updateSpokenFormMaps at end");
9986
this.isInitialized_ = true;
10087
this.notifier.notifyListeners();
10188
}

Diff for: packages/cursorless-engine/src/generateSpokenForm/CustomSpokenFormGenerator.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@ export class CustomSpokenFormGenerator {
2525
);
2626
}
2727

28-
onDidChangeCustomSpokenForms = (listener: Listener<[]>) =>
29-
this.customSpokenForms.onDidChangeCustomSpokenForms(listener);
28+
onDidChangeCustomSpokenForms(listener: Listener<[]>) {
29+
return this.customSpokenForms.onDidChangeCustomSpokenForms(listener);
30+
}
3031

31-
commandToSpokenForm = (command: CommandComplete) =>
32-
this.spokenFormGenerator.command(command);
32+
commandToSpokenForm(command: CommandComplete) {
33+
return this.spokenFormGenerator.command(command);
34+
}
3335

34-
scopeTypeToSpokenForm = (scopeType: ScopeType) =>
35-
this.spokenFormGenerator.scopeType(scopeType);
36+
scopeTypeToSpokenForm(scopeType: ScopeType) {
37+
return this.spokenFormGenerator.scopeType(scopeType);
38+
}
39+
40+
getCustomRegexScopeTypes() {
41+
return this.customSpokenForms.getCustomRegexScopeTypes();
42+
}
3643

37-
getCustomRegexScopeTypes = () =>
38-
this.customSpokenForms.getCustomRegexScopeTypes();
44+
dispose = this.disposer.dispose;
3945
}

Diff for: packages/cursorless-engine/src/scopeProviders/getSpokenFormEntries.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SimpleScopeTypeType } from "@cursorless/common";
1+
import { LATEST_VERSION, SimpleScopeTypeType } from "@cursorless/common";
22
import { readFile } from "fs/promises";
33
import { spokenFormsPath } from "./ScopeInfoProvider";
44
import { SpeakableSurroundingPairName } from "../SpokenFormMap";
@@ -28,7 +28,16 @@ type SpokenFormEntry =
2828

2929
export async function getSpokenFormEntries(): Promise<SpokenFormEntry[]> {
3030
try {
31-
return JSON.parse(await readFile(spokenFormsPath, "utf-8")).entries;
31+
const payload = JSON.parse(await readFile(spokenFormsPath, "utf-8"));
32+
33+
if (payload.version !== LATEST_VERSION) {
34+
// In the future, we'll need to handle migrations. Not sure exactly how yet.
35+
throw new Error(
36+
`Invalid spoken forms version. Expected ${LATEST_VERSION} but got ${payload.version}`,
37+
);
38+
}
39+
40+
return payload.entries;
3241
} catch (err) {
3342
console.error(`Error getting spoken forms`);
3443
console.error(err);

0 commit comments

Comments
 (0)