Skip to content

Commit 0e01381

Browse files
AndreasArvidssonphillcopre-commit-ci-lite[bot]
authored
Don't error if "user.any_alphanumeric_key" is missing from the registry (#2630)
Fixes #2629 Small regression from #2465 since we are now reading that capture all the time now ## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet - [x] Ran Talon grammar tests --------- Co-authored-by: Phil Cohen <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent ec1e282 commit 0e01381

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

cursorless-talon-dev/src/default_vocabulary.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from talon import Context
1+
from talon import Context, Module
2+
3+
mod = Module()
4+
mod.list(
5+
"cursorless_default_any_alphanumeric_key",
6+
desc="Default Cursorless vocabulary any alphanumeric key",
7+
)
28

39
ctx = Context()
410
ctx.matches = r"""
@@ -96,14 +102,18 @@
96102
"pound": "£",
97103
}
98104

99-
any_alphanumeric_keys = {
105+
ctx.lists["user.cursorless_default_any_alphanumeric_key"] = {
100106
**{w: chr(ord("a") + i) for i, w in enumerate(initial_default_alphabet)},
101107
**{digits[i]: str(i) for i in range(10)},
102108
**punctuation_words,
103109
**symbol_key_words,
104110
}
105111

106112

107-
@ctx.capture("user.any_alphanumeric_key", rule="|".join(any_alphanumeric_keys.keys()))
113+
# NB: do not use literals in these captures because `generate_lists_from_capture` does not support them
114+
@ctx.capture(
115+
"user.any_alphanumeric_key",
116+
rule="{user.cursorless_default_any_alphanumeric_key}",
117+
)
108118
def any_alphanumeric_key(m) -> str:
109-
return any_alphanumeric_keys[str(m)]
119+
return m.cursorless_default_any_alphanumeric_key

cursorless-talon/src/get_grapheme_spoken_form_entries.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
from typing import Iterator, Mapping
55
from uu import Error
66

7-
from talon import app, registry
7+
from talon import app, registry, scope
88

99
from .spoken_forms_output import SpokenFormOutputEntry
1010

1111
grapheme_capture_name = "user.any_alphanumeric_key"
1212

1313

1414
def get_grapheme_spoken_form_entries() -> list[SpokenFormOutputEntry]:
15+
if grapheme_capture_name not in registry.captures:
16+
# We require this capture, and expect it to be defined. We want to show a user friendly error if it isn't present (usually indicating a problem with their community.git setup) and we think the user is going to use Cursorless.
17+
# However, sometimes users use different dictation engines (Vosk, Webspeech) with entirely different/smaller grammars that don't have the capture, and this code will run then, and falsely error. We don't want to show an error in that case because they don't plan to actually use Cursorless.
18+
if "en" in scope.get("language", {}):
19+
app.notify(f"Capture <{grapheme_capture_name}> isn't defined")
20+
print(
21+
f"Capture <{grapheme_capture_name}> isn't defined, which is required by Cursorless. Please check your community setup"
22+
)
23+
return []
24+
1525
return [
1626
{
1727
"type": "grapheme",
@@ -32,7 +42,8 @@ def generate_lists_from_capture(capture_name) -> Iterator[str]:
3242
if capture_name.startswith("self."):
3343
capture_name = "user." + capture_name[5:]
3444
try:
35-
rule = registry.captures[capture_name][0].rule.rule
45+
# NB: [-1] because the last capture is the active one
46+
rule = registry.captures[capture_name][-1].rule.rule
3647
except Error:
3748
app.notify("Error constructing spoken forms for graphemes")
3849
print(f"Error getting rule for capture {capture_name}")
@@ -63,7 +74,8 @@ def get_id_to_spoken_form_map(list_name: str) -> Mapping[str, list[str]]:
6374
list to the list of spoken forms that map to the given value.
6475
"""
6576
try:
66-
raw_list = typing.cast(dict[str, str], registry.lists[list_name][0]).copy()
77+
# NB: [-1] because the last list is the active one
78+
raw_list = typing.cast(dict[str, str], registry.lists[list_name][-1]).copy()
6779
except Error:
6880
app.notify(f"Error getting list {list_name}")
6981
return {}

0 commit comments

Comments
 (0)