-
-
Notifications
You must be signed in to change notification settings - Fork 84
Get custom spoken forms from Talon #1940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d92c0d2
Get custom spoken forms from Talon
pokey eb9e422
Improved types
pokey fa94cc1
Log errors and disposer
pokey 9c93fab
More PR feedback
pokey c1bd390
improve error type safety
pokey 0adcc2e
fix imports
pokey 285e614
More PR feedback
pokey 4d20004
More tweaks
pokey 36d027e
More cleanup#
pokey 3086cc4
Throw error
pokey a08bf21
More cleanup
pokey 37e3481
More simplification
pokey e2af29e
Remove export
pokey bb80326
Pr cleanup
pokey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
61 changes: 61 additions & 0 deletions
61
packages/cursorless-engine/src/generateSpokenForm/CustomSpokenFormGeneratorImpl.ts
This file contains hidden or 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,61 @@ | ||
import { | ||
CommandComplete, | ||
Disposable, | ||
Listener, | ||
ScopeType, | ||
} from "@cursorless/common"; | ||
import { SpokenFormGenerator } from "."; | ||
import { CustomSpokenFormGenerator } from ".."; | ||
import { CustomSpokenForms } from "../spokenForms/CustomSpokenForms"; | ||
import { TalonSpokenForms } from "../scopeProviders/TalonSpokenForms"; | ||
|
||
/** | ||
* Simple facade that combines the {@link CustomSpokenForms} and | ||
* {@link SpokenFormGenerator} classes. Its main purpose is to reconstruct the | ||
* {@link SpokenFormGenerator} when the {@link CustomSpokenForms} change. | ||
*/ | ||
export class CustomSpokenFormGeneratorImpl | ||
implements CustomSpokenFormGenerator | ||
{ | ||
private customSpokenForms: CustomSpokenForms; | ||
private spokenFormGenerator: SpokenFormGenerator; | ||
private disposable: Disposable; | ||
|
||
constructor(talonSpokenForms: TalonSpokenForms) { | ||
this.customSpokenForms = new CustomSpokenForms(talonSpokenForms); | ||
this.spokenFormGenerator = new SpokenFormGenerator( | ||
this.customSpokenForms.spokenFormMap, | ||
); | ||
this.disposable = this.customSpokenForms.onDidChangeCustomSpokenForms( | ||
() => { | ||
this.spokenFormGenerator = new SpokenFormGenerator( | ||
this.customSpokenForms.spokenFormMap, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
onDidChangeCustomSpokenForms(listener: Listener<[]>) { | ||
return this.customSpokenForms.onDidChangeCustomSpokenForms(listener); | ||
} | ||
|
||
commandToSpokenForm(command: CommandComplete) { | ||
return this.spokenFormGenerator.processCommand(command); | ||
} | ||
|
||
scopeTypeToSpokenForm(scopeType: ScopeType) { | ||
return this.spokenFormGenerator.processScopeType(scopeType); | ||
} | ||
|
||
getCustomRegexScopeTypes() { | ||
return this.customSpokenForms.getCustomRegexScopeTypes(); | ||
} | ||
|
||
get needsInitialTalonUpdate() { | ||
return this.customSpokenForms.needsInitialTalonUpdate; | ||
} | ||
|
||
dispose() { | ||
this.disposable.dispose(); | ||
} | ||
} |
This file contains hidden or 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,3 @@ | ||
# Node common | ||
|
||
This directory contains utilities that are available in a node.js context. |
77 changes: 77 additions & 0 deletions
77
packages/cursorless-engine/src/nodeCommon/TalonSpokenFormsJsonReader.ts
This file contains hidden or 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,77 @@ | ||
import { Disposable, FileSystem, Notifier } from "@cursorless/common"; | ||
import { readFile } from "fs/promises"; | ||
|
||
import * as path from "path"; | ||
import { | ||
NeedsInitialTalonUpdateError, | ||
SpokenFormEntry, | ||
TalonSpokenForms, | ||
} from "../scopeProviders/TalonSpokenForms"; | ||
|
||
interface TalonSpokenFormsPayload { | ||
version: number; | ||
spokenForms: SpokenFormEntry[]; | ||
} | ||
|
||
const LATEST_SPOKEN_FORMS_JSON_VERSION = 0; | ||
|
||
export class TalonSpokenFormsJsonReader implements TalonSpokenForms { | ||
private disposable: Disposable; | ||
private notifier = new Notifier(); | ||
|
||
constructor(private fileSystem: FileSystem) { | ||
this.disposable = this.fileSystem.watchDir( | ||
path.dirname(this.fileSystem.cursorlessTalonStateJsonPath), | ||
() => this.notifier.notifyListeners(), | ||
); | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
onDidChange = this.notifier.registerListener; | ||
|
||
async getSpokenFormEntries(): Promise<SpokenFormEntry[]> { | ||
let payload: TalonSpokenFormsPayload; | ||
try { | ||
payload = JSON.parse( | ||
await readFile(this.fileSystem.cursorlessTalonStateJsonPath, "utf-8"), | ||
); | ||
} catch (err) { | ||
if (isErrnoException(err) && err.code === "ENOENT") { | ||
throw new NeedsInitialTalonUpdateError( | ||
`Custom spoken forms file not found at ${this.fileSystem.cursorlessTalonStateJsonPath}. Using default spoken forms.`, | ||
); | ||
} | ||
|
||
throw err; | ||
} | ||
|
||
if (payload.version !== LATEST_SPOKEN_FORMS_JSON_VERSION) { | ||
// In the future, we'll need to handle migrations. Not sure exactly how yet. | ||
throw new Error( | ||
`Invalid spoken forms version. Expected ${LATEST_SPOKEN_FORMS_JSON_VERSION} but got ${payload.version}`, | ||
); | ||
} | ||
|
||
return payload.spokenForms; | ||
} | ||
|
||
dispose() { | ||
this.disposable.dispose(); | ||
} | ||
} | ||
|
||
/** | ||
* A user-defined type guard function that checks if a given error is a | ||
* `NodeJS.ErrnoException`. | ||
* | ||
* @param {any} error - The error to check. | ||
* @returns {error is NodeJS.ErrnoException} - Returns `true` if the error is a | ||
* {@link NodeJS.ErrnoException}, otherwise `false`. | ||
*/ | ||
function isErrnoException(error: any): error is NodeJS.ErrnoException { | ||
return error instanceof Error && "code" in error; | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/cursorless-engine/src/scopeProviders/TalonSpokenForms.ts
This file contains hidden or 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,43 @@ | ||
import { Notifier } from "@cursorless/common"; | ||
import { | ||
SpokenFormMapKeyTypes, | ||
SpokenFormType, | ||
} from "../spokenForms/SpokenFormType"; | ||
|
||
/** | ||
* Interface representing a communication mechanism whereby Talon can provide | ||
* the user's custom spoken forms to the Cursorless engine. | ||
*/ | ||
export interface TalonSpokenForms { | ||
getSpokenFormEntries(): Promise<SpokenFormEntry[]>; | ||
onDidChange: Notifier["registerListener"]; | ||
} | ||
|
||
/** | ||
* The types of entries for which we currently support getting custom spoken | ||
* forms from Talon. | ||
*/ | ||
export const SUPPORTED_ENTRY_TYPES = [ | ||
"simpleScopeTypeType", | ||
"customRegex", | ||
"pairedDelimiter", | ||
] as const; | ||
|
||
type SupportedEntryType = (typeof SUPPORTED_ENTRY_TYPES)[number]; | ||
|
||
export interface SpokenFormEntryForType<T extends SpokenFormType> { | ||
type: T; | ||
id: SpokenFormMapKeyTypes[T]; | ||
spokenForms: string[]; | ||
} | ||
|
||
export type SpokenFormEntry = { | ||
[K in SpokenFormType]: SpokenFormEntryForType<K>; | ||
}[SupportedEntryType]; | ||
|
||
export class NeedsInitialTalonUpdateError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "NeedsInitialTalonUpdateError"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to keep this file totally focused on the transport layer (watching a json file). That way we can swap it out for Talon rpc, Rango rpc, etc depending on the context