2
2
from collections .abc import Container
3
3
from dataclasses import dataclass
4
4
from datetime import datetime
5
+ from itertools import groupby
5
6
from pathlib import Path
6
7
from typing import Callable , Iterable , Optional , TypedDict
7
8
@@ -225,40 +226,41 @@ def update_dicts(
225
226
# Create map with all default values
226
227
results_map : dict [str , ResultsListEntry ] = {}
227
228
for list_name , obj in default_values .items ():
228
- for key , value in obj .items ():
229
- results_map [value ] = {"key " : key , "value " : value , "list" : list_name }
229
+ for spoken , id in obj .items ():
230
+ results_map [id ] = {"spoken " : spoken , "id " : id , "list" : list_name }
230
231
231
232
# Update result with current values
232
- for key , value in current_values .items ():
233
+ for spoken , id in current_values .items ():
233
234
try :
234
- results_map [value ]["key " ] = key
235
+ results_map [id ]["spoken " ] = spoken
235
236
except KeyError :
236
- if value in extra_ignored_values :
237
+ if id in extra_ignored_values :
237
238
pass
238
- elif allow_unknown_values or value in extra_allowed_values :
239
+ elif allow_unknown_values or id in extra_allowed_values :
239
240
assert default_list_name is not None
240
- results_map [value ] = {
241
- "key " : key ,
242
- "value " : value ,
241
+ results_map [id ] = {
242
+ "spoken " : spoken ,
243
+ "id " : id ,
243
244
"list" : default_list_name ,
244
245
}
245
246
else :
246
247
raise
247
248
248
249
spoken_form_entries = list (generate_spoken_forms (results_map .values ()))
250
+ print (f"spoken_form_entries: { spoken_form_entries } " )
249
251
250
252
# Assign result to talon context list
251
253
assign_lists_to_context (
252
254
ctx ,
253
255
{
254
- ** { res [ "list" ] : {} for res in results_map . values ()},
255
- ** {
256
- spoken_form_entry . list_name : {
257
- spoken_form : spoken_form_entry . id
258
- for spoken_form in spoken_form_entry . spoken_forms
259
- }
260
- for spoken_form_entry in spoken_form_entries
261
- },
256
+ list_name : {
257
+ spoken_form : entry . id
258
+ for entry in spoken_form_entries
259
+ for spoken_form in entry . spoken_forms
260
+ }
261
+ for list_name , spoken_form_entries in groupby (
262
+ spoken_form_entries , key = lambda x : x . list_name
263
+ )
262
264
},
263
265
pluralize_lists ,
264
266
)
@@ -268,33 +270,33 @@ def update_dicts(
268
270
269
271
270
272
class ResultsListEntry (TypedDict ):
271
- key : str
272
- value : str
273
+ spoken : str
274
+ id : str
273
275
list : str
274
276
275
277
276
278
def generate_spoken_forms (results_list : Iterable [ResultsListEntry ]):
277
279
for obj in results_list :
278
- value = obj ["value " ]
279
- key = obj ["key " ]
280
+ id = obj ["id " ]
281
+ spoken = obj ["spoken " ]
280
282
281
- spoken = []
282
- if not is_removed (key ):
283
- for k in key .split ("|" ):
284
- if value == "pasteFromClipboard" and k .endswith (" to" ):
283
+ spoken_forms = []
284
+ if not is_removed (spoken ):
285
+ for k in spoken .split ("|" ):
286
+ if id == "pasteFromClipboard" and k .endswith (" to" ):
285
287
# FIXME: This is a hack to work around the fact that the
286
288
# spoken form of the `pasteFromClipboard` action used to be
287
289
# "paste to", but now the spoken form is just "paste" and
288
290
# the "to" is part of the positional target. Users who had
289
291
# cursorless before this change would have "paste to" as
290
292
# their spoken form and so would need to say "paste to to".
291
293
k = k [:- 3 ]
292
- spoken .append (k .strip ())
294
+ spoken_forms .append (k .strip ())
293
295
294
296
yield SpokenFormEntry (
295
- obj ["list" ],
296
- value ,
297
- spoken ,
297
+ list_name = obj ["list" ],
298
+ id = id ,
299
+ spoken_forms = spoken_forms ,
298
300
)
299
301
300
302
@@ -303,6 +305,7 @@ def assign_lists_to_context(
303
305
lists : ListToSpokenForms ,
304
306
pluralize_lists : list [str ],
305
307
):
308
+ print (f"lists: { lists } " )
306
309
for list_name , dict in lists .items ():
307
310
list_singular_name = get_cursorless_list_name (list_name )
308
311
ctx .lists [list_singular_name ] = dict
0 commit comments