-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move talon spoken forms json to its own file
- Loading branch information
Showing
5 changed files
with
125 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/cursorless-engine/src/scopeProviders/SpokenFormEntry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Notifier, SimpleScopeTypeType } from "@cursorless/common"; | ||
import { SpeakableSurroundingPairName } from "../SpokenFormMap"; | ||
|
||
export interface TalonSpokenForms { | ||
getSpokenFormEntries(): Promise<SpokenFormEntry[]>; | ||
onDidChange: Notifier["registerListener"]; | ||
} | ||
|
||
export interface CustomRegexSpokenFormEntry { | ||
type: "customRegex"; | ||
id: string; | ||
spokenForms: string[]; | ||
} | ||
|
||
export interface PairedDelimiterSpokenFormEntry { | ||
type: "pairedDelimiter"; | ||
id: SpeakableSurroundingPairName; | ||
spokenForms: string[]; | ||
} | ||
|
||
export interface SimpleScopeTypeTypeSpokenFormEntry { | ||
type: "simpleScopeTypeType"; | ||
id: SimpleScopeTypeType; | ||
spokenForms: string[]; | ||
} | ||
|
||
export type SpokenFormEntry = | ||
| CustomRegexSpokenFormEntry | ||
| PairedDelimiterSpokenFormEntry | ||
| SimpleScopeTypeTypeSpokenFormEntry; | ||
|
||
export class NeedsInitialTalonUpdateError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "NeedsInitialTalonUpdateError"; | ||
} | ||
} |
112 changes: 73 additions & 39 deletions
112
packages/cursorless-engine/src/scopeProviders/getSpokenFormEntries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,87 @@ | ||
import { LATEST_VERSION, SimpleScopeTypeType } from "@cursorless/common"; | ||
import { readFile } from "fs/promises"; | ||
import { homedir } from "os"; | ||
import { SpeakableSurroundingPairName } from "../SpokenFormMap"; | ||
import * as path from "path"; | ||
import { | ||
Disposer, | ||
FileSystem, | ||
LATEST_VERSION, | ||
Notifier, | ||
isTesting, | ||
} from "@cursorless/common"; | ||
import * as crypto from "crypto"; | ||
import { mkdir, readFile } from "fs/promises"; | ||
import * as os from "os"; | ||
|
||
export const spokenFormsPath = path.join( | ||
homedir(), | ||
".cursorless", | ||
"spokenForms.json", | ||
); | ||
import * as path from "path"; | ||
import { | ||
NeedsInitialTalonUpdateError, | ||
SpokenFormEntry, | ||
TalonSpokenForms, | ||
} from "./SpokenFormEntry"; | ||
|
||
export interface CustomRegexSpokenFormEntry { | ||
type: "customRegex"; | ||
id: string; | ||
spokenForms: string[]; | ||
interface TalonSpokenFormsPayload { | ||
version: number; | ||
entries: SpokenFormEntry[]; | ||
} | ||
|
||
export interface PairedDelimiterSpokenFormEntry { | ||
type: "pairedDelimiter"; | ||
id: SpeakableSurroundingPairName; | ||
spokenForms: string[]; | ||
} | ||
export class TalonSpokenFormsJsonReader implements TalonSpokenForms { | ||
private disposer = new Disposer(); | ||
private notifier = new Notifier(); | ||
private spokenFormsPath; | ||
|
||
export interface SimpleScopeTypeTypeSpokenFormEntry { | ||
type: "simpleScopeTypeType"; | ||
id: SimpleScopeTypeType; | ||
spokenForms: string[]; | ||
} | ||
constructor(private fileSystem: FileSystem) { | ||
const cursorlessDir = isTesting() | ||
? path.join(os.tmpdir(), crypto.randomBytes(16).toString("hex")) | ||
: path.join(os.homedir(), ".cursorless"); | ||
|
||
export type SpokenFormEntry = | ||
| CustomRegexSpokenFormEntry | ||
| PairedDelimiterSpokenFormEntry | ||
| SimpleScopeTypeTypeSpokenFormEntry; | ||
this.spokenFormsPath = path.join(cursorlessDir, "spokenForms.json"); | ||
|
||
export async function getSpokenFormEntries(): Promise<SpokenFormEntry[]> { | ||
const payload = JSON.parse(await readFile(spokenFormsPath, "utf-8")); | ||
this.init(); | ||
} | ||
|
||
private async init() { | ||
const parentDir = path.dirname(this.spokenFormsPath); | ||
await mkdir(parentDir, { recursive: true }); | ||
this.disposer.push( | ||
this.fileSystem.watch(parentDir, () => this.notifier.notifyListeners()), | ||
); | ||
} | ||
|
||
/** | ||
* This assignment is to ensure that the compiler will error if we forget to | ||
* handle spokenForms.json when we bump the command version. | ||
* Registers a callback to be run when the spoken forms change. | ||
* @param callback The callback to run when the scope ranges change | ||
* @returns A {@link Disposable} which will stop the callback from running | ||
*/ | ||
const latestCommandVersion: 6 = LATEST_VERSION; | ||
onDidChange = this.notifier.registerListener; | ||
|
||
if (payload.version !== latestCommandVersion) { | ||
// In the future, we'll need to handle migrations. Not sure exactly how yet. | ||
throw new Error( | ||
`Invalid spoken forms version. Expected ${LATEST_VERSION} but got ${payload.version}`, | ||
); | ||
async getSpokenFormEntries(): Promise<SpokenFormEntry[]> { | ||
let payload: TalonSpokenFormsPayload; | ||
try { | ||
payload = JSON.parse(await readFile(this.spokenFormsPath, "utf-8")); | ||
} catch (err) { | ||
if ((err as any)?.code === "ENOENT") { | ||
throw new NeedsInitialTalonUpdateError( | ||
`Custom spoken forms file not found at ${this.spokenFormsPath}. Using default spoken forms.`, | ||
); | ||
} | ||
|
||
throw err; | ||
} | ||
|
||
/** | ||
* This assignment is to ensure that the compiler will error if we forget to | ||
* handle spokenForms.json when we bump the command version. | ||
*/ | ||
const latestCommandVersion: 6 = LATEST_VERSION; | ||
|
||
if (payload.version !== latestCommandVersion) { | ||
// In the future, we'll need to handle migrations. Not sure exactly how yet. | ||
throw new Error( | ||
`Invalid spoken forms version. Expected ${LATEST_VERSION} but got ${payload.version}`, | ||
); | ||
} | ||
|
||
return payload.entries; | ||
} | ||
|
||
return payload.entries; | ||
dispose() { | ||
this.disposer.dispose(); | ||
} | ||
} |