|
| 1 | +import { |
| 2 | + CustomRegexScopeType, |
| 3 | + Disposer, |
| 4 | + Notifier, |
| 5 | + showError, |
| 6 | +} from "@cursorless/common"; |
| 7 | +import { isEqual } from "lodash"; |
| 8 | +import { |
| 9 | + DefaultSpokenFormMapEntry, |
| 10 | + defaultSpokenFormInfo, |
| 11 | + defaultSpokenFormMap, |
| 12 | +} from "./DefaultSpokenFormMap"; |
| 13 | +import { |
| 14 | + SpokenFormMap, |
| 15 | + SpokenFormMapEntry, |
| 16 | + SpokenFormType, |
| 17 | +} from "./SpokenFormMap"; |
| 18 | +import { |
| 19 | + NeedsInitialTalonUpdateError, |
| 20 | + SpokenFormEntry, |
| 21 | + TalonSpokenForms, |
| 22 | +} from "./scopeProviders/SpokenFormEntry"; |
| 23 | +import { ide } from "./singletons/ide.singleton"; |
| 24 | + |
| 25 | +const ENTRY_TYPES = [ |
| 26 | + "simpleScopeTypeType", |
| 27 | + "customRegex", |
| 28 | + "pairedDelimiter", |
| 29 | +] as const; |
| 30 | + |
| 31 | +type Writable<T> = { |
| 32 | + -readonly [K in keyof T]: T[K]; |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Maintains a list of all scope types and notifies listeners when it changes. |
| 37 | + */ |
| 38 | +export class CustomSpokenForms { |
| 39 | + private disposer = new Disposer(); |
| 40 | + private notifier = new Notifier(); |
| 41 | + |
| 42 | + private spokenFormMap_: Writable<SpokenFormMap> = { ...defaultSpokenFormMap }; |
| 43 | + |
| 44 | + get spokenFormMap(): SpokenFormMap { |
| 45 | + return this.spokenFormMap_; |
| 46 | + } |
| 47 | + |
| 48 | + private customSpokenFormsInitialized_ = false; |
| 49 | + private needsInitialTalonUpdate_: boolean | undefined; |
| 50 | + |
| 51 | + /** |
| 52 | + * If `true`, indicates they need to update their Talon files to get the |
| 53 | + * machinery used to share spoken forms from Talon to the VSCode extension. |
| 54 | + */ |
| 55 | + get needsInitialTalonUpdate() { |
| 56 | + return this.needsInitialTalonUpdate_; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Whether the custom spoken forms have been initialized. If `false`, the |
| 61 | + * default spoken forms are currently being used while the custom spoken forms |
| 62 | + * are being loaded. |
| 63 | + */ |
| 64 | + get customSpokenFormsInitialized() { |
| 65 | + return this.customSpokenFormsInitialized_; |
| 66 | + } |
| 67 | + |
| 68 | + constructor(private talonSpokenForms: TalonSpokenForms) { |
| 69 | + this.disposer.push( |
| 70 | + talonSpokenForms.onDidChange(() => this.updateSpokenFormMaps()), |
| 71 | + ); |
| 72 | + |
| 73 | + this.updateSpokenFormMaps(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Registers a callback to be run when the custom spoken forms change. |
| 78 | + * @param callback The callback to run when the scope ranges change |
| 79 | + * @returns A {@link Disposable} which will stop the callback from running |
| 80 | + */ |
| 81 | + onDidChangeCustomSpokenForms = this.notifier.registerListener; |
| 82 | + |
| 83 | + private async updateSpokenFormMaps(): Promise<void> { |
| 84 | + let entries: SpokenFormEntry[]; |
| 85 | + try { |
| 86 | + entries = await this.talonSpokenForms.getSpokenFormEntries(); |
| 87 | + } catch (err) { |
| 88 | + if (err instanceof NeedsInitialTalonUpdateError) { |
| 89 | + // Handle case where spokenForms.json doesn't exist yet |
| 90 | + this.needsInitialTalonUpdate_ = true; |
| 91 | + } else { |
| 92 | + console.error("Error loading custom spoken forms", err); |
| 93 | + showError( |
| 94 | + ide().messages, |
| 95 | + "CustomSpokenForms.updateSpokenFormMaps", |
| 96 | + `Error loading custom spoken forms: ${ |
| 97 | + (err as Error).message |
| 98 | + }}}. Falling back to default spoken forms.`, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + this.spokenFormMap_ = { ...defaultSpokenFormMap }; |
| 103 | + this.customSpokenFormsInitialized_ = false; |
| 104 | + this.notifier.notifyListeners(); |
| 105 | + |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + for (const entryType of ENTRY_TYPES) { |
| 110 | + // FIXME: How to avoid the type assertion? |
| 111 | + const entry = Object.fromEntries( |
| 112 | + entries |
| 113 | + .filter((entry) => entry.type === entryType) |
| 114 | + .map(({ id, spokenForms }) => [id, spokenForms]), |
| 115 | + ); |
| 116 | + |
| 117 | + const defaultEntry: Partial<Record<string, DefaultSpokenFormMapEntry>> = |
| 118 | + defaultSpokenFormInfo[entryType]; |
| 119 | + const ids = Array.from( |
| 120 | + new Set([...Object.keys(defaultEntry), ...Object.keys(entry)]), |
| 121 | + ); |
| 122 | + this.spokenFormMap_[entryType] = Object.fromEntries( |
| 123 | + ids.map((id): [SpokenFormType, SpokenFormMapEntry] => { |
| 124 | + const { defaultSpokenForms = [], isSecret = false } = |
| 125 | + defaultEntry[id] ?? {}; |
| 126 | + const customSpokenForms = entry[id]; |
| 127 | + if (customSpokenForms != null) { |
| 128 | + return [ |
| 129 | + id as SpokenFormType, |
| 130 | + { |
| 131 | + defaultSpokenForms, |
| 132 | + spokenForms: customSpokenForms, |
| 133 | + requiresTalonUpdate: false, |
| 134 | + isCustom: isEqual(defaultSpokenForms, customSpokenForms), |
| 135 | + isSecret, |
| 136 | + }, |
| 137 | + ]; |
| 138 | + } else { |
| 139 | + return [ |
| 140 | + id as SpokenFormType, |
| 141 | + { |
| 142 | + defaultSpokenForms, |
| 143 | + spokenForms: [], |
| 144 | + // If it's not a secret spoken form, then it's a new scope type |
| 145 | + requiresTalonUpdate: !isSecret, |
| 146 | + isCustom: false, |
| 147 | + isSecret, |
| 148 | + }, |
| 149 | + ]; |
| 150 | + } |
| 151 | + }), |
| 152 | + ) as any; |
| 153 | + } |
| 154 | + |
| 155 | + this.customSpokenFormsInitialized_ = true; |
| 156 | + this.notifier.notifyListeners(); |
| 157 | + } |
| 158 | + |
| 159 | + getCustomRegexScopeTypes(): CustomRegexScopeType[] { |
| 160 | + return Object.keys(this.spokenFormMap_.customRegex).map((regex) => ({ |
| 161 | + type: "customRegex", |
| 162 | + regex, |
| 163 | + })); |
| 164 | + } |
| 165 | + |
| 166 | + dispose = this.disposer.dispose; |
| 167 | +} |
0 commit comments